$(function(){
	hideStuff();
	$('#loginLaunch').click(function(){
		lightBox(loginForm, '400px');
		loginValidation();
	});
	menuNav();
	footerNav();
	callouts();
	page3Nav();
	
	//
	windowResize();
	
}); // Doc ready

var windowW = $(window).width();
var windowH = $(window).height();


/* Function for the navigation in page 3 */
function page3Nav(){
	$('#goto1').live('click', function() {
		loadPage(3);
	});
	$('#back').live('click', function() {
		loadPage(3);
	});
	$('#goto2').live('click', function() {
		loadPage(32);
	});
	$('#next').live('click', function() {
		loadPage(32);
	});
} // page3Nav()

function menuNav(){
	$('#menu > li').click(function(e){
		switch(e.target.id){
			case 'opt1':
				loadPage(1);
			break;
			case 'opt2':
				loadPage(2);				
			break;
			case 'opt3':
				loadPage(3);
			break;
			case 'opt4':
				//loadPage(4);
				$('#mask').scrollTo('#page4', 1000);
			break;
			case 'opt5':
				loadPage(5);
			break;
			case 'opt6':
				loadPage(6);
			break;
			case 'opt7':
				loadPage(7);
			break;
		}
	});
} // menuNav()

function footerNav(){
	$('#footerMenu > li a').click(function(e){
		switch(e.target.id){
			case 'option1':
			loadPage(1);
			break;
			case 'option2':
			loadPage(2);
			break;
			case 'option3':
			loadPage(3);
			break;
			case 'option4':
			//loadPage(4);
			$('#mask').scrollTo('#page4', 1000);
			break;
			case 'option5':
			loadPage(5);
			break;
			case 'option6':
			loadPage(6);
			break;
			case 'option7':
			loadPage(7);
			break;
			case 'option8':
			loadPage(8);
			break;
			case 'option9':
			loadPage(9);
			break;
			case 'option10':
			loadPage(10);
			break;
			}
	});
} // footerNav()

function lightBox(file, fileWidth){
	insertLightBox(file, fileWidth);
	$('#lbClose').click(closeLightBox);

}
function insertLightBox(file, fileWidth){
	$('body').append('<div id="lightbox"></div><div id="whiteBox"><a href="#" id="lbClose">CLOSE X</a><div class="clear"></div></div>');
	$('#lightbox').css('height', windowH);
	$('#lightbox').css('width', windowW);
	$('#lightbox').fadeTo('fast', 0.70);
	//$('#whiteBox').css('left', windowW/2-400);
	//$('#whiteBox').css('top', windowH/2-200);
	$('#whiteBox').append(file);
	$('#whiteBox').css('width', fileWidth);
	centerWhiteBox()
}
function closeLightBox(){
 			$('#lightbox').fadeOut("slow", function(){
			$(this).remove();
		});
		$('#whiteBox').remove();
}

function windowResize(){
	$(window).resize(function(){
		windowW = $(window).width();
		windowH = $(window).height();
		$('#lightbox').css('height', windowH);
		$('#lightbox').css('width', windowW);
		$('#lightbox').fadeTo('fast', 0.70);
		centerWhiteBox();
	});
}

function callouts(){
	$('#callouts a').click(function(e){
		switch(e.target.id){
			case 'callout1':
				lightBox(preVideo, '725px');
			break;
			case 'callout3':
				lightBox(HIWvideo, '800px');
			break;
			case 'callout4':
				loadPage(8);
			break;
			}
	});
}

function loginValidation(){
	var correctUser = 'collective';
	var correctPass = 'wisdom';
	$('#loginBtn').click(function(){
		var typedUser = $('#username').val();
		var typedPass = $('#password').val();
		$('#whiteBox p').remove();
		if(typedUser == correctUser && typedPass == typedPass){
			loginSuccess();
		}else{
			$('#whiteBox').append('<p>Incorrect Username or/and Password, please try again!</p>');
		}
	});
}
function loginSuccess(){
	loadPage(1);
	showStuff();
	$('#loginLaunch').remove();
	closeLightBox();
}



function loadPage(page){
	switch(page){
		case 1:
			$('#mask').scrollTo('#page1', 1000);
		break;
		case 2:
			if($('#page2 #content').length){
			$('#mask').scrollTo('#page2', 1000);
			}else{
			startLoad();
			$("#page2").load("pages/overview.html #content", function(){
				endLoad();
				$('#mask').scrollTo('#page2', 1000);
			});
			}
		break;
		case 3:
			if($('#page3 #content').length){
			$('#mask').scrollTo('#page3', 1000);
			}else{
			startLoad();
			$("#page3").load("pages/founder1.html #content", function(){
				endLoad();
				$('#mask').scrollTo('#page3', 1000);
			});
			}
		break;
		case 32:
			if($('#page3-2 #content').length){
			$('#mask').scrollTo('#page3-2', 1000);
			}else{
			startLoad();
			$("#page3-2").load("pages/founder2.html #content", function(){
				endLoad();
				$('#mask').scrollTo('#page3-2', 1000);
			});
			}
		break;
		case 4:
			if($('#page4 #content').length){
			$('#mask').scrollTo('#page4', 1000);
			}else{
			startLoad();
			$("#page4").load("pages/the_team.html #content", function(){
				endLoad();
				$('#mask').scrollTo('#page4', 1000);
			});
			}
		break;
		case 5:
			if($('#page5 #content').length){
			$('#mask').scrollTo('#page5', 1000);
			}else{
			startLoad();
			$("#page5").load("pages/why.html #content", function(){
				endLoad();
				$('#mask').scrollTo('#page5', 1000);
			});
			}
		break;
		case 6:
			if($('#page6 #content').length){
			$('#mask').scrollTo('#page6', 1000);
			}else{
			startLoad();
			$("#page6").load("pages/how.html #content", function(){
				endLoad();
				$('#mask').scrollTo('#page6', 1000);
			});
			}
			$('#hiwVideo_btn').live('click', function() {
				lightBox(HIWvideo, '800px');
			});
		break;
		case 7:
			if($('#page7 #content').length){
			$('#mask').scrollTo('#page7', 1000);
			}else{
			startLoad();
			$("#page7").load("pages/technology.html #content", function(){
				endLoad();
				$('#mask').scrollTo('#page7', 1000);
			});
			}
		break;
		case 8:
			if($('#page8 #content').length){
			$('#mask').scrollTo('#page8', 1000);
			}else{
			startLoad();
			$("#page8").load("pages/contact.html #content", function(){
				endLoad();
				$('#mask').scrollTo('#page8', 1000);
			});
			}
		break;
		case 9:
			if($('#page9 #content').length){
			$('#mask').scrollTo('#page9', 1000);
			}else{
			startLoad();
			$("#page9").load("pages/terms.html #content", function(){
				$('#page9 .rightColumn').css('overflow', 'scroll');
				$('#page9 .rightColumn').css('overflow-x', 'hidden');
				endLoad();
				$('#mask').scrollTo('#page9', 1000);
			});
			}
		break;
		case 10:
			if($('#page10 #content').length){
			$('#mask').scrollTo('#page10', 1000);
			}else{
			startLoad();
			$("#page10").load("pages/privacy.html #content", function(){
				$('#page10 .rightColumn').css('overflow', 'scroll');
				$('#page10 .rightColumn').css('overflow-x', 'hidden');
				endLoad();
				$('#mask').scrollTo('#page10', 1000);
			});
			}
		break;
	}
}

function startLoad(){
	$('#loader').remove();
	$('#wrapper').append('<div id="loader"><p>LOADING</p></div>', function(){
		$('#loader').fadeIn('slow');
	});
}
function endLoad(){
	$('#loader').fadeOut('slow', function(){
		$('#loader').remove();
	});
}

function centerWhiteBox(){
	var elemH = $('#whiteBox').height();
	var elemW = $('#whiteBox').width();
	$('#whiteBox').css('position', 'absolute');
	$('#whiteBox').css('top', (windowH - elemH)/2);
	$('#whiteBox').css('left', (windowW - elemW)/2);
}

function hideStuff(){
	$('#top').hide();
	$('#subSection').hide();
	$('#footerMenu').hide();
	$('#page3').hide();
	$('#page3-2').hide();
	$('#page5').hide();
}

function showStuff(){
	$('#top').show();
	$('#subSection').show();
	$('#footerMenu').show();
	$('#page3').show();
	$('#page3-2').show();
	$('#page5').show();
}

var HIWvideo = '<object height="495" width="800" codebase="http://www.apple.com/qtactivex/qtplugin.cab" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"><param value="Newvideos/OneNews_DemoFinal_800x480.mov" name="src"/><param value="true" name="autoplay"/><param value="true" name="controller"/><param value="false" name="loop"/><embed height="495" width="800" pluginspage="http://www.apple.com/quicktime/" loop="false" controller="true" autoplay="true" src="Newvideos/OneNews_DemoFinal_800x480.mov"/></object>';

var preVideo = '<object height="260" width="725" codebase="http://www.apple.com/qtactivex/qtplugin.cab" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"><param value="mov/OneNewsPresentation.mov" name="src"/><param value="true" name="autoplay"/><param value="true" name="controller"/><param value="false" name="loop"/><embed height="260" width="725" pluginspage="http://www.apple.com/quicktime/" loop="false" controller="true" autoplay="true" src="mov/OneNewsPresentation.mov"/></object>';

var loginForm = '<form id="loginForm"><label>Username:</label><input type="text" id="username" name="username" tabindex="1" /><label>Password:</label><input type="password" id="password" name="password" tabindex="2" /><a href="#" id="loginBtn" tabindex="3">Login</a></form>';