var taglib_arrSlideshowByImageId = new Array();
var taglib_arrSlideshowByGalleryId = new Array();
var taglib_arrSlideshowByToolbar = new Array();

function ImageSlideshow(imageId, galleryId){
  var objImage;
  var imageBloc;
  
  var galleryId;
  var objToolbar;
  
  var bolIsRunning;
  
  var displayTimeout;
  var slideTimeout;
  
  var startImageId;
  
  var strStartSrc;
  var strStopSrc;
  
  var objGallery;
  var bolPreloaded;
  
  this.strStartSrc = '/_images2005/imageviewer/slideshow.gif';
  this.strStopSrc  = '/_images2005/imageviewer/slideshow-active.gif';
  
  this.bolIsRunning = false;
  this.objImage  = getE(imageId);
  this.galleryId = galleryId;
  
  this.objGallery = taglib_arrGalleries[galleryId];
  
  this.imageBloc = getE(imageId).parentNode;
  
  this.startImageId = imageId+'_start';
  this.bolPreloaded = false;
  
  
  taglib_arrSlideshowByGalleryId[galleryId] = this;
  taglib_arrSlideshowByImageId[imageId] = this;
}

ImageSlideshow.prototype.init = function(){
  this.createToolbar();
}

ImageSlideshow.prototype.createToolbar = function(){
  if(!this.objToolbar){
    
    var intHeight = 19;
    
    var intY = getY(this.imageBloc) + getH(this.imageBloc) - intHeight;
    
    var div = document.createElement('div');
    getE(getGlobalDivId()).appendChild(div);
    
    hideE(div);
    
    div.style.position = 'absolute';
    div.style.background = getBackgroundColor();
    
    setX(div, getX(this.imageBloc));
    setY(div, intY);
    
    setH(div, intHeight);
    setW(div, getW(this.objImage));
    
    var intOpacity = 75;
    div.style.opacity = intOpacity  / 100;
    div.style.mozOpacity = div.style.opacity;
    
    if(isIE() && isWin()){
      div.style.filter= 'progid:DXImageTransform.Microsoft.Alpha(opacity='+intOpacity+')';
    }
    
    div.style.zIndex = 1000;
    
    var str = '';
    str += '<table border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">';
    str += '  <tr>';
    str += '    <td width="2"><img src="/_images2005/spacer.gif" width="2" height="1"></td>';
    str += '    <td align="left" height="100%" valign="middle"><a href="javascript:taglib_arrSlideshowByImageId[\''+ this.objImage.id+'\'].prevClick();" onfocus="this.blur();"><img src="/_images2005/imageviewer/back.gif" width="15" height="15" border="0"></a></td>';
    str += '    <td width="15" align="center" valign="middle"><a href="javascript:taglib_arrSlideshowByImageId[\''+ this.objImage.id+'\'].startStop();" onfocus="this.blur();"><img id="'+this.startImageId+'" src="'+this.strStartSrc+'" width="15" height="15" border="0"></a></td>';
    str += '    <td align="right" valign="middle"><a href="javascript:taglib_arrSlideshowByImageId[\''+ this.objImage.id+'\'].nextClick();" onfocus="this.blur();"><img src="/_images2005/imageviewer/next.gif" width="15" height="15" border="0"></a></td>';
    str += '    <td width="2"><img src="/_images2005/spacer.gif" width="2" height="1"></td>';
    str += '  </tr>';
    str += '</table>';
    div.innerHTML = str;
    
    
    this.objImage.onmouseover = function(){
      taglib_arrSlideshowByImageId[this.id].showToolbar();
    }
    
    this.objImage.onmouseout = function(){
      taglib_arrSlideshowByImageId[this.id].hideToolbar();
    }
    
    div.onmouseover = function(){
      taglib_arrSlideshowByToolbar[this].showToolbar();
    }
    
    div.onmouseout = function(){
      taglib_arrSlideshowByToolbar[this].hideToolbar();
    }
    
    this.objToolbar = div;
    taglib_arrSlideshowByToolbar[div] = this;
  }
}

ImageSlideshow.prototype.showToolbar = function(){
  if(this.displayTimeout)
    clearTimeout(this.displayTimeout);
  showE(this.objToolbar);
}

ImageSlideshow.prototype.hideToolbar = function(){
  this.displayTimeout = setTimeout("taglib_arrSlideshowByGalleryId['"+this.galleryId+"'].doHideToolbar();", 100);
}

ImageSlideshow.prototype.doHideToolbar = function(){
  hideE(this.objToolbar);
}

ImageSlideshow.prototype.nextClick = function(){
  this.stopSlideshow();
  this.next();
}

ImageSlideshow.prototype.prevClick = function(){
  this.stopSlideshow();
  this.prev();
}

ImageSlideshow.prototype.next = function(){
  this.objGallery.nextImage();
  
  if(this.bolIsRunning){
    this.slideTimeout = setTimeout("taglib_arrSlideshowByGalleryId['"+this.galleryId+"'].next();", 3000);
  }
}

ImageSlideshow.prototype.prev = function(){
  this.objGallery.prevImage();
}

ImageSlideshow.prototype.startStop = function(){
  if(this.bolIsRunning)
    this.stopSlideshow();
  else
    this.startSlideshow();
}

ImageSlideshow.prototype.startSlideshow = function(){
  if(this.slideTimeout)
    clearTimeout(this.slideTimeout);
    
  getE(this.startImageId).src = this.strStopSrc;
  
  this.slideTimeout = setTimeout("taglib_arrSlideshowByGalleryId['"+this.galleryId+"'].next();", 1000);
  
  this.bolIsRunning = true;
  this.preloadImages();
}

ImageSlideshow.prototype.stopSlideshow = function(){
  if(this.slideTimeout)
    clearTimeout(this.slideTimeout);
    
  getE(this.startImageId).src = this.strStartSrc;
    
  this.bolIsRunning = false;
}

ImageSlideshow.prototype.preloadImages = function(){
  if(this.bolPreloaded)
    return;
   
  var tempImageId = this.imageId+"_preload";
  if(!getE(tempImageId)){
    var div = document.createElement('<DIV>');
    getE(getGlobalDivId()).appendChild(div);
    
    hideE(div);
    div.innerHTML = '<img id="'+tempImageId+'" src="/_images2005/spacer.gif">';
  }
  
  
  var tempImage = getE(tempImageId);
  var preloadedImages = new Array(); 
  var arrImages = this.objGallery.arrImages;
  var strLink;
  for(var i=0; i<arrImages.length; i++){
    
    strLink = arrImages[i].getLink();
    var arr = strLink.split(';');
    strLink = arr[0];
    
    strLink = strLink.replace(this.objImage.id, tempImageId);
    eval(strLink);
    
    preloadedImages[i] = new Image();
    preloadedImages[i].src = tempImage.src;
  }
  
  this.bolPreloaded = true;
}