var $aktu_produkt_id = 0;
var $autowechsler_timeout = 0;
var $autowechsler_standzeit = 10000;
var $autowechsler_wechselzeit = 2000;
var $status_spontanwechsel = false;
//var $produktwechsler_bilder_wurzel_verzeichnis = "/";

$(document).ready(function()
{
	if (jQuery.isEmptyObject($produktwechsler_data))
	{
		//alert (".. leer");
		return;
	}
	
	
	$produkt_id_array = new Array();
	for (x in $produktwechsler_data)
	{
		$produkt_id_array[$produkt_id_array.length] = $produktwechsler_data[x].pws_id;
	}
	//alert("IDs: "+$produkt_id_array);
	
	
	$("div#produktwechsler ul").css("z-index", "10");
	$("div#produktwechsler ul li").each(function(index)
	{
		$(this).css("background-position", (20-3*index)+"px bottom");
		$(this).css("padding-left", (40-3*index)+"px");
	});
	
	$("div#produktwechsler ul li a").attr("class", "inaktiv");
	$temp_is_first = true;
	$("div#produktwechsler ul li a").each(function(index)
	{
		//alert(index + ': ' + $(this).text());
		if (in_array(index+1, $produkt_id_array))
		{
			//$(this).attr("class", "link_top2");
			$(this).attr("produkt_id", index+1);
			
			$(this).text($(this).text());
			$produktwechsler_data[index+1].link = $(this).attr("href");
			//alert(".. Link: "+$produktwechsler_data[index+1].link);
			$(this).removeAttr("href");
			$(this).css("cursor", "pointer");
			
			$produktwechsler_data[index+1].title = $(this).attr("title");
			
			if ($temp_is_first)
			{
				$temp_clone = $("img#produktwechsler_bild").clone();
				//$("img#produktwechsler_bild").remove();
				$("div#produktwechsler").append($temp_clone);
				
				$temp_clone.attr("id", "produktwechsler_bild_clone");
				$temp_clone.css("z-index", "2");
				$temp_clone.css("opacity", "0");
				//alert(".. wech");
				
				//$("img#produktwechsler_bild").append($temp_clone);
				
				
				produktwechsel(index+1, 1);
				$temp_is_first = false;
			}
			
			$(this).click(function()
			//$(this).mouseover(function()
			{
				produktwechsel(index+1, 1);
				window.clearTimeout($autowechsler_timeout);
				$autowechsler_timeout = window.setTimeout("autowechsel()", 3*$autowechsler_standzeit);
			});
		}
	});
	
	$autowechsler_timeout = window.setTimeout("autowechsel()", $autowechsler_standzeit);
	
});


function produktwechsel($nummer, $spontanwechsel)
{
	if ($nummer)
	{
		$status_spontanwechsel = false;
		
		if ($spontanwechsel)
		{
			$timer = 0;
		}
		else
		{
			$timer = $autowechsler_wechselzeit;
			
		}
		$aktu_produkt_id = $nummer;
		$("div#produktwechsler ul li a.aktiv").attr("class", "inaktiv");
		$("div#produktwechsler ul li a[produkt_id="+$nummer+"]").attr("class", "aktiv");
		
		$("img#produktwechsler_bild_clone").attr("src", $produktwechsler_bilder_wurzel_verzeichnis+"images/"+$produktwechsler_data[$nummer].pws_bild);
		$("img#produktwechsler_bild_clone").attr("title", $produktwechsler_data[$nummer].title);
		$("img#produktwechsler_bild_clone").attr("alt", $produktwechsler_data[$nummer].title);
		$("img#produktwechsler_bild_clone").css("cursor", "pointer");
		$("img#produktwechsler_bild_clone").click(function(){window.location.href = $produktwechsler_data[$nummer].link;});
		
		$('img#produktwechsler_bild_clone').animate({
			opacity: 1
		}, $timer,
		
		function()
		{
			if (!$status_spontanwechsel)
			{
				// Animation complete.
				$temp_clone_clone = $("img#produktwechsler_bild_clone").clone();
				$temp_clone_clone.attr("id", "produktwechsler_bild");
				$temp_clone_clone.css("z-index", "1");
				$("img#produktwechsler_bild").replaceWith($temp_clone_clone);
				$("img#produktwechsler_bild_clone").css("opacity", "0");
				//alert(".. getauscht");
				
				// nächstes Bild-Vorladen für schnellere Anzeige
				$aktu_index = 0;
				for (var $i = 0; $i < $produkt_id_array.length; $i++)
				{
					if ($produkt_id_array[$i] == $aktu_produkt_id) $aktu_index = $i;
				}
				$aktu_index++;
				if ($aktu_index >= $produkt_id_array.length) $aktu_index = 0;
				
				$("img#produktwechsler_bild_clone").attr("src", $produktwechsler_bilder_wurzel_verzeichnis+"images/"+$produktwechsler_data[$produkt_id_array[$aktu_index]].pws_bild);
			}
		});
		
		if ($spontanwechsel)
		{
			$status_spontanwechsel = true;
		}
	}
	
}

function autowechsel()
{
	//alert(".. aktu: "+$aktu_produkt_id);
	$aktu_index = 0;
	for (var $i = 0; $i < $produkt_id_array.length; $i++)
	{
		if ($produkt_id_array[$i] == $aktu_produkt_id) $aktu_index = $i;
	}
	$aktu_index++;
	if ($aktu_index >= $produkt_id_array.length) $aktu_index = 0;
	//alert(".. next: #"+$produkt_id_array[$aktu_index]);
	
	produktwechsel($produkt_id_array[$aktu_index]);
	
	$autowechsler_timeout = window.setTimeout("autowechsel()", $autowechsler_standzeit);
}

function in_array(item,arr)
{
	for(p=0;p<arr.length;p++) if (item == arr[p]) return true;
	return false;
}
