	var curPage = "";
	var preloader = new Image();
	var tempContent = $("<div></div>");
	
	$(function (){
		$("#cal").datepicker({
			disableClicks:true,
			markDates: [
				{ date:'5/13/2009', title:"Banner Hanging & Opening Ceremonies", link:"events1" },
				{ date:'5/14/2009', title:"Penny Wars", link:"events2" },
				{ date:'5/15/2009', title:"Meet the Greeks and presentation of Greek God and Goddess & Greek Gladiator", link:"events3" },
				{ date:'5/16/2009', title:"Letter Check-In & Greek Sing", link:"events4" },
				{ date:'5/17/2009', title:"Relay for Life", link:"events5" }
			]
		});
		
		$(tempContent).load("pages.html", function (){
			$.history.init(loadSection);
		});
		
		$("a.link").click(function(){
			$.history.load(this.href.replace(/^.*#/, ''));
			return false;
		});
		
		imageCarrousel(1,'carousel_', 6);
	});
	
	function loadSection(section){
		if (section == "") section = "home";
		if ($(tempContent).find("#"+section).length<=0) section = "notfound";
		var sectionObject = ($(tempContent).find("#"+section).hasClass('section')) ? $(tempContent).find("#"+section) : $(tempContent).find("#"+section).parents('.section');
		var isChild = ($(tempContent).find("#"+section).hasClass('section')) ? false : true;
		if (curPage!=$(sectionObject).attr('id')){
			$("#content").find(".object").children().remove();
			$("#content").hide("slide", {direction:'right'}, "slow", function (){
				$("#content").html($(sectionObject).html());
				$("#content").find(".object").children().hide();
				$("#content").find(".object").click(function () { $(this).children().show(); $(this)[0].scrollIntoView(); });
				$('input.example').example(function() { return $(this).attr('title'); });				
				$("#content").show("slide", {direction:'left'}, "slow", function (){
					$("#content").find(".scrollable").each(function (){
						$(this).css('paddingRight', '0px');
						$(this).jScrollPane();
						if (isChild) $("#content").find("#"+section).effect("pulsate", {times:'0'}, 1500);
					});
					$("#content").find(".votingSection").each( function (){
						getCandidates(this)
					});
					$("#content").find(".resultsSection").each( function (){
						showResults(this);
					});
				});
			});
			curPage=$(sectionObject).attr('id');	
		}
		if (isChild) $("#content").find("#"+section).effect("pulsate", {times:'0'}, 1500);
	}
	
	function imageCarrousel(start, prefix, maxImages){
		if (start>maxImages) start = 1;
		$("#carrousel").css('background',$("#carrousel").find("div").css('background'));
		$("#carrousel").find("div").hide();
		$("#carrousel").find("div").css("background", "url(images/"+prefix+start+".jpg) top left no-repeat");
		$("#carrousel").find("div").fadeIn(5000, function (){
			preloader.src = "images/"+prefix+start+".jpg";
			setTimeout("imageCarrousel("+(start+1)+", '"+prefix+"', "+maxImages+")", 5000);
		});
	}

	function getCandidates(target){
		$.getJSON("vote.php", function (data){
			if (data.status=="opened"){
				var html;
				var innerHTML_M = '<table class="candiates"><tr><th>Greek God</th></tr>';
				var innerHTML_F = '<table class="candiates"><tr><th>Greek Goddess</th></tr>';
				for (i=0; i<data.candidates.length; i++){
					if (data.candidates[i].sex=='M') innerHTML_M += '<tr><td><label><input type="radio" class="candidate" name="cidm" id="cidm_'+i+'" value="'+data.candidates[i].id+'"> <span>'+data.candidates[i].name+'</span></label></td></tr>';
					if (data.candidates[i].sex=='F') innerHTML_F += '<tr><td><label><input type="radio" class="candidate" name="cidf" id="cidf_'+i+'" value="'+data.candidates[i].id+'"> <span>'+data.candidates[i].name+'</span></label></td></tr>';
				}
				innerHTML_M+="</table>";
				innerHTML_F+="</table>";
				html = '<form action="vote.php" onsubmit="return checkForm(this)"><input type="hidden" name="action" value="vote" />'+
					   '<table border="0"><tr><td><span style="color:#FFF;">Your Name:</span></td><td><input type="text" name="name" id="name" title="Your full name"></td></tr>'+
					   '<tr><td><span style="color:#FFF;">Your E-mail:</span></td><td><input type="text" name="email" id="email" title="Your e-mail address"></td></tr>'+
					   '<tr><td colspan="2">&nbsp;</td></tr>'+
					   '<tr><td colspan="2">Please select the candidate you would like to place your vote for:</td></tr>'+
					   '<tr><td>'+innerHTML_M+'</td><td>'+innerHTML_F+'</td></tr>'+
					   '<tr><td colspan="2" align="center"><input type="submit" name="button" id="button" value="Place Vote"></td></tr></table></form>';
				
				$(target).html(html);
				$(target).find('input[@title]').example(function() { return $(this).attr('title'); });
				$(target).jScrollPane();
			}else if(data.status=="not opened"){
				$(target).html('<p>&nbsp;</p><p style="color:#FFF" align="center">Voting will be opened today at 7pm. <br />Thank you for your patience.</p>');
			}else{
				$(target).html('<p>&nbsp;</p><p style="color:#FFF" align="center">Thank you for your interest in voting. Our voting system is now closed<br />Please check back for the results.</p>');
			}
		});
	}
	
	function checkForm(form){
		setTimeout(function (){
			var valid = true;
			$(form).find("input").each(function (){ 
				if (valid == true){
					if ($(this).val()==""){
						$(this).focus();
						alert("Please type in your "+$(this).attr("id")+".");
						valid = false;
						return;
					}
					if ($(this).attr("id").indexOf("email")>=0){
						if ($(this).val().indexOf(".")<=0 || $(this).val().indexOf("@")>= $(this).val().lastIndexOf(".")){
							$(this).focus();
							alert("Please type in a valid e-mail address.");
							valid = false;
							return;
						}
					}
					if ($(this).is(":radio")){
						if ($(form).find("input[@name='"+$(this).attr("name")+"']:checked").length<=0){
							alert("Please select a candidate.");
							valid = false;
							return;
						}
					}
				}
			});
			if (valid == true){
				$.post("vote.php?"+$(form).serialize(), function (){
					$(form).parent().html('<p>&nbsp;</p><p align="center" style="color:#FFF">Your vote has been submitted.<br />Thank you for voting!</p>')
				});
			}
		}, 1);
		return false;
	}
	
function showResults(divTarget){
	$.getJSON("panel/index.php?action=getData", function (data){
		var males = data.male;
		var females = data.female;
		var plotMaleData = new Array();
		var plotFemalesData = new Array();
		var maxVotes = 0;
		males.sort(sortCandidates);
		females.sort(sortCandidates);
		
		for (i=0;i<males.length; i++){
			plotMaleData[plotMaleData.length] = { data: males[i].votes, label: males[i].name + " ("+males[i].votes.length+" votes)", color: males[i].color };
			maxVotes += males[i].votes[males[i].votes.length-1][1];
		}
		for (i=0;i<females.length; i++){
			plotFemalesData[plotFemalesData.length] = { data: females[i].votes, label: females[i].name + " ("+females[i].votes.length+" votes)", color: females[i].color };
		}
		$.plot(
			$("#malegraph"),
			plotMaleData,
			{ xaxis: { mode: 'time' },
			  yaxis: { min: 0 },
			  legend: { position: 'nw' }
		});
		$.plot(
			$("#femalegraph"),
			plotFemalesData,
			{ xaxis: { mode: 'time' },
			  yaxis: { min: 0 },
			  legend: { position: 'nw' }
		});
		$("#maxvotes").html('A total of '+maxVotes+' votes where recorded.');
	});	

}

function sortCandidates(a, b){
	return (b.votes.length - a.votes.length);
}
