/* JavaScript fuer das Produkt-Karussell auf der Startseite */

var current_array = new Array();
// Richtung
var direction = 1;
// Auto-Rotation an/aus
var auto = 0;
// Geschwindigkeit der Rotation
var speed_auto;
// Geschwindigkeit des Positionswechsel von Bildern
var speed_images;
// Cleartime der Rekursion
var auto_clear;
// Left-Position des Karussells
var cp;
var new_subtext;

function carousel(direction, stop_auto) {
	// Anhalten der Rotation
	if(stop_auto) {
		auto = 0;
	}
	
	// Funktion abbrechen, falls schon eine Animation laeuft -> Vermeidung von Mehrfachklicks
	if($('#image_box_inner:animated').length != 0) {
		// mach nix
	} else {
		
		cp = $('#image_box_inner').css('left');
		cp = cp.replace(/px/, '');
		cp = parseInt(cp);
		
		if (direction == 1) {
			
			speed_images = ((widthInnerPicBox-496)+cp)*7;
	
			if (cp > 496-widthInnerPicBox) {
				cp = (496-widthInnerPicBox);
			}
			
		} else {
			
			speed_images = (0-cp)*7;
	
			if (cp <= 0) {
				cp = 0;
			}
		}
		
		$('#image_box_inner').animate({
			left: cp+"px"
		}, speed_images, "linear");
		
		
	}
	
	if (auto) {
		//auto_clear = setTimeout("carousel("+direction+")", speed_auto);
	}
}

function carousel_init() {
	

	// Start der Auto-Rotation
	//auto_clear = setTimeout("start_carousel()", 4000);
}

function start_carousel() {
	clearTimeout(auto_clear);
	// Standardwerte setzen
	auto = 1;
	speed_auto = 5;

	auto_clear = 0;
	// Anzeigen der Produkte
	carousel(direction);
}
setTimeout("carousel_init()", 20);


// linker Pfeil
$("#left_arrow").hover(
	function () {
		direction = -1;
		auto_clear = setTimeout("start_carousel()", 5);
	}, 
	function () {
		$('#image_box_inner').stop();
		clearTimeout(auto_clear);
	}
);
$("#left_arrow").click(
	function () {
		carousel(-1, 1, speed_images);
	}
);
// rechter Pfeil
$("#right_arrow").hover(
	function () {
		direction = 1;
		auto_clear = setTimeout("start_carousel()", 5);
	}, 
	function () {
		$('#image_box_inner').stop();
		clearTimeout(auto_clear);
	}
);
$("#right_arrow").click(
	function () {
		carousel(1, 1, speed_images);
	}
);
// mittlerer Container, der die 3 sichtbaren Bilder enthaelt
$("#image_box").hover(
	function () {
		clearTimeout(auto_clear);
		auto = 0;
	}, 
	function () {
		//auto_clear = setTimeout("start_carousel()", 2000);
	}
);

// fuer zentrales Bild
function remove_border() {
	$('div#image_box a').each(function() {
		$(this).children('img').css('border', 'none');
	});
}

//Oberer Abstand nach Klick
function set_pic_valign() {
	$('div#image_box a').each(function() { 
		$(this).children('img').css({top: '2px'}); 
	});
}

			
$('div#image_box a').each(function() { 
	
	$(this).click(function () { 
			
		remove_border();
		set_pic_valign();
		
		
		var new_src = $(this).children('img').attr('src');
    		$('#bigimage img').attr( {src: new_src} );
 		$(this).children('img').css({border: '2px solid #FF8913', top: '0px'});
 		
 		for (i=0;i<lengthPics;i++) {
			if (new_src == pics[i]) {
				position = i;
				$('.picNumber').text(position+1);
			}
		}
    });    
});

$('a.left').click(function () { 
	
	remove_border();
	set_pic_valign();
	
	if (position == 0) {
		position = lengthPics-1;
	} else {
		position--;
	}
	new_src = pics[position];
	new_subtext = subtext[position];
    	$('#bigimage img').attr( {src: new_src} );
    	$('.picNumber').text(position+1);
    	$('.subtext').text(new_subtext);
    	
    	for (i=0;i<lengthPics;i++) {
			if (new_src == pics[i]) {
				var thumbPos = i+1;
				$('#reference'+thumbPos+' img').css({border: '2px solid #FF8913', top: '0px'});

			}
	}
    	
    	return false;
});

$('a.right').click(function () { 

	remove_border();
	set_pic_valign();
	
	if (position == lengthPics-1) {
		position = 0;
	} else {
		position++;
	}
	new_src = pics[position];
	new_subtext = subtext[position];
    	$('#bigimage img').attr( {src: new_src} );
    	$('.picNumber').text(position+1);
    	$('.subtext').text(new_subtext);
    	
		for (i=0;i<lengthPics;i++) {
		if (new_src == pics[i]) {
			var thumbPos = i+1;
			$('#reference'+thumbPos+' img').css({border: '2px solid #FF8913', top: '0px'});
		}
	}
    	return false;
});

