$(function(){
	
	//-- Navigation mouseover effects
	$("#nav img").hover( navover, navout );
	
	//-- IE7 needs this style reenforced for some reason.
	if( $.browser.msie ) $("#footer").css("clear", "both");
	//-- jQuery1.1 doesn't have the version string: && parseInt($.browser.version) == 7
	
});

function navover() {
	var url = $(this).attr("src");
	url = url.replace(/\.png$/, "-over.png");
	$(this).attr("src", url);
}
function navout() {
	var url = $(this).attr("src");
	url = url.replace(/-over\.png$/, ".png");
	$(this).attr("src", url);
}


/* Binds to "load" which includes page elements; won't preload until normal images are loaded. */
$(window).bind('load', function() {
    var preload = new Array();
    $("#nav img").each(function() {
        s = $(this).attr("src").replace(/\.(.+)$/i, "-over.$1");
        preload.push(s)
    });
    var img = document.createElement('img');
    $(img).bind('load', function() {
        if(preload[0]) {
            this.src = preload.shift();
        }
    }).trigger('load');
});

/*
 * Customizes the page to appear within a specific section.
 * 	(changed default state of section nev pog, changes background gradient)
 * 
 */
function setSection( sec ) {
	
	var gradbgfile = "";
	var navselector = "";
	var url;
	
	
	sec = sec.toLowerCase();
	
	switch( sec ) {
	
		case "products":
			gradbgfile = "bg.pagegrad.products";
			navselector = "#nav-products";
			break;
		
		case "nutrition":
			gradbgfile = "bg.pagegrad.nutrition";
			navselector = "#nav-nutrition";
			break;
		
		case "wheretobuy":
			gradbgfile = "bg.pagegrad.wheretobuy";
			navselector = "#nav-wheretobuy";
			break;
		
		case "community":
			gradbgfile = "bg.pagegrad.community";
			navselector = "#nav-community";
			break;
		
		case "about":
			gradbgfile = "bg.pagegrad.about";
			navselector = "#nav-about";
			break;
		
		default:
			return;
		
	}

	/** Change background gradient image **/
	
	url = $("#page").css("background-image");
	url = url.replace( /bg\.pagegrad\.default\..\.gif/, gradbgfile+".n.gif");
	$("#page").css("background-image", url);


	url = $("#page-inside").css("background-image");
	url = url.replace( /bg\.pagegrad\.default\..\.gif/, gradbgfile+".s.gif");
	$("#page-inside").css("background-image", url);


	
	/** Change navigation (if such a navigation option exists) **/

	if( $(navselector).length ){

		//-- Mark this as the current location
		$(navselector).addClass("now");
		
		//-- Make this option's image show the "mourseover" version
		url = $(navselector+" img").attr("src");
		url = url.replace(/\.png$/, "-over.png");
		$(navselector+" img").attr("src", url);
		
		//-- Ignore the predefined hover effects
		//	(Delayed because the setup of these events is delayed)
		$(function(){
			$(navselector+" img").unbind();	
		});
	
	}

	
}
