// JavaScript Document
$(document).ready(function(){
	// CHecks if this page supports slideshows
	var slideshowActive = true;
	var windowLocation = ""+window.location+"";
	var windowLocationArray = (windowLocation).split('#'); // split window location to remove hash url
	if ($("#lc_site_content").hasClass("lc_no_slide") == true) {
		slideshowActive = false;
	};

	// HEADER HOVER
	$("#lc_site_header").hoverIntent(unfadeContact, fadeContact);
	function unfadeContact(event) {
		$(this).children("#lc_contact_rollover").fadeIn(500);
	};
	function fadeContact(event) {
		$(this).children("#lc_contact_rollover").fadeOut(500);
	};
	// Append colour navigation option to menu
	var colorNavHtml = '<li><a id="color_change" href="#">Colour</a></li>';
	$("#lc_site_functions").append(colorNavHtml);
	
	// colour change click function
	$("#color_change").click(function(){
		$("#lc_color_flash").stop().fadeIn(250, function(){
			var winLocation = window.location.hash; // get the hash url
			var switchStyle = parseInt(winLocation.indexOf("black"));
			// swap hash url and colour schem to opposite colour
			if (switchStyle == -1) {
				colorSchemeStatus = "color-scheme=black";
				document.documentElement.className = "black";
			} else {
				colorSchemeStatus = "color-scheme=white";
				document.documentElement.className = "white";
			}
			$("#lc_color_flash").fadeOut(1500); // fade out the overlay
			newHashUrl = hash+colorSchemeStatus+slideStatus;// change the hash url
			window.location.hash = newHashUrl; // change the window location
		});
		return false;
	});
	
	// slide show click function - not the slideshow button is added by the script for that page if needed
	$("#slide_show").live('click', function(){
		if (slideshowActive == true) {
			var winLocation = window.location.hash;
			// swap the window location to the opposite value
			var slidesOn = parseInt(winLocation.indexOf("true"));
			if (slidesOn == -1) {
				$("#slide_show").addClass("active_nav");
				slideStatus = "&slideshow=true";
			} else {
				slideStatus = "&slideshow=false";
				$("#slide_show").removeClass("active_nav");
			};
			newHashUrl = hash+colorSchemeStatus+slideStatus;
			window.location.hash = newHashUrl;
		};
		return false;
	});
	
	$(".no_click").live('click', function(){
		return false;
	});

	// rewrite the hash url and window location is this information is not present
	var colorSchemeStatus = "";
	var slideStatus = "";
	var hash = "#";
	var newHashUrl = "";
	rewriteHashUrl();
	
	function rewriteHashUrl(){
		var winLocation = window.location.hash;
		var slidesOn = parseInt(winLocation.indexOf("true"));
		
		if (slidesOn != -1 || $("#lc_site_content").hasClass("home")) {
			slideStatus = "&slideshow=true";
		} else {
			slideStatus = "&slideshow=false";
		}
		if (slideshowActive == false) {
			slideStatus = "&slideshow=false";
			$("#slide_show").removeClass("active_nav");
		}  else {
			$("#slide_show").addClass("active_nav");
		};
		var colorScheme = parseInt(winLocation.indexOf("black"));
		if (colorScheme != -1) {
			colorSchemeStatus = "color-scheme=black";
		} else {
			colorSchemeStatus = "color-scheme=white";
		}
		
		newHashUrl = hash+colorSchemeStatus+slideStatus;
		if (newHashUrl != winLocation) {
			window.location.hash = newHashUrl;
		}
	};
	
	// don't know but this seems to resolve browser caching issues hen the user hits the back button
	jQuery(window).bind("unload", function() {
	});
	
	// Check if any of the navigation options are active
	var windowLocation = ""+window.location+"";
	var windowLocationArray = (windowLocation).split('#'); // split window location to remove hash url
	windowLocation = windowLocationArray[0];
	$("#lc_site_menu").find("a").each(function(){
		var linkUrl = $(this).attr("href");
		if (linkUrl == windowLocation) {
			$(this).addClass("active_nav");
			$(this).addClass("no_click");
			if ($(this).hasClass("lc_studio_submenu")) {
				$("#lc_studio_menu").css("display","block");
			};
		}
	});
	$(".lc_page_link a").live('click', function() {
		if (!$(this).hasClass("inactive")) {
			$(this).addClass("active_nav"); 
			slideStatus = "&slideshow=false";
			var newUrl = $(this).attr("href");
			newHashUrl = hash+colorSchemeStatus+slideStatus;
			var gotoLocation = newUrl.concat(newHashUrl);
			$("#lc_site_content").fadeOut(1000, function() {
				window.location = gotoLocation;
			});
		};
		return false;
	});
	$("#lc_studio_toggle").live("click", function(){
		$("#lc_studio_menu").stop().slideToggle(500);
		return false;
	});
	
	checkSiteColors();
	function checkSiteColors(){
		var winLocation = window.location.hash; // get the hash url
		var switchStyle = parseInt(winLocation.indexOf("black"));
		// swap hash url and colour schem to opposite colour
		if (switchStyle == -1) {
			if (colorSchemeStatus != "color-scheme=white") {
				document.documentElement.className = "white";
			}
		} else {
			if (colorSchemeStatus != "color-scheme=black") {
				document.documentElement.className = "black";
			}
		}
		setTimeout(checkSiteColors, 750);
	};

});
