$(document).ready(function(){
	// Hauptnavigation
	$("#navigation li").hover(function() {
		var hoverID =$(this).attr("id");
		//$("#navigation #"+hoverID+" .subnav").fadeIn(300);
		if($.browser.msie){
			$("#navigation #"+hoverID+" .subnav").show();
		}
		else{
			$("#navigation #"+hoverID+" .subnav").fadeIn(300);
		}
	},function() {
		var hoverID =$(this).attr("id");
		if($.browser.msie){
			$("#navigation #"+hoverID+" .subnav").hide();
		}
		else{
			$("#navigation #"+hoverID+" .subnav").fadeOut(300);
		}
	});

	// Sprachwahl
	$('#sprachwahl a.sprachwahllink').removeAttr("href");
	$('#sprachwahl a.sprachwahllink').toggle(function(){
		if($.browser.msie){
			$('#sprachwahl #sprachwahlAuswahl').show();
		} else {
			$('#sprachwahl #sprachwahlAuswahl').fadeIn(300);
		}
		if($.browser.msie && $.browser.version=="6.0") {
			// Funktion deaktiviert in IE6
		} else {
			$('#sprachwahl .sprachwahllink').addClass("aktiv");
		}
	},function(){
		if($.browser.msie){
			$('#sprachwahl #sprachwahlAuswahl').hide();
		} else {
			$('#sprachwahl #sprachwahlAuswahl').fadeOut(300);
		}
		$('#sprachwahl .sprachwahllink').removeClass("aktiv");
	});

	// Listwechsel
	if($.browser.msie && $.browser.version=="6.0") {
		// Funktion deaktiviert in IE6
	} else {
		$("#listwechsel a").not(".aktiv").not(".ansichtenpfeil").children("img").fadeTo("fast",0.5);
		$("#listwechsel a").not(".aktiv").not(".ansichtenpfeil").hover(function() {
			$(this).children("img").fadeTo("fast",1);
		},function() {
			$(this).children("img").fadeTo("fast",0.5);
		});
	}

	// NoFlash-Version der Produktphilosophie-Seite
	$("body#unternehmen-philosophie a.wechselGrafik").hover(function() {
		$(this).find(".nohover").addClass("versteckt");
		$(this).find(".hover").removeClass("versteckt");
	}, function() {
		$(this).find(".hover").addClass("versteckt");
		$(this).find(".nohover").removeClass("versteckt");
	});

	// Produkte filtern
	kleineScrollbox = false;
	var laufzeitButtons = 0;
	var letzterButton = 0;
	var laufzeitProduktfilter = 800; // auch für Listtypbuttons
	$("#produkttypformular #submit").remove();
	/*$("#produkttypliste :checkbox").click(function() {
		$("form#produkttypformular").submit();
	});*/
	$("#produkttypliste li").css({"opacity":"0"});
	$("#produkttypliste li :checkbox").css({"opacity":"0"});
	$("#produkttypliste").css({"display":"block"});
	$("#produkttypliste li").each(function(i) {
		var j = i+1;
		$(this).addClass("produktfilter-"+j);
		produkttypAnimation1 = setTimeout(function() {
			$(".produktfilter-"+j).fadeTo((laufzeitProduktfilter*0.33),1);
			$(".produktfilter-"+j+" :checkbox").fadeTo((laufzeitProduktfilter*0.33),1);
		}, ((laufzeitProduktfilter*0.66)*i)+letzterButton);
		letzterProduktfilter = ((laufzeitProduktfilter*0.66)*i)+letzterButton;
		kleineScrollbox = true;
	});
	$("#produkttypliste :checkbox").click(function() {
		var n = $("#produkttypliste input.checkbox:checked").length;
		if(n >= 1) {
			var klasse = $(this).attr("rel");
			if($(this).attr("checked")) {
				$("div."+klasse).show();
			} else {
				$("div."+klasse).hide();
			}
		} else {
			$(this).attr("checked","checked");
		}
	});
	if(kleineScrollbox) {
		produktlisteSpaeter = setTimeout(function() {
			$("#produktliste").fadeIn("slow");
		}, letzterProduktfilter+(laufzeitProduktfilter*0.33));
	}

	// Durchwechseln von li-Elementen
	$("ul.durchwechseln").cycle({
		fx: 'fade',
		timeout: 10000,
		speed: 2000
	});

	// Infobox
	$("a[title]").each(function(i) {
		var titel = $(this).attr("title");
		if(titel != "") {
			$(this).attr("rel","infobox-"+i).removeAttr("title");
			var markup = '<div class="infobox infobox-'+i+'">'+ titel +'</div>';
			$("body").append(markup);
		}
	});
	$("a[rel*='infobox']").hover(function() {
		var klasse = $(this).attr("rel");
		$(".infobox."+klasse).fadeIn("fast");
	}, function() {
		var klasse = $(this).attr("rel");
		$(".infobox."+klasse).fadeOut("fast");
	}).mousemove(function(e){
		var klasse = $(this).attr("rel");
		var x = e.pageX - 3;
		var y = e.pageY + 22;
		$(".infobox."+klasse).css({"top":y+"px","left":x+"px"});
	});

	//Produktvorschau
	$("a.produktvorschau").hover(function() {
		var rel = $(this).attr("rel");
		if(rel != "") {
			rulesParsing = $(this).attr('class');
			rulesRegExp = /\[(.*)\]/;
			getRules = rulesRegExp.exec(rulesParsing);
			str = getRules[1]
			pattern = /\W+/;
			result= str.split(pattern);
			viewportHoehe = $(window).height();
			if(parseInt(viewportHoehe) > 700) { var faktor = 85; }
			if(parseInt(viewportHoehe) <= 700) { var faktor = 77; }
			if(parseInt(viewportHoehe) <= 630) { var faktor = 72; }
			if(parseInt(viewportHoehe) <= 500) { var faktor = 60; }
			produktfotoBreite = (parseInt(result[0])/100)*parseInt(faktor); // *65
			produktfotoHoehe = (parseInt(result[1])/100)*parseInt(faktor); // *65
			produktfotoBreiteBox = parseInt(produktfotoBreite)+10;
			produktfotoHoeheBox = parseInt(produktfotoHoehe)+5;
			produktfotoBreiteBoxInnen = parseInt(produktfotoBreiteBox)-2;
			produktfotoHoeheBoxInnen = parseInt(produktfotoHoehe)+3;
			var markup = '<div class="produktvorschaubox"><div class="produktvorschauboxInnen"><img src="lib.grafiken/loading.gif" alt="" class="loading" /><img src="'+rel+'" alt="" class="produktvorschauboxfoto" width="'+produktfotoBreite+'" height="'+produktfotoHoehe+'" /></div></div>';
			$("body").append(markup);
			$(".produktvorschaubox").css({"width":produktfotoBreiteBox+"px","height":produktfotoHoeheBox+"px"});
			$(".produktvorschauboxInnen").css({"width":produktfotoBreiteBoxInnen+"px","height":produktfotoHoeheBoxInnen+"px"});
			if($.browser.opera) {
				$(".loading").hide();
				$(".produktvorschauboxfoto").fadeIn("slow");
			} else {
				$("img.produktvorschauboxfoto").load(function() {
					$(".loading").hide();
					$(".produktvorschauboxfoto").fadeIn("slow");
				});
			}
		}
	}, function() {
			$(".produktvorschaubox").remove();
	}).mousemove(function(e){
		/* Mauskorrektur */
		var x = e.pageX - 3;
		var y = e.pageY + 22;
		position = (parseInt(produktfotoHoehe)+20)+parseInt(y);
		viewportHoehe = $(window).height();
		if(position >= viewportHoehe) {
			var yWert = parseInt(y)-(parseInt(produktfotoHoehe)+40);
		} else {
			var yWert = y;
		}
		if(position <= 0) {
			var yWert2 = e.pageY + 22;
		} else {
			var yWert2 = yWert;
		}
		$(".produktvorschaubox").css({"top":yWert2+"px","left":x+"px"});
	});

	//Zoom
	$("a.zoom").hover(function() {
		var rel = $(this).attr("rel");
		if(rel != "") {
			rulesParsing = $(this).attr('class');
			rulesRegExp = /\[(.*)\]/;
			getRules = rulesRegExp.exec(rulesParsing);
			str = getRules[1]
			pattern = /\W+/;
			result= str.split(pattern);
			viewportHoehe = $(window).height();
			faktor = 100;
			produktfotoBreite = (parseInt(result[0])/100)*parseInt(faktor); // *65
			produktfotoHoehe = (parseInt(result[1])/100)*parseInt(faktor); // *65
			produktfotoBreiteBox = parseInt(produktfotoBreite)+10;
			produktfotoHoeheBox = parseInt(produktfotoHoehe)+5;
			produktfotoBreiteBoxInnen = parseInt(produktfotoBreiteBox)-2;
			produktfotoHoeheBoxInnen = parseInt(produktfotoHoehe)+3;
			var markup = '<div class="produktvorschaubox"><div class="produktvorschauboxInnen"><img src="lib.grafiken/loading.gif" alt="" class="loading" /><img src="'+rel+'" alt="" class="produktvorschauboxfoto" width="'+produktfotoBreite+'" height="'+produktfotoHoehe+'" /></div></div>';
			$("body").append(markup);
			$(".produktvorschaubox").css({"width":produktfotoBreiteBox+"px","height":produktfotoHoeheBox+"px"});
			$(".produktvorschauboxInnen").css({"width":produktfotoBreiteBoxInnen+"px","height":produktfotoHoeheBoxInnen+"px"});
			if($.browser.opera) {
				$(".loading").hide();
				$(".produktvorschauboxfoto").fadeIn("slow");
			} else {
				$("img.produktvorschauboxfoto").load(function() {
					$(".loading").hide();
					$(".produktvorschauboxfoto").fadeIn("slow");
				});
			}
		}
	}, function() {
			$(".produktvorschaubox").remove();
	}).mousemove(function(e){
		/* Mauskorrektur */
		var x = e.pageX - 3;
		var y = e.pageY + 22;

			// Verschieben oben / unten
			position = (parseInt(produktfotoHoehe)+20)+parseInt(y);
			viewportHoehe = $(window).height();
			if(position >= viewportHoehe) {
				var yWert = parseInt(y)-(parseInt(produktfotoHoehe)+40);
			} else {
				var yWert = y;
			}
			if(position <= 0) {
				var yWert2 = e.pageY + 22;
			} else {
				var yWert2 = yWert;
			}

			// Verschieben rechts / links
			position2 = (parseInt(produktfotoBreite)+20)+parseInt(x);
			viewportBreite = $(window).width();
			if(position2 >= viewportBreite) {
				var xWert = parseInt(x)-(parseInt(produktfotoBreite)+20);
			} else {
				var xWert = x;
			}
			if(position2 <= 0) {
				var xWert2 = e.pageX;
			} else {
				var xWert2 = xWert;
			}



		$(".produktvorschaubox").css({"top":yWert2+"px","left":xWert2+"px"});
	});

	//Produktansichtwechsel
	$(".produktinformationen .farbenliste a").click(function() {
		//var foto = $(this).attr("href");
		var rel = $(this).attr("rev");
		//$("#produktansicht-gross img").attr("src",foto);
		$(".produktinformationen .farbenliste a").removeClass("versteckt");
		$(".produktinformationen .farbenliste .keinHoverBild").addClass("versteckt");
		$(this).addClass("versteckt");
		$(".produktinformationen .farbenliste .keinHoverBild."+rel).removeClass("versteckt");
		$(".produktinformationen .farbenliste div.farbcode").removeClass("fett");
		$(".produktinformationen .farbenliste div.farbcode."+rel).addClass("fett");
		//return false;
	});


	$(".farbwechselOutfit a").click(function() {
		var foto = $(this).attr("href");
		var rel = $(this).attr("rev");
		$("#"+rel+" .vorschauOutfit").attr("src",foto);
		/*var farbe = $(this").child("img").attr("alt");
		var href = $("#"+rel).attr("href");
		$("#"+rel).attr("href",href+"&farbe="+farbe);*/
		return false;
	});

	//Produktkombinationswechsel
	$("#produktkombinationsteile a.kombinationswechsel").attr("href","#");
	$("#produktkombinationsteile a.kombinationswechsel").click(function() {
		var rel = $(this).attr("rev");
		var html = $(this).html;

		$("ul.produktliste-kombinationen li.produkt-kombinationen").addClass("versteckt");
		$("ul.produktliste-kombinationen li.produkt-kombinationen#"+rel).removeClass("versteckt");
		return false;
	});


	// Haendlersuche Focus
	var haendlersuchePlz = $("#haendlerschnellsucheformular input#haendlersuchePlz").attr("value");
	var haendlersucheOrt = $("#haendlerschnellsucheformular input#haendlersucheOrt").attr("value");
	$("#haendlerschnellsucheformular input#haendlersuchePlz").focus(function() {
		if($(this).attr("value") == haendlersuchePlz) {
			$(this).attr("value","");
		}
	});
	$("#haendlerschnellsucheformular input#haendlersuchePlz").blur(function() {
		if($(this).attr("value") == "") {
			$(this).attr("value",haendlersuchePlz);
		}
	});
	$("#haendlerschnellsucheformular input#haendlersucheOrt").focus(function() {
		if($(this).attr("value") == haendlersucheOrt) {
			$(this).attr("value","");
		}
	});
	$("#haendlerschnellsucheformular input#haendlersucheOrt").blur(function() {
		if($(this).attr("value") == "") {
			$(this).attr("value",haendlersucheOrt);
		}
	});

	// Messetermine Akkordeon
	$("#messetabelle tbody tr").hover(function() {
		$("#messetabelle tr:not(.hover)").addClass("nohover");
		$(this).addClass("hover");
		$(this).find("div.mehr").css("display","block");
		$(this).find("div.weniger").css("display","none");
	},function() {
		$("#messetabelle tr.nohover").removeClass("nohover");
		$(this).removeClass("hover");
		$(this).find("div.weniger").css("display","block");
		$(this).find("div.mehr").css("display","none");
	});

	// Haendlersuche anzeigen
	$(".haendlersuche-link").hide();

	// Ansichtenpfeil Hover
	$("a.ansichtenpfeil").hover(function() {
		$("a.ansichtenpfeil img").attr("src","lib.grafiken/piktogramm.ansichtenpfeil.h.gif");
	}, function() {
		$("a.ansichtenpfeil img").attr("src","lib.grafiken/piktogramm.ansichtenpfeil.gif");
	});

	// MaskedInput für Datum
	$("#gewinnspielformular #geburtsdatum").mask("99.99.9999");
		
	// Optionale Felder
	$("#gewinnspielformular #neuigkeitenRH").change(function () {
		if($(this).attr("checked")) {
	        $("#gewinnspielformular #neuigkeitenRH").attr("checked","checked");
			$("#gewinnspielformular .optionalesFeld").removeClass("versteckt");
	        return;
	    }
		$("#gewinnspielformular #neuigkeitenRH").attr("checked","");
		$("#gewinnspielformular .optionalesFeld").addClass("versteckt");
	});

	// Greybox
	$("a.greybox").each(function(i){
		if($.browser.msie && $.browser.version=="6.0") {
			// Normaler Link
		} else {
			var href = $(this).attr("href");
			$(this).attr("href",href+"&greybox=true").click(function(){
				var t = this.rev || this.innerHTML || this.href;
				GB_show(t,this.href,617,960);
				return false;
			});
		}
	});

	// Flash Detection
	if(jQuery.browser.flash) {
		$("body").addClass("flash");
	} else {
		$("body").addClass("noflash");
	}
	
	// Ungerade Tabellenzellen
	$("table tr:odd").addClass("ungerade");

	// Bildpositionierung für den IE6
	$("p img.rechts").attr("align","right");
	$("p img.links").attr("align","left");

	// Externe Links
	$("a[href][rel*=external]").attr("target","_blank").addClass("extern");

	// png Fix
	$(document).pngFix();
});