function CSlideShow (crossFadeDuration,slideShowSpeed)
{
	this.crossFadeDuration = crossFadeDuration;
	this.slideShowSpeed = slideShowSpeed;
	
	this.timer;
	this.CurrentPic = 0;
	this.LastPic = 0;
	this.preLoad = new Array()
	
	this.fadePercentage = 0;
}

CSlideShow.prototype.startPreload = function(aPics)
{
	for (var i = 0; i < aPics.length; i++) {
	   this.preLoad[i] = new Image();
	   this.preLoad[i].src = aPics[i];
	}
}

CSlideShow.prototype.runSlideShowPrePart = function()
{        
        if(document.getElementById('SlideShowFade') && document.getElementById('SlideShowFade')) { 
		    document.getElementById('SlideShowFade').style.filter = "alpha(opacity=100)";
		    document.getElementById('SlideShowFade').style.opacity = "1";
		    var _this = this;
            
            if(this.preLoad.length > 1) {
		        document.getElementById('SlideShowFade').childNodes[0].onload = function(){_this.runSlideShow()};
                document.getElementById('SlideShowFade').childNodes[0].src = this.preLoad[this.LastPic].src; 
            }
        }
		//setTimeout(function(){_this.runSlideShow()},50);
}

CSlideShow.prototype.runSlideShow = function()
{
		document.getElementById('SlideShow').style.left = "9999px"; 
		document.getElementById('SlideShow').style.filter = "alpha(opacity=0)";
		document.getElementById('SlideShow').style.opacity = "0"; 

	  var _this = this;
		setTimeout(function(){_this.runSlideShowPart2()},50);
}

CSlideShow.prototype.runSlideShowPart2 = function()
{
        if(this.preLoad[0] != null) {
		    var tsource = this.preLoad[this.CurrentPic].src;

            /*
		    var link = document.getElementById('SlideShow').childNodes[0].src;
		    link = link.replace("/medium/", "/large/");
		    document.getElementById('largeImgLink').href = link;
		    */
            clearInterval(this.hiddenTimer);
	    
		    this.fadePercentage = 0;
		      
		     var _this = this;
		     document.getElementById('SlideShow').childNodes[0].onload = function(){_this.runSlideShowPart3()};
		     document.getElementById('SlideShow').childNodes[0].src = this.preLoad[this.CurrentPic].src;
		     //setTimeout(function(){_this.runSlideShowPart3()},50);
       }
}

CSlideShow.prototype.runSlideShowPart3 = function()
{
        document.getElementById('SlideShow').style.left = "0px"; 
		//document.getElementById('SlideShow').style.display = "block"; 
       // document.getElementById('SlideShowFade').style.display = "block";  
	
		 var _this = this;
		 this.hiddenTimer = setInterval(function(){_this.SlideTick()},50);
		 
		 this.LastPic = this.CurrentPic;
		 this.CurrentPic++;
		 if (this.CurrentPic > (this.preLoad.length-1)) this.CurrentPic=0
		 var _this = this;
		 this.timer = setTimeout(function(){_this.runSlideShowPrePart()}, this.slideShowSpeed)
}

CSlideShow.prototype.SlideTick = function()
{
  this.fadePercentage = this.fadePercentage + 5;
  if(this.fadePercentage < 101) {
	    document.getElementById('SlideShow').style.filter = "alpha(opacity="+this.fadePercentage+")";
	 	document.getElementById('SlideShow').style.opacity = (this.fadePercentage/100);
	 	document.getElementById('SlideShowFade').style.filter = "alpha(opacity="+(100-this.fadePercentage)+")";
	 	document.getElementById('SlideShowFade').style.opacity = (1-(this.fadePercentage/100));
	 } else {
	 	clearInterval(this.hiddenTimer);
	}
}

CSlideShow.prototype.showImage = function(img)
{
	clearTimeout(this.timer);
	document.getElementById('SlideShow').childNodes[0].src = img;
	
    /*
	var link = document.getElementById('SlideShow').childNodes[0].src;
	link = link.replace("/medium/", "/large/");
	document.getElementById('largeImgLink').href = link;
    */
	
	this.CurrentPic = this.findImage(img);
	 this.LastPic = this.CurrentPic;
	 this.CurrentPic++;
	 if (this.CurrentPic > (this.preLoad.length-1)) { this.CurrentPic=0 }
}

CSlideShow.prototype.findImage = function(img)
{
	for (var i = 0; i < this.preLoad.length; i++){
	   if(this.preLoad[i].src.indexOf(img) > -1){
	   		return i;
	   	}
	}
	return 0;
}

CSlideShow.prototype.resumeSlideShow = function()
{
	clearTimeout(this.timer);
	var _this = this;
	this.timer = setTimeout(function(){_this.runSlideShowPrePart()}, this.slideShowSpeed );
}

