var navhover = false;
var page = 0;
var currentpos = 0;
var total = -460;
var fuse = false;
var hovered = '';
var hoverfuse = false;

function leftswipe() {
	if (page >= 1) {
		//then we can go left
		var destination;
		page = page - 1;
		if (page == 0) {
			//resume poster cycle
			fuse = false;
			rollout();
			destination = '#posters';	
		}
		else if (page == 1) {
			destination = '#design_content';	
		}
		else if (page == 2) {
			destination = '#vo_content';	
		}
		else if (page == 3) {
			destination = '#podcast_content';	
		}
		else if (page == 4) {
			destination = '#projects_content';	
		}
		else if (page == 5) {
			destination = '#shirts_content';	
		}
		else if (page == 6) {
			destination = '#music_content';	
		}
		else if (page == 7) {
			destination = '#comics_content';	
		}
		else if (page == 8) {
			destination = '#video_content';	
		}
		else if (page == 9) {
			destination = '#about_content';	
		}
		$('#pages').stop().scrollTo( destination, 300), {axis: 'x', easing: 'easeOutBounce'};
	}
}
function rightswipe() {
	if (page == 0) {
		//stop all cycling of posters and reset
		clearTimeout(rollintimer);
		clearTimeout(rollouttimer);
		$('.roll, .poster1, .poster2, .poster3').stop();
		currentpos = '0';
		resetposters();	
	}
	if (page <= 8) {
		//then we can go right
		var destination;
		page = page + 1;
		if (page == 0) {
			//resume poster cycle
			fuse = false;
			rollout();
			destination = '#posters';	
		}
		else if (page == 1) {
			destination = '#design_content';	
		}
		else if (page == 2) {
			destination = '#vo_content';	
		}
		else if (page == 3) {
			destination = '#podcast_content';	
		}
		else if (page == 4) {
			destination = '#projects_content';	
		}
		else if (page == 5) {
			destination = '#shirts_content';	
		}
		else if (page == 6) {
			destination = '#music_content';	
		}
		
		else if (page == 7) {
			destination = '#comics_content';	
		}
		else if (page == 8) {
			destination = '#video_content';	
		}
		else if (page == 9) {
			destination = '#about_content';	
		}
		$('#pages').stop().scrollTo( destination, 300), {axis: 'x', easing: 'easeOutBounce'};
	}
}

function rollout() {
	$('.roll').not(hovered).animate({ opacity: 1 },75);
	position = currentpos + "px";
	$(".row1").not(hovered).animate({
		backgroundPosition: position + " -333px"
	},150);
	$(".row2").not(hovered).animate({
		backgroundPosition: position + " 370px"
	},325);
	$(".row3").not(hovered).animate({
		backgroundPosition: position + " -333px"
	},285);
	$(".row4").not(hovered).animate({
		backgroundPosition: position + " 370px"
	},335);
	$(".row5").not(hovered).animate({
		backgroundPosition: position + " -333px"
	},325);
	$(".row6").not(hovered).animate({
		backgroundPosition: position + " 370px"
	},305);
	$(".row7").not(hovered).animate({
		backgroundPosition: position + " -333px"
	},325);
	$(".row8").not(hovered).animate({
		backgroundPosition: position + " 370px"
	},325);
	if (hoverfuse == true) {
		//resetposters();
		hoverfuse = false;
	}
	rollintimer = setTimeout(rollin,390);
}
function rollin() {
	if (currentpos == (total + 230)) {
		//reset to origin if at the end of the cycle
		currentpos = 230;
	}
	//move playhead
	currentpos = currentpos - 230;
	position = currentpos + "px";
	$(".row1").not(hovered).css('background-position', position + " -333px");
	$(".row2").not(hovered).css('background-position', position + " -370px");
	$(".row3").not(hovered).css('background-position', position + " -333px");
	$(".row4").not(hovered).css('background-position', position + " 370px");
	$(".row5").not(hovered).css('background-position', position + " -333px");
	$(".row6").not(hovered).css('background-position', position + " 370px");
	$(".row7").not(hovered).css('background-position', position + " -333px");
	$(".row8").not(hovered).css('background-position', position + " 370px");
	//animate poster slides
	$(".row1").not(hovered).animate({
		backgroundPosition: position + " 333px"
	},225);
	$(".row2").not(hovered).animate({
		backgroundPosition: position + " 296px"
	},325);
	$(".row3").not(hovered).animate({
		backgroundPosition: position + " 259px"
	},285);
	$(".row4").not(hovered).animate({
		backgroundPosition: position + " 222px"
	},325);
	$(".row5").not(hovered).animate({
		backgroundPosition: position + " 185px"
	},335);
	$(".row6").not(hovered).animate({
		backgroundPosition: position + " 148px"
	},325);
	$(".row7").not(hovered).animate({
		backgroundPosition: position + " 111px"
	},305);
	$(".row8").not(hovered).animate({
		backgroundPosition: position + " 74px"
	},325);	
	$('.roll').not(hovered).animate({ opacity: 0.02 },690);
	rollouttimer = setTimeout('rollout()',8000);	
}
function clickposter(which) {
	
	//set up identification arrays
	poster1options = new Array();
	poster1options[1] = 'nav_vo';
	poster1options[2] = 'nav_shirts';

	
	poster2options = new Array();
	poster2options[1] = 'nav_pc';
	poster2options[2] = 'lb2';

	
	poster3options = new Array();
	poster3options[1] = 'nav_music';
	poster3options[2] = 'nav_pro';

	
	//positions are 0 -230 -460
	//determine which selection we're on based on position
	if (currentpos == -230) {
		pos = 2;	
	}
	else {
		pos = 1;	
	}	
	//kick on mouseover and simulate a click the designated nav item
	//navmouseover();
	if (which == 1) {
		$('#' + poster1options[pos]).click();	
	}
	else if (which == 2) {
		$('#' + poster2options[pos]).click();	
	}
	else if (which == 3) {
		$('#' + poster3options[pos]).click();	
	}
}

$(document).ready(function() {
	//fix background
	var bgleft = $('#maincanvas').offset().left - 17;
	$('body').css({
                backgroundPosition: bgleft + 'px ' + 0
				
	});
	$(window).resize(function() {
		var bgleft = $('#maincanvas').offset().left - 18;
		$('body').css({
					backgroundPosition: bgleft + 'px ' + 0
					
		});
	});
	
	$('body').bind( 'swipeleft', function( e ) {
		rightswipe();
		e.stopImmediatePropagation();
		return false;
  	} );  
	
	
	$('body').bind( 'swiperight', function( e ) {
		leftswipe();
		e.stopImmediatePropagation();
		return false;
  	} );  
	
	
	
	//set up query string array
	var urlParams = {};
	(function () {
	var e,
		a = /\+/g,  // Regex for replacing addition symbol with a space
		r = /([^&=]+)=?([^&]*)/g,
		d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
		q = window.location.search.substring(1);
	
	while (e = r.exec(q))
	   urlParams[d(e[1])] = d(e[2]);
	})();
	if (urlParams['selected']) {
		alert(urlParams['selected']);
	}
	
	//begin poster loop
	setTimeout(rollout,2000);	
	//set up poster hovers
	$('#p1').mouseover(function() {
 		hovered = '.poster1';
		hoverfuse = true;
	});
	$('#p2').mouseover(function() {
 		hovered = '.poster2';
		hoverfuse = true;
	});
	$('#p3').mouseover(function() {
 		hovered = '.poster3';
		hoverfuse = true;
	});
	$('#p1,#p2,#p3').mouseout(function() {
 		hovered = '';
	});

	//set up nav item hovers
	$(".navitem").hover(
	  function () {
		  //mouseover
		  itemmouseover(this);
	  }, 
	  function () {
		  //mouseout
		  if (navhover ==  true) {

		  }
		  if (navhover == false) {
		  	$(this).animate({
				color: "#ccc"
		    },200);		
		  }
	  }
	);
	
	//set up nav bar over/out
	$('.nav').hover(
		function() {
			navhover = true; 
		},
		function() {
			//mouse out
			navhover = false;
			$('.navitem, .spacer').animate({ 
				color: "#000"
			},200);
		    
		}
	);
	
	var listen = [], cipher = "38,38,40,40,37,39,37,39,66,65";
	$(document).keydown(function(e) {
	  listen.push( e.keyCode );
	  if ( listen.toString().indexOf( cipher ) >= 0 ){
		$(document).unbind('keydown',arguments.callee);
		//alert  
		thecode();   
	  }
	});
});

function resetposters() {
	//aligns all poster playhead positions
	$(".row1").not(hovered).css('background-position', currentpos + " -333px");
   	$(".row2").not(hovered).css('background-position', currentpos + " -370px");
   	$(".row3").not(hovered).css('background-position', currentpos + " -333px");
	$(".row4").not(hovered).css('background-position', currentpos + " 370px");
	$(".row5").not(hovered).css('background-position', currentpos + " -333px");
	$(".row6").not(hovered).css('background-position', currentpos + " 370px");
	$(".row7").not(hovered).css('background-position', currentpos + " -333px");
	$(".row8").not(hovered).css('background-position', currentpos + " 370px");	
}

function itemmouseover(which,touch) {
	  $(which).animate({
					color: "#000"
			    },200);
      $('.navitem, .spacer').not(which).animate({ 
				color: "#ccc"
       },200); 
	   
	   if (touch) {
		   		//stop all cycling of posters and reset
		clearTimeout(rollintimer);
		clearTimeout(rollouttimer);
		$('.roll, .poster1, .poster2, .poster3').stop();
		currentpos = '0';
		resetposters();	
		   //console.log('touched');
		   //console.log(which.id);
			if (which.id == "nav_gd") {
					$('#pages').stop().scrollTo( $('#design_content'), 500), {axis: 'x', easing: 'easeOutCirc'};
					page = 1;
			}
			else if (which.id == "nav_vo") {
					$('#pages').stop().scrollTo( $('#vo_content'), 500), {axis: 'x', easing: 'easeOutCirc'};
					page = 2;
			}
			else if (which.id == "nav_pc") {
					$('#pages').stop().scrollTo( $('#podcast_content'), 500), {axis: 'x'};
					page = 3;
			}
			else if (which.id == "nav_pro") {
					$('#pages').stop().scrollTo( $('#projects_content'), 500), {axis: 'x'};
					page = 4;
			}
			else if (which.id == "nav_shirts") {
					$('#pages').stop().scrollTo( $('#shirts_content'), 500), {axis: 'x'};
					page = 5;
			}
			else if (which.id == "nav_music") {
					$('#pages').stop().scrollTo( $('#music_content'), 500), {axis: 'x'};
					page = 6;
			}
		
			else if (which.id == "nav_comic") {
					$('#pages').stop().scrollTo( $('#comics_content'), 500), {axis: 'x'};
					page = 7;
			}
			else if (which.id == "nav_video") {
					$('#pages').stop().scrollTo( $('#video_content'), 500), {axis: 'x'};
					page = 8;
			}
			else if (which.id == "nav_about") {
					$('#pages').stop().scrollTo( $('#about_content'), 500), {axis: 'x'};
					page = 9;
			}
			else if (which.id == "lb2") {
					$('#pages').stop().scrollTo( $('#labs_content'), 500), {axis: 'x'};
					page = 10;
			}
		   
			$('.navitem, .spacer').not(which).animate({ 
				color: "#000"
       		},200); 
	   
	   }
}

function gohome() {
		//resume poster cycle
			fuse = false;
			rollout();
	$('#pages').stop().scrollTo( $('#posters'), 500), {axis: 'x'};
		
	page = 0;
}
function thecode() {
	runner();
	$('#pages').stop().scrollTo( $('#thecode'), 500), {axis: 'x'};	
}

function runner() {
	$("#stage1").stop().css('background-position', '0 0');
	$("#stage1").animate({
			backgroundPosition: "-384px 0px"
	}, 5300, 'linear', function() {
       //runner();
  	});
	setTimeout('runner()',5300);
	
}
