// JavaScript Document

var element 	= document.getElementById("DIVSliderContent");
var childs 		= getElementChildren(element);
var IntStep		= Math.floor(createInt(getStyle(childs[0],'width'))+createInt(getStyle(childs[0],'margin-Right')));
var counter		= 0;
var step 		= IntStep;

var timer1;
var timer2;
var timer3;

ForwardSpeed = 51-ForwardSpeed;
LoopBackSpeed = 51-LoopBackSpeed;

if(childs.length == 2) LoopBackSpeed = ForwardSpeed;

if(ForwardAnimate != true) ForwardSpeed = 1;
if(LoopBackAnimate != true) LoopBackSpeed = 1/childs.length;

element.style.width = (childs.length*IntStep)+IntStep+'px';

if(childs.length > 1) beginSlide(pause);

function beginSlide(offSet){
	counter = counter + 1;
	createInfo(counter);
	timer1 = window.setInterval("nextSlide();", offSet);
}

function PBSgoTo(goToCounter){
	window.clearInterval(timer1);
	window.clearInterval(timer2);
	window.clearInterval(timer3);
	counter = goToCounter-1;
	step = IntStep*counter;
	element.style.marginLeft = "-"+step+"px";
	step = step+IntStep;
	beginSlide(PauseAfterClick);
	MouseOutAutoStart = 0;
	MouseOverFreeze = 0;
	var tmpTimer = window.setInterval(function() {
		MouseOutAutoStart = 1;
		MouseOverFreeze = 1;
		window.clearInterval(tmpTimer);
	},PauseAfterClick)
}

function createInfo(c){
	var i;
	var strHTML = '';
	for(i=1;i<=childs.length;i++){
		if(i == c){
		strHTML = strHTML + '<img src="/img/icon/icon_bullet_gruen.gif" />';
		} else {
		strHTML = strHTML + '<img src="/img/icon/icon_bullet_grau.gif" onclick="PBSgoTo('+i+');" style="cursor:pointer;" />';
		}
	}
	document.getElementById("DIVSliderInfo").innerHTML = strHTML;
}

function stopSlide(){
	window.clearInterval(timer1);
	window.clearInterval(timer2);
	window.clearInterval(timer3);
	counter = counter - 1;
}

function nextSlide(){
	//alert('counter: '+counter+'\n\nstep: '+step);
	if(counter < childs.length){
		createInfo(-1);
		window.clearInterval(timer1);
		timer2 = window.setInterval("slide();", 30);
		window.clearInterval(timer3);
	} else {
		window.clearInterval(timer1);
		window.clearInterval(timer2);
		timer3 = window.setInterval("slideBack();", 30);
		createInfo(-1);
	}
}

function slide(){
	var MarginPos = createInt(element.style.marginLeft) - (IntStep/ForwardSpeed);
	
	if(isNaN(MarginPos)) MarginPos = 0;
	element.style.marginLeft = MarginPos + "px";

	if(MarginPos <= 0-step){
		window.clearInterval(timer2);
		element.style.marginLeft = "-"+step+"px";
		step = step + IntStep;
		beginSlide(pause);
		createInfo(counter);
	}
}

function slideBack(){
	var MarginPos = createInt(element.style.marginLeft) + (IntStep/LoopBackSpeed);
	element.style.marginLeft = MarginPos + "px";

	if(MarginPos >= 0){
		window.clearInterval(timer3);
		element.style.marginLeft = "0px";
		step = IntStep;
		counter = 0;
		beginSlide(pause);
	}
}

function getElementChildren(el){	
	var children = [];
	var child = el.firstChild;
	while (child)
	{
		if (child.nodeType == 1){
			children.push(child);
		}
		child = child.nextSibling;
	}
	return children;
}

function createInt(mix){
	mix = mix.replace(/px/g,'');
	mix = mix.replace(/%/g,'');
	return Math.floor(mix);
}

function getStyle(el,styleProp)
{
	if (el.currentStyle)	//IE
		var y = el.currentStyle[styleProp.replace(/-/g,'')];
	else if (window.getComputedStyle)	//Mozilla
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
	return y;
}


