function startSlideShow(slideShow){
  if(slideShow.timeout_id==null){ 
    switchImage(slideShow,1);
    slideShow.timeout_id=setInterval('switchImage(' + slideShow.photoCollectionName + ',1);',slideShow.interval);}}

function resetTimer(slideShow){
  if(slideShow.timeout_id!=null){
    clearInterval(slideShow.timeout_id);
    slideShow.timeout_id=setInterval('switchImage(' + slideShow.photoCollectionName + ',1);' ,slideShow.interval);}}

function stopSlideShow(slideShow){
  if(slideShow.timeout_id){
    clearInterval(slideShow.timeout_id); 
    slideShow.timeout_id=null;}}

function nextClick(slideShow){
  resetTimer(slideShow);
  switchImage(slideShow,-1);}

function previousClick(slideShow){
  resetTimer(slideShow);
  switchImage(slideShow,1);}

function getSlide(slideShow,offset){
  slideShow.index =(slideShow.index + offset)%(slideShow.images.length - 1);
  if(slideShow.index==0)slideShow.index=slideShow.images.length - 1;
  return(slideShow.images[slideShow.index]);}

function switchImage(slideShow,offset){
  if(slideShow.waitingOnImage==true)return;  
  var image=getSlide(slideShow,offset);
  scheduleImageChange(slideShow,image);}

function switchImageByIndex(slideShow,index){
  if(slideShow.waitingOnImage==true)return; 
  var image=slideShow.images[index];
  scheduleImageChange(slideShow,image);}

function switchVideoByIndex(slideShow,index){
	  var video=slideShow.videos[index];
	  displayVideo(slideShow,video);}

function removeChildren(node){
  if(node.childNodes.length > 0){ 
    childCount=node.childNodes.length; 
    for(var i=0; i < childCount; i++){ 
       node.removeChild(node.firstChild);}}}

function displayLoadingMessage(slideShow){
  var imageTag=document.getElementById(slideShow.imageTagId);
  imageTag.style.display='none';
  var messageNode=document.createElement('p'); 
  messageNode.appendChild(document.createTextNode('Image loading . . . ')); 
  messageNode.style.fontWeight='bold'; 
  messageNode.style.position='absolute'; 
  messageNode.style.width='300px'; 
  messageNode.style.marginLeft='-150px'; 
  messageNode.style.top='35%'; 
  messageNode.style.left='50%'; 
  imageTag.parentNode.insertBefore(messageNode,imageTag );}

function removeLoadingMessage(slideShow){
  var imageTag=document.getElementById(slideShow.imageTagId);
  imageTag.style.display='inline';
  imageTag.parentNode.removeChild(imageTag.parentNode.firstChild );}

function scheduleImageChange(slideShow,image){
	  if(image.complete==false){ 
	    slideShow.waitingOnImage=true; 
	    displayLoadingMessage(slideShow); 
	    image.onload=function(){ removeLoadingMessage(slideShow); displayImage(slideShow,image); }; 
	  }else{
		 displayImage(slideShow,image);}}

function scheduleVideoChange(slideShow,image){
	  displayVideo(slideShow,image);}

function displayImage(slideShow,image){
  replaceImage(slideShow,image);
  replaceLinks(slideShow,image);
  replaceText(slideShow,image);
  replaceComments(slideShow,image);
  slideShow.waitingOnImage=false;}
  
function displayVideo(slideShow,image){
	  replaceVideo(slideShow,image);
	  replaceLinks(slideShow,image);
	  replaceText(slideShow,image);
	  replaceComments(slideShow,image);}
	  
function replaceImage(slideShow,image){
	  var imageTag=document.getElementById(slideShow.imageTagId);
	  imageTag.src=image.src;
	  imageTag.alt=image.alt;
	  if(image.height > parseInt(imageTag.parentNode.style.height)){ 
	     imageTag.style.height=imageTag.parentNode.style.height;
	     var aspect= image.width / image.height;
	     var width=aspect * parseInt(imageTag.style.height);
	     imageTag.style.width=width + 'px';
	  }else { 
	     imageTag.style.width=image.width + 'px';
	     imageTag.style.height=image.height+ 'px';}
	  imageTag.style.marginLeft='-' +  (imageTag.width  / 2)+ 'px';
	  imageTag.style.marginTop='-' +  (imageTag.height / 2)+ 'px';}

function replaceVideo(slideShow,image){
	  var frame=document.getElementById('embedding-frame');
	  frame.src=image.src;}

function replaceLinks(slideShow,image){
var photoDeleteTag=document.getElementById(slideShow.deleteTagId);
if(photoDeleteTag!=null)photoDeleteTag.href=image.deleteLink;  
var photoEditTag=document.getElementById(slideShow.editTagId);
if(photoEditTag!=null)photoEditTag.href=image.editLink; 
var downloadLinkTag=document.getElementById(slideShow.downloadLinkId);
if(downloadLinkTag!=null)downloadLinkTag.href=image.downloadLink;
var postCommentOptionTag=document.getElementById(slideShow.postCommentTagId);
if(postCommentOptionTag!=null)postCommentOptionTag.href=image.postCommentLink;

var photoLinkUrlTag=document.getElementById(slideShow.linkUrlTagId);
if(photoLinkUrlTag!=null){
  if(photoLinkUrlTag.childNodes[0]!=null){ 
    photoLinkUrlTag.childNodes[0].data=image.linkUrl;  
    if(image.linkUrl.indexOf('http://')== -1){   
      photoLinkUrlTag.href='http://' + image.linkUrl;}
    else{                                             
      photoLinkUrlTag.href=image.linkUrl;}}
  else { 
    photoLinkUrlTag.appendChild(document.createTextNode(image.linkUrl));}}
}

function replaceText(slideShow,image){
replaceTagText(slideShow.titleTagId,image.title);
replaceTagText(slideShow.descriptionTagId,image.description);}

function replaceTagText(tagId,newText){
var tag=document.getElementById(tagId);
if(tag!=null){
removeChildren(tag); 
if(newText!=null)tag.appendChild(document.createTextNode(jsDecode(newText)));}}

function replaceComments(slideShow,image){
  var photoCommentsTag=document.getElementById(slideShow.commentsTagId);
  var photoCommentsHeaderTag=document.getElementById(slideShow.commentHeaderTagId);
  if(photoCommentsTag!=null){
  while(photoCommentsTag.firstChild){
     photoCommentsTag.removeChild(photoCommentsTag.firstChild);} 
  if(image.comments!=null){
    var commentCount=image.comments.length;
    for(var i=0; i < image.comments.length; i++){
      var singleComment=document.createElement("p");
      singleComment.style.padding='5px 10px';
      singleComment.style.margin='0';
      if(i % 2==0)singleComment.className=slideShow.commentStyleClass;
      else  singleComment.className=slideShow.commentAltStyleClass;
      photoCommentsTag.appendChild(singleComment);
      var commentText=document.createTextNode(image.comments[i]);
      singleComment.appendChild(commentText);}
    photoCommentsTag.style.display='block';
    photoCommentsHeaderTag.style.display='block';}
  else {
    photoCommentsTag.style.display='none';
    photoCommentsHeaderTag.style.display='none';}}}

function jsDecode(str){
str=str.replace(/"&amp;"/g,"&");
str=str.replace(/"&#39;"/g,"'");
return str;}

