var position = 0;
var width = 0;
var height = 0;
var total = 0;
var toolbarheight = 0;
var outerheight = 0;
var state = "home";
var urlhash = location.hash.replace(/#/,"");

$(window).resize(function(){
	centerBackground();
});

$(document).ready(function() {
	//Dimension Independence
	total = $("#slider ul .child").length; //total number of LIs
	width = $("#slides .child").width() + parseFloat($("#slides .child").css("margin-right")); // calculate width of LI + MARGIN		
	height = $("#slides .child").height();
	toolbarheight = $("#toolbar").height();
	outerheight = height + toolbarheight;	

	$("#container").css("height", outerheight);
	$("#slider #slides").css("width", width * total); //set width of #slides UL
	$("#home-icons").css("height", outerheight); //Sets height of home menu


	//Center PDA verticallly and horizontally onload
	$("#bg").vAlign().hAlign(); //http://www.nealgrosskopf.com/tech/thread.asp?pid=37


	//Center html background-image verticallly and horizontally onload
	centerBackground();


	//Enables bookmarking of slides
	$("#home-icons a").each(function(i){
		if ( $(this).attr("href").replace(/#/,"") == urlhash ) { selectSlide(i); }
	});
	

	//Home button click toggling
	$("#home-button").click(function(){
		if(state == "home")
		{
			$("#home-icons").animate({height: outerheight}, 200);
			$("#home-button").attr("href","#home");
			$("#home-button").html("Close Screen");
			state = "close";
			setGoogleAnalytics("home");
			ieToolbarBug();
		}
		else
		{
			$("#home-icons").animate({height: 0}, 200);
			$("#home-button").attr("href","#close");
			$("#home-button").html("Home Screen");
			state = "home";
		}
	});
	
	$("#logo").click(function(){
		window.location = "http://www.speedqueen.com/comm/";
	});

	//Home icon bobbing effect
	$("#home-icons img").hover(function(){
		$(this).animate({bottom: 5}, 200);
	},
	function(){
		$(this).animate({bottom: 0}, 200);
	});


	//Clock
	updateTime();
	setInterval("updateTime()", 1000);


	//Parent slide dynamic click binding - http://www.bennadel.com/blog/534-The-Beauty-Of-The-jQuery-Each-Method.htm
	$("#home-icons img").each(function(i){
		$(this).bind("click", function(){ selectSlide(i); });
	});


	//Gold Mine
	$("#goldmine li").each(function(i){
		$(this).bind("click", function(){
			$("#goldmine li:nth-child("+(i+1)+") p").slideToggle(200);
			var selector = "#goldmine li:nth-child("+(i+1)+") h3";
			if ($(selector).css("backgroundPosition") == "0% 100%")
			{
				$(selector).css("backgroundPosition","0% 0%");
			}
			else
			{
				$(selector).css("backgroundPosition","0% 100%");
			}
		});
	});


	//Distributor Locator
	$("#locator button").click(function() {
		setGoogleAnalytics("distributors/search");
	});


	//Green Mail
	//$("#email a").each(function(i){
	$("#email tbody tr").each(function(i){	
		$(this).bind("click", function(){ selectMessage(i); });
	}).click(function() {
		$(this).attr("class","read");
		$("#email").fadeOut(200);
	});
	
	$(".email-date").each(function(i){	
		var input = $(this).html().split("/");		
		var arr = jQuery.makeArray(input);
		
		var month = arr[0];
		var day = parseFloat(arr[1]);
		var yeartime = arr[2];
		
		var date = new Date();
		currday = date.getDate();
		
		if(i == 0) uboundday = day; //Assumes first email has highest day
		
		//if(currday > uboundday) month = month - 1;
		
		day = day + Math.abs(currday - uboundday); //Staggers dates
		
		$(this).text(month + "/" + day + "/" + yeartime);	
	});	
	

	$("#message-back a").click(function(){
		$("#messages li").css("display","none");
		$("#message-back").css("display","none");
		$("#messages table").remove();
		$("#messages h3").remove();
		$("#email").fadeIn(200);
	});


	//Forcast
	$("#forcasts li:nth-child(" + Math.floor(Math.random()*9+1) + ")").css("display","block"); //http://www.shawnolson.net/a/789/make-javascript-mathrandom-useful.html	
});


//Vertical Align JQuery Plugin
(function ($) {
	$.fn.vAlign = function(){
		return this.each(function(i){
			var h = $(this).height();
			var oh = $(this).outerHeight();
			var mt = (h + (oh - h)) / 2;
			$(this).css("margin-top", "-" + mt + "px");
			$(this).css("top", "50%");
			$(this).css("position", "absolute");
		});
	};
})(jQuery);


//Horizontal Align JQuery Plugin
(function ($) {
	$.fn.hAlign = function(){
		return this.each(function(i){
			var w = $(this).width();
			var ow = $(this).outerWidth();
			var ml = (w + (ow - w)) / 2;
			$(this).css("margin-left", "-" + ml + "px");
			$(this).css("left", "50%");
			$(this).css("position", "absolute");
		});
	};
})(jQuery);


function centerBackground()
{
	//Background doesn't work in Mac-Safari
	var macsafari = false;
	if (navigator.platform.indexOf("Mac")!=-1)
	{
		if (navigator.vendor.indexOf("Apple")!=-1) macsafari = true;
	}
	
	if(macsafari == false)
	{
		var pos = "-" + (1201 - $(window).height())/2 + "px";
		$("html").css("backgroundPosition", "center " + pos);
	} else {
		var pos = "-" + (1201 - $(window).height())/2 + "px";
		$("html").css("backgroundPosition", "center " + pos);
		//alert("1201 - "+$(window).height()+"/2 = "+pos);
	}
}


function setGoogleAnalytics(urlname)
{	
	try
	{
		pageTracker._trackPageview("/" + urlname.replace(/#/,"") + "/" );
	}
	catch(err)
	{
	}
}


function ieToolbarBug()
{
	//Rendering bug in IE6. This is my best solution to overcoming it.
	if ($.browser.msie && $.browser.version <= 6 ) $("#toolbar").css("bottom",-1);
}


function selectSlide(p)
{
	setGoogleAnalytics($("#icon-"+p).parent().attr("href")); //gets href attribute from parent of clicked element
	ieToolbarBug();

	position = -(p * width) + "px";
	var speed = 300;

	//Home state change
	$("#home-icons").animate({height: 0}, {queue: true, duration: 200});
	$("#home-button").html("Home Screen");
	state = "home";

	$("#slides .child").css("overflow", "hidden"); //temporarily hide scrollbars during animation

	/* Testing:	0px | -642px | -1284px | -1926px | -2568px | -3210px | -3852px | -4494px - alert(position + " = -" + p + " * " + width + "px"); */
	$("#slider #slides").animate({left: position}, {queue: true, duration: speed}, function() {} );

	setTimeout('$("#slides .child").css("overflow", "auto")', speed); //show scrollbars after animation is finished
}


function updateTime()
{
	//http://www.quackit.com/javascript/javascript_date_and_time_functions.cfm
	var date = new Date();
	var h = date.getHours();
	var m = date.getMinutes();
	var s = date.getSeconds();
	var ampm = "AM";

	if(h > 11) ampm = "PM";
	if(h > 12) h = h - 12;
	if(m < 10) m = "0" + m;
	if(s < 10) s = "0" + s;

	$("#time").html(h + ":" + m + ":" + s + " " + ampm);
}


function selectMessage(i)
{
	i = i + 1;

	var email = "#email tbody tr:nth-child("+i+")";
	var message = "#messages li:nth-child("+i+")";

	var subject = $(email + " a").text();
	var date = $(email + " .email-date").text();

	//http://snook.ca/archives/javascript/multi-line-javascript
	var output = '<table> \
	<tr class="message-from"><td style="width: 50px;"><b>From:</b></td><td><a href="mailto:william.bittner@alliancels.com">Bill Bittner</a></td></tr> \
	<tr class="message-to"><td><b>To:</b></td><td>Quantum User</td></tr> \
	<tr class="message-date"><td><b>Date:</b></td><td>'+date+'</td></tr> \
	<tr class="message-subject"><td><b>Subject:</b></td><td>'+subject+'</td></tr> \
	</table>';	

	$(message).prepend(output);

	$(message).css("display","block");
	$("#message-back").css("display","block");
}