$(function() {

	navRollover("#sidebar .section-nav li a");
	navRollover("#global-nav li a");	   
	//$('.column-holder').eqByRow({element:".column"});
	$('.image-gallery').eqByRow();
	
	/* call goofy nav functions if current page is not homepage */
	// Note, renamed from home to homeZ to enable Goofy on the homepage.
	if(!$("body").is("#homeZ")){
		goofySwitch("&laquo; Switch to Regular");
		styleSwitch();
	}
	
	niceOverlays();
	
	//$('.retailer-list').each(function(){
	//	$(this).children('ul:last').removeClass("border-bottom");
	//});
	
	$('#product-holder .product').each(function(){
		$content = $(this).find(".content");
		$description = $(this).find(".description");
		$top = (($content.height() - $description.height()) / 2);
		//$description.css("margin-top",$top);
		$description.css("margin-top","0");
	});
	
	if($('.product-collection').length > 0){ $('.product-collection .image-overlay').biggerlink({hoverclass:'hover'}); }	
	if($('.ambassador-gallery').length > 0){ $('.ambassador-gallery li a').colorbox({transition:"fade",opacity:"0.75"});}
	
	if($("#back-to-top").length > 0) {
	backToTop($("#back-to-top").position().top + 13);
	}	

	homeHeading();
	$('#splash .maximage').maxImage({isBackground:"true"});
	
	$("#region-selector").bind("change", function() {
		region_code = $("#region-selector").val();
		
		if(region_code == "")
			return;
			
		// Strip off the forward slash if there is one
		if(region_code.indexOf("/") >= 0)
			region_code = region_code.substring(1);
			
		var destination_url = base_url + region_code + "/page/home";
		window.location = destination_url;
	});	
	
});

/* #FUNCTION IENAVROLLOVER
-- add tilda to rollover, allows for flip on goofy switch */
function navRollover(navitem)
{	
	/* set state for selected nav items */
	$(navitem).each(function(){
		if($(this).hasClass("selected")){
			/*if($('body').is('#home'))
				$(this).prepend($("<span>~</span>"));	
			else */
                $(this).append($("<span>~</span>"));
		}
	});
	
	$(navitem).hover(
		function(){
			if(!$(this).hasClass("selected")){
				/*if($('body').is('#home'))
					$(this).prepend($("<span>~</span>"));
				else */
                    $(this).append($("<span>~</span>"));
			}
		}, 
		function(){
			if(!$(this).hasClass("selected")){
				$(this).find("span:last").remove();
			}
		}
	);	
}

/* #FUNCTION GOOFYSWITSH
-- set goofy toggle link text */
function goofySwitch(defaultext){
	if($.cookie("rusty-goofy") || $("body").is(".goofy")) {
		if(!$("body").is(".goofy")) {
			/* add these classes for IE6 */
			if($("body").is(".nav-inverse")) $("body").addClass("goofy-inverse");
			if($("body").is(".nav-background")) $("body").addClass("goofy-background");
			$("body").addClass("goofy");
		}
		$("#switch").html(defaultext);
	}
}

/* #FUNCTION NICEOVERLAYS 
-- tidies up image overlay display for IE6 */
function niceOverlays(){
    
	$max = 0;
	$(".image-overlay" ).each(function(){
		$overlay = $(this).children(".overlay");
		$abstract = $(this).children(".abstract");
        
		if(!$(this).hasClass("vertical")){
			if($overlay.height() < $(this).height() ){
				$overlay.height($(this).height());                                
			}
            
			$top = (($overlay.height() - $abstract.height()) / 2) - 20;
			$abstract.css("background", "none");
			$abstract.css("filter", "none");
			$abstract.css("top",$top);
            
		} else if($(this).parent().hasClass("product-collection")) {
			if($abstract.height() > $max) {
				$max = $abstract.height();
			}
			$overlay.height($max + 100);
			$bottom = ($overlay.height() - $abstract.height())/2 + 80;
			$abstract.css("bottom",$bottom);
		}
		else {
            
            if($(this).parent().parent().attr("id") == "content-highlight"){
                $overlay.height($abstract.height() + 25);    
                $bottom = ($overlay.height() - $abstract.height()) / 2 - 15; 
                $abstract.css("bottom", $bottom);
            }
            else{  //vertical
            
                $overlay.height($abstract.height() + 80);
			    $bottom = ($overlay.height() - $abstract.height())/2 - 20;
			    $abstract.css("bottom",$bottom);
            }            
		}        
	});
    //Agi: adjust overlay height (heighest size)
    if($(".image-overlay").parent().hasClass("product-collection"))
        $(".image-overlay .overlay" ).height( $max + 100);
}
 
/* #FUNCTION STYLESWITCH
 -- switches to goofy style and back */
function styleSwitch(){
	$("#switch").click(function()
	{
		if($("body").is(".goofy")) {
			$("body").removeClass("goofy");
			$("body").removeClass("goofy-inverse");
			$("body").removeClass("goofy-background");
			$("#switch").html("Switch to Goofy &raquo;");
			$.cookie("rusty-goofy", null, {expires:365, path: '/'}); 
		}
		else {
			$("body").addClass("goofy");
			if($("body").is(".nav-inverse")) $("body").addClass("goofy-inverse");
			if($("body").is(".nav-background")) $("body").addClass("goofy-background");
			$("#switch").html("&laquo; Switch to Regular");
			$.cookie("rusty-goofy", 1, {expires:365, path: '/'});
		}
	});
}

/*  FUNCTION EQBYROW
-- Equalise the heights of columns in a holder by row
usage: $('#eq-this-list').eqByRow(); */
jQuery.fn.eqByRow = function(params){
	params = $.extend({ element: "li"}, params);
	var inRow 	= Math.floor( $(this).width() / $(this).children(params.element+":first").width() );
	var items 	= $(this).children(params.element);
	for (var i = 0; i < ($(items).length / inRow); i++){
		var max 	= 0;
		var start 	= (i * inRow);
		for (var j = start; j < start + inRow; j++){
			if(items[j]){
				if( $($(items)[j]).height() > max ) max = $($(items)[j]).height();
			}
		};
		for (var k = start; k < start + inRow; k++){
			if(items[k]){
				$($(items)[k]).height(max);
			}
		};
	};
}

/*  FUNCTION HOMEHEADING
-- Format heading for homepage */
function homeHeading(){
	heading = $(".home-heading h1");
	span = $(".home-heading h1 span");
	hw = heading.width();
	$leftpos = (hw/2) + 10;
	$rightpos = $leftpos + 370;
	$(".home-heading").css("background-position", -$leftpos);
	heading.css("display", "block");
	heading.css("background-position", $rightpos);
}

/*  FUNCTIONS BACKTOTOP & POSITIONLINK
-- Positions back to top link */
function backToTop(pos){
	positionLink(pos);
	$(window).scroll(function() {
		positionLink(pos);
	});
}

function positionLink(pos){
	$wt = $(window).scrollTop();
	$elmpos = $wt + 20;
	if($(this).scrollTop() >= pos) {
		$("#back-to-top").hide();
		$("#back-to-top").css('top',$elmpos);
		$("#back-to-top").fadeIn();
	}
	else {
		$("#back-to-top").css('top',pos-13);
	}
}
