// JavaScript Document

// load default actions

addLoadEvent(blurLinks);
addLoadEvent(preloadMenu);
addLoadEvent(EmailUnobsfuscate);
addLoadEvent(prepareContactForm);


///////////////////////////////////////////////////////////////////

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}



/* remove all focus lines from links when clicked */
function blurLinks() {
	var links = document.getElementsByTagName('a');
	for (i=0;i<links.length;i++) {
		links[i].onfocus = function() {
			this.blur();
		}
	}
}


function EmailUnobsfuscate() {
	
	// find all links in HTML
	var link = document.getElementsByTagName && document.getElementsByTagName("a");
	var email, e;
	
	// examine all links
	for (e = 0; link && e < link.length; e++) {
	
		// does the link have use a class named "email"
		if ((" "+link[e].className+" ").indexOf(" email ") >= 0) {
		
			// get the obfuscated email address
			email = link[e].firstChild.nodeValue.toLowerCase() || "";
			
			// transform into real email address
			email = email.replace(/dot/ig, ".");
			email = email.replace(/\(at\)/ig, "@");
			email = email.replace(/\s/g, "");
			
			// is email valid?
			if (/^[^@]+@[a-z0-9]+([_\.\-]{0,1}[a-z0-9]+)*([\.]{1}[a-z0-9]+)+$/.test(email)) {
			
				// change into a real mailto link
				link[e].href = "mailto:" + email;
				link[e].firstChild.nodeValue = email;
		
			}
		}
	}
}




function correctPNG(){ // correctly handle PNG transparency in Win IE 5.5 & 6.


   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}




function prepareContactForm() {
	var fields = $$('.leftColumn form .txt');
	
	 for(var i=0; i<fields.length; i++) {
		 fields[i].onfocus = function() {
			this.className='txtActive'; 
		 }
		 
		 fields[i].onblur = function() {
			this.className='txt'; 
		 }
	 }
}


function preloadMenu() {
	if(document.images) {
	  var image_array = new Array();
		
	  // path to the directory with images
	  var path = 'assets/site/img/';
	
	  // enumeration of the "active" images
	  image_array[0] = path + "btn/about_over.png";
	  image_array[1] = path + "btn/contact_over.png";
	  image_array[2] = path + "btn/tutorials_over.png";
	  image_array[3] = path + "btn/blog_over.png";
	  image_array[4] = path + "btn/work_over.png";
	  image_array[5] = path + "misc/ico_bk.jpg";
	  image_array[6] = path + "misc/ico_del.jpg";
	  image_array[7] = path + "misc/ico_fb.jpg";
	  image_array[8] = path + "misc/ico_flickr.jpg";
	  image_array[9] = path + "misc/ico_linked.jpg";
	  image_array[10] = path + "misc/ico_plurk.jpg";
	  image_array[11] = path + "misc/ico_tumblr.jpg";
	  image_array[12] = path + "misc/ico_twitter.jpg";
	  image_array[13] = path + "misc/ico_youare.jpg";
	  
	 
	
	  var preload_image = new Array ();
	 
	  for(var i=0; i<image_array.length; i++)
	  {
		preload_image[i]= new Image();
		preload_image[i].src = image_array[i];
	  }
	}

}





