/*Beim laden der Seite wird ermittelt, ob Buttons eingeblendet werden müssen*/

var staticNr; //weil bei einem Funktionsaufruf in setTimout keine Parameter übergeben werden können, wird dies in einer statischen Variable gesetzt, wenn ein ...Cont aufgerufen wird.

function initSlider(s, nr) { /* s=Startparameter, wieviele Items nach links verschoben, nr=slidernummer */

	/* TODO: Position des aktuellen leftmargin-Wertes bei Seitenwechsel übergeben. */

	var selectNr = "#slider" + nr; //Anhand der Nummer wird Slider selektiert
	var amountProductElements = $(selectNr).find('.productmediumFrame').size(); //Anzahl der Elemente bestimmen
	
	if (amountProductElements < 6) { //when less than 6 elements no slider function necessary
		$(selectNr).find(".moveleft").css({display:"none"});
		$(selectNr).find(".moveright").css({display:"none"});
	}else{
		var sliderWidth = (amountProductElements * 143) + 11;	//Breite von slidercontent ermitteln (Anzahl Grafiken*143 + 11 px margin	
		$(selectNr).find('.slidercontent').css({width:sliderWidth});
		/* Initilisiere Slider-Navigation */	
		if (nr==1) {
			$(selectNr).find(".moveleft").mousedown(function(){scrollLeftFun(1);});
			$(selectNr).find(".moveleft").mouseup(function(){stop(1);});	
			$(selectNr).find(".moveleft").mousemove(function(){stop(1);});	
			$(selectNr).find(".moveright").mousedown(function(){scrollRight(1);});
			$(selectNr).find(".moveright").mouseup(function(){stop(1);});
			$(selectNr).find(".moveright").mousemove(function(){stop(1);});	
		}
		if (nr==2) {
			$(selectNr).find(".moveleft").mousedown(function(){scrollLeftFun(2);});
			$(selectNr).find(".moveleft").mouseup(function(){stop(2);});	
			$(selectNr).find(".moveleft").mousemove(function(){stop(2);});	
			$(selectNr).find(".moveright").mousedown(function(){scrollRight(2);});
			$(selectNr).find(".moveright").mouseup(function(){stop(2);});
			$(selectNr).find(".moveright").mousemove(function(){stop(2);});
		}	
		moveLeftStart(s, nr);
	}
}



/* --- SLIDER --- */

var aktiv1 = false;
var aktiv2 = false;
var aktiv3 = false;
var aktiv4 = false;



	function scrollLeftFun(nr) {
		moveLeft(nr);
		if(nr==1) {
			aktiv1 = window.setTimeout(scrollLeftCont, 400);
			staticNr=1;
		}else{
			aktiv3 = window.setTimeout(scrollLeftCont, 400);	
			staticNr=2;
		}
	}
	function scrollLeftCont() {
		moveLeft(staticNr);
		if(staticNr==1) {
			aktiv2 = window.setTimeout(scrollLeftCont, 300);
		}else{
			aktiv4 = window.setTimeout(scrollLeftCont, 300);
		}
	}
	function moveLeft(nr) {
		var l = getMarginLeft(nr) + 143;
		updateButtons(l, nr);
		return false;
	}
	
	function moveLeftStart(amount, nr) {
		var l = -amount * 143;
		updateButtons(l, nr);
		return false;
	}

	function scrollRight(nr) {
		moveRight(nr);
		if(nr==1) {		
			aktiv1 = window.setTimeout(scrollRightCont, 400);
			staticNr=1;
		}else{
			aktiv3 = window.setTimeout(scrollRightCont, 400);
			staticNr=2;
		}
	}	
	function scrollRightCont() {
		moveRight(staticNr);
		if(staticNr==1) {				
			aktiv2 = window.setTimeout(scrollRightCont, 300);
		}else{
			aktiv4 = window.setTimeout(scrollRightCont, 300);
		}
	}		
	function moveRight(nr) {
		var l = getMarginLeft(nr) - 143;
		updateButtons(l, nr);
		return false;
	}
	
	function stop() {	
		if (aktiv1 != false) {
			window.clearTimeout(aktiv1);
		}
		if (aktiv2 != false) {
			window.clearTimeout(aktiv2);
		}
		if (aktiv3 != false) {
			window.clearTimeout(aktiv3);
		}
		if (aktiv4 != false) {
			window.clearTimeout(aktiv4);
		}
		aktiv1 = false;
		aktiv2 = false;
		aktiv3 = false;
		aktiv4 = false;
		return false;
	}
	
	function updateButtons(l, nr) { //l = margin left
		var selectNr = "#slider" + nr;
		var maxw = $(selectNr).find('.slidercontent').css("width");

		if ($(selectNr).find('.slidercontent').length > 0) {
			var length = maxw.length;	
		
			if (length == 3) { maxw = maxw.charAt(0); }
			if (length == 4) { maxw = (maxw.charAt(0) + maxw.charAt(1)); }
			if (length == 5) { maxw = (maxw.charAt(0) + maxw.charAt(1) + maxw.charAt(2)); }
			if (length == 6) { maxw = (maxw.charAt(0) + maxw.charAt(1) + maxw.charAt(2) + maxw.charAt(3)); }
		}
		
		maxw = -maxw + 854;
		if (l > 0) l = 0;
		if (l < maxw) l = maxw;
		
		/* Wert wird auf ein vielfaches von 20 gerundet, so können bei schnellem Klicken keine Werte dazwischen entstehen */
		l = l/143;
		l = Math.round(l);
		l = l*143;
		
		$(selectNr).find('.slidercontent').stop().animate({marginLeft:l}, 500, "swing");

		/*TODO: Buttons ein- ausblenden*/
		
		if (l==0) {
			$(selectNr).find('.moveleft').css({display:"none"});
		}else{
			$(selectNr).find('.moveleft').css({display:"block"});		
		}

		if (l<=(maxw+20)) {
			$(selectNr).find('.moveright').css({display:"none"});
		}else{
			$(selectNr).find('.moveright').css({display:"block"});		
		}		
	}

	function getMarginLeft(nr) {
		var selectNr = "#slider" + nr;	
	
		if ($(selectNr).find('.slidercontent').length > 0) {
			var l = $(selectNr).find('.slidercontent').css("margin-left");
			var length = l.length;
			
		if (length == 3) { l = l.charAt(0); }
		if (length == 4) { l = (l.charAt(0) + l.charAt(1)); }
		if (length == 5) { l = (l.charAt(0) + l.charAt(1) + l.charAt(2)); }
		if (length == 6) { l = (l.charAt(0) + l.charAt(1) + l.charAt(2) + l.charAt(3)); }
		
		return parseInt(l);
		}else{
			return 0;
		}
	}
	