// JavaScript Document
if(typeof WORXpro=='undefined'){
	WORXpro=function(){};
}

WORXpro.SlideShow=function(){
	
	this.images=new Array(); //array of images e.g. = new Array('images/1.jpg','images/2.jpg')
	this.speed=1000; //time to switch image in millisecond
	
	this.container1=document.getElementById("image1") //default image containers
	this.container2=document.getElementById("image2") //two are needed so that animation is smooth
	
	this.width=200; //overwrite with your image width and height
	this.height=200;
	
	this.texts=new Array() //array of texts for each slide
	this.textContainer=document.getElementById("textContainer");
	
	this.repeat=true;
	
	//private var
	_currentSlidePosition=0;
	_iTransitionEffect=null //timer for transition Effect
	_iTransition=null //timer for slide change
	_opacity=10;
	___pvtSlideShow=this;
		this.setContainers=function(_imageHolder1,_imageHolder2,_textContainer){
		this.container1=document.getElementById(_imageHolder1);
		this.container2=document.getElementById(_imageHolder2);
		this.textContainer=document.getElementById(_textContainer);
		this._setStyles();
	}
	this.setDimension=function(_iWidth,_iHeight){
		this.width=_iWidth
		this.height=_iHeight
		this._setStyles();
	}
	//do some style jobs;
	this._setStyles=function(){
		with(this.container1.style){
			display='block';
			width=this.width +"px"
			height=this.height +"px"
		}
		
		with(this.container2.style){
			display='block';
			position='relative';
			top=((-1)*this.height)+'px';//send it at same pos as container 1
			zIndex=-1; //send it behind			
			width=this.width +"px"
			height=this.height +"px"
			
		}
	}
	this._setStyles();
	this.first=function(){
		_currentSlidePosition=-1;
		this.next();
	}
	this.goto=function(n){
		if(n<this.images.length){
			_currentSlidePosition=n-2
			this.next();
			this.pause();
		}
	}
	
	this.last=function(){
		_currentSlidePosition=this.images.length-2;
		this.next();
	}
	
	this.next=function(){
		//load next image into back
		if(this.hasNext()){
			this.container2.src=this.images[_currentSlidePosition+1]
		}else{
			this.container2.src=this.images[0]
			if(this.repeat){this.first();}else{return false;}
		}
		
		//clear old timer if it's still animating
		if(_iTransitionEffect) {window.clearInterval(_iTransitionEffect);}
		
		//now fade out the first slide
		___pvtSlideShow=this;
		_iTransitionEffect=window.setInterval("_fadeOut()",50);
		
		if(_currentSlidePosition<this.images.length-1){
			_currentSlidePosition++;
		}
		//load texts
		if(this.textContainer && (typeof this.texts[_currentSlidePosition]!='undefined')){
			this.textContainer.innerHTML=this.texts[_currentSlidePosition];
		}
		
	}
	
	this.hasNext=function(){
		return _currentSlidePosition<this.images.length-1;
	}
	
	this.prev=function(){
		if(!this.hasPrev()) {
		 if(this.repeat){this.last();return;}else{return false;}
		}
		_currentSlidePosition=_currentSlidePosition-2;
		this.next();
	}
	
	this.hasPrev=function(){
		return _currentSlidePosition!=0;
	}
	
	_fadeOut=function(){
		
		_opacity=_opacity-1;
		
		___pvtSlideShow.container1.style.opacity = _opacity/10;
		___pvtSlideShow.container1.style.filter = 'alpha(opacity=' + _opacity*10 + ')';
		
		//fading is done now let's reset
		if(_opacity==0){
			_opacity=10;
			
			window.clearInterval(_iTransitionEffect);
			___pvtSlideShow.container1.src=___pvtSlideShow.container2.src;
			
			___pvtSlideShow.container1.style.opacity = 10/10;
			___pvtSlideShow.container1.style.filter = 'alpha(opacity=' + 10*10 + ')';
		}
	}
	
	this.play=function(){
	___pvtSlideShow=this;
	_iTransition=window.setInterval("_autoPlay()",this.speed);
	}
	
	_autoPlay=function(){
		if(___pvtSlideShow.hasNext()){
			___pvtSlideShow.next()
		}else{
			if(___pvtSlideShow.repeat)
			___pvtSlideShow.first();
		}
	}
	
	this.pause=function(){
		if(_iTransition)
		 window.clearInterval(_iTransition);
	}
	
}
