/* JavaScript support for WeddingsAkumal.com */

/* 
   1.000   04/18/2009   ddsp   Initial menu development      
   1.001   06/02/2009   ddsp   Add timeout for long displays, add return for trip offer "window"
   1.002   06/08/2009   ddsp   Preload all menu icons
   1.003   06/14/2009   ddso   Abandon long/short for popup window: all new colors
   
   2.001   11/04/2009   ddsp   Start new design with new logo...
   2.002   03/27/2010   ddsp   New strucure: Weddings is not the root...
   
   -------------------------------------------------------- */

  function $dId(el) { return document.getElementById(el); }
  

  var oldMenu = 1;
  var activeMenu = 1;
  
  function selectMenu(thisMenu)
  {
  	 //  Darker blue if hovering...
  	 var mindex=(thisMenu<0)?-thisMenu-1:thisMenu;
  	 if (mindex==activeMenu) return;
  	 $dId("mainMenu"+mindex).style.backgroundColor=(thisMenu<0)?"#48b5cc":"#0099a2";
  }
  
  function setMenu(newMenu)
  {
  	 oldMenu=activeMenu;
     activeMenu=newMenu;
     clearPopup();
     var mindex = 0;
     while ($dId("div"+mindex))
     {
    	  if ($dId("mainMenu"+mindex))
     	  {
     	     //  all backgrounds to turquoise
      	   $dId("mainMenu"+mindex).style.backgroundColor="#48b5cc";
     	     //  text of selected to white...
     	     $dId("mainMenu"+mindex).style.color=(activeMenu==mindex)?"#FFFFFF":"#FF7F50";
     	  }
        $dId("div"+mindex).className = ((activeMenu==mindex)?"infoTextShow":"infoTextHide");
        mindex++;
     }   
     window.location.href="#WeddingsAkumal";
  }
  
  function setTabs(group,newTab)
  {
     var tindex = 0;
     while ($dId(group+"tab"+tindex))
     {
        $dId(group+"tab"+tindex).className = "tab200"+((newTab==tindex)?"Active":"Inactive");
        $dId(group+"tab"+tindex+"Contents").className = "infoText"+((newTab==tindex)?"Show":"Hide");
        tindex++;
     }
  }
  
   
  function doOnload()
  {
     if ($dId("bodyEnd"))
     {
       setMenu(0);
       setTabs("div1_",0);
       setUpGallery()
     }
     else
     {
       setTimeout("doOnload()",10);
     }
  }
  
  /* *************** Div0 expand/contract text */
  
  var  inPopup = false;
  
  function enteredPopup()
  {
  	 inPopup = true;
  }
  function leavePopup()
  {
  	 if (inPopup) { clearPopup(); }
  }
  function clearPopup()  
  {
  	 inPopup = false;
  	 $dId("popup").className="infoTextHide";
  }
  function setPopup(thisBase)
  {
     $dId("popupText").innerHTML=$dId("div0_"+thisBase+"Popup").innerHTML;
     $dId("popup").className="popup";
  }  
  
  /* ******************* JUST FOR PHOTOGALLERY ********************** */
  var  galleries       = new Array ("1Food","2Wedding","3Party","4Unallocated","5Unallocated","6Romero","7Town","8Delfin","9Lagoon");
  var  galleryCount    = new Array ( 7,     1,          4,       0,             0,             12,       9,      6,       0);
  var  fileNames       = new Array();
  var  imageCount      = 0;
  var  galleryImages   = new Array();
  
  var  thumbAreas      = 0;                      //  Number of thumbnail image areas shown
  var  firstThumbIndex = 0;
  var  currentImageIndex = 0;
  
  var  photoHeight     = 600;
  var  photoWidth      = 800;
  
  var  imageDelay      = 3000;
  
  var  imageTimer;
  
  var  slideShowActive = false;                     // 0=false 1=true
  
  function setUpGallery()
  {
     
    if (document.images) 
    {
    	  //  alert("setUpGallery");
        photorun =new Image();  photorun.src ="/images/photorun.gif"; 
        photorunover = new Image(); photorunover.src = "/images/photorunover.gif";
        photorundown = new Image(); photorundown.src = "/images/photorundown.gif";
        
        photoall =new Image();  photoall.src ="/images/photoall.gif"; 
        photoallover = new Image(); photoallover.src = "/images/photoallover.gif";
        photoalldown = new Image(); photoalldown.src = "/images/photoalldown.gif";
        
        photoback =new Image();  photoback.src ="/images/photoback.gif"; 
        photobackover = new Image(); photobackover.src = "/images/photobackover.gif";
        photobackdown = new Image(); photobackdown.src = "/images/photobackdown.gif";
        
        photopause =new Image();  photopause.src ="/images/photopause.gif"; 
        photopauseover = new Image(); photopauseover.src = "/images/photopauseover.gif";
        photopausedown = new Image(); photopausedown.src = "/images/photopausedown.gif";
        
        photoplus =new Image();  photoplus.src ="/images/photoplus.gif"; 
        photoplusover = new Image(); photoplusover.src = "/images/photoplusover.gif";
        photoplusdown = new Image(); photoplusdown.src = "/images/photoplusdown.gif";
        
        photominus =new Image();  photominus.src ="/images/photominus.gif"; 
        photominusover = new Image(); photominusover.src = "/images/photominusover.gif";
        photominusdown = new Image(); photominusdown.src = "/images/photominusdown.gif";
        initialSetup();
    }
  }
  function loadGallery()
  {   
    for (var indexg=0; indexg<galleries.length; indexg++)
    {
    	 if (galleryCount[indexg]>0)
    	 {
    	 	  var galleryBase = galleries[indexg];
    	 	  // alert("load "+galleryBase+" with "+galleryCount[indexg]+" images");
    	 	  for (var indexgg=0; indexgg<galleryCount[indexg]; indexgg++)
    	 	  {
         	  imageCount++;
    	 	  	fileNames[imageCount-1] = galleryBase+(""+(100+indexgg+1)).substring(1)+".jpg";
         	  galleryImages[imageCount-1] = new Image(800,600);
         	  galleryImages[imageCount-1].src = "/gallery/"+fileNames[imageCount-1];
         	  // alert("loaded /gallery/"+fileNames[imageCount-1]+" as "+imageCount);
         }
      }
    }
    // alert("imageCount set to "+imageCount);
  }
    
   /* ================ EVENT PROCESSING =================== 
      NOTE: See above for javascript executed before body load...
      
      onload:      initialSetup
      onresize:    doResize
      onKeypress:  doKeyPress
   */
   
   function initialSetup()
   {
   	  // alert("in initial setup");
   	  setThumbAreas();
   	  setPhotoSize();
      loadGallery();
      reloadThumbs(0,0);
      setPhotoScroll();
      div4_setTabs(0);
      nextImage(0,0);
   }   	  
   
      
   function doKeyPress(thisEvent)
   {
     var keynum;
     var keychar;

     if(window.event) // IE
     {
       keynum = window.event.keyCode;
       // alert("IE: "+keynum);
     }
     else if(thisEvent.which) // Netscape/Firefox/Opera
     {
       keynum = thisEvent.which;
       // alert("Firefox: "+keynum);
     }
     else
     {
     	 // alert("not working!?");
     	 keynum = -1;
     }
     if (!slideShowActive) 
     {
       if (keynum == 37) { nextImage(1,-1); }
       else if (keynum == 39) {nextImage(1,1); }
     }  	
   }
   
   /* ================ DUMMY FUNCTION TO MAKE HREFs LOOK BETTER ===== */
   function setPhotos() { }
   
   
   /* ================ THUMBNAILS PROCESSING ============== */
   
   
   function div4_setTabs(activeTab)      // 0=Intro  1=Defin 2=Complex 3=Romero 4=Angel 6=Tang 7=Lagoon 
   {
   	   setTabs("div4_",activeTab)
       slideShowActive = (activeTab == 1);
       if (slideShowActive) { nextAutoImage(); }
       else   {     clearTimeout(imageTimer); }
   }

   
   function setPhotoSize()
   {
   	  //  Problemn with IE:  Can't get proper width of the div!! 
   	  //  var maxWidth = $dId("bodySection").clientWidth-40;
   	  //  alert("bodySection clientWidth:"+maxWidth+", style.width:"+$dId("bodySection").style.widtn);
   	  var photoWidth=990-40;
      	
      photoHeight  = photoWidth * 3 / 4;
      $dId("ShowPhoto").width = photoWidth;
      $dId("ShowPhoto").height = photoHeight;
   }
   
   function setThumbAreas()
   {
   	  //  Problemn with IE:  Can't get proper width of the div!! 
   	  //  var maxWidth = $dId("bodySection").clientWidth-40;
   	  //  alert("bodySection clientWidth:"+maxWidth+", style.width:"+$dId("bodySection").style.widtn);
   	  var maxWidth=990-40;
   	 
   	  if (maxWidth < 400) maxWidth = 400;
   	  maxWidth = (maxWidth-50) / 110;
   	  thumbAreas = Math.floor(maxWidth);
   	  
   	  
   	  // Set the table to hold thumbs and slideshow values
   	  $dId("div4_tabContainer").style.width = (110*thumbAreas+50+20)+"px";  
   	  //  alert("in setThumbAreas(): thumbAreas="+thumbAreas+", width="+$dId("div4_tabContainer").style.width);

      // Set the table to hold the thumbs...   	  
   	  $dId("thumbs").width = thumbAreas*110+50+"px";
   	  var currentClassName = "visiblethumb";
   	  for (var indexd = 0; indexd <= 19; indexd++)
   	  {
         $dId("thumb"+indexd).className = currentClassName;
         if (indexd == thumbAreas-1) { currentClassName = "infoTextHide"; }
      }
   }	  

   function reloadThumbs(startIndex)
   {
   	  var nextThumbImageIndex = startIndex;
 	 	  if (nextThumbImageIndex < 0)                   { nextThumbImageIndex = 0; }
    	if (nextThumbImageIndex + thumbAreas >= imageCount) { nextThumbImageIndex = imageCount-thumbAreas; }    	 	
    	//  Set the next arrows...
    	if (nextThumbImageIndex == 0) 
    	{	$dId("thumbShiftLeft").innerHTML = "&nbsp;"; }
    	else 
    	{	$dId("thumbShiftLeft").innerHTML = "&lt;"; }
    	if (nextThumbImageIndex == imageCount - thumbAreas)
    	{ $dId("thumbShiftRight").innerHTML = "&nbsp;"; }
    	else  
    	{ $dId("thumbShiftRight").innerHTML = "&gt;"; }
    	
   	  var thisClass = "";
   	  firstThumbIndex = nextThumbImageIndex;
   	  for (var indexd = 0; indexd < thumbAreas; indexd++)
   	  {
   	  	 //  alert("set thumb "+indexd+" to galleryImage "+nextThumbImageIndex+" with "+thumbAreas+" thumb areas and "+imageCount+" images");
   	  	 $dId("timage"+indexd).src = galleryImages[nextThumbImageIndex].src;
   	  	 if (nextThumbImageIndex == currentImageIndex) { thisClass = "activethumb"; } else { thisClass = "normalthumb"; }
   	  	 // thisClass = (nextThumbImageIndex == currentImageIndex)? "activethumb": "normalthumb";
   	  	 $dId("thumbLink"+indexd).className = thisClass;
   	  	 // alert("thumbLink"+indexd+".className = "+thisClass);
   	  	 nextThumbImageIndex++;
   	  }
   }

    /* nextAutoImage()    Display next image in loop */
    function nextAutoImage()
    {
      clearTimeout(imageTimer);
      nextImage(1,1);
	    imageTimer = setTimeout("nextAutoImage()",imageDelay);
    }  
    
    //  nextImage: 
    //  0=absolute,  1=relative +/-, 2=thumbPosition
    
    function nextImage(moveType, moveValue)
    {
    	 // alert("nextImage("+moveType+","+moveValue+")");
    	 var nextImageIndex = 0;
    	 if      (moveType == 0) 
    	 { 
    	 	  nextImageIndex = moveValue; 
    	 }
    	 else if (moveType == 1) 
    	 { 
    	 	  nextImageIndex = currentImageIndex + moveValue; 
    	 }
    	 else
    	 { 
    	 	  nextImageIndex = firstThumbIndex + moveValue;
    	 }
    	 
    	 if (slideShowActive)     //  Slideshow wraps, manual does not
    	 {
         if (nextImageIndex < 0)           { nextImageIndex = imageCount-1; }
    	   if (nextImageIndex >= imageCount) { nextImageIndex = 0; }    	 	  
       currentImageIndex = nextImageIndex;    	 
    	 }
    	 else
    	 {
         if (nextImageIndex < 0)           { nextImageIndex = 0; }
    	   if (nextImageIndex >= imageCount) { nextImageIndex = imageCount-1; }    	   
         currentImageIndex = nextImageIndex;    	 
    	 
         //  highlight any thumb with three or more either side.
         if (nextImageIndex < 3) 
         { 
         	  reloadThumbs(0); 
         }
         else if (imageCount - nextImageIndex < 3) 
         { 
       	    reloadThumbs(imageCount - thumbAreas); 
         }
         else if (nextImageIndex - firstThumbIndex < 3) 
         { 
       	    reloadThumbs(nextImageIndex - 3); 
         }
         else if (firstThumbIndex + thumbAreas-1 - nextImageIndex < 3) 
         { 
       	    reloadThumbs(nextImageIndex + 3 - thumbAreas); 
         }
         else reloadThumbs(firstThumbIndex);
       }
       
       $dId("ShowPhoto").src = galleryImages[currentImageIndex].src;
       setPhotoScroll();
   }
   function setPhotoScroll()
   {
       var  leftsideWidth = ((thumbAreas * 110 - 40) * ((0.0 + currentImageIndex) /(0.0 + imageCount)))+20;
       // alert("setPhotoScroll: Leftside="+leftsideWidth+", thumbAreas="+thumbAreas+", currentImageIndex="+
       //     currentImageIndex+",imageCount="+imageCount);
       $dId("leftslider").width = leftsideWidth+"px";
       $dId("rightslider").width = (thumbAreas * 110 -20 - leftsideWidth)+"px"; 
       if (currentImageIndex * 2 > imageCount) 
       {
         $dId("leftslider").innerHTML = (currentImageIndex+1)+" of "+imageCount+"&nbsp;";
         $dId("rightslider").innerHTML = "<br>";
       }
       else
       {
       	 $dId("leftslider").innerHTML = "<br>";
         $dId("rightslider").innerHTML = "&nbsp;"+(currentImageIndex+1)+" of "+imageCount;
       }
   } 	 
    
   /*  BUTTON SERVICE FUNCTIONS */ 
    
   function mouseovr(photofn) {
     if (document.images) 
       eval('document.'+photofn+'show.src='+photofn+'over.src');
   }

   function mouseout(photofn) {
     if (document.images) 
       eval('document.'+photofn+'show.src='+photofn+'.src');
   }
   
   function mousedown(photofn) {
   	 if (document.images)
   	   eval('document.'+photofn+'show.src='+photofn+'down.src');
   }
    
    function priorImage()
    {
    	clearTimeout(imageTimer)
    	mousedown('photoback');
    	setTimeout("mouseovr('photoback')",20)
    	nextImage(1,-1);
   }  
    
   function runImages()
   {
     mousedown('photorun');
     setTimeout("mouseovr('photorun')",20)
     imageTimer = setTimeout("nextAutoImage();",imageDelay);
   }
   
   function pauseImages()
   {
    	mousedown('photopause');
    	setTimeout("mouseovr('photopause')",20)
   	  clearTimeout(imageTimer);
   } 
    
   function increaseTimer()
   { 	
     clearTimeout(imageTimer);
     mousedown('photominus');
     setTimeout("mouseovr('photominus')",20)
     if (imageDelay < 12000) 
     { 
     	  imageDelay = imageDelay + 1000; 
     }
   	 imageTimer = setTimeout("nextAutoImage()",imageDelay);
   } 
   
   function decreaseTimer()
   {
     clearTimeout(imageTimer);
    	mousedown('photoplus');
    	setTimeout("mouseovr('photoplus')",20)
     
     if (imageDelay > 1000) 
     { imageDelay = imageDelay - 1000; }
  	 imageTimer = setTimeout("nextAutoImage()",imageDelay);
   } 

