  SlideshowX = function(id,opts) {
  	 this.div = document.getElementById(id);
		 this.linkCtrl = opts.linkElement ? document.getElementById(opts.linkElement) : undefined;
  	 this.image = this.div.getElementsByTagName('img')[0];
  	 this.transferTime = opts.transferTime || 5000;
  	 this.images = opts.images;
  	 this.stayTime = opts.stayTime || 5000;
  	 this.transferRate = opts.transferRate || 100;
  	 this.idx = 0;
  	 this.image.style.backgroundImage = 'url(' + this.images[0].picture + ')';
		 if (this.linkCtrl) this.linkCtrl.href = this.images[0].link;
		 if (opts.linksTarget) {
		 	 this.links = {}; 
  		 var linksCont = document.getElementById(opts.linksTarget);
  		 linksCont.innerHTML = '';
  		 for(var i=0,l=this.images.length;i<l;i++){
  		 		var il = linksCont.appendChild(document.createElement('a'));
  				il.href = '#';
  				il.onclick = this.linkClick;
  				il.idx = i;
  				il.innerHTML = i+1;
  				il.slideshow = this;
  				this.links[i] = il; 
  		 }
  		 this.setHoverLink(0);
		 }
  	 var my = this;
  	 setTimeout(function () {my.setImage(1);}, this.stayTime); 
  }
	SlideshowX.prototype.linkClick = function(){
	  this.slideshow.stop();
		this.slideshow.setImage(this.idx);
		return false;
	}
	
	
  SlideshowX.prototype.stop = function(){
	  this.stopped = 1;
	}

  SlideshowX.prototype.setHoverLink = function(idx){
	  if (!this.links) return false;
	  for(var i in this.links) {this.links[i].className = '';}
	  if (this.links[idx]) {
		  this.links[idx].className = 'hover';
		}
	}
  SlideshowX.prototype.setImage = function(idx){
		 if (this.stopped) {
    	 this.image.style.backgroundImage = 'url(' + this.images[idx].picture + ')';
    	 this.setOpacity(1);
			 this.setHoverLink(idx);
			 clearInterval(this.transInt);
			 return;
		 }
     if (idx >= this.images.length) return false;
  	 this.div.style.backgroundImage = 'url(' + this.images[this.idx].picture + ')';
  	 this.setOpacity(0);
  	 this.image.style.backgroundImage = 'url(' + this.images[idx].picture + ')';
  	 this.idx = idx;
  	 var my = this;
  	 this.transInt = setInterval(function () {my.transfer()},this.transferRate);
  } 
  SlideshowX.prototype.transfer = function () {
  	 this.opacity += this.transferRate / this.transferTime;
		 if (this.linkCtrl && this.opacity > 0.7) {
		 		this.linkCtrl.href = this.images[this.idx].link;
		 }
  	 if (this.opacity > 1) {
  	 		this.opacity = 1;
  			clearInterval(this.transInt);
			  this.setHoverLink(this.idx);
  			var idx = this.idx == this.images.length-1 ? 0 : this.idx + 1;
  			var my = this;
  			setTimeout(function () {my.setImage(idx);}, this.stayTime); 
  	 }
  	 this.setOpacity(this.opacity);
  }
  SlideshowX.prototype.setOpacity = function(opacity) {
  	 this.image.style.opacity = this.opacity = opacity;
  	 if (this.image.filters) {
       nOpacity = this.opacity * 100;
       var oAlpha = this.image.filters['DXImageTransform.Microsoft.alpha'] || this.image.filters.alpha;
       if (oAlpha) oAlpha.opacity = nOpacity;
       else this.image.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";
  	 }
  }
