// Slideshow functions
//
var ss_pos = 0
var slide_num=0
var opac=100
var fade_dir="out"
var fade_step=10
var fade_speed=25

function image(reference, caption) {
  this.reference = reference;
  this.caption = caption;
}

function ss_change(direction) {
   slideImage=document.getElementById("slide1")
  if (fade_dir=="out") {
   if ((opac-=fade_step) <=0) {
     fade_dir="in"
      slide_num = slide_num + direction
   if (slide_num >= imgArray.length) { slide_num = 0 }
   if (slide_num < 0) { slide_num = imgArray.length-1 }
   document.getElementById("imageCaption").innerHTML = imgArray[slide_num].caption + ''
      if (imgArray[slide_num].caption == '') {
   document.getElementById("imageCaption").style.display = 'none'
   } else {
   document.getElementById("imageCaption").style.display = 'block'
   }
   ss_updatecounter()
   document.images["slide1"].src = imgArray[slide_num].reference
    setTimeout("ss_change("+direction+")",fade_speed+50)
    } else {
    setTimeout("ss_change("+direction+")",fade_speed)
  }
  } else {
   if ((opac+=fade_step) >=100) fade_dir="out"
   else setTimeout("ss_change("+direction+")",fade_speed)
  }
  // Set opacity in as many different ways as possible
  slideImage.style.filter="alpha(opacity="+opac+")";
  slideImage.style.MozOpacity=(opac/100)+0.1;
  slideImage.style.opacity=(opac/100);
}

// Update Slideshow text (1 of 4)
function ss_updatecounter() {
 document.getElementById("ss_counter").innerHTML= (slide_num+1) +" of "+imgArray.length
}

// Cache Slideshow Images
function ss_cache() {
 for (i=0; i < imgArray.length; i++) {
   ex = 'imgLocation' + i + '= new Image(200,140)'
   ex2 = 'imgLocation' + i + '.src = "' + imgArray[i].reference + '"'
  eval(ex)
   eval(ex2)
 }
}

// Pass in slide show 'type' ie; Location, Accommodation, etc..
function ss_init(type) {
 imgType = type
 imgArray = eval("arr"+type+"Images")
 ss_cache()
   if( imgArray.length > 1 ) {
   ss_updatecounter()
    if (imgArray[0].caption == '') {
     document.getElementById("imageCaption").style.display = 'none'
    } else {
     document.getElementById("imageCaption").style.display = 'block'
    }
 }
}

/**
 * This function is used to change the opacity of
 * 'slideshowtd' and then replace the image within it.
 * Then its opacity is increased.
 */
function fadeSlide(index)
{
  var slidecontainer = getElem("slideshowtd");
  if (fade_dir=="out") {
     if ((opac-=fade_step) <=0) {
        fade_dir="in";
        slidecontainer.innerHTML = imgtag[index];
       if(document.getElementById("viewLargePhotoBottom"))
        {
		if (index!=0 && imgarray[index-1].alt != "")
		{
           document.getElementById("viewLargePhotoBottom").innerHTML = imgarray[index-1].alt;
		}
		else
	    {
	       document.getElementById("viewLargePhotoBottom").innerHTML= "Click on thumbnails to view large photos"
	    }
        }
        fitImage();
        setTimeout("fadeSlide("+index+")",fade_speed+50);
     } else {
        setTimeout("fadeSlide("+index+")",fade_speed);
     }
  } else {
      fitImage();
     if ((opac+=fade_step) >=100)
     {
        fade_dir="out"
     }else{
        setTimeout("fadeSlide("+index+")",fade_speed)
     }
  }
  var image = getElem('slideshowimg');
  image.style.filter="alpha(opacity="+opac+")"
  image.style.MozOpacity=(opac/100)+0.1;
  image.style.opacity=(opac/100);
}

function fitImage(){
   if (document.getElementById('slideshowimg'))
   {
      slideImage = document.getElementById('slideshowimg');
      if(slideImage.width > 410) slideImage.width = 410;
      else if(slideImage.height > 410) slideImage.height = 410;
   }
}

/* toCamelCase: example "font-size".toCamelCase() -> "fontSize" */
String.prototype.toCamelCase = function() {
    return this.replace(/-([a-z])/g, function(a, b) { return b.toUpperCase(); });
};

/*
currentStyle: returns the current value of a style property of a given element
el = element
property = style property (e.g. 'display' or 'font-size')
*/
function currentStyle(el, property) {
  var d = document;
  var v = null;
  if (d.defaultView && d.defaultView.getComputedStyle) {
    var style = d.defaultView.getComputedStyle(el, null);
    v = style && style.getPropertyValue(property);
  } else if (el.currentStyle) {
    v = el.currentStyle[property.toCamelCase()];
  }
  if (v == null && el.style) {
    v = el.style[property.toCamelCase()];
  }
  return v;
}

function displayContent(elementId, fromIndex) {
  var el = getElem(elementId);
  if (el) {
    var heading = el.getElementsByTagName('h3')[0];
    var content = el.firstChild;
    while (content && content.tagName != 'DIV') { content = content.nextSibling }
    if (content) {
      if (currentStyle(content, 'display') == 'block') {
        if (!fromIndex) {
          content.style.display = 'none';
          heading.style.backgroundImage = 'url("/th/common/images/misc/icon_orange_circle_arrow.gif")';
        }
      } else {
        content.style.display = 'block';
        heading.style.backgroundImage = 'url("/th/common/images/misc/icon_orange_circle_arrow_in.gif")';
      }
    }
  }
}

function displayDetailContent(sectionId) {
   if (getElem("details"+sectionId))
   {
      if (getElem("details"+sectionId).style.overflow == 'visible')
      {
         getElem("details"+sectionId).style.overflow = 'hidden';
         getElem("details"+sectionId).style.height = '19px';
         getElem("details"+sectionId+"_img").src = "/th/common/images/misc/icon_orange_circle_arrow_yellow.gif";

      }else{
         getElem("details"+sectionId).style.display = 'block';
         getElem("details"+sectionId).style.overflow = 'visible';
         getElem("details"+sectionId).style.height = 'auto';
         getElem("details"+sectionId+"_img").src = "/th/common/images/misc/icon_orange_circle_arrow_yellow_in.gif";
      }
   }
}

function displayThumbnails(image,index)
{
   if (image.src.indexOf("/asset/lps/")>-1)
   {
      if(image.src.indexOf("/lps/"))
       {
          var tempSrc1 = image.src.substring(image.src.indexOf("/lps/")+5,image.src.length);
          tempSrc1 = tempSrc1.substring(0,tempSrc1.indexOf(lpsExt))
          tempSrc1= tempSrc1+lppExt;
          var tempSrc2 = image.src.substring(image.src.indexOf("/asset/")+7, 0);
          var newImgSrc = tempSrc2+tempSrc1;
       }
      image.src = newImgSrc;
      imgarray[index] = image;

      //resize image
      var slideImage = new Image();
      slideImage.src = newImgSrc;
      if(slideImage.width > 410) slideImage.width = 410;
      else if(slideImage.height > 410) slideImage.height = 410;
      //set image to image array
      if (slideImage.height==0 || slideImage.width==0)
      {
         imgtag[index] = "<img src='"+newImgSrc+"' name='slideshowimg' id='slideshowimg' onerror='displayMainImage(this,"+index+");' />";
      }else{
         imgtag[index] = "<img width='"+slideImage.width+"' height='"+slideImage.height+"' src='"+newImgSrc+"' name='slideshowimg' id='slideshowimg' onerror='displayMainImage(this,"+index+");' />";
      }
      if(document.getElementById("viewLargePhotoBottom"))
      {
	   if (index!=0 && imgarray[index-1].alt != "")
	   {
		  document.getElementById("viewLargePhotoBottom").innerHTML = imgarray[index-1].alt;
	   }
	   else
	   {
		  document.getElementById("viewLargePhotoBottom").innerHTML= "Click on thumbnails to view large photos"
	   }
      }
   }else{
      imgarray[index] = "";
      imgtag[index] = "";
   }
}

function newURLFormation(image, app)
{
   var newStr="";
   if(image.indexOf("/asset/")>-1){
      var tempStr1 = image.substring(image.indexOf("/asset/")+7, image.length);
      var tempStr2 = image.substring(image.indexOf("/asset/"),7);
      if (app == "lps/")
      {
         var tempStr6 = image.substring(0, image.indexOf(lppExt));
         var tempStr7 = tempStr6.substring(tempStr6.indexOf("/asset/")+7,tempStr6.length);
         tempStr7 = tempStr7+lpsExt;
         newStr = tempStr2+app+tempStr7;
      }
     else{
         //alert("inside lpp block");

         newStr = tempStr2+app+tempStr1;

              //alert("lpp"+newStr);

     }
      //alert(newStr);
      return newStr;
   }
}
function displayMainImage(image,index)
{
   if (image.src.indexOf("/asset/lpp/")>-1){
      if (image.src.indexOf("/lpp/" ))
      {
          var tempSrc1 = image.src.substring(image.src.indexOf("/lpp/")+5,image.src.length);
          var tempSrc2 = image.src.substring(image.src.indexOf("/asset/")+7, 0);
          var newImgSrc = tempSrc2+tempSrc1;
      }
      image.src = newImgSrc;

      //resize image
      var slideImage = new Image();
      slideImage.src = newImgSrc;
      if(slideImage.width > 410) slideImage.width = 410;
      else if(slideImage.height > 410) slideImage.height = 410;
      //set image to image array
      if (slideImage.height==0 || slideImage.width==0)
      {
         imgtag[index] = "<img src='"+newImgSrc+"' name='slideshowimg' id='slideshowimg' onerror='displayMainImage(this,"+index+");' />";
      }else{
         imgtag[index] = "<img width='"+slideImage.width+"' height='"+slideImage.height+"' src='"+newImgSrc+"' name='slideshowimg' id='slideshowimg' onerror='displayMainImage(this,"+index+");' />";
      }
   }else{
      image.parentNode.innerHTML = "No image available";
   }
}

function loadThumbs()
{
   var thumbcode = "<table>";
   var arrcount = 0;
   var newImgArr = new Array();
   var newImgTagArr = new Array();
   for(i=0;i<imgarray.length;i++)
   {
      if (imgarray[i]!="")
      {
         newImgArr[arrcount] = imgarray[i];

         imageSrc = imgarray[i].src;
		 imageAlt = imgarray[i].alt;
         if (imageSrc.indexOf("lps")>-1)
         {
            imageSrc = "/lpp/" + imageSrc.substring(imageSrc.indexOf("lps")+4,imageSrc.length);
         }
         else
         {
            imageSrc = "/lpp/" + imageSrc.substring(imageSrc.indexOf("asset")+6,imageSrc.length);
         }
         if (imageSrc.indexOf("asset")==-1)
         {
            imageSrc = "/asset" + imageSrc;
         }
        if(imageSrc.indexOf(lpsExt) > -1)
         {
           imageSrc = imageSrc.substring(0,imageSrc.indexOf(lpsExt))
           imageSrc= imageSrc+lppExt;
         }
         //resize image
         var slideImage = new Image();
         slideImage.src = imageSrc;
         if(slideImage.width > 410) slideImage.width = 410;
         else if(slideImage.height > 410) slideImage.height = 410;
         //set image to image array
         if (slideImage.height==0 || slideImage.width==0)
         {
            newImgTagArr[arrcount+1] = "<img alt='"+imageAlt+"' title=\""+imageAlt+"\" src='"+imageSrc+"' name='slideshowimg' id='slideshowimg' onerror='displayMainImage(this,"+(arrcount+1)+");' />";
         }else{
            newImgTagArr[arrcount+1] = "<img width='"+slideImage.width+"' height='"+slideImage.height+"' src='"+imageSrc+"' name='slideshowimg' id='slideshowimg' onerror='displayMainImage(this,"+(arrcount+1)+");' />";
         }
         thumbcode = thumbcode + "<tr><td><a href=\"javascript:showThumbnail("+arrcount+")\"><img src=\""+imgarray[i].src+"\" alt=\""+imageAlt+"\"  title=\""+imageAlt+"\" width=\"70\" height=\"70\" onerror=\"displayThumbnails(this,"+i+")\" /></a></td></tr>";
         arrcount++;
      }
   }
   imgarray = new Array();
   imgarray = newImgArr;
   imgtag = new Array();
   imgtag = newImgTagArr;
   thumbcode = thumbcode + "</table>";
   if (imgtag.length>1)
   {
      getElem("slidescontainer").innerHTML = thumbcode;
      getElem("slideshowtd").innerHTML = imgtag[1];
   }else{
      getElem("photos").style.display = "none";
      getElem("photoslink").style.display = "none";
   }
}

var currentSlide = 0;
var timeout = "";

function stopSlideShow()
{
   clearTimeout(timeout);
}

function playSlideShow()
{
   clearTimeout(timeout);
   timeout = setTimeout("playSlideShow()",5000)
   browseSlideShow("1");
}

function browseSlideShow(func)
{
   currentSlide = currentSlide + parseInt(func);
   if (currentSlide<0)
   {
      currentSlide = imgtag.length-2;
   }else if (currentSlide>=imgtag.length-1){
      currentSlide = 0;
   }
   fadeSlide(currentSlide+1)
   fitImage();
}
function showThumbnail(slideNumber)
{
   stopSlideShow();
   fadeSlide(slideNumber+1);
   currentSlide = slideNumber;
   fitImage();
   if (imgarray[slideNumber].alt != "")
   {
	  document.getElementById("viewLargePhotoBottom").innerHTML = imgarray[slideNumber].alt;
   }
   else
   {
	  document.getElementById("viewLargePhotoBottom").innerHTML= "Click on thumbnails to view large photos"
   }
}
function showDetail(index, divId)
{
   switch(divId)
   {
      case '1': document.getElementById("details"+index).style.display = "block";
                document.getElementById("details"+index).style.height = 'auto'
                document.getElementById("oneliner"+index).style.display = "none";
                break;
      case '2': document.getElementById("oneliner"+index).style.display = "block";
                document.getElementById("oneliner"+index).style.height = '20'
                document.getElementById("details"+index).style.display = "none";
                break;
   }
}
function hidePreviousDiv(index)
{
   document.getElementById("details"+index).style.display = "none";
   document.getElementById("detailsContainer"+index).style.display = "none";
}function setToolTipForFirstImage()
{
   if (imgarray[0].alt != "")
   {
      document.getElementById("viewLargePhotoBottom").innerHTML = imgarray[0].alt;
   }
   else
	{
	   document.getElementById("viewLargePhotoBottom").innerHTML= "Click on thumbnails to view large photos"
	}
}
