$(document).ready(function(){ $(window).resize(resizeWin); resizeWin(); initPages();});
var currentPage = 0;
var slideDir;
var nextProjectURL;

function initPages(){
	$.swapImage(".hoverImage", true, true, "mouseenter", "mouseleave");
		
	$("#bg").hide();
	$("#bgCanvas").hide();
	$("#bg").css("visibility","visible");
	$("#bgCanvas").css("visibility","visible");
			
	$("#less").hide();
	$(".closed").hide();
	// $("#loader").hide();
	
	$("#video-button").hide();
		
	if( window.pages )
	{
		if( pages.length > 0 ){
			$("#bg").load( onBgLoad );	
		 	if( window.showLast == 1 ) setPage( pages.length-1, true );
			else setPage( 0, true );
		}
	}
	else $("#bg").fadeIn('slow');
	
	$("#more").click( function(){
		$("#footer").animate({height:'166px'},'fast');
		$("#less").show();
		$("#more").hide();
		} );
		 
	$("#less").click( function(){
		$("#footer").animate({height:'32px'},'fast');
		$("#more").show();
		$("#less").hide();
		} );
}

function resizeWin(){
	var w = $(window).width();
	// if( w < 750 ) width = 750;
	var width = w;
	
	var h = $(window).height() - 53;
	// if( h < 600 ) h = 600;
	var height = h;
	
	var aspect = 2500/2000;
	
	if(true) {
		if(w < h*aspect) {
			h = w/aspect
		} else {
			w = h*aspect;
		}
	} else {		
		if(w > h*aspect) {
			h = w/aspect
		} else {
			w = h*aspect;
		}
	}
	
	$("#bg").css("width",w + "px");
	$("#bg").css("height",h + "px");
	$("#bg").css("bottom",Math.round((height-h)/2) + "px");
	$("#bg").css("left",Math.round((width-w)/2) + "px");
	
	$("#bgCanvas").css("width",w + "px");
	$("#bgCanvas").css("height",h + "px");
	$("#bgCanvas").css("bottom",Math.round((height-h)/2) + "px");
	$("#bgCanvas").css("left",Math.round((width-w)/2) + "px");
	
	// $("#content_column").css("height",$(window).height()-68 + "px");
}

function hideDone() {
	// $(window).width();
	// $("#bg").css("left", $(window).width() + "px");
	// $("#bg").attr('src', pages[currentPage]);
}

function showDone() {
	
}

function onBgLoad() {
	if( pages.length > 1 )
	{
		var hideDir = "left";
		if( slideDir == "left" ) hideDir = "right";

		if( slideDir )
		{			
			$("#bg").show("slide", { direction: slideDir }, 500, showDone);
			$("#bgCanvas").hide("slide", { direction: hideDir }, 500, showDone);
		}
		else{
			$("#bg").fadeIn('slow');
		}
		setPageLinks();
	}
	$("#loader").hide();
}

function onBgCanvasLoad() {
	$("#bg").attr('src', pages[currentPage]);
	if( currentPage == 0 && slideDir == null ) $("#bgCanvas").fadeIn('slow');
	$("#loader").hide();
}

function thumbnailOver( obj ) {
	// $(".project-thumbnail").mouseenter( function(){ this.fadeOut(); } );
	// alert( $(obj).(".project-thumbnail") );
	// obj.fadeOut();
}

function showChildren( obj ) {
	if( $(obj).siblings(".children").is(":hidden") ) $(obj).siblings(".children").slideDown();
	else $(obj).siblings(".children").slideUp();
}

function setPage( dir, direct ) {
  var curBg = pages[currentPage];
  
  if( dir > 0 ) slideDir = "right";
  else if( dir < 0 ) slideDir = "left";

  if( direct ){
  	slideDir = null;	
  }
 
  // if( direct && dir > currentPage ) slideDir = "left";
  // if( direct && dir < currentPage ) slideDir = "right";
 	
  if( direct ) currentPage = dir;
  else if( currentPage + dir >= 0 && currentPage + dir < pages.length ) currentPage += dir;

  if( currentPage == pages.length-1 )
  {
	$("#plus").hide();
 	$("#next").show();	
	$("#video-button").delay(500).fadeIn(600);
  }
  else
  {
	$("#plus").show();
	$("#next").hide();
	$("#video-button").fadeOut(100);	
  }
  
  if( currentPage == 0 ){
	   $("#minus").hide();
	   $("#prev").show();	
  }
  else
  {
	  $("#minus").show();
	  $("#prev").hide();	
  }
 
  if( curBg != $("#bgCanvas").attr('src' )  ){
	$("#bgCanvas").load( onBgCanvasLoad );
	$("#bgCanvas").attr('src', curBg);
  }
  else{
	 $("#bg").hide();
	 $("#bg").attr('src', pages[currentPage]);
  }
  
  $("#loader").show();
}

function goToPic( ind ) {
	$("#bg").hide();
	$("#bgCanvas").hide();
	setPage( ind, true );
	$("#gallery_column").hide();	
}

function setPageLinks() {
	var linkString = "";
	for( var i=0; i < pages.length; i++ )
	{
		if( i != currentPage ) linkString = linkString + '<a class="pagelink" href="#" onclick="setPage('+ i +', true);">' + (i+1) + '</a>';
		else linkString = linkString + '<span class="pagelink">' + (i+1) + '</span>';
	}
	$("#project-links").html( linkString );
}

function setProject( url ) {
	nextProjectURL = url;
	$("#bgCanvas").hide();
	$("#bg").fadeOut('slow', gotoProject);
}

function gotoProject() {
	window.location = nextProjectURL;
}
