// *********************************************
// PROJECT: ILLEGAL PETE'S
// AUTHOR: DREW DAHLMAN via Cactus
// DATE: 9.6.2011
// VERSION: 1.0
// *********************************************

// GLOBAL VARS
var current,_new,_off,bIMG,cPage,cbtn,parent,nbg;

// set up namespaces and pages
var _index = function() {
	return {
		init:function() {
			setTimeout('$("#petes").animate({opacity:1})',500);
			setTimeout('$("#home").animate({"width":"830px"},500)',500);
			setTimeout('$("#promoArea").animate({"width":"511px"},500)',700);
			setTimeout('$("#headlines").animate({top:315})',1100);
			setTimeout('$("#copy").animate({height:94})',1700);
			setTimeout('promos.init()',1300);
			setTimeout('$("#footer").animate({bottom:0})',500);
			setTimeout('$("#recentPosts").fadeIn()',1900);
			setTimeout('$("#promoNav").fadeIn()',1900);
		},
		kill:function() {
			setTimeout('$("#promoNav").fadeOut()',500);
			setTimeout('$("#petes").animate({opacity:0})',500);
			setTimeout('$("#home").animate({"width":"0px"},500)',1700);
			setTimeout('$("#promoArea").animate({"width":"0px"},500,function() {$("#images").hide();})',1200);
			setTimeout('$("#headlines").animate({top:445})',900);
			setTimeout('$("#footer").animate({bottom:-50})',500);
			setTimeout('$("#copy").animate({height:0})',700);
			setTimeout('$("#recentPosts").fadeOut()',300);
			setTimeout('background.kill()',2100);
			setTimeout('navigation.seek(_new);',2300);
		}
	}
}();
// set up namespaces and pages
var _music = function() {
	return {
		init:function() {
			setTimeout('$("#musicNavWrap").animate({left:0},900)',500);
			setTimeout('$("#petes").animate({opacity:1})',500);
			setTimeout('$("#home").animate({"width":"830px"},500)',500);
			setTimeout('$("#promoArea").animate({"width":"511px"},500)',700);
			setTimeout('$("#headlines").animate({top:293})',1100);
			setTimeout('$("#copy").animate({height:94})',1700);
			setTimeout('promosB.init()',1300);
			setTimeout('$("#footer").animate({bottom:0})',500);
			setTimeout('$("#recentPosts").fadeIn()',1900);
			setTimeout('$("#promoNav").fadeIn()',1900);
		},
		kill:function() {
			setTimeout('$("#musicNavWrap").animate({left:-800},900)',500);
			setTimeout('$("#promoNav").fadeOut()',500);
			setTimeout('$("#petes").animate({opacity:0})',500);
			setTimeout('$("#home").animate({"width":"0px"},500)',1700);
			setTimeout('$("#promoArea").animate({"width":"0px"},500,function() {$("#images").hide();})',1200);
			setTimeout('$("#headlines").animate({top:445})',900);
			setTimeout('$("#footer").animate({bottom:-50})',500);
			setTimeout('$("#copy").animate({height:0})',700);
			setTimeout('$("#recentPosts").fadeOut()',300);
			setTimeout('background.kill()',2100);
			setTimeout('navigation.seek(_new);',2300);
		}
	}
}();
var _upcoming = function() {
	return {
		init:function() {
			setTimeout('$("#musicNavWrap").animate({left:0},900)',500);
			
		},
		kill:function() {
			setTimeout('$("#musicNavWrap").animate({left:-800},900)',500);
			setTimeout('background.kill()',2100);
			setTimeout('navigation.seek(_new);',2300);
			
		}
	}
}();

var _upcomingSort = function() {
	return {
		init:function() {
			setTimeout('$("#musicNavWrap").animate({left:0},900)',500);
			
		},
		kill:function() {
			setTimeout('$("#musicNavWrap").animate({left:-800},900)',500);
			setTimeout('background.kill()',2100);
			setTimeout('navigation.seek(_new);',2300);
			
		}
	}
}();

var promos = function() {
	
	var _count = 1;
	var pIMGS = 0;
	var pIMGHolder = 0;
	var initX = 0;
	var t = 0;
	
	return {
		init:function() {
			
			// Promos nav
			$("#right").hover(
				function() {
					$(this).animate({opacity:1});	
				},
				function() {
					$(this).animate({opacity:.5});
				}
			);
			$("#left").hover(
				function() {
					$(this).animate({opacity:1});
				},
				function() {
					$(this).animate({opacity:.5});	
				}
			);
			
			pIMGS = $("#imgHolder").children().length;
			pIMGHolder = pIMGS*511;
			initX = -(pIMGHolder-511);
			
			// set images
			document.getElementById('imgHolder').style.width = pIMGHolder+'px';
			$("#images").animate({width:511});
			document.getElementById('imgHolder').style.left = initX+'px';
			
			// set headlines
			document.getElementById('headlineHolder').style.width = pIMGHolder+50+'px';
			
			$("#right").click(this.goNext);
			$("#left").click(this.goPrev);
			
			var hc = $("#copyHolder").children(".desc").children('p');
			
			$(hc).each(function(i) {
				var c = $(this).text().length;
				//$(this).css("width",c*5.8);
			});
			
			t = setTimeout(this.tick,3500);
		},
		tick:function() {
			if(_count < pIMGS){
				$("#imgHolder").animate({"left":"+=511"});
				$("#headlineHolder").animate({"left":"-=520"});
				$("#copyHolder").animate({"top":"-=110"});
				_count = _count+1;
			}
			else {
				$("#imgHolder").animate({"left":initX});
				$("#headlineHolder").animate({"left":'0'});
				$("#copyHolder").animate({"top":"0"});
				_count = 1;
			}
			t = setTimeout('promos.tick()',3500);
		},
		goNext:function() {
			clearInterval(t); 
			t = 0;
			if(_count < pIMGS){
				$("#imgHolder").animate({"left":"+=511"});
				$("#headlineHolder").animate({"left":"-=520"});
				$("#copyHolder").animate({"top":"-=110"});
				_count = _count+1;
			}
			else {
				$("#imgHolder").animate({"left":initX});
				$("#headlineHolder").animate({"left":'0'});
				$("#copyHolder").animate({"top":"0"});
				_count = 1;
			}
		},
		goPrev:function() {
			clearInterval(t); 
			t = 0;
			if(_count > 1){
				$("#imgHolder").animate({"left":"-=511"});
				$("#headlineHolder").animate({"left":"+=520"});
				$("#copyHolder").animate({"top":"+=110"});
				_count = _count-1;
			}
			else {
				$("#imgHolder").animate({"left":0});
				$("#headlineHolder").animate({"left":(-pIMGS*520)+520});
				$("#copyHolder").animate({"top":(-pIMGS*110)+110});
				_count = pIMGS;
			}

		}
	}
}();
var promosB = function() {
	
	var _count = 1;
	var pIMGS = 0;
	var pIMGHolder = 0;
	var initX = 0;
	var t = 0;
	
	return {
		init:function() {
			
			// Promos nav
			$("#right").hover(
				function() {
					$(this).animate({opacity:1});	
				},
				function() {
					$(this).animate({opacity:.5});
				}
			);
			$("#left").hover(
				function() {
					$(this).animate({opacity:1});
				},
				function() {
					$(this).animate({opacity:.5});	
				}
			);
			
			pIMGS = $("#imgHolder").children().length;
			pIMGHolder = pIMGS*511;
			initX = -(pIMGHolder-511);
			
			// set images
			document.getElementById('imgHolder').style.width = pIMGHolder+'px';
			$("#images").animate({width:511});
			document.getElementById('imgHolder').style.left = initX+'px';
			
			// set headlines
			document.getElementById('headlineHolder').style.width = pIMGHolder+50+'px';
			
			$("#right").click(this.goNext);
			$("#left").click(this.goPrev);
			
			var hc = $("#copyHolder").children(".desc").children('p');
			
			$(hc).each(function(i) {
				var c = $(this).text().length;
				//$(this).css("width",c*5.8);
			});
			
			t = setTimeout(this.tick,3500);
		},
		tick:function() {
			clearInterval(t);
			if(_count < pIMGS){
				$("#imgHolder").animate({"left":"+=511"});
				$("#headlineHolder").animate({"left":"-=478"});
				$("#copyHolder").animate({"top":"-=110"});
				_count = _count+1;
			}
			else {
				$("#imgHolder").animate({"left":initX});
				$("#headlineHolder").animate({"left":'0'});
				$("#copyHolder").animate({"top":"0"});
				_count = 1;
			}
			t = setTimeout('promosB.tick()',3500);
		},
		goNext:function() {
			clearInterval(t);
			t = 0;
			if(_count < pIMGS){
				$("#imgHolder").animate({"left":"+=511"});
				$("#headlineHolder").animate({"left":"-=478"});
				$("#copyHolder").animate({"top":"-=110"});
				_count = _count+1;
			}
			else {
				$("#imgHolder").animate({"left":initX});
				$("#headlineHolder").animate({"left":'0'});
				$("#copyHolder").animate({"top":"0"});
				_count = 1;
			}
		},
		goPrev:function() {
			 clearInterval(t);
			t = 0;
			if(_count > 1){
				$("#imgHolder").animate({"left":"-=511"});
				$("#headlineHolder").animate({"left":"+=478"});
				$("#copyHolder").animate({"top":"+=110"});
				_count = _count-1;
			}
			else {
				$("#imgHolder").animate({"left":0});
				$("#headlineHolder").animate({"left":(-pIMGS*520)+520});
				$("#copyHolder").animate({"top":(-pIMGS*110)+110});
				_count = pIMGS;
			}

		}
	}
}();

var _locations = function() {
	
	return {
		init:function() {
			$('.loc').each(function() {
				$(this).fadeIn();
			});
		},
		kill:function() {
			setTimeout('$("#locations").fadeOut()',300);
			setTimeout('background.kill()',900);
			setTimeout('navigation.seek(_new);',1800);
		},
	}
}();

var _menu = function() {
	
	return {
		init:function() {
			
			// section intro
			setTimeout('$("#subnavB").animate({top:0},900)',500);
					
			// animate a fuggin taco! supa fresh
			$("#fresh img").animate({height:0, width:0},0);
			setTimeout('$("#tacoArea").animate({top:20},function() {$("#tacoArea").animate({top:0},300)})',1300);
			setTimeout('$("#fresh").animate({right:-80,top:25},200);',1900);
			setTimeout('$("#fresh img").animate({height:143,width:162},function() {$("#fresh img").animate({height:124,width:141})})',1800);
			
			// text is nice
			setTimeout('_menu.intro()',1500);
			
			// listen for nav
			$(".subBTN a").hover(
				function() {
					$(this).animate({opacity:1});	
				},
				function() {
					$(this).animate({opacity:.5});
				}
			);
			
			
		},
		intro:function() {
			var hc = $("#menuIntro p");
			$(hc).each(function(i) {
				
				$(this).fadeIn(350*i);
			});
			
		},
		kill:function() {
			setTimeout('$("#subnavB").animate({top:-300},900)',500);	
			setTimeout('$("#tacoArea").animate({opacity:0});',500);
			setTimeout('background.kill()',900);
			setTimeout('$("#menuIntro").fadeOut()',300);
			setTimeout('navigation.seek(_new);',1500);
		}
	}
}();

var _templateA = function() {
	
	return {
		
		init:function() {
			var hc = $(".menuDesc").children('p');
			
			$(hc).each(function(i) {
				var c = $(this).text().length;
				//$(this).css("width",c*5.8);
			});
			
			setTimeout('$("#subnavB").animate({top:0},900)',500);
			setTimeout('$("#menuArea").fadeIn()',1200);	
			setTimeout('$("#intro").fadeIn()',1200);
			
			$(".catBTN").click(function() {
				
				
				if(cbtn == null){
					$("#intro").fadeOut();
				}
				else {
					$("#"+cbtn).fadeOut();
					$(".catActive").addClass('catBTN');
					$(".catActive").removeClass('catActive');
				}
				var mbtn = $(this).children('a').attr('href');
				$(this).toggleClass('catActive');
				
				
				$("#"+mbtn).fadeIn();
				cbtn = mbtn;
				
				return false;
			});
		},
		kill:function() {
			setTimeout('background.kill()',900);
			setTimeout('$("#subnavB").animate({top:-300},900)',500);	
			setTimeout('$("#menuArea").fadeOut()',800);	
			setTimeout('navigation.seek(_new);',1500);
		}
			
	}
}();

var _templateB = function() {
	
	return {
		
		init:function() {
			setTimeout('$("#subnavB").animate({top:0},900)',500);
			setTimeout('$("#menuArea").fadeIn()',1100);
			var hc = $(".menuDesc").children('p');
			
			$(hc).each(function(i) {
				var c = $(this).text().length;
				//$(this).css("width",c*5.8);
			});
			
			
		},
		kill:function() {
			setTimeout('background.kill()',900);
			setTimeout('$("#subnavB").animate({top:-300},900)',500);	
			setTimeout('$("#menuArea").fadeOut()',1100);	
			setTimeout('navigation.seek(_new);',1500);
		}
			
	}
}();

var _catering = function() {
	
	return {
		
		init:function() {
			setTimeout('$("#intro").fadeIn()',1200);
			cbtn = null;
			$(".cBTN").click(function() {
				
				
				if(cbtn == null){
					$("#intro").fadeOut();
				}
				else {
					$("#"+cbtn).fadeOut();
					$(".cActive").addClass('cBTN');
					$(".cActive").removeClass('cActive');
				}
				var mbtn = $(this).children('a').attr('href');
				$(this).toggleClass('cActive');
				
				
				$("#"+mbtn).fadeIn();
				cbtn = mbtn;
				
				return false;
			});
		},
		kill:function() {
			setTimeout('background.kill()',900);
			setTimeout('navigation.seek(_new);',1200);
			setTimeout('$("#cateringArea").fadeOut()',500);
		}
		
	}
}();

var _about = function() {
	
	return {
		
		init:function() {
			// section intro	
			setTimeout('$("#aboutContent").fadeIn()',500);
			setTimeout("_aboutConstant.init()",500);
			// listen for nav
			$(".subBTN a").hover(
				function() {
					$(this).animate({opacity:1});	
				},
				function() {
					$(this).animate({opacity:.5});
				}
			);
		},
		kill:function() {
			setTimeout('background.kill()',900);
			setTimeout('navigation.seek(_new);',1500);
			setTimeout('$("#aboutContent").fadeOut()',1100);
			setTimeout("_aboutConstant.kill()",500);
		}
			
	}
}();
var _history = function() {
	
	var hCount = 0,
		hMax = 0;
		
	return {
		
		init:function() {
			setTimeout("_aboutConstant.init()",500);
			hMax = $("#historyHolder").children().length;
			var hw = $("#historyHolder").children().length*window.innerWidth;
			
			//$("#hContainer").css({width:window.innerWidth});
			$("#historyHolder").css({width:hw});
			$(".itemHolder").css({width:window.innerWidth});
			
			$("#rightArrow").click(function() {
				if(hCount < hMax-1) {
					hCount = hCount +1;
					$("#historyHolder").animate({left:-+window.innerWidth*hCount});
					
				}
				else {
					$("#historyHolder").animate({left:0});
					hCount = 0;
				}
			});
			
			$("#leftArrow").click(function() {
				if(hCount > 0) {
					hCount = hCount -1;
					$("#historyHolder").animate({left:-+window.innerWidth*hCount});
					
				}
				else {
					$("#historyHolder").animate({left:0});
					hCount = 0;
				}
			});
			
		},
		kill:function() {
			setTimeout('background.kill()',900);
			setTimeout('$("#history").fadeOut();',500);
			setTimeout('$("#historyCopy").fadeOut();',500);
			setTimeout('navigation.seek(_new);',1500);
			setTimeout("_aboutConstant.kill()",500);
		}
		
	}
}();
var _contact = function() {
	
	return {
		init:function() {
			setTimeout('$("#contactForm").fadeIn()',500);
			setTimeout("_aboutConstant.init()",500);	
		},
		kill:function() {
			setTimeout('$("#contactForm").fadeOut()',500);
			setTimeout('background.kill()',900);
			setTimeout('navigation.seek(_new);',1500);
			setTimeout("_aboutConstant.kill()",500);
		}
	}
}();
var _jobs = function() {
	return {
		init:function() {
			setTimeout('$("#listings").fadeIn()',500);
			setTimeout("_aboutConstant.init()",500);
		},
		kill:function() {
			
			setTimeout('$("#listings").fadeOut()',500);
			setTimeout('background.kill()',900);
			setTimeout('navigation.seek(_new);',1500);
			setTimeout("_aboutConstant.kill()",500);
		}
	}
}();
var _people = function() {
	
	var ppl;
	var pplOpen = false;
	
	var pID;
	
	return {
	
		init:function() {
			setTimeout("_aboutConstant.init()",500);
			
			$(".cBTN").click(function() {
				ppl = $(this);
				if(pplOpen){
					setTimeout("_people.filter()", 1200);
					_people.resetFeat();
					
				}else{
					_people.filter();
				}
				
			});
			
			$('#peopleHolder li').click(function(){
				pID = $(this).children(".pID").html();
				
				if(pplOpen){
					_people.resetFeat();
					setTimeout('_people.choose()', 800);
						
				}else{
					_people.choose();
				}
			});
		},
		filter:function() {
			$('#peopleHolder').filterable();
			$(".cBTN").removeClass("cActive");
			ppl.toggleClass("cActive");
		},
		kill:function() {
			
			setTimeout('background.kill()',900);
			setTimeout('navigation.seek(_new);',1500);
			setTimeout('$("#people").fadeOut()',500);
			setTimeout("_aboutConstant.kill()",500);
		},
		choose:function(){
			$.ajax({
				url: '../peopleRequest.php',
				data: 'id='+pID,
				type: 'POST',
				success:function(msg){
					$("#peopleFeat").html(msg);
					setTimeout('$("#peopleFeat").slideDown("slow")', 400);
					
					$("#x").click(function() {
						_people.resetFeat();
					});
					
					pplOpen = true;
				},
				error:function(msg){
					//alert("error");
				}
			});
			
		},
		resetFeat:function(){
			$("#peopleFeat").slideUp(600);
			pplOpen = false;
		}
	}
}();
var _aboutConstant = function() {
		return {
			init:function() {
				$("#historyHead").animate({left:0});
				$("#subnavB").animate({top:0},900);
			},
			kill:function() {
				$("#historyHead").animate({left:-500});
				$("#subnavB").animate({top:-300},900);
			}
		}
}();

// THE MIX
var _TheMix = function() {
	return {
		init:function() {
			// css for .held img is display:none;
			// active class on first image
			$(".held img.active").show();
			
			// set active image identifier
			var i = 0;
			// set total number of images to rotate
			var total = $(".held").children('img').length-1;
			
			// click the previous arrow
			$("a.prev").click(function() {
				
				(i == 0) ? i = total : i--;
				$(".held img.active").fadeOut('fast', function() {
					$(".held img.active").removeClass("active");
					$(".held img").eq(i).fadeIn('fast');
					$(".held img").eq(i).addClass("active");
				});
			
			});
			$("#subnavB").animate({top:0},900);
			
			// click the next arrow
			$("a.next").click(function() {
				
				(i == total) ? i = 0 : i++;
				$(".held img.active").fadeOut('fast', function() {
					$(".held img.active").removeClass("active");
					$(".held img").eq(i).fadeIn('fast');
					$(".held img").eq(i).addClass("active");
				});
			});
		},
		kill:function() {
			$("#subnavB").animate({top:-300},900);
			setTimeout('background.kill()',900);
			setTimeout('navigation.seek(_new);',1500);
		}
	}
}();

// MERCH
var _merch = function() {
	return {
		init:function() {
			$("#historyHead").animate({left:0});
		},
		kill:function() {
			$("#historyHead").animate({left:-500});
			setTimeout('background.kill()',900);
			setTimeout('navigation.seek(_new);',1500);
		}
	}
}();
// TWITTER
var _twitter = function() {
	
	var tweetNav;
	var tweetX = 0;
	var tweetMax = $(".tweet_list").length;
	
	return {
		init:function(T) {
			
			tweetMax = T;
			
			$("#twitter #nxt").bind('mouseover', function(){
				tweetNav = "nxt";
				_twitter.over();
			});
			$("#twitter #prev").bind('mouseover', function(){
				tweetNav = "prev";
				_twitter.over();
			});
			
			$("#twitter #nxt").bind('mouseleave', _twitter.off);
			$("#twitter #prev").bind('mouseleave', _twitter.off);
			
			$("#twitter #nxt").bind('click', function(){
				tweetNav = "nxt";
				_twitter.clk();
			});
			$("#twitter #prev").bind('click', function(){
				tweetNav = "prev";
				_twitter.clk();
			});
			
		},
		
		over:function() {
			
			if(tweetNav == "nxt"){
				$(".nxtover").fadeIn();
				$(".prevover").hide();
			}else{
				$(".prevover").fadeIn();
				$(".nxtover").hide();
			}
		},
		off:function() {
			
			$(".nxtover").hide();
			$(".prevover").hide();
		},
		clk:function(){
			
			if(tweetNav == "nxt"){
				tweetX = tweetX + 1;
			}if(tweetNav == "prev"){
				tweetX = tweetX - 1;
			}else{
				//do nothing
			}
			_twitter.add();
		},
		add:function(){
			
			if(tweetX >= tweetMax){
				tweetX = 0;
				_twitter.move();
			}if(tweetX <= 0){
				tweetX = 0;
			}if(tweetX != tweetMax && tweetX < tweetMax){
				_twitter.move();
			}
			
		},
		move:function(){
			//alert(tweetX);
			$(".tweet_list").animate({top:(-35*tweetX)});
		}
	}
}();

// GLOBAL NAME SPACES
var navigation = function() {
	
	return {
		init:function() {
			
			// handle nav clicks
			$(".nBTN a").click(function() {
				if($(this).parent().attr('id') == 'dlMenuBTN'){
					
				}
				else {
				current.kill();
				_new = this.href;
				//return false;
				}
			});
			
			$(".subBTN a").click(function() {
				current.kill();
				_new = this.href;
				//return false;
			});
				// find current and set
				$("#"+cPage+"BTN").addClass('nActive');
				$("#"+cPage+"BTN").removeClass('nBTN');
				
			if(parent == ""){
				
			}
			else {
				// find current and set
				$("#"+parent+"BTN").addClass('nActive');
				$("#"+parent+"BTN").removeClass('nBTN');	
			}
			
			// easter egg
			$("#flag").mouseover(function() {
				t = setTimeout('$("#choose").animate({height:100})',500);	
			});
			$("#choose").mouseleave(function() {
				t = setTimeout('$("#choose").animate({height:0})',500);	
			});
			$(".pLoc").hover(
				function() {
					$(this).animate({opacity:1});	
				},
				function() {
					$(this).animate({opacity:.5});
				}
			);
			$(".pLoc").click(function() {
				//alert(this.id+"IMG");
				nbg = this.id+"IMG";
				setTimeout('background.newBG(nbg)',500);
				
				$.ajax({
					url: 'update.php',
					data: 'loc='+this.id,
					type:'POST',
					success:function(msg) {
						//alert(msg);	
					}
				});
			});
		},
		seek:function(e) {
			$.pjax({
			  url: e,
			  container: '#content'
			});
		}
	}
}();
var _starving = function(){
	return {
		init:function() {
			
		}
	}
}();
var artists = function(){
	return {
		init:function() {
			
		}
	}
}();

$(function() {
	
	// on page load let's bring in the background shall we?
	if(bIMG == ""){
		background.init("lodo");
	}
	else {
		background.init(bIMG);	
	}
	
	
	// also lets start the navigation
	navigation.init();
	if(current == null){
		background.init();
	}
	else {
		current.init();
	}
});

