/*	
	Create Team Previews
*/

if($.browser.msie && $.browser.version < 7) {
	
} else {
	document.write("<style type='text/css'>");
	document.write(".theTeam, .teamContact");
	document.write("{");
	document.write("display: none;");
	document.write("}");
	document.write("</style>");
	
	$(document).ready(function(){
		$(".theTeam").addClass("activated");
		$(".teamDetail").css({display:"none"});
		$("#teamContact").hide();
		$("#teamContact .formRow:first").hide();
		var teamMembers = [];
		var teamNames = [];
		var teamTitles = [];
		$('.teamDetail').each(function() {
			teamMembers.push($(this).attr("id"));
			teamNames.push($(this).find("h2").html());
			teamTitles.push($(this).find(".job-title").html());
		});
		$(".theTeam").append('<ul class="thumbs"></ul>');
		$theForm = $("#teamContact").html();
		$theFormAction = $("#teamContact").attr("action");
		$theFormMethod = $("#teamContact").attr("method");
		var listCounter = 0;
		for(i=0;i<teamMembers.length;i++) {
			listCounter++;
			$(".theTeam ul.thumbs").append('<li class="'+teamMembers[i]+'"><a href="#'+teamMembers[i]+'"><img src="assets/images/content/the-team/'+teamMembers[i]+'_thmb.jpg" width="100" height="100" alt="'+teamNames[i]+'" /><br />'+teamNames[i]+'<br /><strong>'+teamTitles[i]+'</strong></a></li>');
			if (listCounter == 1) {
				$("."+teamMembers[i]).addClass("first");
			}
			if (listCounter == 4) {
				$("."+teamMembers[i]).addClass("last");
				listCounter = 0;
			}
			$("#"+teamMembers[i]).find("div.descrip").after('<form action="'+$theFormAction+'" method="'+$theFormMethod+'" class="teamContact" id="'+teamMembers[i]+'Contact">'+$theForm+'</form>');
			//$("#"+teamMembers[i]+"Contact #ctrl_1 option[value='"+teamMembers[i]+"']").attr("selected","selected");
			$("#"+teamMembers[i]+"Contact #ctrl_1 option."+teamMembers[i]).attr("selected",true);
			//$("#"+teamMembers[i]+"Contact #ctrl_1 option."+teamMembers[i]).addClass("testing");
		}
		$(".theTeam ul.thumbs a").hover(function() {
			if ($(this).attr("class") != "activated") {
				var $file = [];
				$offState = $(this).find("img").attr("src");
				$file = $offState.split("thmb.");
				$(this).find("img").attr("src", $file[0]+"thmb_on."+$file[1]);
			}
		}, function() {
			if ($(this).attr("class") != "activated") {
				$(this).find("img").attr("src", $offState);
			}
		});
		$(".teamDetail form h2").hide();
		$(".teamContact").hide();
		var initialTab = window.location.hash;
		if (initialTab) {
			var refOffset = initialTab.indexOf('#')+1;
		  	var refEnd = initialTab.length-1;
		  	var newDest = initialTab.substring(refOffset, refEnd);
		  	if (newDest != "teamContact") {
				$("#"+newDest).show();
				$("."+newDest).find("a:first").addClass("activated");
				//activate thumbnail
				var $file = [];
				$offState = $("."+newDest).find("img").attr("src");
				$file = $offState.split(".");
				$("."+newDest).find("img").attr("src", $file[0]+"_on."+$file[1]);
				$("."+newDest).find("img").addClass("activated");
		  	}
		}
		$(".theTeam .thumbs li a").click(function(){
			if ($(this).attr("class") != "activated") {
				$(".theTeam .thumbs li a").removeClass("activated");
				//disable thumbnails
				var $file = [];
				$(".theTeam .thumbs li a img.activated").each(function() {
					$onState = $(this).attr("src");
					$file = $onState.split("_on.");
					$(this).attr("src", $file[0]+"."+$file[1]);
					$(this).removeClass("activated");
				});
				$(this).addClass("activated");
				$(this).find("img").addClass("activated");
				$teamMember = $(this).attr("href");
				var refOffset = $teamMember.indexOf('#')+1;
			  	var refEnd = $teamMember.length;
			  	var $shortTeamMember = $teamMember.substring(refOffset, refEnd);
				$(".teamDetail").hide();
				//$("#"+$shortTeamMember).fadeIn();
				$("#"+$shortTeamMember).show();			
		   		window.location.hash = $shortTeamMember+"_";
			}
			return false;
		});
		$(".teamDetail p.moreInfo a").click(function(){
			$teamMember = $(this).parents(".teamDetail").attr("id");
			if ($(this).attr("class") != "activated") {
				$origLink = $(this).html();
				$(this).parents(".teamDetail").find("div.descrip").slideUp(500,function(){
					$("#"+$teamMember+"Contact").slideDown(500);
				});
				$(this).addClass("activated");
				$(this).html("Cancel");
			} else {
				$("#"+$teamMember+"Contact").slideUp(500,function(){
					$(this).parents(".teamDetail").find("div.descrip").slideDown(500);
				});
				$(this).removeClass("activated");
				$(this).html($origLink);
			}
			return false;
		});
	});
	$(document).ready(function(){
		$(".theTeam").css({display:"block"});
		$(".teamDetail").addClass("panel").addClass("activated");
	});
}

