/*


Sabertooth Interactive LLC
Author: Jesse Pinuelas



*/



var totalFeatureHeight 		= 0;
var totalFeaturedProjects   = $('#fpmContainer').find('[class="featuredProject"]').length;
var currentFeature			= 0;
var featurePositions 		= [];
var intval					= "";


$(function uiInit() {
		$(document).ready(function(){
			$('#nextProject').click(nextProject);
			$('#previousProject').click(previousProject);
			initFeaturedProjects();
			initViewProjectOptions();
			return false;
		});
});




//gather all the featured projects and store their combined heights into totalFeatureHeight
function initFeaturedProjects()
{
	for(var i = 0; i < totalFeaturedProjects; i++)
	{
		// offset for each featured projects top offset  featuredProject1
		featurePositions[i] = totalFeatureHeight;
		totalFeatureHeight += $('#fpmContainer .featuredProject').height();
	}
	// update fpmContainer height to the height of all the features combined	
	$('#fpmContainer').height(totalFeatureHeight);

	// update fpmContainer height to the height of all the features combined	
	$('#fpmContainer').height(totalFeatureHeight);
	intval = window.setInterval(intNextProject, 10000); // updates featured project every 10 secs
}


/*
 * Next feature project handler
 * 
 */

function intNextProject() {
	
	currentFeature++;
	if(currentFeature == totalFeaturedProjects)
	{	
		currentFeature = 0;
		$('#fpmContainer').stop(true,false).animate({top:'442px'},0);
		$('#fpmContainer').stop(true,false).animate({top:-featurePositions[currentFeature]+'px'},450);
	} 
	else
	{
		$('#fpmContainer').stop(true,false).animate({top:-featurePositions[currentFeature]+'px'},450);
	}	
	return false;
}


function nextProject() {
	
	window.clearInterval(intval)
	currentFeature++;
	if(currentFeature == totalFeaturedProjects)
	{	
		currentFeature = 0;
		$('#fpmContainer').stop(true,false).animate({top:'442px'},0);
		$('#fpmContainer').stop(true,false).animate({top:-featurePositions[currentFeature]+'px'},450);
	} 
	else
	{
		$('#fpmContainer').stop(true,false).animate({top:-featurePositions[currentFeature]+'px'},450);
	}	
	return false;
}


/*
 * Previous feature project handler
 * 
 */
function previousProject() {
	window.clearInterval(intval);
		
	currentFeature--;
  	if(currentFeature == -1)
  	{
  		currentFeature = totalFeaturedProjects -1;
  		$('#fpmContainer').stop(true,false).animate({top:-totalFeatureHeight, opacity:0},0);
  		$('#fpmContainer').stop(true,true).animate({top:-featurePositions[currentFeature]+'px', opacity:1},450);
  	}	
  	else
  	{	
		$('#fpmContainer').stop(true,false).animate({top:-featurePositions[currentFeature]+'px'},450);  	
	}
	return false;
}

/*
 * Initializes view project options - click, mouseover and mouseout
 */
function initViewProjectOptions()
{
	for(var i = 0; i < totalFeaturedProjects; i++)
	{
		$('#fpmHitArea'+i).click(function () { viewProject($(this))});
		$('#fpmHitArea'+i).mouseover(function() { showViewProject()});
		$('#fpmHitArea'+i).mouseout(function() { hideViewProject()});
	}
}






/*
 * Enable and disable view project option handler
 */
function showViewProject()
{
	$('.fpmViewer').fadeIn();
}

function hideViewProject()
{
	$('.fpmViewer').fadeOut();
}





/*
 * View project options handler
 */
function viewProject(hitAreaId)
{
	switch(hitAreaId.attr("id"))
	{
		case "fpmHitArea0":
            location.href = "work/interactive/powerrangers/";
        break;
		case "fpmHitArea1":
            location.href = "work/interactive/goarmy/";
        break;
		case "fpmHitArea2":
			location.href = "work/interactive/game_of_thrones/";
        break;
		case "fpmHitArea3":
            location.href = "work/interactive/fordF150/";
        break;
		case "fpmHitArea4":
            location.href = "work/interactive/pepsi/";
        break;
		case "fpmHitArea5":
            location.href = "work/interactive/person_of_interest/";
        break;
		case "fpmHitArea6":
            location.href = "work/interactive/marlboro/";
        break;
	}
}
