$(document).ready(function() {
	// Hover Navigation
	$("#menu li").hover(
		function() { $("ul", this).slideDown("fast"); }, 
		function() { $("ul", this).hide(); }
	);	
	if (document.all) {
		$("#menu li").hoverClass ("hover");
	}
	
	// My BCF
	if($(".logged-in").length>0) {
		$(".my-bcf h2").hover(
			function() { 
				if($(this).parent().find(".options").attr("class").match("hide")) {
					$(this).parent().addClass("my-bcf-expanded"); 
					$(this).parent().append('<div class="info-tooltip">Click to use your personalised menu!</div>');
				}
			},
			function() { 
				$(this).parent().removeClass("my-bcf-expanded") ;
				$(this).parent().find(".info-tooltip").remove();
			}
		);
		$(".my-bcf h2 a").click( 
			function() {
				$(this).parents(".my-bcf:first").toggleClass("my-bcf-expanded");
				$(this).parents().find(".info-tooltip").remove();
				
				($(this).parents(".my-bcf:first").find(".options").attr("class").match("hide") ? showMyBCF() : hideMyBCF())
				
				return false;
			} 
		);
		/*
		$(".my-bcf").hover(
			function() {
				$(this).addClass("my-bcf-expanded");				
				showMyBCF();
			},
			function() {
				$(this).removeClass("my-bcf-expanded");				
				hideMyBCF()
			}
		);
		*/
	}
	if($("h1").text().match("Club BCF")) {
		showMyBCF();
		setTimeout(hideMyBCF, 3800)
	}
	
	$(".equalise").vjustify();
	
	// Tab Set Behaviour
//	$(".tabs-inside ul.tabs li, .tabs-inside ul.mini-tabs li").click(
//		function() { 
//			// reset elements
//			$(this).parent().children("li").each(function(i){ 
//				$(this).removeClass("selected");
//				var menuText = $(this).text();
//				$(this).empty();
//				$(this).append('<a href="'+window.location+'#'+urlCleanFormat(menuText)+'Info">'+menuText+'</a>');
//			});
//			$(this).addClass("selected");
//			
//			// Show the related container that contains the content
//			$(this).parents(".tabs-inside").find(".infocontianer, .mini-infocontainer").addClass("hide");
//			$("#"+urlCleanFormat($(this).text()) + "Info").toggleClass("hide");
//			
//			return false;
//		}
//	);
	
	// Accordian
	$(".accordian h3, .accordian h2").click(
		function() {
			if(!$(this).parent().attr("class").match("expanded")) {
				// Hide currently expanded
				$(this).parents("ul").find("li.expanded h2, li.expanded h3").next().slideUp("slow");
				// Remove expanded class
				$(this).parents("ul").find("li").removeClass("expanded");
				// Slide new one
				$(this).next().slideDown("slow").removeClass("hide");
				// add expanded class
				$(this).parent().addClass("expanded");
			}
		}
	);
	
	// Bragboard
	$(".bragboard-rating-area ol li").hover(
		function() { $(this.parentNode).addClass("hover"+this.className); }, 
		function() { $(this.parentNode).removeClass("hover"+this.className); }
	);
	$(".bragboard-rating-area ol a").click(
		function() { 
			fromLatestCatchesBox = false;
			picId = parseInt(this.parentNode.parentNode.id.replace("bid", ""));
			if(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id=="latestBragboard")
			{
				$("<span class='saving'><small>Saving...</small><img src='media/images/bragboard/loader-onwhite.gif' style='margin: 0 0 0 5px; border: 0;' class='fleft' /></span>").insertBefore(this.parentNode.parentNode.parentNode); 
				picId = parseInt(this.parentNode.parentNode.id.replace("lbid", ""));
				fromLatestCatchesBox = true;
			}
			else
			{			
				$("<span class='saving'><small>Saving...</small><img src='media/images/bragboard/loader.gif' style='margin: 0 0 0 5px; border: 0;' class='fleft' /></span>").insertBefore(this.parentNode.parentNode.parentNode); 
			}
			$(this.parentNode.parentNode.parentNode).addClass("offleft"); 
			
			classToPass = parseInt(this.parentNode.className.replace("rating", ""));
			//saveScore(idToPass, classToPass);
			//code from save score			
			var guid = getCookie("BRBGUID");			
			BCF.CmsModules.ImageGallery.wcListing.submitRating(picId,classToPass,guid);	
			//code from save score
			var avgScore = getAvgRating(picId);			
			var avgScoreRounded = getAverageRoundedForStyling(avgScore.value);			
			//updateValues(17, 2, 2.34);
			
			updateValues(picId, avgScoreRounded.value, avgScore.value, fromLatestCatchesBox);
			
			return false; 
		}
	);
	
	// Flash :: FishID
	if(document.getElementById('fishid')) {
		var version = deconcept.SWFObjectUtil.getPlayerVersion();
	
		if (version['major'] > 8) {
			var so = new SWFObject("media/flash/fishid.swf", "Fish ID", "505", "720", "8", "#ffffff");	 
			so.addParam("wmode", "transparent");
			so.addVariable("currentFunction", document.getElementById('fishid').className);
		    so.write("fishid");	
		}
		else
		{
			var so = new SWFObject("media/flash/fishid_updater.swf", "Fish ID", "505", "720", "5", "#ffffff");
			so.addParam("wmode", "transparent");
			so.addVariable("versionNumber", version['major']);
			so.write("fishid");
		}
	}
	if(document.getElementById('specie')) {
		imgSrc = "media/contentresources/fishid/fishimages/"+document.getElementById('specie').className+".jpg";
	
		var so = new FlashObject("media/flash/fishid_specie.swf", "so_specie", "400", "240", "7", "#ffffff");	   
		so.addVariable("fv_species_source",imgSrc);
		so.addParam("menu", "false");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("wmode", "transparent");
		
		so.write("specie");	
	}
	if(document.getElementById('habitat')) {
		imgSrc = "media/contentresources/fishid/habitatimages/"+document.getElementById('habitat').className+".jpg";
	
		var so = new FlashObject("media/flash/fishid_habitat.swf", "so_habitat", "130", "140", "7", "#ffffff");	   
		so.addVariable("fv_habitat_source",imgSrc);
		so.addParam("menu", "false");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("wmode", "transparent");
		
		so.write("habitat");
	}
	// Flash :: DigiBook
	if(document.getElementById('digiBook')) {	
		var so = new FlashObject("media/flash/flipbook.swf", "so_digibook", "885", "646", "7", "#ffffff");
		so.addParam("menu", "false");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("wmode", "transparent");
		so.addVariable("fv_cataloguePath", $("#digiBook").attr("rel"));
		
		so.write("digiBook");	
	}
	// Flash :: FishingClassic
	if(document.getElementById('fishingGame')) {	
		var so = new FlashObject("media/flash/loader.swf", "mymovie", "750", "550", "7", "#043152");	   
		so.addVariable("targetSwf","media/flash/bcf_fishing.swf");
		so.addVariable("sendFriendURL","http://sendtofriend.url.com");
		so.addVariable("bonusTimeMultiplier","700");
		so.addVariable("trackingURL","media/flash/bcf_fishing.txt");
		so.addParam("wmode", "transparent");
		
		so.write("fishingGame");	
	}
	//Flash :: Happy Hour Specials
	if(document.getElementById('happyHourCountdown')) {	
		happyHourCountDownOff();
//		setTimeout("happyHourCountDownOn()",6000);
	}
	 
	// Flash :: Brag board rotator
	if(document.getElementById('bragBoardRotator')) {	
		var so = new FlashObject("media/flash/brag-board-rotator.swf", "so_brag-rotator", "202", "104", "7", "#ffffff");	   
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("wmode", "transparent");
		
		so.write("bragBoardRotator");
	}
	// Flash :: Header Flash
	if(document.getElementById('flashHeader')) {	
		var so = new FlashObject("media/flash/header.swf", "so_specie", "934", "139", "8", "#ffffff");	   
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("wmode", "transparent");
		
		so.write("flashHeader");
		$("#flashHeader").css("backgroundImage", "none");
	}
	
	
	
	// Weather Forecast
	if(!$(".site").attr("class").match("logged-in") && document.getElementById("weatherForecast")) rotateWeather();
	
	// Member Promotion
	buildTheMemberPromotion();
	
});

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};


function happyHourCountDownOn() {
	var so = new FlashObject("media/flash/countdown.swf", "so_specie", "466", "205", "7", "#ffffff");	   
	so.addParam("wmode", "transparent");
	so.write("happyHourCountdown");	
	setTimeout("happyHourCountDownOff()",15000);//10000
}

var lastRotatedCountDownImage = 0;
var replacementImage = new Array();

replacementImage[0] = new Array();
replacementImage[0][0] = '<h2>Tent Guide</h2><a class="accessible-target" href="/Products/TentGuide/index.aspx" title="Tent Guide">Tent Guide</a>';
replacementImage[0][1] = '/media/images/placeholder-tent-guide.jpg';
replacementImage[0][2] = 'primary2 column column-by2';


replacementImage[1] = new Array();
replacementImage[1][0] = '<h2>Online Store</h2><a href="https://store.bcf.com.au" class="accessible-target">Visit Online Store</a>';
replacementImage[1][1] = '/media/images/placeholder-happy-hour.jpg';
replacementImage[1][2] = 'primary column column-by2';

if (document.images) {
  var preloadPlaceholderImage = new Image(466,205); 
  preloadPlaceholderImage.src = '/media/images/placeholder-tent-guide.jpg'; 
}

function happyHourCountDownOff() {
	
//	lastRotatedCountDownImage = lastRotatedCountDownImage == 0 ? 1 : 0;
	
	document.getElementById('happyHourCountdown').innerHTML = replacementImage[lastRotatedCountDownImage][0];
	document.getElementById('happyHourCountdown').background = replacementImage[lastRotatedCountDownImage][1];
	document.getElementById('happyHourCountdown').className = replacementImage[lastRotatedCountDownImage][2];
	setTimeout("happyHourCountDownOn()",4000);//6000
}


function urlCleanFormat(s) {
	s = s.replace(/\s/g, "").toLowerCase();
	s = s.replace(/&/g, ""); 
	s = s.replace(/\//g, ""); 
	s = s.replace(/'/g, ""); 
	return s;
};

jQuery.fn.vjustify=function() {
    var maxHeight=0;
    this.each(function(){
        if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
    });
    this.each(function(){
        $(this).height(maxHeight + "px");
        if (this.offsetHeight>maxHeight) {
            $(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
        }
    });
};

function rotateWeather() {
	if($("#weatherForecast").attr("class").match("active")) {
		var i = 0; 
		var count=0;
		var found=false;
		$("#weatherForecast").find("dl dt").each(
			function() {
				if(!$(this).attr("class").match("offleft")) found=true;
				if(!found) i++;
				count++;
			}
		);
		var nextPosition = (i<count-1 ? (i+1) : 0);
		$("#weatherForecast dt:eq("+i+"), #weatherForecast dd:eq("+i+")").hide().addClass("offleft");
		$("#weatherForecast dt:eq("+nextPosition+"), #weatherForecast dd:eq("+nextPosition+")").removeClass("offleft").fadeIn("fast");
	} else {
		$("#weatherForecast").addClass("active");
	}
	setTimeout ("rotateWeather()",5000);
}

function buildTheMemberPromotion()
{
	if(document.getElementById("memberPromotion"))
	{
		document.getElementById("memberPromotion").innerHTML = '<iframe width="572" height="1280" src="http://old.bcf.com.au/index.cfm?fuseaction=competition.main" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>';
	}
}

function showMyBCF() {	
	$(".my-bcf").addClass("my-bcf-expanded");
	$(".my-bcf .options").removeClass("hide").slideDown("slow");
}
function hideMyBCF() {	
	$(".my-bcf").removeClass("my-bcf-expanded");
	$(".my-bcf .options").fadeOut("slow").addClass("hide");
}



