
var g_aOSlideshows = new Array();

function StartSlideExt(iSlideShow, iSlide) {
	if(iSlide < 0)
		g_aOSlideshows[iSlideShow].SlideToNext();
	else
		g_aOSlideshows[iSlideShow].StartSlide(iSlide - g_aOSlideshows[iSlideShow].m_iSlide);
}

function SlideShow(sId, bAutoSlide, iAutoSlideDelay) {
	this.m_sId                  = sId;
	this.m_oPlaceholder         = document.getElementById(sId);
	this.m_aOSlides             = null;
	this.m_iSlide               = 0;
	this.m_iSlidePrevious       = -1;
	this.m_oSlideTimer          = null;
	this.m_oAutoSlideTimer      = null;
	this.m_iSildeTimerDelay     = 20;
	this.m_iAutoSildeTimerDelay = (iAutoSlideDelay > 0) ? iAutoSlideDelay : 10000;
	this.m_bSliding             = false;
	this.m_bAutoSlide           = bAutoSlide;
	this.m_iAlpha               = 0;
	this.m_iFadeSpeed           = 10;
	this.m_iSlideWidth          = 690;
	this.m_iSlideX              = 0;
	this.m_iSlideSpeed          = 0;
	this.m_iSlideSpeedMax       = 50;
	this.m_iSlideAcceleration   = 5;
	this.m_iSlideType           = 2;
	this.m_bIsIE                = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
	this.m_iGlobalIndex         = g_aOSlideshows.length;
	g_aOSlideshows.push(this);
	
	this.Start();

}

SlideShow.prototype.Start = function() {
	if (this.m_aOSlides == null || this.m_aOSlides.length == 0) {
		this.m_aOSlides = this.m_oPlaceholder.getElementsByTagName("div");
		var oPager = document.createElement("span");
		oPager.id = this.m_sId + "Pager";
		oPager.className = "pager";
		for (var i = 0; i < this.m_aOSlides.length; i++) {
			var oBall = document.createElement("img");
			oBall.id = this.m_sId + "Ball" + i;
			oBall.src = "http://www.vuokranantajat.fi/images/2011/sliderBall" + ((this.m_iSlide == i) ? "A" : "") + ".png";
			oBall.onclick = new Function("StartSlideExt(" + this.m_iGlobalIndex + ", " + i + ");");
			oPager.appendChild(oBall);
			var aHrefs = this.m_aOSlides[i].getElementsByTagName("a");
			for (var j = 0; j < aHrefs.length; j++) {
				if (aHrefs[j].className == "nextSlide")
					aHrefs[j].onclick = new Function("StartSlideExt(" + this.m_iGlobalIndex + ", " + (i + 1) + ");");
			}
		}
		this.m_oPlaceholder.appendChild(oPager);
		// Create nav links
		
		

	}

	var oSlideShow = this;
	var SlideToNextInt = function() {
		oSlideShow.SlideToNext();
	}
	if (this.m_bAutoSlide)
		this.m_oAutoSlideTimer = setTimeout(SlideToNextInt, this.m_iAutoSildeTimerDelay);
}

SlideShow.prototype.UpdatePager = function(iSlide, iPreviousSlide) {
	var oPager        = document.getElementById(this.m_sId + "Pager");
	var oBall         = document.getElementById(this.m_sId + "Ball" + iSlide);
	var oBallPrevious = document.getElementById(this.m_sId + "Ball" + iPreviousSlide);
	if (oBall)
		oBall.src = "http://www.vuokranantajat.fi/images/2011/sliderBallA.png";
	if (oBallPrevious)
		oBallPrevious.src = "http://www.vuokranantajat.fi/images/2011/sliderBall.png";
}

SlideShow.prototype.SlideToNext = function() {
	this.StartSlide(1);
}

SlideShow.prototype.StartSlide = function(i) {
	if(i != 0) {
		if (!this.m_bSliding) {
			if (this.m_oSlideTimer != null) {
				clearTimeout(this.m_oSlideTimer);
				this.m_oSlideTimer = null;
			}
			this.m_iSlidePrevious = this.m_iSlide;
			this.m_iSlide         = this.m_iSlide + i;
			if (this.m_iSlide < 0)
				this.m_iSlide = this.m_iSlide - 1;
			else if (this.m_iSlide >= this.m_aOSlides.length)
				this.m_iSlide = 0;
			this.UpdatePager(this.m_iSlide, this.m_iSlidePrevious);

			if (this.m_iSlideType == 1) {
				this.m_aOSlides[this.m_iSlide].style.zIndex  = parseInt(this.m_aOSlides[this.m_iSlidePrevious].style.zIndex) + 1;
				this.m_aOSlides[this.m_iSlide].style.left    = m_iSlideWidth + "px";
				this.m_aOSlides[this.m_iSlide].style.display = "block";
				this.m_iSlideX                               = this.m_iSlideWidth;
				this.m_iSlideSpeed                           = 40;
			} else {
				if (this.m_bIsIE)
					this.m_aOSlides[this.m_iSlide].style.filter       = "alpha(opacity: 0)";
				this.m_aOSlides[this.m_iSlide].style.alpha            = "0";
				this.m_aOSlides[this.m_iSlide].style.MozOpacity       = "0";
				this.m_aOSlides[this.m_iSlide].style.position         = "relative";
				this.m_aOSlides[this.m_iSlide].style.display          = "block";
				this.m_aOSlides[this.m_iSlidePrevious].style.position = "absolute";
				this.m_iAlpha = 0;
			}

			var oSlideShow = this;
			var SlideInt = function() {
				oSlideShow.Slide();
			}
			this.m_bSliding = true;
			this.m_oSlideTimer = setTimeout(SlideInt, this.m_iSildeTimerDelay);

		}
		if (this.m_oAutoSlideTimer) {
			clearTimeout(this.m_oAutoSlideTimer);
			this.m_oAutoSlideTimer = null;
		}
		if (this.m_bAutoSlide) {
			var oSlideShow = this;
			var SlideToNextInt = function() {
				oSlideShow.SlideToNext();
			}
			this.m_oAutoSlideTimer = setTimeout(SlideToNextInt, this.m_iAutoSildeTimerDelay);
		}
	}
}

SlideShow.prototype.Slide = function() {
	if (this.m_oSlideTimer != null) {
		clearTimeout(this.m_oSlideTimer);
		this.m_oSlideTimer = null;
	}
	var oSlideShow = this;
	var SlideInt   = function() {
		oSlideShow.Slide();
	}
	if (this.m_iSlideType == 1) {
		if (this.m_iSlideX - this.m_iSlideSpeed > 0) {
			this.m_iSlideX     -= this.m_iSlideSpeed;
			this.m_oSlideTimer  = setTimeout(SlideInt, this.m_iSildeTimerDelay);
		} else {
			this.m_aOSlides[this.m_iSlidePrevious].style.display = "none";
			this.m_iSlideX                                       = 0;
			this.m_iSlideSpeed                                   = 0;
			this.m_bSliding                                      = false;
		}
		this.m_aOSlides[this.m_iSlide].style.left = this.m_iSlideX + "px";
	} else {
		if (this.m_iAlpha + this.m_iFadeSpeed < 100) {
			this.m_iAlpha      += this.m_iFadeSpeed;
			this.m_oSlideTimer  = setTimeout(SlideInt, this.m_iSildeTimerDelay);
		} else {
			this.m_aOSlides[this.m_iSlidePrevious].style.display = "none";
			this.m_iAlpha                                        = 100;
			this.m_bSliding                                      = false;
		}
		if (this.m_bIsIE)
			this.m_aOSlides[this.m_iSlidePrevious].style.filter = "alpha(opacity: " + (100 - this.m_iAlpha) + ")";
		this.m_aOSlides[this.m_iSlidePrevious].style.alpha      = ((100 - this.m_iAlpha) / 100);
		this.m_aOSlides[this.m_iSlidePrevious].style.MozOpacity = ((100 - this.m_iAlpha) / 100);
		if (this.m_bIsIE)
			this.m_aOSlides[this.m_iSlide].style.filter = "alpha(opacity: " + this.m_iAlpha + ")";
		this.m_aOSlides[this.m_iSlide].style.alpha      = (this.m_iAlpha / 100);
		this.m_aOSlides[this.m_iSlide].style.MozOpacity = (this.m_iAlpha / 100);
	}
}
