$(document).ready(function(){
	$("#debugTool").click(function() {$("#debugTool").html("")}) // ofr clearing the debugging window
	//show and hide logo
	//logo2011.hideLogo_as2();
	//logo2011.showLogo_as2();
	//========================LILA DANCE ANIMATION JAVASCRIPT/JQUERY====================================
	//Ended up beng a bit spaghetti coded but i'll try my best at commenting things for you 
	// basic situation is that each menu nav triggers the animation of three grey colour blocks
	// once these are settled the content fades in (it is already set up via php and css, javascript just reveals it)
	// there are bespoked animations for certain sections, such as the mayakaras and the donate page
	//==================================================================================================
	//***************************************************************************************************
	// BEFORE WE START SHOULD POINT OUT THERE ARE SOME GLOBAL VARIABLES WHICH ARE IN THE GLOBAL.JS
	// these refer to;
	// window.bgPhotoJsArray = new Array(); - background images
	// window.initialphoto = 0; - for background image to know where to start in the sequence of bg images
	// window.t=0; - to give a place to pop in the timer for the mayakara images so we can stop or start the timer
	// for mayakaras fading picture gallery
	// window.mayaSectionClicked = false;
	//***************************************************************************************************
      //==============================================================================================
      // talking to flash movie and back again - 
      // thanks to http://www.viget.com/inspire/bi-directional-actionscript-javascript-communication
      //==============================================================================================
        
	 // $('#logo')[0].hideLogo_as3();
	 // $('#logo')[0].showLogo_as3();
  
	  //function getFlashMovie(movieName) {
      	 // var isIE = navigator.appName.indexOf("Microsoft") != -1;
         // return (isIE) ? window[movieName] : document[movieName];
         //    }
		 
		 //--------------------------------------------------------------------------------------------------------------------------------------------------------------------
	//--FADE IN BACKGROUND IMAGE INITIALLY--------------------------------------------------------------------------------------------------------------------------------
		
		$("#main-bg").animate({opacity:1.0},3000);
		
		

      	function callToActionscript_hideLogo() 
             {
                   $("#logo").hide();
                   $("#debugTool").append("<br />hitting logo")//debug
                  //getFlashMovie("logo2011").hideLogo_as3();
				 
             }

        function callToActionscript_showLogo() 
             {
			      $("#logo").show();
				  if (IE_in_use != true) {
                  getFlashMovie("logo2011").showLogo_as3();
				  } 
				 
             }
             
         function sendToJavaScript(val)
            {
             // alert("done");
            }
    
	
	
	//--BUILD ARRAYS FOR THE THREE GREY BLOCKS THAT WILL ANIMATE WHEN MAIN NAV ELEMENTS ARE CLICKED --------------------------------------------------------------------------
	
		//set up block-one, block-two, block-three
		//-------left,top,left,top,left,top
		//block info in arrays below are left px , top px, width, height, opacity for each block
		if (IE_in_use == true && browserNo == "6.0"){//for ie6
			var home = new Array(/*block1*/0,0,0,0,0.0,/*block2*/0,-560,0,0,0.0,/*block3*/0,-1120,0,0,0.0,/*section-tray-position*/0);
			var newsletter = new Array(/*block1*/0,0,0,0,0.0,/*block2*/0,-560,0,0,0.0,/*block3*/0,-1120,0,0,0.0,/*section-tray-position*/0); 
			} else {
			var home = new Array(/*block1*/0,0,1000,600,0.0,/*block2*/0,-560,1000,600,0.0,/*block3*/0,-1120,1000,600,0.0,/*section-tray-position*/0);
			var newsletter = new Array(/*block1*/0,0,1000,600,0.0,/*block2*/0,-560,1000,600,0.0,/*block3*/0,-1120,1000,600,0.0,/*section-tray-position*/0); 
			}
		if (IE_in_use == true) { //if it is IE that the site is being viewed on
			var mayakaras = new Array(/*block1*/0,20,400,90,0.4,/*block2*/615,39,359,340,0.6,/*block3*/8,-310,250,54,0.8,/*section-tray-position*/-560);
			var danceworks = new Array(/*block1*/0,20,310,90,0.4,/*block2*/0,-70,310,90,0.4,/*block3*/0,-160,310,90,0.4,/*section-tray-position*/-560);
		  } else {
			var mayakaras = new Array(/*block1*/0,20,390,90,0.4,/*block2*/615,39,359,340,0.6,/*block3*/8,-310,240,54,0.8,/*section-tray-position*/-560);
			var danceworks = new Array(/*block1*/0,20,300,90,0.4,/*block2*/0,-70,300,90,0.4,/*block3*/0,-160,300,90,0.4,/*section-tray-position*/-560);
		  }
		  
			var people = new Array(/*block1*/0,0,1000,200,0.6,/*block2*/0,0,1000,200,0.6,/*block3*/0,0,1000,200,0.6,/*section-tray-position*/0);
			var other = new Array(/*block1*/0,0,1000,200,0.6,/*block2*/0,0,1000,200,0.6,/*block3*/0,0,1000,200,0.6,/*section-tray-position*/0);
			var ethos = new Array(/*block1*/0,20,240,90,0.4,/*block2*/0,-70,240,90,0.4,/*block3*/0,-160,240,90,0.4,/*section-tray-position*/-560);
			var community = new Array(/*block1*/0,20,240,90,0.4,/*block2*/0,-70,240,90,0.4,/*block3*/0,-160,240,90,0.4,/*section-tray-position*/-560);
			var workshops = new Array(/*block1*/0,310,440,300,0.4,/*block2*/0,-300,440,120,0.4,/*block3*/0,-300,440,190,0.4,/*section-tray-position*/-2240);
			var blog = new Array(/*block1*/0,0,1000,600,0.4,/*block2*/0,-560,1000,600,0.4,/*block3*/0,-1120,1000,600,0.4,/*section-tray-position*/-3360);
			var contact = new Array(/*block1*/-600,0,1000,600,0.1,/*block2*/-600,-600,1000,600,0.1,/*block3*/-600,-1200,1000,600,0.1,/*section-tray-position*/-4920);
			var donate = new Array(/*block1*/0,60,210,90,0.4,/*block2*/0,-30,210,90,0.4,/*block3*/0,-120,210,90,0.4,/*section-tray-position*/0);
			var menuArray = [people, other, ethos, community, workshops, danceworks, blog, contact, donate, home, newsletter, mayakaras] //array collating all of the above
		
	// variables that will help throughout the various functions
	
		var i = 0; //for counters
		var ph = 0; //for mayakara photos
		
		var donatePress = false; //for bg administration
		var mayakaraPress = false; //for bg administration
		var mayaSectionClicked = false;
		
		//=================================================================================
		//for slideshow administration, putting it outside
		var z_indexToFade = 13;
		var z = 0; //for setting the initial z-index's
		var z_level = 0;
		MayaZindex = new Array();
		MayaZindex = [1,2,3,4,5,6,7,8,9,10,11,12,13];
			
		function  startMayaSlideShow() {
					$("#debugTool").append("Should start animating==============================");
					 t=setTimeout( nextMayaPhoto, 1500);				  
				  
				}
				
		function nextMayaPhoto() { 
		
					if ( mayaSectionClicked != true) {  clearTimeout(t) } else {
										
					 //$("#debugTool").append("photos of Maya");
					  $("#debugTool").append("should fade away");
						$('#mayaphotos img').each(function() { //set the initial z-index's
						var imgID = $(this).attr("src");
						var zindexInfo = $(this).attr("style");
						  // $("#debugTool").append("<br />"+imgID+" / "+zindexInfo);
						  if ($(this).css('z-index') == z_indexToFade){
						 $(this).animate({opacity:0},2000, function(){
								//$(this).css('z-index',1); 
								var lastVal = MayaZindex[0];
								MayaZindex.shift();
								MayaZindex.push(lastVal);
								moveUpZIndex();
								});
						} else {
												
						}
				 function moveUpZIndex(){
				   
				   $('#mayaphotos img').each(function() {
				  $(this).animate({opacity:1},1);
					 $(this).css('z-index',MayaZindex[z_level]); 	
					   z_level++;
				   })
				   z_level = 0;
				   startMayaSlideShow();
				 }
			   
				})
				
				}
				
			   }
		//--------------------------------------------------------------------------------
		var blockOneSettled = false; //for later to check if block animation is completed
		var blockTwoSettled = false; //for later to check if block animation is completed
		var blockThreeSettled = false; //for later to check if block animation is completed
		var speedOfBlocks = 600; //speed of movement
		
		var tabSelecter = 0; //for menu array selection
		var menuTabClicked = false; //to help with yukky animation
		var sectionChoice = ""; //section choice to display relevant content
		var sectionArray = ["#people","#otherpeople","#ethos","#comm","#workshops","#dance-works","#blog","#contact","#donate","#home","#newsletter","#want-to-know-more","#aboutus","#mayakaras"]; //sections built
		//need to have a variable that stores the last menu tab clicked so you can make sure odd animation doesn't occur
		var sectionCheck = new Array();
		//links that click to sections of the site
		$("a.comm").click(function(){$("#debugTool").append("<br />workshop "); 
				tabSelecter = 3;
				sectionChoice = "#comm";
				tabChoice = ".comm";
				 menuCheck(sectionChoice);
				 menuTabHighlight(tabChoice);
				 callToActionscript_hideLogo();
				  peopletabreset();
				  aboutusstretch();
		});
		
		//clicks from the menu tabs ---------------------------------------------------------------------------------------//
		$("#main-nav li").click(function() { 
		mayaSectionClicked = false;
		clearTimeout(t);
		//mayaZsort();
		
		var menutab = $(this).attr("class"); //gives name of the li class name user is clicking
		//to stop timing for Mayakaras
		//if ( mayaSectionClicked == true ) {
		
			
		//	mayaSectionClicked = false;
		//	$("#debugTool").append("Should stop animating==============================");
		//}
		//alert(menutab);
		if (menuTabClicked == false) {
		switch(menutab)  //has a switch function which uses the li label to then feed into the menuCheck function to cue the bespoke animation of the three blocks
			{
				case "people":
				  tabSelecter = 0;
				  sectionChoice = "#people";
				  tabChoice = ".people";
				  menuCheck(sectionChoice);
				 menuTabHighlight(tabChoice);
				 peopletabstretch();
				 callToActionscript_hideLogo(); 
				  break;
				  
				 case "otherpeople":
				 tabSelecter = 1;
				 sectionChoice = "#otherpeople";
				 tabChoice = ".otherpeople";
				 menuCheck(sectionChoice);
				 menuTabHighlight(tabChoice);
				 peopletabstretch();
				 callToActionscript_hideLogo();
				 break;
				  
				case "ethos":
				 tabSelecter = 2;
				 sectionChoice = "#ethos";
				  tabChoice = ".ethos";
				  menuCheck(sectionChoice);
				 menuTabHighlight(tabChoice);
				 peopletabreset();
				 aboutusstretch();
				 callToActionscript_hideLogo();
				  break;
				  
				case "comm":
				  tabSelecter = 3;
				  sectionChoice = "#comm";
				   tabChoice = ".comm";
				   menuCheck(sectionChoice);
				 menuTabHighlight(tabChoice);
				  peopletabreset();
				  aboutusstretch();
				  callToActionscript_hideLogo();
				   break;
				case "workshops":
				 tabSelecter = 4;
				 mayaZsort();
				 sectionChoice = "#workshops";
				 tabChoice = ".workshops"; 
				 menuCheck(sectionChoice);
				 menuTabHighlight(tabChoice);
				 peopletabreset();
				 callToActionscript_hideLogo(); 
				  break;
				case "danceworks":
				  tabSelecter = 5;
				  mayaZsort();
				  sectionChoice = "#dance-works";
				  tabChoice = ".danceworks";
				  menuCheck(sectionChoice);
				  menuTabHighlight(tabChoice);
				  peopletabreset();
				  callToActionscript_hideLogo();
				  break;
				case "blog":
				 tabSelecter = 6;
				 mayaZsort();
				 sectionChoice = "#blog";
				 tabChoice = ".blog";
				 menuCheck(sectionChoice);
				 menuTabHighlight(tabChoice);
				 peopletabreset();
				 callToActionscript_showLogo();
				  break;
				case "contact":
				  tabSelecter = 7;
				  mayaZsort();
				  sectionChoice = "#contact";
				  tabChoice = ".contact"; 
				  menuCheck(sectionChoice);
				  menuTabHighlight(tabChoice);
				  peopletabreset();	
				  callToActionscript_showLogo();
				  break;
				case "donate":
				 tabSelecter = 8;
				 mayaZsort();
				 sectionChoice = "#donate";
				 tabChoice = ".donate";
				 menuCheck(sectionChoice);
				 menuTabHighlight(tabChoice);
				 peopletabreset();
				 callToActionscript_hideLogo();
				  break;
				case "home":
				 tabSelecter = 9;
				 mayaZsort();
				 sectionChoice = "#home";
				 tabChoice = ".home";
				 menuCheck(sectionChoice);
				 menuTabHighlight(tabChoice);
				 peopletabreset();
				 callToActionscript_showLogo();
				 break;
				 case "newsletter":
				 tabSelecter = 10;
				 mayaZsort();
				 sectionChoice = "#newsletter";
				 tabChoice = ".newsletter";
				 menuCheck(sectionChoice);
				 menuTabHighlight(tabChoice);
				 peopletabreset();
				 callToActionscript_showLogo();
				 break;
				 
				 case "mayakaras":
				 tabSelecter = 11;
				 mayaSectionClicked = true;
				 mayaZsort();
				 sectionChoice = "#mayakaras";
				 tabChoice = ".mayakaras";
				 menuCheck(sectionChoice);
				 menuTabHighlight(tabChoice);
				 peopletabreset();
				 callToActionscript_hideLogo();
				  break;
				  
			default:
			   $("#debugTool").append("<br />social nav hit");//debug
			  
			}
		}
		//animateSections(tabSelecter); 
		
		});
		
		function resetTabs(){
			$(".home").css("color","#666");
			$(".people").css("color","#666");
			$(".ethos").css("color","#666");
			$(".comm").css("color","#666");
			$(".workshops").css("color","#666");
			$(".danceworks").css("color","#666");
			$(".blog").css("color","#666");
			$(".mayakaras").css("color","#666");
			$(".contact").css("color","#666");
			$(".donate").css("color","#666");
			$(".newsletter").css("color","#666");
		
		}
		function menuTabHighlight(sectionChoice) {
			$("#debugTool").append("<br />menu tab: "+sectionChoice)//debug
			resetTabs();
			$(tabChoice).css("color","white");
			
		}
		
		function menuCheck(sectionChoice) {
				//pushs the choice into an array to help with debugging
			  sectionCheck.push(sectionChoice);
			  menuTabClicked = true; //tells the sequence a tab's been clicked
			  
			  if (sectionCheck.length > 1) {
				 //checks to see if the array has more than one value if it has check previous value as if it is the same nothing should happen
				 //alert(sectionCheck);
				 checkPrev();
				 
			   } else if (sectionCheck.length <= 1) {
				 //if this is the first value in the array go to the animation process
				animateSections(tabSelecter);
			   }
			  
			  function checkPrev() {
			   if (sectionCheck[0] == sectionCheck[1]) {
				 //alert("nothing need doing");
				 //same so no animation needed
				 sectionCheck.shift();
				 menuTabClicked = false;
					} else {animateSections(tabSelecter);
					//animate remove value in 0 position in array, shifting the last thing stored to move to that place ready for next interaction
					sectionCheck.shift();
					}
			  }
			
			
		}
		
		function animateSections(tabSelector) {
			
			//reset block booleans to false. This is so the animation doesn't get interrupted with users clicks, to minimise bugs
			blockOneSettled = false;
			blockTwoSettled = false; 
			blockThreeSettled = false;
			//goes through a hides all sections named in sectionArray.
			for (i=0; i<sectionArray.length; i++) {
			$(sectionArray[i]).hide();
			}
		animateBlocks(tabSelecter); //fires off background rectangle animation
		}
		
		function animateBlocks(tabSelecter) {
		  //alert("animate: "+menuArray[tabSelecter]+"  "+menuArray[tabSelecter][1]);
		  $("#block-one").animate({
									  left:menuArray[tabSelecter][0]+"px",
									  top:menuArray[tabSelecter][1]+"px",
									  width:menuArray[tabSelecter][2]+"px",
									  height:menuArray[tabSelecter][3]+"px",
									  opacity:menuArray[tabSelecter][4]},
									  speedOfBlocks,'swing',
									  function(){blockOneSettled = true;
		  });
		  
		  $("#block-two").animate({
									  left:menuArray[tabSelecter][5]+"px",
									  top:menuArray[tabSelecter][6]+"px",
									  width:menuArray[tabSelecter][7]+"px",
									  height:menuArray[tabSelecter][8]+"px",
									  opacity:menuArray[tabSelecter][9]},
									  speedOfBlocks,'swing',
									  function(){blockTwoSettled = true;
		  });
		  
		  $("#block-three").animate({
									  left:menuArray[tabSelecter][10]+"px",
									  top:menuArray[tabSelecter][11]+"px",
									   width:menuArray[tabSelecter][12]+"px",
									  height:menuArray[tabSelecter][13]+"px",
									  opacity:menuArray[tabSelecter][14]},
									  speedOfBlocks,'swing',
									  function(){
									  blockThreeSettled = true; 
									  checknGo();
		  });
		  
		  function checknGo() {
			if (blockOneSettled==true && blockTwoSettled==true && blockThreeSettled==true  ) { 
			bringInCorrectContent()
			};
			}
		}
		
		function aboutusstretch() {
		 $("#debugTool").append("<br />should be stretching menu ");
			$(".comm").animate({width:120},"fast");
			$(".ethos").html("artistic policy");
			$(".ethos").animate({width:88},"fast");
		}
		
		function peopletabstretch() {
		 $("#debugTool").append("<br />should be stretching menu ");
			$(".otherpeople").animate({width:150},"fast");
			//$("div").animate({width:300},"slow");
		   $(".comm").animate({width:0},"fast");
		   $(".ethos").html("about us");
		   $(".ethos").animate({width:60},"fast");
		}
		
		function peopletabreset() {
		   $(".otherpeople").animate({width:0},"fast");
		   $(".comm").animate({width:0},"fast");
		   $(".ethos").html("about us");
		   $(".ethos").animate({width:60},"fast");
			
		}
		function bringInCorrectContent(){
		//function is for bringing in content and also controlling the background images. 
		//bespoke sections are mayakaras and donate
		 //alert("bring in: "+sectionChoice)
		 menuTabClicked = false;
		 if (sectionChoice == "#donate") {
		  $("#debugTool").append("<br />==========IS DONATE");
			 //donatePress = true;
			 donateBG();
		 } else if (sectionChoice == "#mayakaras"){
		 
			$("#debugTool").append("<br />==========IS MAYAKARAS");
			 mayakarasBG();
			 
		 } else {
		 
		  $("#debugTool").append("<br />==========NOT DONATE");
		 defaultBG();
		 }
		 
		 $(sectionChoice).css({opacity:0});
		 $(sectionChoice).show();
		 $(sectionChoice).animate({opacity:1.0});
		 
		}
		
		
		
		//=======================for donate background
		function donateBG(){
			
				$("#debugTool").append("<br />donatePress = "+donatePress);
				$("#main-bg").animate({opacity:0},1000,function(){
				$('#main-bg').css('background-image', 'url("http://www.liladance.co.uk/wp-content/themes/lila2011/imgs/donatebg.jpg")');
				$("#main-bg").animate({opacity:1.0},1000);
				donatePress = true;
				
							});
							 
							
		};
		//======================for mayakara background
		function mayakarasBG(){
			
				$("#debugTool").append("<br />MayakaraPress = "+donatePress);
				$("#main-bg").animate({opacity:0},1000,function(){
				mayaphotoreveal();
				mayakaraPress = true;
				
							});
							 
							
		};
		
		function defaultBG() {
		
			if (donatePress == true || mayakaraPress == true) {
			
			$("#debugTool").append("<br />background image: "+bgPhotoJsArray[initialphoto]);
			//$("#debugTool").append("<br />background image: ");
				$("#main-bg").animate({opacity:0},1000,function(){
								$('#main-bg').css('background-image', 'url("' + bgPhotoJsArray[initialphoto] + '")');
								$("#main-bg").animate({opacity:1.0},1000);
													
								donatePress = false;
								mayakaraPress = false;
									});			
								}
							}
							
		function mayaZsort(){
		  	
			$("#debugTool").append("ready....");
			$("#debugTool").append("<br />"+MayaZindex);
			//set up for photos
			//get information about the z-index and such things
			$('#mayaphotos img').each(function() { //set the initial z-index's
				   //at the end we have the highest z-index value stored in the z variable
				  $(this).css('z-index', MayaZindex[z]); //apply increased z-index to <img>
				z++;
			});
		}

		
		function mayaphotoreveal() {
					
				 $("#debugTool").append("<br />in comes the Mayakaras ");
				 $("#mayaphotos").animate({opacity:1.0},1000, function(){
				   startMayaSlideShow();
			});
							   
			}
	
});
