// var erikSays = "jQuery rocks!";
$(document).ready(function(){
	// jQuery rocks!
	 
	$("#mainnav a, #secondnav a").attr({title:""});
	
	// country switch
	$("#selectcountry").click(
			function() {
				$("#countries").css({opacity:"0.8"});
				$("#countries").slideToggle("500");
			}
	);
	
	$("#selectcountry").blur(
		function(){
			$("#countries").animate({opacity:"0.0",speed:"slow"});
			$("#countries").slideUp();
		}
	);
	
	$("#mainnav a").each(function(i){
		if ($(this).attr("class") == "active") {
			var thisid = $(this).attr("id");
			var activeitem = thisid;
			//console(this.id); 
		};
		$("#"+activeitem).addClass("active");
	});
	
	var navconfig = {    
	     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
	     interval: 200, // number = milliseconds for onMouseOver polling interval
	     over: navOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 200, // number = milliseconds delay before onMouseOut
	     out: navOut // function = onMouseOut callback (REQUIRED)
	};

	if ($(this).attr("class") != "active") {
		$("#mainnav a, #residential, #support").css({
			// opacity:"0.8",
			// filter:"alpha(opacity=80)"
		});
	}
	$("#mainnav a").hoverIntent( navconfig );
	
	var navhideconfig = {    
	     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
	     interval: 400, // number = milliseconds for onMouseOver polling interval
	     over: dummyOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 400, // number = milliseconds delay before onMouseOut
	     out: navhideOut // function = onMouseOut callback (REQUIRED)
	};
	
	function dummyOver () {
		// nothing
	}
	
	function navhideOut () {
		$("#secondnav ul").hide();
		$("#secondnav").slideUp(200,
			function(){
				subnavvis = false;
			}
		);
		$("#mainnav").addClass("nosecondnav");
		$("#mainnav ul li").addClass("rounded");
	}
	
	$("#secondnav").hoverIntent( navhideconfig );
	
	function navOver () {
		var thisid = $(this).attr("id");
		
		// if subnav is visible, and current elem has no subnav, hide it.
		if ("wheretobuy" == thisid && subnavvis == true) {
			$(this).animate({
					/* opacity:"1.0",
					filter:"alpha(opacity=100)" */
				},200);
				$(this).addClass("active");
			$("#secondnav ul").hide();
			$("#secondnav").slideUp(200,
				function(){
					subnavvis = false;
				}
			);
			
			$("#mainnav").addClass("nosecondnav");
			$("#mainnav ul li").addClass("rounded");
		} else {
		
			if ($(this).attr("class") !== "active") {
				$(this).animate({
				//	opacity:"1.0",
				//	filter:"alpha(opacity=100)"
				},200);
				$(this).parent().css({ // opacity:"1.0",filter:"alpha(opacity=100)"
				});
				$(this).addClass("active");	
			}
			if ( "wheretobuy" !== thisid && thisid !== "" ) {
				// show the subnav
				$("#mainnav").removeClass("nosecondnav");
				$("#mainnav ul li").removeClass("rounded");
				// $("#secondnav ul").css({opacity:"1.0", filter:"alpha(opacity=100)"});
				
				$("#secondnav").slideDown(200,
					function(){				
						$.get("http://www.iqair.com/inc/inc_mainnav.php",
				       		{ajax:"true",sub: thisid},
				       		function(returned_data) {
			
				         			$("#secondnav ul").show( 
								    
								    function(){
								    		
										$("#secondnav").html(returned_data);
										
									}
								);
				         		}
						);
						$("#secondnav ul").show();
						$("#secondnav ul").css({
							// opacity:"1.0",filter:"alpha(opacity=100)"
						});
						subnavvis = true;
					}
				);
				
			}	
				
			//console("activeitem: "+activeitem+" thisid: "+thisid+" subnavvis: "+subnavvis);
			if (thisid !== activeitem && subnavvis == true && thisid !== "wheretobuy") {
				//alert("activeitem: "+activeitem+" thisid: "+thisid+" subnavvis: "+subnavvis);
				$.get("http://www.iqair.com/inc/inc_mainnav.php",
		       		{ajax:"true",sub: thisid},
		       		function(returned_data) {
	
		         			$("#secondnav ul").show( 
						    
						    function(){
						    		
								$("#secondnav").html(returned_data);
								
							}
						);
		         		}
				);
			var activeitem = thisid;
			}
		}	
	}
	
	function navOut () {
		var currentsub;
		var thisclass = $(this).attr("class");
		if ( thisclass == "active" ) {
			$(this).animate({
				//opacity:"0.8",
				//filter:"alpha(opacity=80)"
			});
		} 
		$(this).removeClass("active");
		//$("#residential, #support").css({opacity:"0.9",filter:"alpha(opacity=90)"});
			// load default ajax subnav here, set active to default here
		}
		
	function hideSecondNav () {
		$("#secondnav ul").hide();
		$("#secondnav").slideUp(200,
			function(){
				subnavvis = false;
			}
		);
		$("#mainnav a, #residential, #support").css({
		// opacity:".8", filter:"alpha(opacity=80)"
		}); 
	}

	$.fn.search = function( ) {
		this.attr({autocomplete:"off"});
		$(this).css({color:"gray"});
		return this.focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
				$(this).css({color:"black"});
			}
		}).blur(function() {
			if( !this.value.length ) {
				$(this).css({color:"gray"});
				this.value = this.defaultValue;
			}
		});
		};
	$("#searchterm").search();	
});
