$(document).ready(function() {

	$(function() {
		$('.smoothscrollLink').bind('click',function(event){
	
			var $anchor = $(this);
			
			$('html, body').stop().animate({
				scrollTop: $($anchor.attr('href')).offset().top
			}, 1200,'easeInOutExpo');
	
			event.preventDefault();
		});
	});


	// Init basevalues
	currentPage = "main";
	caseOpen = false;
	globalWorkPageHeight = 0;
	
	$('#works').height("0px");
	$('.servicebox-hover').css({ opacity: 0 });
	$('.hover').css({ opacity: 0 });
	$('.work-item-zoom').css({ opacity: 0 });
	
	$('.servicebox-hover').mouseenter(function() {

		$(this).find($('li')).css({ opacity: 0, marginTop:17});
		$(this).css({ opacity: 1 });
			$(this).find($('li')).each(function(index) {
				tweenTime = index * 50 + 200;
				delayTime = index * 250;
   				$(this).delay(delayTime).animate({opacity: 1, marginTop:0}, {queue: false, duration:tweenTime, easing:"easeOutQuad"});
  			});
	});
	
	$('.servicebox-hover').mouseleave(function() {
		$(this).animate({ opacity: 0 }, {duration:150, easing:"easeOutQuad"});
	});
	
	$('.link_works').click(function() {
		if(currentPage != "works") openWorks();
	});
	
	$('.link_main').click(function() {
		if(currentPage != "main") openMain();
	});
	
	
	$('.work-item-thumb').mouseenter(function() {
		$(this).find('.work-item-zoom').css("top", "80px");
		$(this).find('.work-item-caption').css("top", "-40px");
		$(this).find('.hover').stop().animate({ opacity: 1 }, {duration:150, easing:"easeOutExpo"});
		$(this).find('.work-item-zoom').stop().delay(50).animate({ top:60, opacity: 1 }, {duration:250, easing:"easeOutExpo"});
		$(this).find('.work-item-caption').stop().delay(50).animate({ top:10 }, {duration:250, easing:"easeOutExpo"});
	});
	
	$('.hover').mouseleave(function() {
		$(this).stop().animate({ opacity: 0 }, {duration:250, easing:"easeOutExpo"});
		$('.hover').find('.work-item-zoom').stop().animate({ top: 100, opacity:0 }, {duration:150, easing:"easeOutExpo"});
		$('.hover').find('.work-item-caption').stop().animate({ top: -40}, {duration:150, easing:"easeOutExpo"});
	});
	
	
	
});

function viewCase(project) {
	caseOpen = true;
	repositionCaseImages();
	$('#activework-content').css({ opacity: 0 });	
	$('#header').css("opacity", 0);
	$('#inverted-header').stop().animate({ opacity:0}, {duration:300, easing:"easeOutQuad"});
		
	$('#activework').stop().animate({ height:'100%' }, {duration:450, easing:"easeInOutExpo"});
	$("#activework-verticalaligner").empty().html('<div style="left: 450px; top: 290px; position: absolute;"><img src="images/ajax-loader-1.gif" /></div>');
	$('#activework-verticalaligner').load('workdata/' + project + '.html', function(data){
		$('#activework-content').stop().delay(150).animate({ opacity:1 }, {duration:300, easing:"easeOutQuad"});
		initSlideshow();
	});

}

function closeCase() {
	caseOpen = false;
	resetContainerHeights();
	$('#activework-content').stop().animate({ opacity:0 }, {duration:300, easing:"easeOutQuad"});
	$('#activework').stop().animate({ height:'0px' }, {duration:650, easing:"easeInOutExpo"});
	
	$('#inverted-header').stop().animate({ opacity:1}, {duration:300, easing:"easeOutQuad", complete:function() {
			$('#header').css("opacity", 1);
		}
	});
}

	
function initSlideshow() {
	$('#closeCaption').css({ opacity: 0 });
	
	$("#next").hover(function() {
		$('#next').stop().animate({ opacity:0.7 }, {duration:300, easing:"easeOutQuad"});
	},
	function() {
		$('#next').stop().animate({ opacity:1 }, {duration:300, easing:"easeOutQuad"});
	});
	
	$("#next").click(function() {
			nextImage();
	});
	
	
	$("#prev").hover(function() {
		$('#prev').stop().animate({ opacity:0.7 }, {duration:300, easing:"easeOutQuad"});
	},
	function() {
		$('#prev').stop().animate({ opacity:1 }, {duration:300, easing:"easeOutQuad"});
	});
	
	$("#prev").click(function() {
			prevImage();
	});
	
	$("#close").hover(function() {
		$('#close').stop().animate({ opacity:0.7 }, {duration:300, easing:"easeOutQuad"});
		$('#closeCaption').stop().animate({ opacity:1, top: "40px" }, {duration:300, easing:"easeOutQuad"});
	},
	function() {
		$('#close').stop().animate({ opacity:1 }, {duration:300, easing:"easeOutQuad"});
		$('#closeCaption').stop().animate({ opacity:0, top: "30px" }, {duration:300, easing:"easeOutQuad"});
	});
	
	$("#close").click(function() {
		closeCase();
	});

}


function nextImage(){
	  var $active = $('#slides .active');
      var $next = ($('#slides .active').next().length > 0) ? $('#slides .active').next() : $('#slides img:first');
      $next.css('z-index',2);//move the next image up the pile
	  $active.fadeOut(500,function(){//fade out the top image
	  	$active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
      	$next.css('z-index',3).addClass('active');//make the next image the top one
      });	
}

function prevImage(){
	  var $active = $('#slides .active');
      var $next = ($('#slides .active').prev().length > 0) ? $('#slides .active').prev() : $('#slides img:last');
      $next.css('z-index',2);//move the next image up the pile
	  $active.fadeOut(500,function(){//fade out the top image
	  	$active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
      	$next.css('z-index',3).addClass('active');//make the next image the top one
      });	
}


function openWorks() {
	$('#works').css("display", "block");
	currentPage = "works";
	$('#wrapper').animate({ opacity:0 }, {duration:250, easing:"easeOutSine"});
	$('#inverted-header').delay(600).show( "blind", {direction: "vertical", easing:"easeOutExpo"}, 750 );
	$('#works').delay(200).animate({ height: $(document).height() + "px" }, {duration:800, easing:"easeInOutExpo"});
	globalWorkPageHeight = $(document).height();
}

function openMain() {
	
	if(caseOpen == true) 
	{
		additionalDelay = 350;
	} else {
		additionalDelay = 0;
	}

	currentPage = "main";
	
	$('#activework').stop().animate({ height:0 }, {duration:450, easing:"easeOutExpo"});
	$('#wrapper').delay(600 + additionalDelay).animate({ opacity:1 }, {duration:250, easing:"easeOutSine"});
	$('#inverted-header').delay(200+additionalDelay).hide( "blind", {direction: "vertical", easing:"easeOutExpo"}, 750 );
	$('#works').delay(0 + additionalDelay-(additionalDelay*0.25)).animate({ height: 0 }, {duration:900, easing:"easeInOutExpo"});
}


$(window).resize(function() {
	if(caseOpen == true) {
		repositionCaseImages();
	}
});

function repositionCaseImages() {
	if($(window).height() > 780) {
		topValue = $(window).height()/2 - 400;
		$('#activework-verticalaligner').css("top", topValue);
		//$('#activework').css("height", $(window).height());
		$("body").css("overflow", "auto");
	} else {
		$("body").css("overflow", "auto");
		//$('#activework').css("height", 780);
		$('#works-content').css("height", 780);
		$('#works').css("height", 780);
		$('#container').css("height", 780);
	}
}

function resetContainerHeights() {
	$("body").css("overflow", "scroll");
	$('#works-content').css("height", "100%");
	$('#works').css("height", globalWorkPageHeight + "px");
	$('#container').css("height", "100%");
}

