var _width = 901;
var _currSection;
var _margin;
var _outerWidth;

function initSlider()
{
	_outerWidth = $("body").outerWidth();
	var wTo = _outerWidth * 4;
	$("#section-slider").css("width", wTo);
	$("#atmosphere").css("width", wTo+3000);
}

function resizeSlider()
{
	var w = $("body").outerWidth();
	var wTo = w * 5;
	$("#section-slider").css("width", wTo);
	resizeSections(w);
	
	var h = $('body').outerHeight();
	
	$("#atmosphere-holder").css("width", w);
	$("#atmosphere-holder").css("height", h);
	$("#atmosphere").css("width", wTo+3000);
}

function resizeSections(w)
{
	_margin = (w/2 - _width/2);
	var _smargin = (5  + (w/2 - _width/2)).toString()+"px";
	var toX;
	var _section = "slide-"+_currSection;
	$("section").each(function()
	{  
		if( $(this).attr('id') != 'slide-home' ) { $(this).css('margin-left', _smargin); }
		if( $(this).attr('id') == _section ) 
		{ 
			var ofs = $(this).position();
			toX = ofs.left;
			if(toX == 0){ toX = _margin; } else {toX = (toX+5)*-1; }
		}
	});	
	
	$("#section-slider").css("left", toX+"px");
	
}

function nextSection()
{
	switch(_currSection)
	{	
		case 'home':
		$.history.load("about");
		break;
		
		case 'about':
		$.history.load("innovation");
		break;
		
		case 'board':
		$.history.load("contact");
		break;
		
		case 'innovation':
		$.history.load("board");
		break;
		
		case 'contact':
		$.history.load("home");
		break;
		
	}
}

function prevSection()
{
	switch(_currSection)
	{	
		case 'home':
		$.history.load("contact");
		break;
		
		case 'about':
		$.history.load("home");
		break;
		
		case 'innovation':
		$.history.load("about");
		break;
		
		case 'board':
		$.history.load("innovation");
		break;
		
		case 'contact':
		$.history.load("board");
		break;		
	}
}

function slideTo(section)
{
	_currSection = section;
	var _section = "slide-"+section;
	var toX;
	
	switch(_currSection)
	{
		case 'home':
		$('#nav-home').children().fadeIn();
		$('#nav-about').children().fadeOut();
		$('#nav-board').children().fadeOut();
		$('#nav-innovation').children().fadeOut();
		$('#nav-contact').children().fadeOut();
		playMovie();	
		break;
		
		case 'about':
		stopMovie();
		$('#nav-home').children().fadeOut();
		$('#nav-about').children().fadeIn();
		$('#nav-board').children().fadeOut();
		$('#nav-innovation').children().fadeOut();
		$('#nav-contact').children().fadeOut();	
		break;
		
		case 'board':
		stopMovie();
		$('#nav-home').children().fadeOut();
		$('#nav-about').children().fadeOut();
		$('#nav-board').children().fadeIn();
		$('#nav-innovation').children().fadeOut();
		$('#nav-contact').children().fadeOut();	
		break;
		
		case 'innovation':
		stopMovie();
		$('#nav-home').children().fadeOut();
		$('#nav-about').children().fadeOut();
		$('#nav-board').children().fadeOut();
		$('#nav-innovation').children().fadeIn();
		$('#nav-contact').children().fadeOut();	
		break;
		
		case 'contact':
		stopMovie();
		$('#nav-home').children().fadeOut();
		$('#nav-about').children().fadeOut();
		$('#nav-board').children().fadeOut();
		$('#nav-innovation').children().fadeOut();
		$('#nav-contact').children().fadeIn();	
		break;
	}
	
	$("section").each(function()
	{  
		if( $(this).attr('id') == _section ) 
		{ 
			var ofs = $(this).position();
			toX = ofs.left;
			if(toX == 0){ toX = _margin; } else {toX = (toX+5)*-1; }
		}
	});	
	
	$("#section-slider").animate({left: toX}, {queue: false, duration: 1100, easing: 'swing'});
	$("#atmosphere").animate({left: toX-800}, {queue: false, duration: 1300, easing: 'swing'});
}

function stopMovie()
{
	$('video')[0].pause();	
}

function playMovie()
{
	$('video')[0].play();    
}


function checkIE()
{
     var rv = -1;
    if (navigator.appName == 'Microsoft Internet Explorer') 
    {
        if(navigator.userAgent.indexOf("MSIE 8.0") != -1 || navigator.userAgent.indexOf("MSIE 7.0") != -1 || navigator.userAgent.indexOf("MSIE 6.0") != -1  )
      	{
      		return(true);
      	}
      else{return(false)}
     }
     else{return(false)}
}

function init()
{
	resizeSlider();
	$('#mainNav').show();
	$('#section-wrapper').show();
	$('footer').show();
		
	$(window).resize(function(){ resizeSlider() });
	
	$("#left-arrow").mousedown(function(){ prevSection(); });
	$("#right-arrow").mousedown(function(){ nextSection(); });
	
	$("#mainNav a").mouseenter(function() {	$(this).children().fadeIn('slow'); });
	$("#mainNav a").mouseleave(function(){ if($(this).attr('id') != "nav-"+_currSection) { $(this).children().fadeOut('fast'); } });
	$("#mainNav a").mouseup(function(){ var id = $(this).attr('id'); _currSection = id.split("nav-")[1]; console.log(_currSection)  });
	
	$("#logo a").mouseup(function(){ _currSection = 'home' });
	
	$.history.init(function(hash)
	{
    	if(hash == "") 
    	{
        	slideTo('home');
        } else {
        	slideTo(hash);
        }
    }, { unescape: ",/" });
}


jQuery(document).ready(function($)
{
	if(checkIE())
	{
		window.location = "badbrowser.html";	
	}
	else
	{
		stopMovie();
		setTimeout(init, 750);
	}
});



