

$(document).ready(function() {
	addPrintLink();
	toggleFieldValue($('#q'));
	toggleFieldValue($('#m3zip'));
	toggleFieldValue($('#m2zip'));
	toggleFieldValue($('#cntnt01zip'));
	rebuildImageMarkup();
	//heroImageAnimation.init();
	produktfinder();
	pimpMyProducts();
	
	$("a#videodsl").click(function(){
		window.open(this.href,"Diese Seite wird einem neuen Fenster angezeigt","width=442,height=291");
		return false;
	})
	
	$("a#videoisdn").click(function(){
		window.open(this.href,"Diese Seite wird einem neuen Fenster angezeigt","width=302,height=213");
		return false;
	})
	
});
window.onload = function(){
	//window.setTimeout("heroImageAnimation.animate()",1000);	//timeout to prevent ie crashes
	window._uacct = "UA-299719-18";
	if(window.urchinTracker)window.urchinTracker();
}

function pimpMyProducts(){
	$("#products").find("li a").each(function(i) {
		$(this).click(function(){
			if(window.lastActiveProduct == undefined){
				$("#products").find("li").each(function(i) {
					$(this).fadeTo("normal", 0.2);
				});
			}else{
				window.lastActiveProduct.parent().fadeTo("normal", 0.2);
			}
			$(this).parent().fadeTo("normal", 1.0);
			var productId = $(this).attr("id");
			productId = productId.split("_");
			productId = productId[1];			
			var backbutton = "<div id=\"detailinfo_handles\"><a href=\"javascript:backToOverview();\" id=\"back_to_product_start\">zur&uuml;ck</a></div>";			
			$.get("/index.php/getproduct/"+productId+"/77/77/88",null, 
				function(data){ 
					$("#detailinfo").html(data+backbutton);
					$("#defaultinfo").hide();
					$("#detailinfo").fadeIn("slow");
				}
			);
			window.lastActiveProduct = $(this);
			this.blur();
			return false;
		});		
	});
}
function backToOverview(){
	$("#detailinfo").hide();
	$("#defaultinfo").fadeIn("normal");
	$("#products").find("li").each(function(i) {
		$(this).fadeTo("normal", 1.0);
	});
	window.lastActiveProduct = undefined;
}




function produktfinder(){
	$('#productfinder #type').change(function(){
		document.getElementById('m2moduleform_1').submit();
	})
	$('#productfinder2 #brand').change(function(){
		document.getElementById('m2moduleform_2').submit();
	})
}
function addPrintLink(){
	if(window.print){
		$("#metamenu ul").append("<li class=\"printlink\"><a href=\"javascript:window.print();\">Seite drucken</a></li>");
	}
}
function toggleFieldValue(o){
	if(!o)return;
	o.focus(function() {
		if(this.value == this.defaultValue) this.value = "";
	});
	o.blur(function() {
		if(this.value == "") this.value = this.defaultValue;
	});
}

function rebuildImageMarkup(){
	$('#content img').each(function(){
		if($(this).parent().is("p")){
			//$(this).before("</p>");
			//$(this).after("<p>");
			//console.log($(this));
			/*if($(this).prev().is("p") && $.trim($(this).prev().html()) == ""){
				$(this).prev().remove();
			}
			if($(this).next().is("p") && $.trim($(this).next().html()) == ""){
				$(this).next().remove();
			}*/
		}
		if($(this).attr("align") == "left"){
			if($(this).attr("width") == 394){
				$(this).wrap("<p class=\"contentpic-big\"></p>");
			}else if($(this).attr("width") == 190){
				$(this).wrap("<p class=\"contentpic-small-left\"></p>");				
			}
			$(this).removeAttr("align");
			$(this).after($(this).attr("alt"));
		}else if($(this).attr("align") == "right"){
			if($(this).attr("width") == 143){
				$(this).wrap("<p class=\"teaserpic-horizontal\"></p>");	
			}else if($(this).attr("width") == 238){
				$(this).wrap("<p class=\"teaserpic-vertical\"></p>");	
			}
			$(this).removeAttr("align");
			$(this).after($(this).attr("alt"));
		}
	});
	/*$('#content p').each(function(){
		if($(this).html() == ""){
			//$(this).remove();
			$(this).css({ border: "1px solid blue" });
		}
	})*/
}