// cookie variables
var COOKIE_NAME = "gender";
var COOKIE_SAVE = "url";
var options = { path: "/", expires: 14 };


$(function() {
	
	// generate two random numbers
	var x = Math.random();
	var r = Math.floor(x*2);
	if (r == 2) {
		r = 0;	// can only have values 0 or 1
	}
	
	var y = Math.random();
	var s = Math.floor(y*3);
	if (s == 0) {
		s = 3; // reverse indexing, can have values 1, 2, 3
	}
	
	// setup array of cookie values
	var g = new Array("male","female");
	
	// set cookie with initial gender based on random number
	// first check to see if cookie exists
	if ($.cookie(COOKIE_NAME)) {
		if (($.cookie(COOKIE_NAME)) == "male") {
			// set to female
			$.cookie(COOKIE_NAME, "female", options);
		} else {
			// set to male
			$.cookie(COOKIE_NAME, "male", options);
		}
	} else {
		// pick a gender to start with randomly
		$.cookie(COOKIE_NAME, g[r], options); 
	}
	
	// now, set background image of #wrapper based on cookie 
	var femaleFileName = "url(images/bg0" + (2*s) + ".jpg)";
	var maleFileName = "url(images/bg0" + ((2*s)-1) + ".jpg)";
	
	if ($.cookie(COOKIE_NAME) == "female") {
		// use a female background image
		// grab image with even number image (i.e. bg02.jpg)
		$("#wrapper").css({"background-image" : femaleFileName});
	} else {
		// use a male background image
		// grab image with odd number image (i.e. bg03.jpg)
		$("#wrapper").css({"background-image" : maleFileName});
	}
	
});


$(document).ready(function() {
	
	if ($.cookie(COOKIE_SAVE))	{
		if (!($("#saveLocation").is(":checked"))) {
			$("#saveLocation").attr("checked","");	
		} else {
			window.location.href = $.cookie(COOKIE_SAVE);
		}
	}	
	
			
	$(".navigation ul li a").each(function(){	
		
		$(this).click(function() {
			if ($(this).hasClass("expand")) {
						
				// check to see if expanded nav is showing, if not, show it		
				if (!($(".navigation").hasClass("expanded"))) {				 
					$(".navigation").addClass("expanded");
				}
				
				// hide all other fly-outs
				$('.subselect').hide();
				// show this fly-out	
				var id = $(this).attr("id");
				$("#"+id+"_sub").toggle();	
				
			} else {
				
				// check to see if expanded nav is showing, if so, hide it
				if ($(".navigation").hasClass("expanded")) {				 
					$(".navigation").removeClass("expanded");
					$(".subselect").hide();
				}
								
				// no fly-out, create cookie on click
				if ($("#saveLocation").is(":checked")) {
					var url = $(this).attr("href");
					$.cookie(COOKIE_SAVE, url , options);
				}
			}
		});
	});
	
	$(".subselect .callout a").mouseover(function() {
		//$(this).children("span").toggleClass("hover");
		$(this).children("span").children("img").attr("src","images/callout.arrow.hover.gif");
	});
	$(".subselect .callout a").mouseout(function() {
		//$(this).children("span").toggleClass("hover");
		$(this).children("span").children("img").attr("src","images/callout.arrow.gif");
	});
	$(".subselect .callout a").click(function() {
		// if Save my Selection is checked, set cookie value to url to redirect to
		if ($("#saveLocation").is(":checked")) {
			var url = $(this).attr("href");
			$.cookie(COOKIE_SAVE, url , options);
		}									  
	});
	
	$(".trainer a").click(function() {
		// if Save my Selection is checked, set cookie value to url to redirect to
		if ($("#saveLocation").is(":checked")) {
			var url = $(this).attr("href");
			$.cookie(COOKIE_SAVE, url , options);
		}					   
	});
	
	
});