debug = false;

/************** Übersetzungen ***********/
var jetzt_bestellen = new Array();
jetzt_bestellen[0] = "Jetzt bestellen";
jetzt_bestellen[1] = "Order now";

/************** Produkte ****************/
	var color = new Array();
	var size = new Array();
	var detailbilder = new Array();
	var startseite = new Array();
	var product_sizes = new Array();
	
	// Größen der Produkte
	product_sizes["OF"] = new Array("100", "150", "180", "015", "050");
	product_sizes["GE"] = new Array("100", "150", "180", "015", "050");
	product_sizes["SO"] = new Array("100", "150", "180", "015", "050");
	product_sizes["BO"] = new Array("073", "092", "110", "015", "050");
	product_sizes["KA"] = new Array("145", "190");
	product_sizes["PF"] = new Array("120", "170");
	
	// ID der Detailbilder-Seite
	detailbilder["OF"] = 23;
	detailbilder["GE"] = 67;
	detailbilder["SO"] = 105;
	detailbilder["BO"] = 83;
	detailbilder["KA"] = 129;
	detailbilder["PF"] = 143;
	
	// ID der Größe & Farben Seite 
	startseite["SO"] = 104;
	startseite["OF"] = 48;
	startseite["GE"] = 66;
	startseite["BO"] = 82;
	startseite["KA"] = 128;
	startseite["PF"] = 141;
	
	//Offizier
	color["OF"] = new Array();
	color["OF"]["B1"]= "#box_79";
	color["OF"]["T1"]= "#box_80";
	color["OF"]["S1"]= "#box_81";
	
	//General
	color["GE"] = new Array();
	color["GE"]["B1"] = "#box_108";
	color["GE"]["T1"] = "#box_107";
	color["GE"]["S1"] = "#box_106";
	
	//Bogenschütze
	color["BO"] = new Array();
	color["BO"]["B1"] = "#box_140";
	color["BO"]["T1"] = "#box_139";
	color["BO"]["S1"] = "#box_138";
	
	//Soldat
	color["SO"] = new Array();
	color["SO"]["B1"] = "#box_159";
	color["SO"]["T1"] = "#box_158";
	color["SO"]["S1"] = "#box_157";
	
	//Kaiser
	color["KA"] = new Array();
	color["KA"]["B1"] = "#box_159";
	color["KA"]["T1"] = "#box_158";
	color["KA"]["S1"] = "#box_157";
	
	//Pferd
	color["PF"] = new Array();
	color["PF"]["B1"] = "#box_159";
	color["PF"]["T1"] = "#box_158";
	color["PF"]["S1"] = "#box_157";

/************** Produkte Ende************/	

$(document).ready(function() {
	/*
	 *	Thickbox auch bei nachgeladenen Inhalten
	 */
	$('a.livethickbox').live('click', function() {
		tb_show($(this).attr('title'), $(this).attr('href'), false);
		return false;
	});
	
	/*
	 *	InnerFade auf der Startseite
	 */
	show = true;
	if($.browser.mozilla) {
		if($.browser.version.match(/^1.8/))
			show = false;
	}
	
	if(show)
		$("#warriorslide").innerfade({ speed: 'slow', timeout: 6000, type: 'sequence'});
	
	/*
	 *	Witwen in den Überschriften vermeiden
	 */
	$("h1, h2, h3").avoidWidows();
	
	/*
	 *	Währungsumschalter
	 */
	$("#currency-change select").change(function() {
		$(this).parent().submit();
	});
	
	/*
	 *	Klickboxen!!!
	 */
	//Für den Warenkorb
	$(".jetztbestellen").clickbox();
	//Für den Bestellbutton beim Bestellen
	//$("form.bestellen fieldset.submitbutton li.send").clickbox();
	
	//YouTube Videos im Dokubereich
	$("body.doku #inhalt div.inhalt").clickbox();
	
	//Startseite Produkt details
	$("body.shop #box_5").clickbox(); // Startseite Offizier
	
	//Links im Bereich "Die Armee"
	$("body.links #inhalt .inhalt").clickbox();
	
	
	/*
	 *	SHOP
	 */
	
	/* Bestellformular */
	$("form.bestellen #bezahlart").change(function() {
		sel = $(this).val();
		$("option", this).each(function() {
		 if(sel == $(this).val()) {
		  $("form.bestellen fieldset."+sel).fadeIn(function() {
		   $(this).find("input:first").focus();
		  });
		 }
		 else {
		 	if($(this).val().length > 0) {
		  	$("form.bestellen fieldset."+$(this).val()).hide();
		  }
		 }
		});
	});
	
	/* Ändern des Landes */
	$("form #select_destination").change(function() {
		$.cookie("destination", $(this).val());
		$(this).parent().submit();
	});
	
	
	/* Darstellung */
	//Anzeige der Anzahl Artikel im Warenkorb
	countBasket();
	
	//Allgemeine Angaben zum Produkt
	product = $("body").attr("rel");
	
	if(product) {
		var standard = 0;
		
		
		/******** Aktuelle Auswahl aus Cookiedaten laden ********/
		//Prüfen ob ein Cookie mit der Farbe gesetzt wurde
		cc = $.cookie(product+"-color");
		//console.log(product+" Farbe: "+cc);
		if(cc) {
			//farbe setzen - wenn nicht direkt angegeben
			if(!$("#kriegerbild").attr("style"))
				$("#kriegerbild").css("background-image", "url(fileadmin/produkte/"+product+"/start-"+cc+".png)");
			//hinweistext anzeigen
			$(color[product][cc]).fadeIn();		
		}
		else {
			cc = "B";
			standard++;
		}
		
		//Prüfen ob ein Cookie mit der Größe gesetzt wurde
		cs = $.cookie(product+"-size");
		if(cs) {
			//größe setzen
			$("ul.size li a[rel="+cs+"]").addClass("aktiv");
			
			//beschreibungstext einfügen, abhängig vom produkt
			/*if(product == "OF") {
				$(size[product][cs]).fadeIn();
			}*/
		}
		else {
			cs = product_sizes[product][1];
			standard++;
		}
		
		if(standard < 2) {
			selectWarrior(product, cs, cc);
		}
		
		//Farbe und Größe
		if( $("body").is(".colorandsize") ) {
			//Bilder
			$("ul.size a[rel="+product_sizes[product][0]+"] img").attr("src", "fileadmin/produkte/"+product+"/"+product+product_sizes[product][0]+cc+"-size-front.png");
			$("ul.size a[rel="+product_sizes[product][1]+"] img").attr("src", "fileadmin/produkte/"+product+"/"+product+product_sizes[product][1]+cc+"-size-front.png");
			$("ul.size a[rel="+product_sizes[product][2]+"] img").attr("src", "fileadmin/produkte/"+product+"/"+product+product_sizes[product][2]+cc+"-size-front.png");
			//Hinweistext
			$(color[product]["B1"]+", "+color[product]["T1"]+", "+color[product]["S1"]).hide();
			$(color[product][cc+"1"]).fadeIn();
			
			//selectWarrior(product, cs, cc);
		}
		
		//Vorauswahl setzen - bei den Detailbilder und der Größen und Farbenauswahl
		if($("body").is(".colorandsize") || $("body").is(".detail"))
			selectWarrior(product, cs, cc);
		
		
		/************** FARBE ****************/
		//Bei Klick das Hintergrundbild ändern
		$("ul.farben li.color a").click(function() {
			c = $(this).attr("rel");
			//Speichern
			$.cookie(product+"-color", c, {expires: 7, path: "/"});
			//Bilder Ändern - handelt es sich um Größe und Farbe?
			if($("body").is(".colorandsize")) {
				//drei krieger ändern
				$("ul.size a[rel="+product_sizes[product][0]+"] img").attr("src", "fileadmin/produkte/"+product+"/"+product+product_sizes[product][0]+c+"-size-front.png");
				$("ul.size a[rel="+product_sizes[product][1]+"] img").attr("src", "fileadmin/produkte/"+product+"/"+product+product_sizes[product][1]+c+"-size-front.png");
				$("ul.size a[rel="+product_sizes[product][2]+"] img").attr("src", "fileadmin/produkte/"+product+"/"+product+product_sizes[product][2]+c+"-size-front.png");
				//Hinweistext
				$(color[product]["B1"]+", "+color[product]["T1"]+", "+color[product]["S1"]).hide(); //leider funzt join nicht mit arrays die ein string als key haben
				$(color[product][c+"1"]).fadeIn();
			}
			/*else {
				//Hintergrundbild tauschen
				$("#kriegerbild").css("background-image", "url(fileadmin/produkte/"+product+"/start-"+color+".png)");
				//Hinweistext
				$("#box_60, #box_61, #box_62").hide(); //leider funzt join nicht mit arrays die ein string als key haben
				$(OFcolor[color]).fadeIn();
			}*/
			
			//Link
			$(this).blur();
			//Auswahl setzen
			selectWarrior(product, cs, c, true);
			cc = c;
			return false;
		});
		
		
		
		/************** GRÖßEN ****************/
		//Bei Klick die Größenauswahl
		$(".colorandsize ul.size li a").click(function() {
			$("ul.size li a").removeClass("aktiv");
			s = $(this).attr("rel");
			//Speichern
			$.cookie(product+"-size", s, {expires: 7, path: "/"});
			//Anzeigen
			$(this).addClass("aktiv").blur();
			//Beschreibungstext
			//$(size[product].join(",")).hide();
			//$(size[product][s]).fadeIn();
			//Auswahl setzen
			selectWarrior(product, s, cc, true);
			cs = s;
			return false;
		});		
		
	} // Ende if(product)	
	else {
		if(debug) {
			alert("Keine Produktreferenzierung im Attribut rel im Bodytag gefunden.");
		}
		
	}
	
	/************** WICHTELKRIEGER ****************/
	$(".wichtelkrieger ul.size li a").click(function() {
		$("ul.size a.aktiv").removeClass("aktiv");
		product = $(this).addClass("aktiv").blur().attr("rel").split(",");
		selectWarrior(product[0], product[1], product[2], true);
		return false;
	});
	
	
	/************** Bestellformular ****************/
	$("body.bestellen input[type=text]:first").focus();
});

/*
 *	Aktualisiert die Anzeige der Auswahl
 *	
 *	Mit "glow" kann das aufglühen des unteren Buttons eingeschaltet werden.
 */
function selectWarrior(product, size, color, glow) {
	//hole beschreibungstext
	$.ajax({
		url: "typo3conf/ext/products/basket/get.php",
		type: "GET",
		data: {product: product, size: size, color: color, sprache: sprache, currency: currency},
		async: false,
		success: function(e) {
			if(e) {
				//Inhalte in beide Boxen schreiben
				
				if(color == "W" || color == "K") {
					start = 140;
				}
				else {
					start = startseite[product];
				}
				buyLink = "typo3conf/ext/products/basket/buy.php?id="+product+size+color+"&height=75&width=300&modal=true&from="+start+"&sprache="+sprache+"&ajax=1&fe=1&anz=1";
				$("#footer #insidefooter .auswahl-mi a").attr("href", buyLink).find("span").text(jetzt_bestellen[sprache]+": ");
				
				$("#footer #insidefooter .auswahl-mi a .bodytext").html(e);
				
				//Orangene Box aufleuchten lassen
				if(glow)
					$("#footer #insidefooter #box_glow").animate({opacity: "show"}, "fast").animate({opacity: "hide"}, "slow");
				
				//Link zu Detailbilder aktualisieren
				href = $("#footer-menu li:first a").attr("href");
				if(href.match(/[A-Z][A-Z][0-9][0-9][0-9][A-Z]$/))
				  href = href.replace(/[A-Z][A-Z][0-9][0-9][0-9][A-Z]$/g, product+size+color);
				else
				  href+= "?detail="+product+size+color;
				
				$("#footer-menu li:first a").attr("href", href);						/* quick and dirty */
				
				//Und für Google Analytics
				pageTracker._trackPageview("show_warrior_"+product+size+color);
				
			}
		},
		error: function(e) {
			alert("Fehler!\n"+e);
		}
	});
	
	//Ist es colorandsize?
	if($("body").is(".colorandsize")) {
		//Aktualisiere Artikel beschreibung
		$.ajax({
			url: "typo3conf/ext/products/basket/get.php",
			type: "GET",
			data: {product: product, size: size, color: color, show: "big", sprache: sprache, currency: currency},
			async: false,
			success: function(e) {
				if(e) {
					//$("#footer #insidefooter #auswahl a").attr("href", "typo3conf/ext/products/basket/buy.php?id="+product+size+color+"&height=75&width=300").find("span").text("Jetzt bestellen: ");
					// Beschreibung des Kriegers anzeigen
					$("body.colorandsize .produkt_details .details").html(e);
					// Link zu Detailbildern aktualisieren
					$("body.colorandsize .farben .detailbilder a").attr("href", "index.php?id="+detailbilder[product]+"&detail="+product+size+color+"&L="+sprache).attr("class", "detailbilder "+product+size+color);
					// Gespeicherter Wert im Graphen anzeigen
					$("ul.size a[rel="+size+"]").addClass("aktiv");
				}
			},
			error: function(e) {
				alert("Fehler!\n"+e);
			}
		});
	}
}

/*
 *	Lädt die Anzahl Artikel im Warenkorb
 */
function countBasket() {
	bullet = $("#top-navi li:last a .basket-items");
	
	$.ajax({
		url: "typo3conf/ext/products/basket/count.php",
		async: false,
		success: function(e) {
			$(bullet).text(e);
			
			if($(bullet).css("display") != "block" && e > 0)
				$(bullet).css("display", "block");
			else {
				if(e == 0)
					$(bullet).css("display", "none");
			}
		}
	});
	
}