var frameTotal; //total number of "frames" in a section
var currentFrame = 1; //currently displayed frame
var SCROLL_INCREMENT = 900;
var homePage = true;
var newsScroll = 1;
var timeoutId;

//Change out the HTML for the main data section.
function changeHTML(newData, newFrameCount) {
	currentFrame = 1;
	
	//if (newFrameCount)
		//frameTotal = newFrameCount;
	//else
		//frameTotal = 1;
		
	$("#portfolio_home").load(newData, loadComplete);

    if (!window.homePage) {
        swapNews(0);
        clearInterval( window.timeoutId );
    }
    else {
        swapNews(1);
        homePage = false;
    }
}
function setNewFrameTot(nfc) {
	frameTotal = nfc;
	checkBtns();
}
function loadComplete(){
	$('#mainProject').scrollTo(0, 1);
	checkBtns();
}

//Check the state of the frames and set the buttons appropriately
function checkBtns(){
	if (frameTotal == 1){
		$("#btn_start, #btn_rewind, #btn_forward, #btn_end, #navInstru").css("visibility","hidden");
	}
	else if (currentFrame == 1){
		$("#btn_start, #btn_rewind").css("visibility","hidden");
		$("#btn_forward, #btn_end, #navInstru").css("visibility","visible");
	}
	else if (currentFrame > 1 && currentFrame < frameTotal){
		$("#btn_start, #btn_rewind, #btn_forward, #btn_end, #navInstru").css("visibility","visible");
	}
	else if (currentFrame == frameTotal){
		$("#btn_start, #btn_rewind, #navInstru").css("visibility","visible");
		$("#btn_forward, #btn_end").css("visibility","hidden");
	}
}

function scrollToFrame(frameNumber){
	var coordinate = (frameNumber-1) * SCROLL_INCREMENT;
	$('#mainProject').scrollTo( coordinate+'px' );
	currentFrame = frameNumber;
	checkBtns();
}

function swapNews(position){
    $('.news').each( function(n){
        if (this.id.indexOf( position ) != -1){
          this.style.display = 'block';
        }
        else {
          this.style.display = 'none';
        }
    });
}

function scrollNews(){
    swapNews( window.newsScroll );
    ( window.newsScroll == 3 )? window.newsScroll= 1 : window.newsScroll++;
}

//Once the doc is loaded fully
$(document).ready(function() {

	scrollToFrame(1);
	
	//Show one of three random projects.
	var rand_no = Math.floor(7*Math.random())
	switch(rand_no) {
		case 0:
			changeHTML('Stockton_home_360.html');
			break;
		case 1:
			changeHTML('Boulevard_home_360.html');
			break;
		case 2:
			changeHTML('HRB_home_360.html');
			break;
		case 3:
			changeHTML('360office_home_360.html');
			break;
		case 4:
			changeHTML('IRS_home_360.html');
			break;
		case 5:
			changeHTML('Lathrop_home_360.html');
			break;
		case 6:
			changeHTML('Wabash_home_360.html');
			break;
	}
	
	
	//Main menu hover	
	var hoverConfig = {    
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)  	     
		interval: 200, // number = milliseconds for onMouseOver polling interval     
		over: function(){$(this).children('ul.menu').slideDown();},
		timeout: 100, // number = milliseconds delay before onMouseOut    
		out: function(){$(this).children('ul.menu').slideUp();}
	};
	$("div.menuItem").hoverIntent(hoverConfig);


	//Set the nav button behavior
	$.scrollTo.defaults.axis = "x";
	$.scrollTo.defaults.onAfter = checkBtns; 
	$.scrollTo.defaults.easing = "easeInOutQuad"; 
	$.scrollTo.defaults.duration = 1000; 
	
	$("#btn_start").click(function(){
		$('#mainProject').scrollTo(0);
		currentFrame = 1;
		checkBtns();
	});
	
	$("#btn_rewind").click(function(){
		$('#mainProject').scrollTo( '-='+SCROLL_INCREMENT+'px' );
		currentFrame--;
		checkBtns();
	});
	
	$("#btn_forward").click(function(){
		$('#mainProject').scrollTo( '+='+SCROLL_INCREMENT+'px' );
		currentFrame++;
		checkBtns();
	});
	
	$("#btn_end").click(function(){
		$('#mainProject').scrollTo(SCROLL_INCREMENT*(frameTotal-1));
		currentFrame = frameTotal;
		checkBtns();
	});

    $(".btn, .btn2").each(function(b){
        $(this).click( function() { 
            swapNews(new Number( $(this).text() ) )
            clearInterval( window.timeoutId );
            window.newsScroll = 1;
        });
    });

   // $("#home").click(function(){
     //   window.homePage = true;
    //});

    //Start the news scroller
    window.timeoutId = setInterval( "scrollNews()", 10000 );
});
