$(document).ready(function(){
	
	// Hide and show the offers and payoff elements depending on cookie
	var offers_shown = stringToBool((readCookie('showoffers')==null) ? 'true' : readCookie('showoffers'));
	var payoff_shown = stringToBool((readCookie('showpayoff')==null) ? 'true' : readCookie('showpayoff'));
	
	$("#offers td").css('cursor', 'pointer');
	$("#offers a:hover").css('background', 'none');
	
	if (!(offers_shown)) {
		$("#offers").hide();
		$("#showoffers").parents("ul.sidebar-wrapper").show();
	}
	
	if (!(payoff_shown)) {
		$("#payoff").hide();
		$("#showpayoff").parents("ul.sidebar-wrapper").show();
	}
	
	// Show the payoff or offers element when selected in side menu
	// Remove the cookie
	$("a.#showpayoff, a.#showoffers").click(function(e){
		e.preventDefault();
		$(this).parents("ul.sidebar-wrapper").hide();
		var id = $(this).attr("id");
		eraseCookie(id);
		var element_id = id.substring(4);
		$("#"+element_id).fadeIn().animate({ backgroundColor: "#ff6600"}, 1)
			.animate({ backgroundColor:"white"}, 1000);
		$("#"+element_id+" td").each(function(){
			var color = $(this).css("background-color");
			$(this).animate({ backgroundColor: "#ff6600" }, 500)
			.animate({backgroundColor: color}, 500);
		});
		scroll(0,0);
	})
	
	// Hide the payoff or offers element when close button is clicked. 
	// Set the cookie.
   	$("a.close-button, .close-image").click(function(){
     	$(this).parents(".can-close").fadeOut();
		var id = 'show' + $(this).parents(".can-close").attr("id");
		createCookie(id, 'false', 1);
		$("#"+id).parents("ul.sidebar-wrapper").show()
			.animate({ backgroundColor: "#ff6600" }, 500)
			.animate({ backgroundColor: "black" }, 500);
   	});

	// Show the lightbox window when selecting an offer
	$("#offers td").click(function(e){
		
		e.preventDefault();
		// Set height and width to mask to fill up whole screen
		var maskHeight = $(document).height() + 25; // add 25px margin on top of #page element
		var maskWidth = $(window).width();
		$("#modal-overlay").css({'width':maskWidth, 'height':maskHeight});
		$("#modal-overlay").css({'left':-(maskWidth - 962)/2})
		$('#modal-overlay').show();
		
		//Set the popup window to center
		var id = $(this).attr('name');
		var winW = $('#page').width();
		var winH = $(window).height();
		var vertOffset = 200;
		$(id).css('left', winW/2-$(id).width()/2);
		var topPosition = winH/2 - $(id).height()/2 - vertOffset/2;
		if (topPosition < -vertOffset/2) {
			topPosition = 0;
		}
		$(id).css('top', topPosition);
		$(id).css('cursor', 'auto');
		$(id).fadeIn(500);
	});
	
	// Hide the lightbox window when closing it or clicking anywhere in the lightbox
	$("a.modal-close").click(function(e){
		$("#modal-overlay, .modal-window").hide();
		e.stopPropagation();		
	});
	
	$("#modal-overlay, .modal-window").click(function(e) {
		$('#modal-overlay').hide();
		$('.modal-window').hide();
		e.stopPropagation();
	});
	
	// Change color of yellow tag blocks to make it readable
	$(".yellow").each(function() {
		var color = $(this).css("background-color");
		if (color == "rgb(255, 255, 0)") {
			$(this).find("a").css('color', '#999').addClass("yellowtag");
			$(this).css('color', '#999').addClass("yellowtag");
		}
	});
	
	$(".yellowtag").hover(
		function () {
			$(this).css('color', 'white');
		},
		function () {
			$(this).css('color', '#999');
		}
	);
	
 });

// Create a cookie with name/value pair and an expiry date of |days| from now
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

// Read the value of the cookie for the key |name|
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// Remove the cookie key/value pair where key is |name|
function eraseCookie(name) {
	createCookie(name,"",-1);
}

// Return the bool value for the strings "true" and "false"
function stringToBool(stuff) {
	switch (stuff) {
		case 'true':
		return true;
		break;
		case 'false':
		return false;
		break;
	}
	return false;
}