
//category index selected
var catIndex = 0;

//coverFlow size specs
var ImageWidth = 140;
var ImageHeight = 140;
var CoverFlowWidth = 440;

//blankSheet display states
var sheetOpen = false;
var dropOpen = false;

//keyboard tooltip flags
var toolTipInitalized = 0;
var keyboardAttemptLimit = 12;

// project container markup. deemed it neccessary to load this in dymanically since it's, well, dynmaic and keeps the source markup clean. 
var projectUIElements = '<div id="project-details-container"><div id="openSheet-head"><h2 class="title-display">Project Title</h2><div id="openSheet-cat-icon"></div><ul id="category-dropdown"><li id="0" class="selected"><a href="/_work/mobile/">on mobile</a></li><li id="1" class="default"><a href="/_work/web/">on the web</a></li><li id="2" class="default"><a href="/_work/location/">on location</a></li></ul><!-- category-dropdown --></div> <!-- openSheet-head --><ul id="project-nav-buttons"><li><a href="#" id="prev-project-btn">prev project</a></li><li><a href="#" id="next-project-btn">next project</a></li></ul><!-- project-nav-buttons --><div id="openSheet-content"></div></div>';



// ON TO THE FUNCTIONS

// move forward a coverflow slide
function moveForward(){
	if(sheetOpen == true){
		if(work[catIndex].Covers.length-1 != work[catIndex].CurrentCoverFlowItem().CoverIndex){
			displayProjectInfo();
		}
	}
	

	if(work[catIndex].Covers.length-1 == work[catIndex].CurrentCoverFlowItem().CoverIndex && catIndex < work.length-1){
			catIndex++
		switchCategory(catIndex);	
		if(sheetOpen == true)displayProjectInfo();
		return;
	}else if(work[catIndex].CurrentCoverFlowItem().CoverIndex == work[catIndex].Covers.length-1){
		catIndex = 0;
		switchCategory(catIndex);
		setTimeout('displayProjectInfo();',500);
		return;
	}
	
	work[catIndex].Flip(1);
	
	
}

// move backward a coverflow slide
function moveBackward(){
	if(sheetOpen == true){
		var patch = work[catIndex].CurrentCoverFlowItem().CoverIndex +' '+catIndex;
		if(patch != '0 0'){
			displayProjectInfo();
		}
	}
	
	if(work[catIndex].CurrentCoverFlowItem().CoverIndex == 0 && catIndex != 0){
		catIndex--;
		switchCategory(catIndex,'reverse');	
		if(sheetOpen == true)displayProjectInfo();					
	}else{
		work[catIndex].Flip(-1);	
	}
}

function switchCategory(categoryIndex,resetDirection){
	catIndex = categoryIndex;
	
	if(resetDirection =='reverse'){
		work[catIndex].GotoLast();
	}else{
		work[catIndex].GotoFirst();
	}
	
	closeOpenDropDownCat(categoryIndex);
	
	
	$('.title-display').fadeOut(0);
	setTimeout("$('.title-display').fadeIn('fast');",550)
		
	//set classes for tabs
	$('#coverFlow-cat-nav li').removeClass('selected');
	$('#coverFlow-cat-nav li').eq(categoryIndex).addClass('selected');
	
	$('.category').animate({'opacity':'0'},400);
	$('.category').eq(categoryIndex).animate({'opacity':'1'},400);	

}

//cateogry dropdown states and function
function closeOpenDropDownCat(subButtonID,state){	
	if(subButtonID || subButtonID == 0){
		$('#openSheet-cat-icon').animate({'background-position':subButtonID * (-75)});
		$('li#'+subButtonID).prependTo('#category-dropdown').removeClass('default').addClass('selected');
		$('li#'+subButtonID).siblings('.selected').removeClass('selected').addClass('default');
	}
	
	var listHeight = $('#category-dropdown li').length  * 27;
	if(state == 'close')$('#category-dropdown').css({'height':'27px'});
 	if(state == 'open')$('#category-dropdown').css({'height':listHeight+3});

}

//loader functions for the projects
function displayProjectInfo(){
	if(sheetOpen == true){
		$('#openSheet').css({'background':'url("_imgs/ajax-loader.gif") no-repeat 380px 250px'});
		keyboardKill('dead');$('#openSheet-content').fadeOut(0);
		setTimeout("$('#openSheet-content').load(work[catIndex].CurrentCoverFlowItem().Href+'?ajax',function(){$('#openSheet-content').fadeIn(0,function(){$('#openSheet').css({'background':'none'});keyboardKill()})})",550)
	}
}

//scripted event for opening blanksheet
function openBlankSheet(){	
	$('.markup-text').html('<span class="markup">Sounds like a project you have in Mind?</span>');
	keyboardKill('dead');
	$('.page-fold').html('<a class="close-icon" href="#">close</a>');
	$('#content').fadeOut(400);
	$('#brand').delay(300).fadeOut(300,function(){ $('#blankSheet').css({'background':'url("_imgs/openSheet_bg.png") no-repeat bottom left #fff'})});
	$('#openSheet').delay(600).animate({'width':'882px'},400).css({'border-color':'#B9C1CA'}).append(projectUIElements);
	$('#openSheet-head,#project-nav-buttons').delay(700).fadeIn(400);
	$('#blankSheet').append('<div id="openSheet-shadow"></div>');
	$('#openSheet-shadow').delay(800).animate({'opacity':'1'},200);
	$('#openSheet-content').fadeIn(400,function(){displayProjectInfo();})
	sheetOpen = true;
	//switch the selected cateogry in the dropdownlist
	closeOpenDropDownCat(catIndex);
	
	
}
//scripted event for closing blanksheet
function closeBlankSheet(){
	$('.markup-text').html('<span class="markup">matchstick</span>');
	
	keyboardKill('dead');
	$('.page-fold').html('');
	$('#openSheet-content').empty().css({'display':'none'});	
	$('#openSheet-shadow').delay(20).animate({'opacity':'0'},200,function(){$('#blankSheet').css({'background':'#fff'});}).detach();
	$('#openSheet-head,#project-nav-buttons').delay(200).fadeOut(200, function(){});
	$('#openSheet').delay(500).animate({'width':'404'},400,function(){$(this).html($('#brand'));}).css({'border-color':'#fff'});
	
	$('#brand').delay(840).fadeIn(400);
	$('#content').delay(940).fadeIn(300,function(){keyboardKill()});
	
	sheetOpen = false;	
}			



function keyboardToolTip(){
	if(toolTipInitalized != keyboardAttemptLimit){
			$('#tooltip').fadeIn('slow');
		}else{
			$('#tooltip').fadeOut('slow');
	}
}

//important, setting the timeout here judging the 'smoothness' of the coverflow and also performance tax. this function might fun on-load
function update (){ 
	work[catIndex].Update();
	$('.title-display').html(work[catIndex].CurrentCoverFlowItem().Title);
	refresh = setTimeout("update()", 30);	
}

$(document).ready(function(){
	
update();
	
//ie, you'll never cease to rickroll me
if($.browser.msie){	$('.category').css({'opacity':'0'}); $('#mobile-pages').css({'opacity':'1'});}


//COVERFLOW TOP TABSCATGORY-NAV
$('#coverFlow-cat-nav li a').click(function(){ switchCategory($(this).parents('li').index()); work[catIndex].GotoFirst();	return false; });

//COVERFLOW-FOOT-NAV AND KEYBOARD NAV
$('#prev-tab a,#prev-project-btn').live('click', function(){ moveBackward(); return false; });
$('#next-tab a,#next-project-btn').live('click', function(){  if(toolTipInitalized < keyboardAttemptLimit){toolTipInitalized++; keyboardToolTip();};  moveForward(); return false;});
$('#view-project-tab').click(function(){ openBlankSheet(); return false });

$(document).keypress(function(e){

	if(keyEnabled == true){
		
	
		
		switch(e.which)
		{
			case 107:	moveForward(); 	if(toolTipInitalized <= keyboardAttemptLimit){keyboardToolTip(); toolTipInitalized++;};
						break;	

			case 106:	moveBackward(); if(toolTipInitalized <= keyboardAttemptLimit){keyboardToolTip(); toolTipInitalized++;};
						break;
						
			case 108:	if(sheetOpen == true){closeBlankSheet();}else{openBlankSheet();}
						break;
		}
	}

});


//OPENSHEET CATEGORY DROPDOWN

$('#category-dropdown li:first-child a,#category-dropdown').live('click',function(){ 
if($('#category-dropdown').height() > 27){
	closeOpenDropDownCat(null,'close');
}else{
	closeOpenDropDownCat(null,'open');
}
return false; 
});

$('#category-dropdown li.default a').live('click',function(){ 
	switchCategory($(this).parents('li').attr('id')) ;
	displayProjectInfo();
	work[catIndex].GotoFirst();			
	closeOpenDropDownCat(null,'close');
			
	return false; 
});


//CLOSE OPENSHEET
$('.close-icon').live('click',function(){
	closeBlankSheet();
	return false;
});



});
