$(document).ready(function() {
	var startHash = window.location.hash;
	startHash = startHash.replace("#","");
	if(!startHash){
		startHash = "home";
	}
	var screenWidth = $('body').width();
	var slides = ["home","about", "what-we-do", "who-we-work-with", "work","contact"];
	var currIndex = 0;
	for(i=0;i<slides.length;i++){
		if(startHash == slides[i]){
			currIndex = i;
		}
	}

	$('.next').each(function(){
		$(this).css("right",(screenWidth-980)/2+"px");
		$(this).hide();
	});
	$('.prev').each(function(){
		$(this).css("left",(screenWidth-980)/2+"px");
		$(this).hide();
	});
	$('.slide').each(function(){
		$(this).css("width",screenWidth+"px");
	})
	$('#logo').css("right",(screenWidth-980)/2+20+"px");
	
	$('#table-content').css("width",$('.slide').size()*screenWidth+"px");
	if(currIndex+1<slides.length){
		$('#next-'+startHash).show();
	}
	if(currIndex-1>=0){
		$('#prev-'+startHash).show();
	}
	if(currIndex==0){
		$("#logo").addClass('home');
	} else {
		$("#logo").removeClass('home');
	}
	
	$('.work-piece').each(function(i){
		if((i+1)%4 == 0 && i!=0){
			$(this).addClass('row-end');	
		}
	});
	
	$(window).resize(function () {
		screenWidth = $('body').width();
		$('#table-content').css("width",$('.slide').size()*screenWidth+"px");
		$('.next').each(function(){
			$(this).css("right",(screenWidth-980)/2+"px");
		});
		$('.prev').each(function(){
			$(this).css("left",(screenWidth-980)/2+"px");
		});
		$('.slide').each(function(){
			$(this).css("width",screenWidth+"px");
		})
		$('#logo').css("right",(screenWidth-980)/2+20+"px");
		window.location.hash=slides[currIndex];
		$('html').scrollTop(0);
	});
	$.localScroll.defaults.axis = 'x';
	$.localScroll.hash({
		target: '#content', // Could be a selector or a jQuery object too.
		queue:true,
		duration:0,
		onBefore:function(object,element,target){
			$('html').scrollTop(0);
		}
	});

	$.localScroll({
		target:'#content',
		duration:1000,
		hash:true,
		onBefore:function(object,element,target){
			for(i=0;i<slides.length;i++){
				if(slides[i]==$(element).attr("id")){
					currIndex = i;
					pageTracker._trackPageview('/slides/'+slides[i]);
				}
			}
			if(currIndex==0){
				$("#logo").addClass('home');
			} else {
				$("#logo").removeClass('home');
			}
			$('.next').each(function(i){
				if(i!=currIndex){
					$(this).hide();
				} else {
					if(i+1<slides.length){
						$(this).show();
					}
				}
			});
			
			$('.prev').each(function(i){
				if(i!=currIndex){
					$(this).hide();
				} else {
					if(i-1>=0){
						$(this).show();
					}
				}
			});
		}
	});

	$(".work-piece").colorbox({width:900,height:668,opacity:0.75,onComplete:function(){													 
		pageTracker._trackPageview('/slides/work/'+$('#client h2').attr('id'));
		$('#testimonial').css('top','50%');
		$('#testimonial').css('margin-top','-'+($('#testimonial').height()/2+13)+'px');
		var client = $('#client h2').html();
		$('.piece').each(function(index){
			if (index != 0) {
				$(this).hide();
			} else {
				$(this).addClass('active');
				$('#client h2').html(client+"&nbsp;&nbsp;<span class=\"piece-title\">"+$('#pieces .active h3').html()+"</span>");
			}
			$('h3',this).hide();
			$('.description',this).hide();
			$('.open-description',this).click(function(){
				$('.description',$(this).parent()).animate({height:"toggle"});
				$(this).hide();
			})
			$('.description .close',$(this)).click(function(){
					$(this).parent().animate({height:"toggle"});
					$('.open-description',$(this).parent().parent()).show(1000);
				})
		});
		var index = 0;
		if(index+1>=$('#piece-nav li').size()){
					$('#work-next').hide();
				} else {
					$('#work-next').show();
				}
				if(index-1<0){
					$('#work-prev').hide();
				} else {
					$('#work-prev').show();
				}
		$('#piece-nav li a').each(function(index){
			if(index==0){
				$(this).addClass('active');
			}
			$(this).click(function(){
				index = $(this).parent().index();
				$('#piece-nav li a.active').removeClass('active');
				$(this).addClass('active');
				var id = '#'+$(this).attr('id').replace('nav-','');
				if(index+1>=$('#piece-nav li').size()){
					$('#work-next').hide();
				} else {
					$('#work-next').show();
				}
				if(index-1<0){
					$('#work-prev').hide();
				} else {
					$('#work-prev').show();
				}
				$('#client h2').fadeOut('slow');
				$('#pieces .active').fadeOut('slow',function(){
					$(this).removeClass('active');
					$(id).addClass('active');
					$('#client h2').html(client+"&nbsp;&nbsp;<span class=\"piece-title\">"+$('#pieces .active h3').html()+"</span>");
					$('#client h2').fadeIn('slow');
					$(id).fadeIn('slow');	
				});
			});
		});
		$('#work-next').click(function(){
			var li = $('#piece-nav li').get(index+1);
			index = index+1;
			var id = "#" + $('a',li).attr('id').replace('nav-','');
			$('#piece-nav li a.active').removeClass('active');
			$('a',li).addClass('active');
			if(index+1>=$('#piece-nav li').size()){
					$('#work-next').hide();
				} else {
					$('#work-next').show();
				}
				if(index-1<0){
					$('#work-prev').hide();
				} else {
					$('#work-prev').show();
				}
			$('#client h2').fadeOut('slow');
			$('#pieces .active').fadeOut('slow',function(){
				$(this).removeClass('active');
					$(id).addClass('active');
					$('#client h2').html(client+"&nbsp;&nbsp;<span class=\"piece-title\">"+$('#pieces .active h3').html()+"</span>");
					$('#client h2').fadeIn('slow');
					$(id).fadeIn('slow');	
			});
		})
		
		$('#work-prev').click(function(){
			var li = $('#piece-nav li').get(index-1);
			index = index-1;
			var id = "#" + $('a',li).attr('id').replace('nav-','');
			$('#piece-nav li a.active').removeClass('active');
			$('a',li).addClass('active');
			if(index+1>=$('#piece-nav li').size()){
					$('#work-next').hide();
				} else {
					$('#work-next').show();
				}
				if(index-1<0){
					$('#work-prev').hide();
				} else {
					$('#work-prev').show();
				}
			$('#client h2').fadeOut('slow');
			$('#pieces .active').fadeOut('slow',function(){
				$(this).removeClass('active');
					$(id).addClass('active');
					$('#client h2').html(client+"&nbsp;&nbsp;<span class=\"piece-title\">"+$('#pieces .active h3').html()+"</span>");
					$('#client h2').fadeIn('slow');
					$(id).fadeIn('slow');	
			});
		})
	}});
	
	
	

	
	
	


});



