var imgs = new Array("heritage", "canada", "natural", "range", "superior", "savour");
var currItem = 0;

function completeTrans(thenewimg) {
	/*
	thisnewimg = imgs[currItem];
	if(thisnewimg == "heritage") {
		$(".item_heritage").show();
	}
	else {
		$(".item_heritage").hide();
	}
	var d = new Date();
	//alert('/images/home_' + thisnewimg + '.jpg?' + d.getTime());
	var tmpImg = '/images/home_' + thisnewimg + '.jpg?' + d.getTime();
	$('<img />').attr('src', tmpImg).bind('load', function(){
		//alert("here");
		$('#mainbg').attr('src', tmpImg);
		$('#txtheader').attr('src', 'images/txt_header_' + thisnewimg + '.png');
		$('#txtcopy').attr('src', 'images/txt_copy_' + thisnewimg + '.png');
		$('#txtlink a img').attr('src', 'images/txt_button_' + thisnewimg + '.png');
		thislink = "our-story-heritage-belief.php";
		if(thisnewimg == "savour") {
			thislink = "in-their-words.php";
		}
		else if(thisnewimg == "savour") {
			thislink = "in-their-words-canadian-mountain-holidays.php";
		}
		else if(thisnewimg == "canada") {
			thislink = "standards-canadian-beef-advantage.php";
		}
		else if(thisnewimg == "natural") {
			thislink = "our-story-healthy-animals.php";
		}
		else if(thisnewimg == "superior") {
			thislink = "standards-quality.php";
		}
		else if(thisnewimg == "range") {
			thislink = "our-story-sustainability.php";
		}
		$('#txtlink a').attr('href', thislink);
		$('#loading').slideUp("500");	
	});
	*/
	var d = new Date();
	//alert('/images/home_' + thisnewimg + '.jpg?' + d.getTime());
	var tmpImg = '/images/home_' + thenewimg + '.jpg?' + d.getTime();
	$('<img />').attr('src', tmpImg).bind('load', function(){
		//alert("here");
		$('#mainbg').attr('src', tmpImg);
		$('#loading').slideUp("500");
	});
}
	
$(document).ready(function() {	
	
	/*
	Background Resizing
	*/
	
	var theWindow = $(window),
	$bg = $("#mainbg"),
	aspectRatio = $bg.width() / $bg.height();
	
	function resizeBg() {
		if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
			$bg.removeClass().addClass('bgheight');
		}
		else {
			$bg.removeClass().addClass('bgwidth');
		}
	}
	
	theWindow.resize(function() {
		resizeBg();
	}).trigger("resize");


	//footer logo over state
	$('.hoverimg').mouseover(function() {
		if($(this).is('img') || $(this).is('input')){
		  $(this).fadeTo(225, 0.7);
		}
		else {
			var tmpClass = $(this).attr("class");
			$("." + tmpClass + " img").fadeTo(225, 0.7);
		}
	});
	
	$('.hoverimg').mouseout(function() {
		if($(this).is('img') || $(this).is('input')){
		  $(this).fadeTo(225, 1.0);
		}
		else {
			var tmpClass = $(this).attr("class");
			$("." + tmpClass + " img").fadeTo(225, 1.0);
		}
	});

	$('#bo_button').click(function() {
		if($('#bo_button a').text().indexOf("More") >= 0) {
			$('#bo_button a').text("Close X")
		}
		else {
			$('#bo_button a').text("More +")
		}
		$("#bo_content_display").slideToggle("800");	
	});
	
	/*
	imgs
	currItem
	*/
	/*
	$("#home #link_prev img, #home #link_next img").click(function() {
		//thisnewimg = $(this).attr("id");
		
		if($(this).attr("src").indexOf("right") >= 0) {
			currItem += 1;
		}
		else {
			currItem -= 1;
		}
		if(currItem < 0) {
			currItem = imgs.length - 1;
		}
		else if(currItem >= imgs.length) {
			currItem = 0;
		}
		thisnewimg = imgs[currItem];
		$("#bo_content_display").slideUp("800");
		$('#bo_button a').text("More +")
		$('#loading').slideDown("500");	
		//alert("here" + thisnewimg);
		setTimeout("completeTrans()",500);
		
	});
	*/
	$("#link_prev img, #link_next img, #txtlink a img").mouseover(function() {
		$(this).fadeTo(100, 0.75);
	});
	
	$("#link_prev img, #link_next img, #txtlink a img").mouseout(function() {
		$(this).fadeTo(100, 1.0);
	});
	
	/*  Hash Changes */
	 var newHash      = "",
        $mainContent = $("#bottom_overlay_replace"),
        $pageWrap    = $("body"),
        baseHeight   = 0,
        $el;
        
    /*
	$("body:not(#home,#bottom_actionbar_show,#bottom_actionbar_hide)").delegate("a[target!='_blank']:not(#footer_homelink,#header_homelink,.contact,.emaillink,#pagelink_home,.nyroModal)", "click", function() {
        if($(this).attr("id") != "bottom_actionbar_show" && $(this).attr("id") != "bottom_actionbar_hide") {
			window.location.hash = $(this).attr("href");
			return false;
		}
    });
	*/
    
	$(window).bind('hashchange', function(){
		//alert("Hash Change");
        newHash = window.location.hash.substring(1);
        if (newHash) {
			$('#loading').slideDown("500");	
			//alert("index.php?view=" + newHash + "#bottom_overlay");
			$mainContent.load("index.php?view=" + newHash + " #bottom_overlay_replace", function() {
				$('#bo_button a').text("More +")
				var prevlink  = $('#newlastpage').val();
				var nextlink = $('#newnextpage').val();
				$("#link_next a").attr("href", "#" + nextlink);
				$("#link_prev a").attr("href", "#" + prevlink);
				/*
				
				*/
				/*
				if(prevlink == "index.php") {
					$("#link_prev a").attr("id", "pagelink_home");
				}
				else {
					$("#link_prev a").attr("id", "");
				}
				if(prevlink != "") {
					$("#link_prev a").attr("href", prevlink);
					$("#link_prev").show(500);
				}
				else {
					$("#link_prev").hide(500);
				}
				if(nextlink == "index.php") {
					$("#link_next a").attr("id", "pagelink_home");
				}
				else {
					$("#link_next a").attr("id", "");
				}
				if(nextlink != "") {
					$("#link_next a").attr("href", nextlink);
					$("#link_next").show(500);
				}
				else {
					$("#link_next").hide(500);
				}
				*/
				/*
				End swap
				*/
				var newbgimg = $('#newbgimg').val();
				setTimeout(function(){completeTrans(newbgimg)},500);
				//setTimeout("completeTransSub()",500);
			});
			_gaq.push(['_trackPageview', "index.php?view=" + newHash]);
        };
    });
    
    $(window).trigger('hashchange');
	
});
