$(document).ready(function() {
	
	/*
	 * Lazy Loader: http://mattdunlap.org/website-development/javascript/function-for-lazy-loading-jquery-javascript-and-css-files.html
	 */
	function lazy_loader(file_array, callback) { 
	   //reverse the array because we pop off the file we need
	   file_array.reverse();
	   remaining = file_array.length;
	   if(remaining>0) {
	   url_array = file_array.pop();
	   file_array.reverse(); //put the array back in order
	   var head= document.getElementsByTagName('head')[0];
	   switch(url_array[0]) {
			case"scripts":
				f=document.createElement('script');
				f.type= 'text/javascript';
				f.src=url_array[1];
			break;
	
			case"links":
				f=document.createElement('link');
				f.rel= 'stylesheet';
				f.href=url_array[1];
			break;
	   }
	   head.appendChild(f);
	   
	   //if there are no more files, then do the callback
	   if(remaining==1){
			if(f.rel=='stylesheet')
			{
			eval(callback+"()");
			}
			else
			{
			if(navigator.appName!='Netscape')
			f.onreadystatechange = function () { if(this.readyState=='loaded' || this.readyState=='complete') eval(callback+"()");}
			else
			f.onload= function () {eval(callback+"()");}
			}
	   } else {
	   //if there are more files, then load them
			if(f.rel=='stylesheet')
			{
			lazy_loader(file_array, callback);
			}
			else
			{
			if(navigator.appName!='Netscape')
			f.onreadystatechange= function () { if(this.readyState=='loaded' || this.readyState=='complete') lazy_loader(file_array, callback)}
			else
			f.onload=function () {lazy_loader(file_array, callback)}
			}
	   }
	   
	   }
	}
	

   //http://www.sohtanaka.com/web-design/examples/mega-dropdowns/
   function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo(0, 1).show();
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).outerWidth(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
		//$(this).children('a.on').removeClass('on');
	  $(this).find(".sub").stop().fadeTo(0, 0, function() {
		  $(this).hide(); 
		  
	  });
	}


	var config = {    
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);

	$("ul#topnav > li").mouseleave(function(){
		$(this).children('a').removeClass('on');
	});
	
	$("ul#topnav > li").mouseenter(function(){
		$(this).children('a').addClass('on');
	});
		

		

	
	/*
<?php
if ($_COOKIE['customerId'] && $_COOKIE['customerLogin']) {
?>
document.getElementById('password').focus();
<?php
}else {
?>
document.getElementById('login').focus();
<?php
}
?>
	*/
});

