/*
Name:       Slider
Version:    0.0.1 (16. Juni 2011)
Author:     Marc Köster
Support:    koester@stadtwerk.org
*/

function Slider()
{
	var slideWidth = 229;
	var margin = -229;
	var timerlen = 5;
	var slideAniLen = 500;
	
	var slider;
	var count;
	var direction;
	var timerID;
	var startTime;
	var moving = false;
	var pause = false;

	/* Init the Slider */
	this.init = function()
	{
		slider = document.getElementById("slider");
		count = slider.getElementsByTagName("div").length-2; // counts slide Elements (-2 because duplicates at begin and end)
		if(count > 1)
			setInterval('sliderclass.slideDir(false, "left");', 5000);  //slide every 5 sek
		// this.setEvents();
	};
	
	/* Set Click Events to slide the Slider */
	this.setEvents = function()
	{
		console.log("event");
		document.getElementById("sliderleft").onclick = sliderclass.slideLeft();
		document.getElementById("sliderright").onclick = sliderclass.slideRight();
	}
	
	/* user Click to slide left, after a click the slideshow pause for 10sek */
	this.clickLeft = function()
	{
		pause = true;
		this.slideDir(true, "left");
		setTimeout('sliderclass.endPause()', 10000);
	}
	
	/* user Click to slide right, after a click the slideshow pause for 10sek */
	this.clickRight = function()
	{
		pause = true;
		this.slideDir(true, "right");
		setTimeout('sliderclass.endPause()', 10000);
	}
	
	/* end pause */
	this.endPause = function()
	{
		pause = false;
	}
	
	/* slide to on direction */
	this.slideDir = function(force, dir)
	{
		 // slide when not moving already and not in pause except it get forced by user click
		if(!moving && (!pause || force)){
			direction=dir;
			moving=true;
			startTime = new Date().getTime();
			timerID = setInterval('sliderclass.slideTick();',timerlen);
		}
	
	};
	
	/* move the Slider each interval a tick */
	this.slideTick = function()
	{
		var elapsed = (new Date()).getTime() - startTime;
		if (elapsed > slideAniLen)
			this.endSlide()
		else 
		{
			if(direction=="left"){
				var d =Math.round(margin - (elapsed / slideAniLen * slideWidth));
			}
			else{
				var d =Math.round(margin + (elapsed / slideAniLen * slideWidth));
			}
			
			slider.style.marginLeft = d + "px";
		}
		return;
	};
	
	/* clear slide interval an set slider to final position */
	this.endSlide = function()
	{
		clearInterval(timerID);
		
		if(direction == "left")
		{
			margin -= slideWidth;
			// if slider at last position (duplicate from position 2), set to position 2
			if (margin <= -((count+1)*slideWidth))
				margin = -slideWidth;
			slider.style.marginLeft = margin + "px";
		}
		else
		{
			margin += slideWidth;
			// if slider at first position (duplicate from last position -1), set to last position -1
			if (margin >= 0)
				margin = -count * slideWidth;
			slider.style.marginLeft = margin + "px";
		}
		moving = false;
	};
}

var sliderclass;
domReady(function()
{
	sliderclass = new Slider();
	sliderclass.init();
});
