
Slider = function(sliderID, slides) {
	this._el_Slider = document.getElementById(sliderID);
	this._el_NextSlide = null;
	this._el_PreviousSlide = null;
	this._el_StopSlide = null;
	
	this._Data = slides;
	this._TimerID = null;
	this._SlideInterval = 12000;
	this._FadingSpeed = 3;
	this._CurrentSlideInd = 0;
	this._set_IsSlideShow(false);
	this._FadingSlideInd = -1;
	
	this.initialize();
}

Slider.prototype = {
	
	initialize : function(){
		var slider = this._el_Slider;
		slider.style.backgroundImage = "";
		slider.style.backgroundPosition = "left top";
		slider.style.backgroundRepeat = "no-repeat";
		var t = this;
		this._CurrentSlide = document.createElement("img");
		this._CurrentSlide.id = slider.id + "$imgSlide";
		this._CurrentSlide.src = this._Data[this._CurrentSlideInd].ImageUrl;
		this._CurrentSlide.style.cursor = "pointer";
		this._CurrentSlide.onclick = function(eventArgs) { t._Slider_OnClick(eventArgs); };
		slider.appendChild(this._CurrentSlide);
	},
	
/*Methods Section*/
	StartSlideShow : function(){
		this._set_IsSlideShow(true);

		if (this._TimerID != null)
			window.clearTimeout(this._TimerID);
		var t = this;
		this._TimerID = window.setTimeout(function(){ t.FadingShowSlide(t._CurrentSlideInd + 1, 1); }, this._SlideInterval);
	},
	
	StopSlideShow : function(){
		this._set_IsSlideShow(false);

		this._el_Slider.style.backgroundImage = "";
		this._SetOpacity(99);

		if (this._TimerID != null)
			window.clearTimeout(this._TimerID);
	},
	
	ShowSlide : function(slideIndex){
		if (slideIndex < 0)
			this._CurrentSlideInd = this._Data.length -1;
		else if (slideIndex >= this._Data.length)
			this._CurrentSlideInd = 0;
		else
			this._CurrentSlideInd = slideIndex;
		
		this._el_Slider.style.backgroundImage = "";
		this._SetOpacity(99);
		this._CurrentSlide.src = this._Data[this._CurrentSlideInd].ImageUrl;
		
		if (this._IsSlideShow)
			this.StartSlideShow();
	},
	
	FadingShowSlide : function(slideIndex, opacityValue){
		if (slideIndex < 0)
			slideIndex = this._Data.length -1;
		else if (slideIndex >= this._Data.length)
			slideIndex = 0;
		else
			slideIndex = slideIndex;

		if (this._FadingSlideInd == -1){
			this._el_Slider.style.backgroundImage = "url(" + this._Data[this._CurrentSlideInd].ImageUrl + ")";
			this._CurrentSlide.src = this._Data[slideIndex].ImageUrl;
			this._FadingSlideInd = slideIndex;
		}
		
		this._SetOpacity(opacityValue);
		
		++opacityValue;
		var t = this;

		if (opacityValue < 100)
			window.setTimeout(function(){ t.FadingShowSlide(slideIndex, opacityValue); }, this._FadingSpeed);
		else {
			this._FadingSlideInd = -1;
			this._el_Slider.style.backgroundImage = "";
			this._CurrentSlideInd = slideIndex;
			if (this._IsSlideShow)
				this.StartSlideShow();
		}
	},
	
	_SetOpacity : function(opacityValue){
		if(document.all) {
			this._CurrentSlide.style.filter = 'alpha(opacity=' + opacityValue + ')';
		} else {
			this._CurrentSlide.style.opacity = Math.max(0.01,opacityValue/100);
		}
	},
	
	_Slider_OnClick : function(eventArgs){
		var index = this._FadingSlideInd > -1 ? this._FadingSlideInd : this._CurrentSlideInd;
		window.location.href = this._Data[index].NavigateUrl;
	},
	
	NextSlide : function(){
		this.ShowSlide(this._CurrentSlideInd + 1);
	},
	
	PreviousSlide : function(){
		this.ShowSlide(this._CurrentSlideInd - 1);
	},
	
	PauseSlideShow : function(){
		if (this._IsSlideShow) {
			this.StopSlideShow();
		}
		else {
			this.StartSlideShow();
		}
		this._StopSlideControl_OnMouseEvent(true);
	},
	
	_StopSlideControl_OnMouseEvent : function(isOverEvent) {
		if (this._el_StopSlide != null)
			this._el_StopSlide.src = (this._IsSlideShow ? this._PauseImageUrl : this._PlayImageUrl) + "_" + (isOverEvent ? "on" : "off") + ".png";
	},
/*End of Methods Section*/
	
/*Properties Section*/
	_set_IsSlideShow : function(value){
		this._IsSlideShow = value;
		this._StopSlideControl_OnMouseEvent(false);
	},

	set_SlideInterval : function(value){
		this._SlideInterval = value;
	},

	set_FadingSpeed : function(value){
		this._FadingSpeed = value;
	},
	
	set_PreviousSlideControl : function(value){
		this._el_PreviousSlide = document.getElementById(value);
		var t = this;
		if (this._el_PreviousSlide != null)
			this._el_PreviousSlide.onclick = function () { t.PreviousSlide(); return false; };
	},
	
	set_NextSlideControl : function(value){
		this._el_NextSlide = document.getElementById(value);
		var t = this;
		if (this._el_NextSlide != null)
			this._el_NextSlide.onclick = function () { t.NextSlide(); return false; };
	},
	
	set_StopSlideControl : function(value, playImageUrl, pauseImageUrl){
		this._el_StopSlide = document.getElementById(value);
		this._PauseImageUrl = pauseImageUrl;
		this._PlayImageUrl = playImageUrl;
		var t = this;
		if (this._el_StopSlide != null){
			this._el_StopSlide.onclick = function () { t.PauseSlideShow(); return false; };
			this._el_StopSlide.onmouseover = function () { t._StopSlideControl_OnMouseEvent(true); };
			this._el_StopSlide.onmouseout = function () { t._StopSlideControl_OnMouseEvent(false); };
			this._StopSlideControl_OnMouseEvent(false);
		}
	}
/*End of Properties Section*/

}