sizeChecker = new Image();


function slideshow(sbox, sarea, sclass, shndlb, shndlf)
	{

	var _self = this;

	this.slideBox = document.getElementById(sbox);
	this.slideArea = document.getElementById(sarea);
	this.slidesClassname = sclass;
	this.slideHandleBack = document.getElementById(shndlb);
	this.slideHandleFwd = document.getElementById(shndlf);
	
	this.dimensionUnit = "px";
	this.slideCount = 0;
	
	this.slideshowWitdh = 0;
	this.slideAreaWidth = 0;

	this.maxLeft = 0;
	this.minLeft = 0;


	this.slideStep = 5;
	this.slideTimeoutFwd = "";
	this.slideTimeoutBack = "";
	this.slideTimeoutIntervallMax = 140;
	this.slideTimeoutIntervallMin = 20;
	this.slideTimeoutIntervall = 20;
	this.slideTimeoutIntervallStep = 20;

	this.getLeftPos = function(e)
		{
		return parseInt(e.offsetLeft, 0);
		}

	this.setLeftPos = function(e, lpos)
		{
		e.style.left = lpos + this.dimensionUnit;
		}


	this.setHandels = function ()
		{
		if (this.slideAreaWidth > this.slideshowWitdh)
			{
			this.slideHandleBack.style.display = "block";
			this.slideHandleFwd.style.display = "block";
			this.startHandels();
			}
		}

	this.startHandels = function ()
		{
		this.slideHandleBack.onmouseover = function () {_self.moveBack()}
		this.slideHandleBack.onmouseout = function () {_self.stopMove()}
		this.slideHandleBack.onclick = function () {_self.pauseMove()}
		this.slideHandleFwd.onmouseover = function () {_self.moveFwd()}
		this.slideHandleFwd.onmouseout = function () {_self.stopMove()}
		this.slideHandleFwd.onclick = function () {_self.pauseMove('fwd')}
		}


	this.moveFwd = function()
		{
		var step = - this.slideStep;
		var currentPos = this.getLeftPos(this.slideArea);
		
		//this.slideTimeoutIntervall = Math.max(this.slideTimeoutIntervall - this.slideTimeoutIntervallStep, this.slideTimeoutIntervallMin)
		
		if(currentPos > this.maxLeft)
			{
			this.setLeftPos(this.slideArea, currentPos + step)
			}
		else
			{
			this.stopMove(); return;
			}
		this.slideTimeoutFwd = window.setTimeout(function () { _self.moveFwd() }, this.slideTimeoutIntervall)
		}


	this.moveBack = function()
		{
		var step = this.slideStep;
		var currentPos = this.getLeftPos(this.slideArea);
		
		//this.slideTimeoutIntervall = Math.max(this.slideTimeoutIntervall - this.slideTimeoutIntervallStep, this.slideTimeoutIntervallMin)

		if(currentPos < this.minLeft)
			{
			this.setLeftPos(this.slideArea, currentPos + step)
			}
		else
			{
			this.setLeftPos(this.slideArea, this.minLeft)
			this.stopMove(); return;
			}
		this.slideTimeoutBack = window.setTimeout(function () { _self.moveBack() }, this.slideTimeoutIntervall)
		}



	this.stopMove = function ()
		{
		window.clearTimeout(this.slideTimeoutFwd); this.slideTimeoutFwd = "";
		window.clearTimeout(this.slideTimeoutBack); this.slideTimeoutBack = "";
		//this.slideTimeoutIntervall = this.slideTimeoutIntervallMax;
		}

	this.pauseMove = function(p)
		{
		if (this.slideTimeoutFwd || this.slideTimeoutFwd != "" || this.slideTimeoutBack || this.slideTimeoutBack != "")
			{
			this.stopMove(); return
			}
		
		if(p=='fwd') 
			{
			this.moveFwd()
			}
		else
			{
			this.moveBack()
			}
		
		}


	this.iniSlideShow = function()
		{
		this.slideshowWitdh = parseInt(this.slideBox.offsetWidth, 0);
		for (i=0;  i<this.slideArea.childNodes.length; i++)
			{
			var childElement = this.slideArea.childNodes[i]
			if(childElement.className == this.slidesClassname)
				{ this.slideAreaWidth+= parseInt(childElement.offsetWidth, 0); this.slideCount++; }
			}
		this.slideBox.style.width = this.slideshowWitdh + this.dimensionUnit;
		this.slideArea.style.width = this.slideAreaWidth + this.dimensionUnit;
		
		this.maxLeft = Math.min(0 - (this.slideAreaWidth - this.slideshowWitdh) , 0)
		this.setHandels();
		}

	this.iniSlideShow();

	makeFirstSlide(this.slideArea);
	}


function makeFirstSlide(e)
	{
	for(i=0; i<e.childNodes.length; i++)
		{
		if(e.childNodes[i].className == "slideshowSlide")
			{
			e.childNodes[i].onclick(); return
			}
		}
	}


function showSlide(e, previewSrc)
	{
	document.getElementById("slideshowStage").style.backgroundImage = "url(/cms/it/images/loading-small.gif)";
	SlideElement = e; 
	
	sizeChecker.src = previewSrc;
	
	sizeChecker.onload = onImgLoaded;
	
	
	function onImgLoaded()
		{
		//alert('done')
		
		document.getElementById("slideshowStage").style.backgroundImage = "url(" + sizeChecker.src + ")";
		
		jsStageWidth = parseInt(document.getElementById("slideshowStage").offsetWidth, 0);
		jsStageHeight = parseInt(document.getElementById("slideshowStage").offsetHeight, 0);
		
		jsStageBox = document.getElementById("slideshowStageTextBox");
		
		for (i=0;  i<SlideElement.childNodes.length; i++)
			{
			if(SlideElement.childNodes[i].className == "slideshowSlideContent")			
				{
				
				imgWidth = parseInt(sizeChecker.width, 0);
				imgHeight = parseInt(sizeChecker.height, 0); 
				
				imgOffsetWidth = parseInt((jsStageWidth - imgWidth)/2);
				imgOffsetHeight = parseInt((jsStageHeight - imgHeight)/2);
	
				jsStageBox.style.left = imgOffsetWidth + "px";
				jsStageBox.style.width = imgWidth + "px";
				
				jsStageBox.innerHTML = SlideElement.childNodes[i].innerHTML;
	
	
				for(j=0; j<jsStageBox.childNodes.length; j++)
					{
					if(jsStageBox.childNodes[j].className == "slideshowSlideContentText")
						{
						textHeight = parseInt(jsStageBox.childNodes[j].offsetHeight, 0);
						}
					}
	
				jsStageBoxHight = textHeight;
	
				for(j=0; j<jsStageBox.childNodes.length; j++)
					{
					if(jsStageBox.childNodes[j].className == "slideshowSlideContentBG")
						{
						jsStageBox.childNodes[j].style.width = imgWidth + "px";
						jsStageBox.childNodes[j].style.height = textHeight + "px";
						}
					}
	
				jsStageBox.style.top =  1 + imgHeight + imgOffsetHeight - jsStageBoxHight + "px";
				
				}
			}
		}
	}


