// JavaScript Document
if(!Array.indexOf){
  Array.prototype.indexOf = function(obj){
   for(var i=0; i<this.length; i++){
    if(this[i]==obj){
     return i;
    }
   }
   return -1;
  }
}
	$("document").ready(function(){
		debug = false;
		var selectedseason = "";
		var selectedmonth = "";
		var navpos;
		var i =0;
		var monthticker=9;
		var firstrun= true;
		
		
		var month=new Array("january","march","march","april","may","june","july","august","september","october","november","december");
		var season=new Array("winter","spring","summer","fall");
		var seasonpos=new Array(-1863,0,-621,-1242);
		var monthpos = new Array(-992, -1085, 0, -100, -180, -273, -366, -438, -541, -662, -752, -866 );
		var d = new Date();
		var thismonth = d.getMonth(); 
		var thisseason ="";

		/*----------------------------------------------*/



 


		switch (thismonth){
			case 11:
			case 0:
			
				thisseason = 0;
				break;
			case 1:
			case 2:
			case 3:
			case 4:
				thisseason = 1;
				break;
			case 5:
			case 6:
			case 7:
				thisseason = 2;
				break;
			case 8:
			case 9:
			case 10:
				thisseason = 3;
				break;
			default:
				thisseason = "who knows!";
		}
		
		
		/*----------------------------------------------*/
		function slideinfo(x){
			$("#" + x + " .infobg").animate({
				bottom: 0
				}, 300, function() {
				// bg animation complete.
			});
			$("#" + x +" h3").fadeIn().animate({
				left: 40
				}, 300, function() {
				// title animation complete.
			});
			$(".activemonth").animate({
				top: 5
				}, 300, function() {
				// title animation complete.
				$(".activemonth h4").animate({
				left: -98
				}, 500, function() {
				// title animation complete.
				});
			});
			
		}
		/*----------------------------------------------*/
		function resetslideinfo(x){
			$(".activemonth .infobg").animate({
				bottom: -54
				}, 100, function() {
				// bg animation complete.
			});
			$(".activemonth").animate({
				top: 105
				}, 100, function() {
				// title animation complete.
				$(".activemonth h4").animate({
				left: -208
				}, 10, function() {
				// title animation complete.
				});
			});
			x.removeClass("activemonth");
		}
		/*----------------------------------------------*/
		function setnavpos(x){
			y = month.indexOf(x);
			$(".nav").animate({
				left: monthpos[y]
			  	}, 200, function() {
				// Animation complete.
			  });
			 
			
			}
		/*----------------------------------------------*/
		function gotoseason(x, y){
			x = x.replace("#","");
			
			$(".debug").html(x + " " + y);
			$(".debug").append(" " + y);
			
			var season;
			switch(x){
				case "winter" : season = 0; break;
				case "spring" : season = 1; break;
				case "summer" : season = 2; break;
				case "fall"   : season = 3; break;
			}
			$(".active").removeClass("active");
			$("#"+x).addClass("active");
			resetslideinfo($(".activemonth"));
			$("."+y).fadeIn().addClass("activemonth");
			if( x != selectedseason){
				selectedseason = x;
				if(firstrun){
					$(".bannersholder").css({left: seasonpos[season]});
					slideinfo(x);
					firstrun= false;
				}
				else{
					$(".bannersholder").animate({
						left: seasonpos[season]
						}, 1000, function() {
							// season animation complete.
							slideinfo(x);
					 });
				}
				 
				 
				 
			}
			else{
				slideinfo(x);
			}
			
			
			
			
			setnavpos(y);

		}
		/*----------------------------------------------*/
		//itialize season and month setting
		if(debug == true){$(".debug").fadeIn();}
		gotoseason(season[thisseason], month[thismonth]);	
		selectedseason = thisseason;
		selectedmonth = thismonth;
		
		var wintermonths = new Array(11,0,1);
		var springmonths = new Array(2,3,4);
		var summermonths = new Array(5,6,7);
		var fallmonths = new Array(8,9,10);
		var monthscollection = new Array(wintermonths, springmonths, summermonths, fallmonths);
		
		
		
		$(".homebanner").everyTime(8000, 'controlled', function() {
			var offset = season.indexOf($(".active").attr("id"));	
			if(i<=2){
				//resetslideinfo($(".activemonth"));
				$(".debug").html("tick " + i + " o:"+offset+ " ts:"+ season[thisseason] + " tm:" + month[thismonth] + " sm:" + selectedmonth + " " + monthscollection[i]);
				$(".debug").html(monthscollection[offset][i].toString());
				gotoseason($(".active").attr("id"), month[monthscollection[offset][i]]);
				i++;
			}
			else{i=0;}
		});
		
		
		/*----------------------------------------------*/
		$(".homebanner .nav .btn").click(function(){
			//alert("clicked");
			$(".debug").html($(this).attr("href"), $(this).attr("rel"));
			gotoseason($(this).attr("href"), $(this).attr("rel"));
			selectedmonth = month.indexOf($(this).attr("rel"));
			$(this).parents(".homebanner").stopTime('controlled');
			
			return false;
		});
		$(".homebanner .btn.next").click(function(){
			selectedmonth++;
			if(selectedmonth>= 11){
				selectedmonth=0;
				$(".nav").animate({
					left: monthpos[selectedmonth]
					}, 1, function() {
					// Animation complete.
				  });
			}
			else{
				$(".nav").animate({
					left: monthpos[selectedmonth]
					}, 200, function() {
					// Animation complete.
				  });
			}
			return false;
		});
		$(".homebanner .btn.prev").click(function(){
			selectedmonth--;
			if(selectedmonth<= 0){selectedmonth=11}
			$(".debug").html(selectedmonth);
			$(".nav").animate({
				left: monthpos[selectedmonth]
			  	}, 200, function() {
				// Animation complete.
			  });
			return false;
		});
		
	});


