function setupExternal(i){ // i= identifier
	$("#"+i+" a.external").click(function(){
		var iIndex = $("#"+i+" a.external").index($(this))+1;
		if($("#"+i+"_"+iIndex).length==0){
			$(this).closest(".section").after(
				'<div class="section child" style="display:none" id="'+i+'_'+iIndex+'"></div>'
				);
			$("#"+i+"_"+iIndex).load(i+".php?i="+$(this).attr("href").substring(1), function(){
				$("#"+i+"_"+iIndex).hide().slideToggle('fast');
				var titleSpan = $("#"+i+"_"+iIndex+" ."+i+"-title");
				$("#"+i+"_"+iIndex).attr("title", titleSpan.html());
				titleSpan.remove();
				$("#"+i+"_"+iIndex+" .close").click(function(){
					$(this).closest(".section").slideToggle('fast');
					//return false;
				});
				Cufon.refresh();
			});
		}else{
			$("#"+i+"_"+iIndex).slideToggle('fast');
		}
		return false;
	});
}
		
var sectionStart = 0;
/*
function setCrumbs(){
	var sections = $(".section[title!='']");
	$("#crumb li:not(.static)").remove();
	sections.each(function(i){
		if($(this).position().top- $(window).scrollTop() + $(this).height() > sectionStart){
			if($(this).hasClass('child')){
				$("#crumb").append(
					'<li class="columns-1">'+$(this).prevAll(":not(.child):first").attr("title")+'</li>'
					);
			}
			$("#crumb").append(
				'<li class="columns-1">'+$(this).attr("title")+'</li>'
				);
			return false;
		}
	});
}

$(function(){
	sectionStart = $("#crumb").position().top - $(window).scrollTop() + $("#crumb").height();
	$(window).scroll(function(){
		setCrumbs();
	});
	setCrumbs();
})
*/

function switchMovemberPhotos(){
	$(".movember-photos li").each(function(i, photoItem){
		var activeImg = $(photoItem).find("img.active");
		var nextImage = activeImg.next();
		if(nextImage.length < 1){
			nextImage = $(photoItem).find("img:first");
		}
		$(".november-date").html($(nextImage)[0].id + "/11");
		$(photoItem).find("img").removeClass("active").addClass("inactive");
		nextImage.removeClass("inactive").addClass("active");
	});
}

function updateMovemberVotes(votes){
	$(".movember-photos a").each(function(i, photoLink){
		var name = $(photoLink).attr("id");
		var capitalisedName = name.charAt(0).toUpperCase() + name.slice(1);
		$(photoLink).find(".vote-box").html(capitalisedName + " <br/>has " + eval("votes."+name) + " vote" + (eval("votes."+name) == 1 ? "" : "s"));
		//$(photoLink).find(".vote-box").html(eval("votes."+name) + " <br/>for <br/>"+capitalisedName);
	});
	$(".movember-photos a .vote-box").show();
	$(".movember-photos a").unbind('mouseenter mouseleave')
		.click(function(){return false;});
}

function movember(){
	setInterval("switchMovemberPhotos();", 1000);
	var hasClicked = false;
	$(".movember-photos a").hover(function(){
		$(this).find(".vote-box").show();	
	}, function(){
		$(this).find(".vote-box").hide();	
	});
	$(".movember-photos a").click(function(){
		if (!hasClicked)
		{
			$.ajax({
	  			url: "movember/vote.php?name="+$(this).attr("id"),
	  			dataType : "json",
	  			success: function(data){
	  				if(typeof(data.error) != "undefined"){
	  					if(data.error == "already voted"){
	  						updateMovemberVotes(data.votes);
	  						$(".movember-already-voted").show();
							$(".movember-description").hide();
	  					}
	  					// handle error response
	  				}else{
	  					updateMovemberVotes(data.votes);
	  					$(".movember-thank-you").show();
						$(".movember-description").hide();
	  				}
	  			}
			});
		}
		$(".movember-photos li a").css('cursor','default');
		hasClicked = true;
		return false;
	});
}

		
$(document).ready(function(){
	setupExternal("contact");
//	setupExternal("about");
//	setupExternal("jobs");
 });
 
 $(window).ready(function(){
 	movember();
 });
 
 
 		

