document.write('<link rel=\"StyleSheet\" href=\"/inc/corejs.css\" type=\"text/css\" media=\"screen\" />')

google.setOnLoadCallback(function() {
	$(document).ready(function() {	
		newWindowLinks();
		clickable();			
		homeSlideshowLoad();
		theTeam();
		slideShow(1);		
	});	
});


var slideShowSpeed 	= 4000;
var slideShowCurrent	= -1;
var slideShowSlides	= 0;
var slideShowTimeout;		
function slideShow() {
	slideShowSlides = $(".slideshow img").size();
	if(slideShowSlides>1) {
		nextSlideshowImage();
	} else {
		$(".slideshow img:eq(0)").fadeIn(slideShowSpeed);
	}	
}
function nextSlideshowImage() {		
	$(".slideshow img:not(:eq("+slideShowCurrent+"))").css({zIndex:"1"}); // All slides that are not the current one set to lowest z-index		
	$(".slideshow img:eq("+slideShowCurrent+")").css({zIndex:"2"}); 	// current set to 2
	slideShowCurrent++; // set current to the next one we want to see
	if(slideShowCurrent>slideShowSlides-1) {
		slideShowCurrent=0;
	}
	if(slideShowCurrent<0) {
		slideShowCurrent = slideShowSlides-1 ;
	}
	nextImage = new Image();
	nextImage.onload = function() {
		$(".slideshow img:eq("+slideShowCurrent+")").hide().css({zIndex:"3"}).fadeIn(2000,function(){
			setTimeout("nextSlideshowImage()",slideShowSpeed);
		}); // hide new slide, set to highest z-index and fadeIn
	}
	nextImage.src = $(".slideshow img:eq("+slideShowCurrent+")").attr("src")
}

/**	
 * Target new windows
 */
function newWindowLinks(){
	$('a.pop').unbind('click').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
}


/* Remove default value and replace */
function defaultText() {
	$(".defaultText").unbind("focus,blur").focus(function () {		
		if ($(this).val() == $(this)[0].defaultValue) {
        	$(this).val("");
        }
	 }).blur(function() {
        if ($(this).val() == "") {
        	$(this).val($(this)[0].defaultValue);
     	}
	 });
}

/* Make a whole box clickable */
function clickable() {
	$(".clickable:has(a)").css({"cursor":"pointer"}).unbind("click").click(function() {
		var link = $("a",this).eq(0).attr("href");
		window.location = link;
	}).hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	})
}

/* Un-obfuscate email */
function fnE(inarr) {
	inarr = inarr.split(",");
	var out = "";
	for(i=inarr.length-1;i>=0;i--) {
		out += inarr[i];
	}
	document.write('<a href="mailto:'+out+'">'+out+'</a>');	
}

/* Home slideshow */
function homeSlideshowLoad() {
	if($("#homeSlideshow").size()>0) {
		var firstimage = $("#homeSlideshow #slides img:eq(0)");
		var parts = firstimage.attr("alt").split(" ~ ");
			$("#homeSlideshow").append('<div id="overlayWrapper" class="overlay"><div id="overlay"><div class="inner"><h3>'+parts[0]+'</h3><p>'+parts[1]+'</p></div><p id="controls"><a href="/work/'+parts[2]+'" id="fom">Find out more &gt;</a><a id="previous">Previous</a><a id="next">Next</a></p></div></div>');
		$("#next").click(function(){
			homeSlideshow("next");
		});	
		$("#previous").click(function(){
			homeSlideshow("previous");
		});
	}	
}
function homeSlideshow(direction) {
	var i		= 0
	var ss 		= $("#homeSlideshow");
	var imgs	= $("img",ss);
	var slides	= $("#slides");
	var overlay = $("#overlayWrapper");
	var width	= 950;	
	slides.css({width:(imgs.size()*width)+"px"});	
	var currentX 	= parseInt(slides.css("left").split("px")[0]);
	var maxX		= -parseInt(imgs.size()-1) * width;
	
	// Direction
	if(direction=="next") {
		var nextX		= currentX-width;
	} else {
		var nextX		= currentX+width;
	}	
	
	// Start or end slides	
	if(nextX<maxX) {
		nextX	= 0;
	}
	if(nextX>0) {
		nextX	= maxX;
	}		
	
	// Image preload
	var imageIndex = (nextX/width)*-1
	var nextimage = $("#homeSlideshow #slides img:eq("+imageIndex+")");
	newImage = new Image();
	newImage.onload = function() {
		overlay.fadeOut(500,function(){
			slides.animate({left:nextX+"px"},1000,"swing",function(){
				var parts = nextimage.attr("alt").split(" ~ ");
				$(".inner",overlay).html('<h3>'+parts[0]+'</h3><p>'+parts[1]+'</p>');
				$("#controls #fom").attr("href","/work/"+parts[2]);
				overlay.fadeIn(500);
			})
		})		
	}
	newImage.src = nextimage.attr("src");
}

// The Team
function theTeam() {
	$("#theTeam").append('<div id="teamMemberInfo" class="overlay"><div class="inner"></div></div>');
	$("#theTeam img").hover(function(){
		var text = $(this).attr("alt");
	
		text = text.split(":");
		var html = '<h3>'+text[0]+'</h3><p>'+text[1]+'</p>'
		$("#theTeam .inner").html(html);
		var offset = $(this).position();
		if(offset.left<475) {
			$("#teamMemberInfo").removeClass("left right").addClass("left").css({left:(offset.left+150)+"px"});			
		} else {
			$("#teamMemberInfo").removeClass("left right").addClass("right").css({left:(offset.left-300)+"px"});
		}
		$("#teamMemberInfo").fadeIn(100)
	},function(){
		$("#teamMemberInfo").hide();
	})
}


// Image Loader
function loadImage(src,targID) {
	var i = $("#"+targID);
	i.fadeOut(300,function() {
		newImage = new Image();
		newImage.onload = function() {
			i.attr("src",src).fadeIn(500)
		}
		newImage.src = src;	
	});
}
