jQuery(function(){
	function setPhoto(index){
		jQuery("a.masthead_links").attr("class","masthead_links");
		jQuery("a.masthead_links[index="+index+"]").attr("class","masthead_links active");
		jQuery("#client").text(masthead[index].client);
		if (navigator.userAgent.indexOf("MSIE 7") == -1){
			jQuery("#slide a:first").stop(true,true).animate({marginLeft:index*912*(-1)},"medium");
		}
		else{
			jQuery("#slide a").not(index).stop(true,true).fadeOut("slow");
			jQuery("#slide a:eq("+index+")").stop(true,true).fadeIn("slow");
		};
		jQuery("#learnmore a").attr("href",masthead[index].more);
	};
	
	function cyclePhoto(){
		var index = jQuery("a.masthead_links.active").attr("index");
		if (index < jQuery("a.masthead_links").length-1)index++;
		else index = 0;
		setPhoto(index);
	};
	
	var links = jQuery("a.masthead_links").each(function(){
		jQuery(this).click(function(e){
			var index = $(this).attr("index");
			e.returnValue = false;
			e.cancelBubble = true;
			if (e.preventDefault)e.preventDefault();
			if (e.stopPropagation)e.stopPropagation();
			clearTimeout(timerID);
			timerID = setInterval(cyclePhoto,5000);
			setPhoto(index);
		});
	});

	for (i=1; i<masthead.length; i++){
		var a = document.createElement("a");
		a.href = masthead[i].more;
		var img = document.createElement("img");
		img.src = masthead[i].photo;
		jQuery(a).append(img);
		jQuery("#slide").append(a);
	};
	
	if (navigator.userAgent.indexOf("MSIE 7") > -1){
		jQuery("#slide a").css("position","absolute").css("display","none");
		jQuery("#slide a:first").css("display","block");
	};
	
	var timerID = setInterval(cyclePhoto,5000);
});
