/************************************/
/*** General JavaScript functions ***/
/************************************/

/*** Pretty opacity fade in/out ***/

var lightboxShowHideTimer = null;
var navShowHideTimer = null;

function showDiv(divId) {
  if(divId == "lightboxDiv" && lightboxShowHideTimer != null) {
    clearTimeout(lightboxShowHideTimer);
    lightboxShowHideTimer = null;
  }
  if(divId == "navigationDiv" && navShowHideTimer != null) {
    clearTimeout(navShowHideTimer);
    navShowHideTimer = null;
  }
  changeOpac(100,divId);
}

function shadeDiv(divId) {
  changeOpac(40,divId);
}

function checkShadeDiv(divId) {
  if(navShowHideTimer == null)
    navShowHideTimer = setTimeout("shadeDiv('" + divId + "')",250);
   if(lightboxShowHideTimer == null)
    lightboxShowHideTimer = setTimeout("shadeDiv('" + divId + "')",250);
}

//change the opacity for different browsers 
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    if(opacity > 0) {
      object.visibility = "visible";
      object.opacity = (opacity / 100);
      object.MozOpacity = (opacity / 100);
      object.KhtmlOpacity = (opacity / 100);
      object.filter = "alpha(opacity=" + opacity + ")";
    } else {
      object.visibility = "hidden";
    }
}

/*** Image gallery functions ***/

var currentIdx = -1;

function selectThumbnail(idx,imgURL,lightboxMode) {
  //alert("Highlight border idx:" + idx + ", imgURL: " + imgURL);
  var thisThumb = document.getElementById("thumb" + idx);
  //Highlight thumbnail border
  thisThumb.style.borderColor = "#9999CC";
  thisThumb.style.borderStyle = "solid";
  thisThumb.style.borderWidth = "3px";
  //Remove old thumbnail border
  if(currentIdx != idx) {
    if(currentIdx > -1 ) {
      //User has clicked on at least one thumbnail
      var browser = navigator.appName;
      var b_version = navigator.appVersion;
      //Detect browser and do not display loading panel if it is IE6
      //as the image onLoad method does not work properly in this browser
      document.getElementById("currentImagePanel").style.visibility = "hidden";
      document.getElementById("imageLoadingPanel").style.visibility = "visible";
      var oldThumb = document.getElementById("thumb" + currentIdx);
      oldThumb.style.borderColor = "white";
      oldThumb.style.borderStyle = "solid";
      oldThumb.style.borderWidth = "3px";
      currentIdx = idx;
    } else {
      //This is called on the initial page load
      currentIdx = 0;
    }
  }
  //Display image from thumbnail
  if(imgURL.length > 0) {
    document.getElementById("currentImage").src = imgURL;
  }
  
  if(lightboxMode) {
    //document.getElementById("lightboxDiv").style.visibility = "hidden";
    //document.getElementById("navigationDiv").style.visibility = "hidden";
    var prevButton = document.getElementById("prevImageButton");
    var nextButton = document.getElementById("nextImageButton");
    if(currentIdx == 0) {
      prevButton.disabled = true;
      nextButton.disabled = false;
    }
    if((currentIdx + 1) == totalImages) {
      prevButton.disabled = false;
      nextButton.disabled = true;
    }
    if(((currentIdx + 1) < totalImages) && currentIdx > 0) {
      prevButton.disabled = false;
      nextButton.disabled = false;
    }
  }
  
}

function selectAlbumThumbnail(idx,imgURL,lightboxMode) {
  initialLoad = false;
  document.getElementById("albumIntroText").style.display = "none";
  document.getElementById("imageLoadingPanel").style.display = "block";
  //If there is a description available for this album, display it
  if(albumDetails["albumDescription_" + idx]) {
    //Description exists, so show it at the bottom of the album picture
    document.getElementById("albumDescriptionPanel").style.visibility = "visible";
    document.getElementById("albumTitle").innerHTML = albumDetails["albumDescription_" + idx]["title"];
    document.getElementById("albumDescription").innerHTML = albumDetails["albumDescription_" + idx]["description"];
  } else {
    //No description, hide it from the screen
    document.getElementById("albumDescriptionPanel").style.visibility = "hidden";
  }
  selectThumbnail(idx,imgURL,lightboxMode);
}

function displayLoadedImage() {
  document.getElementById("imageLoadingPanel").style.visibility = "hidden";
  document.getElementById("currentImagePanel").style.visibility = "visible";
  //alert(document.getElementById("currentImagePanel").style.display);
  //document.getElementById("lightboxDiv").style.visibility = "visible";
  //document.getElementById("navigationDiv").style.visibility = "visible";
}

function displayWeddingBookPageNumbers() {
  if(document.getElementById("imageIndicator")) {
    var pageString;
    if(currentIdx <= 0)
      pageString = "Cover";
    else
      pageString = ((currentIdx * 2)) + " and " + (1 + (currentIdx * 2));
    document.getElementById("imageIndicator").innerHTML = pageString;
  }
}

function nextImage(lightboxMode) {
  if((currentIdx + 1) < totalImages) {
    var thisThumbSrc = document.getElementById("thumb" + (currentIdx + 1)).src;
    var thisThumbSrcArray = thisThumbSrc.split("/");
    var thisFile = thisThumbSrcArray.pop();
    var thisImgURL = imgPath + thisFile;
    selectThumbnail(currentIdx + 1,thisImgURL,lightboxMode);
  }
}

function prevImage(lightboxMode) {
  if((currentIdx - 1) > -1) {
    var thisThumbSrc = document.getElementById("thumb" + (currentIdx - 1)).src;
    var thisThumbSrcArray = thisThumbSrc.split("/");
    var thisFile = thisThumbSrcArray.pop();
    var thisImgURL = imgPath + thisFile;
    selectThumbnail(currentIdx - 1,thisImgURL,lightboxMode);
  }
}
