// JavaScript Document

function setupPage(){
		$("#overlay").css({"display":"block"}).addClass("loading");
		$("#logoouter").css({'z-index':'1100'});
		
		
		// Manipulate scrolls bars on the page
		$('body').css({'overflow':'hidden'});
		$('html').css({'overflow':'hidden'});
		// Create and set values for the window height
		var windowHeight = $("#wrapper").height();
		$("a").removeAttr("title");
		
		// Get varibles from post on page

		var bgurl = $(".imgurl").text();
	

		// Create and set content margin values
		
		var hozmargin = 50;
		hozmargin = Math.round(windowHeight/10);
		if (hozmargin < 50) {
			hozmargin = 50;
		}
		
		
		// Create and set values for text size 
		var textHeight = 11;
		if (windowHeight > 800) {
			textHeight = 12;
		}
		if (windowHeight > 900) {
			textHeight = 13;
		}
		var lineheight = Math.round(textHeight * 1.4);
		
		//Create and set logoheight and menuheight values 
		var logoheight = Math.round(windowHeight * 0.2);
		var menuheight = Math.round(lineheight * 10);
		
		// Change text size on body
		$("body").css({"font-size":textHeight+"px", "line-height":lineheight+"px"});
		
		// Setup menus
		$("#mainmenu").children("li").children("a").addClass("primary");
		$(".temp").remove();
		$("li.menutitle").remove(); //removes the menu title
		$("#mainmenu").css({"display":"block", "position":"absolute"});
		$(".submenu").css({"display":"none", "position":"absolute", "left":($("#mainmenu").width()+50)+"px"});
		$("li:first").addClass("selected");
		$("li:first").addClass("current");
		$("li").removeClass("current-cat");

		
		// Set height values for each main element and position other elements accordingly
		
		$("#logoouter").css({"height":logoheight+"px"});
		$("#menuouter").css({"height":menuheight+"px", "top":logoheight+"px"});
		
		// Set margin values for logo and menu
		$("h1").css({"left":hozmargin+"px", "top":((logoheight/2)-20)+"px"});
		$("#menu").css({"left":hozmargin+"px"});
	
		// load image	
		
		var imgbg = new Image();
		$(imgbg).load(function () {
			
			$("#slidingbg").append(imgbg);
			$("#overlay").fadeOut(2400);
			$("#slidingfg").height(0).stop().delay(2400).animate({"height":(windowHeight-$("#logoouter").height())+"px"},1200);

			
		})

		.error(function () {
			$("#overlay").fadeOut(2400);
		});
		$(imgbg).attr({'src': bgurl});
};

					
$(document).ready(function(){
	var minWidth=parseInt($(".imgwidth").text());
	if (minWidth > 3000){
		minWidth = 3000;
	} else if (minWidth < 0) {
		minWidth=0;
	};
	var centerImage = false;
	if ($(".imgposition").text() == "Center") {
		centerImage = true;
	} else if ($(".imgposition").text() == "Bottom-right") {
		$("#slidingbg").css({"bottom":"0px","right":"0px"});
	} else {
		$("#slidingbg").css({"top":"0px","left":"0px"});
	};
	setupPage();
	resizeImage();

	$(window).resize(function() {
	resizeImage();
	});
	if ($(".homecol").text() == "Black"){
		$("body").removeClass("white").addClass("black");
	};
	function resizeImage(){
		var windowHeight = $("#wrapper").height();
		var windowWidth = $("#wrapper").width();
		var imageRatio = 2130 / 1200;
		var minHeight = minWidth/imageRatio;
		var windowRatio = windowWidth/windowHeight;
		if (windowRatio < imageRatio) {
			$("#slidingbg").width(windowHeight * imageRatio).height(windowHeight);
			if ($("#slidingbg").height() < minHeight) {
				$("#slidingbg").width(minHeight * imageRatio).height(minHeight);
			};
		} else {
			$("#slidingbg").width(windowWidth).height(windowWidth / imageRatio);
			if ($("#slidingbg").width() < minWidth) {
				$("#slidingbg").width(minWidth).height(minWidth / imageRatio);
			};
		};
		if (centerImage == true) {
			$("#slidingbg img").css({"left":((windowWidth - $("#slidingbg").width())/2)+"px","top":((windowHeight - $("#slidingbg").height())/2)+"px"});
		};
		$("#slidingfg").height(windowHeight-$("#logoouter").height()).width(windowWidth).css({"bottom":"0px"});
		
	};
	var mouseAbove = false;
	$('body').mousemove(function(event) {
		var windowHeight = $(window).height();
		var mouseY = event.pageY;
		var topPos = $("#logoouter").height()+$("#menuouter").height();
		if (mouseY < topPos && mouseAbove == false) {
			
			var newHeight = (parseInt(windowHeight-$("#logoouter").height()-$("#menuouter").height()))+"px";
			mouseAbove = true;
			$("#slidingfg").stop().animate({
				height: newHeight
			}, 1200);
			
		} 
		if (mouseY > topPos && mouseAbove == true) {
			var newHeight = (parseInt(windowHeight-$("#logoouter").height()))+"px";
			mouseAbove = false;
			$("#slidingfg").stop().animate({
				height: newHeight
			}, 1200);
		};
	});
	// setup actions for flyout menu
	$("a").click(function(e){
		
		if($(this).hasClass("follow")) {
		} else {
			if($(this).hasClass('expand')) {
				var index = $(".expand").index(this);
				$(".submenu:visible").hide();
				$(".submenu:eq("+index+")").show();
				$("li").removeClass("selected");
				$(this).parent("li").addClass("selected");
				return false;
			} else if($(this).parent("li").hasClass("current")) {
				return false;
			} else {
				if($(this).hasClass("primary")) {
					$("li").removeClass("selected");
					$(this).parent("li").addClass("selected");
				} else {
					$(this).parent("li").addClass("selected");
				};
				$("#slidingfg").unbind('mouseenter mouseout');
				var address=$(this).attr("href");
				$("#menuouter").css({'z-index':'9000'});
				$("#logoouter").css({'z-index':'9000'});
				$("#overlay").css({'z-index':'8000'}).stop().removeClass("loading").fadeIn('slow', function(){
					window.location=address;
					return false;
				});
			};
		};
	});	
});

