$(function() {	
	// Some hovers
	$("button, #produktliste h2, .toggle-check").hover(
		 function() { $(this).addClass("hover"); }
		,function() { $(this).removeClass("hover"); }
	);
	
	
	// Click on search field
	$("#search input").focus(function() {
		if ( ! $(this).attr("rel")) {
			$(this).attr("rel", "1");
			$(this).val("");
		} //end if
	}).blur(function() {
		if ( ! $(this).val()) {
			$(this).val("Suchbegriff oder Artikelnummer");
			$(this).removeAttr("rel");
		} //end if
	});
	
	
	// Birthday fields
	$("#inp-tag").focus(function() {
		if ( ! $(this).attr("rel")) { $(this).attr("rel", "1"); $(this).val(""); };
	}).blur(function() {
		if ( ! $(this).val()) { $(this).val("TT"); $(this).removeAttr("rel"); };
	});
	$("#inp-monat").focus(function() {
		if ( ! $(this).attr("rel")) { $(this).attr("rel", "1"); $(this).val(""); };
	}).blur(function() {
		if ( ! $(this).val()) { $(this).val("MM"); $(this).removeAttr("rel"); };
	});
	$("#inp-jahr").focus(function() {
		if ( ! $(this).attr("rel")) { $(this).attr("rel", "1"); $(this).val(""); };
	}).blur(function() {
		if ( ! $(this).val()) { $(this).val("JJJJ"); $(this).removeAttr("rel"); };
	});
	
	
	// Navigation
	$("#nav li").hover(
		 function() { 
			 $(this).addClass("hover");
			 if(lidx==-1) lidx = $("#main #produktdetail").css("z-index");
			 $("#main #produktdetail").css({"z-index":0});
		}
		,function() { 
			$(this).removeClass("hover");
			$("#main #produktdetail").css("z-index",lidx);
		}
	);
	
	/* Navigation NEU
	$("#nav li > a").each(function(){
		if($(this).next().width() < ($(this).width()+10))
			$(this).next().css({
				width:($(this).width()+9)
			});
		if($.browser.msie && parseInt($.browser.version) <= 6){
			$(this).parent().css({
				width:($(this).children().children().width()+20)
			});	
			$(this).next().css({
				width:($(this).children().children().width()+19)		
			});
		}
	});
	*/
	
	if($.browser.mozilla){
		var lidx = -1;
		$("#nav li").hover(
			 function(){ 
				 if(lidx==-1) lidx = $("#main #produktdetail").css("z-index");
				 $("#main #produktdetail").css({"z-index":0});
			}
			,function(){
				$("#main #produktdetail").css("z-index",lidx);
			}
		);
	}
	
	// correct Navigation text-alignment
	$("#nav li *").css({'text-align':'left'});
		
	// correct promotionbox-height in assosiation to top-marken
	if($("#inner-sidebar .marken").length>0){
		$("#inner-sidebar .marken img").load(function(e){
			$(this).removeAttr("width").removeAttr("height");
			$("#main .produkt-gruppe-1 img").load(function(e){
				var mh = $("#inner-sidebar .marken").height();
				var wh = $("#main .produkt-gruppe-1 .wrap").height();
				if(mh > wh){
					$("#main .produkt-gruppe-1 .wrap").css({'min-height':$("#inner-sidebar .marken").height()});
					$("#main .produkt-gruppe-1 h2").css({'margin-bottom':(parseInt((mh-wh)/2))});
				}
			}).each(function(){ 
				//IE Fix for Image Load Event
				if ($(this)[0].complete && $(this)[0].naturalWidth !== 0 ) {
					$( this ).trigger('load');
				}
			});
		}).each(function(){ 
			$(this).attr("width",1).attr("height",1);
			//IE Fix for Image Load Event
			if ($(this)[0].complete && $(this)[0].naturalWidth !== 0 ) {
				$( this ).trigger('load');
			}
		});
		
		
	}
	
	// add .txt class to all text and password input boxes
	$("input[type=text], input[type=password]").addClass("txt");
	
	$("#produktdetail div.txt").show();
	// Add tab functionality
	$(".tab-me").tabMe();
	
	
	// Fancy box
	$("a.fancybox").fancybox({
		 'overlayShow': 		true
		,'hideOnContentClick': 	true
		,'imageScale': 			true
		,'centerOnScroll': 		true
	});
	fancyFsiViewer();
	fancyKaufinfos();
	
	
	// Create custom selectboxes and checkboxes
	custom_selectboxes();
	custom_checkboxes();
	
	
	// Password forgot mask
	$(".login-reg a.forgot").click(function() {
		$("#password-forgot-1").fadeIn(200);
		return false;
	});
	$(".login-reg .password-forgot a.close, .login-reg .password-forgot button.close, #password-forgot-1 button.b1").click(function() {
		$("#password-forgot-1").fadeOut(200);
		$("#password-forgot-2").fadeOut(200);
		return false;
	});
	
	
	// Interresen overlay
	$("#interessen-link").click(function() {
		$("#interessen-overlay").fadeIn(200);
		return false;
	});
	$("#interessen-overlay a.close, #interessen-overlay button.close").click(function() {
		$("#interessen-overlay").fadeOut(200);
		return false;
	});
	
	
	// Fix the bug when clicking a link inside a label
	$("label.prettyCheckbox a").click(function() {
		$(this).parent().parent().click();
		return true;
	});

	
	
	// Toggle bestellungen and on init show the first one
	$(".bestellung .products:eq(0)").show();
	$(".bestellung a.toggle:eq(0)").toggleClass('toggle-open');
	$(".bestellung a.toggle").click(function() {
		$(this).toggleClass('toggle-open');
		$(this).next().slideToggle();
		return false;
	});
	
	
	
	// Browser specific fixing 
	var is_chrome = /chrome/.test(navigator.userAgent.toLowerCase());
	var is_win = /windows/.test(navigator.userAgent.toLowerCase());
	var is_firefox = /firefox/.test(navigator.userAgent.toLowerCase());
	
	// fixing safari
	if ($.browser.safari) {
		$("body").addClass("sfr");
		//$("#search input").val("Safari");
	} //end if

	// fixing chrome
	if (is_chrome) {
		$("body").addClass("chr");
		//$("#search input").val("Chrome");
	} //end if

	// fixing opera
	if ($.browser.opera) {
		$("body").addClass("opr");
		//$("#search input").val("Opera");
	} //end if
	
	// fixing firefox 3 on win
	if ($.browser.mozilla && is_firefox && is_win) {
		$("body").addClass("ffwin");
		//$("#search input").val("Firefox on Windows");
	} //end if	
	
	if ($.browser.msie && is_win) {
		$("body").addClass("msie"+parseInt($.browser.version));
	}
	
	align_images();	
	if($.browser.msie && parseInt($.browser.version) == 8){
		if($(".clearfix.main-dark").length>0 && $("#pg-sportart").length == 0){
			$(".clearfix.main-dark").append(
				$("<div></div>").addClass("clear")
			);
			$(".clearfix").removeClass("clearfix");
		}
		/* IE8 registration bugfix */
		if($(".konto").length > 0)
			window.setTimeout(function(){
				$(".inp2").removeClass("inp2").addClass("inp2");
				window.setTimeout(function(){
					$("p.inp2 span").css({"left":-20});
					$(".inp2").removeClass("inp2").addClass("inp2");
					$("p.inp2 span").css({"left":-70000});
					window.setTimeout(function(){
						$("p.inp2 span").css({"left":-20});
					},50);
				},50);
			},5);

	}
	$("#produktdetail").css({'z-index':5});
	
	$("a#error-dialog-trigger").fancybox({
		 'overlayShow': 		true
		,'hideOnContentClick': 	false
		,'imageScale': 			true
		,'centerOnScroll': 		true
		,'frameWidth': 		    300
		,'frameHeight':		    100
	});
	
	if($("#produktdetail .c50l h2 span").length > 0){
		var spnh = $("#produktdetail .c50l h2 span").innerHeight( );
		if(spnh > $("#produktdetail .c50l h2").height()){
			$("#produktdetail .c50l h2").css({
				height:spnh
			});
			$("#produktdetail .c50r .tab-me .tabs").css({
				height:spnh
			});
		}
	}
	
	$("input.spinner").JSpinner();
		
});

var c1 = false;
$(document).load(function(){

	// Resize the sidebar
	if(!c1){
		$("#sidebar").css({"min-height":$("#main").height()});
		// correct width of stattpreis
		if($.browser.msie && parseInt($.browser.version) < 7){
			$(".former em").css({width:$(".former").width()});
		}
		if($.browser.msie && parseInt($.browser.version) > 7){
			if($(".clearfix.main-dark").length>0){
				$(".clearfix:after").css({height:"auto"});
			}
		}
		c1=true;
	}
});


function align_images() {	
	$("#main .product-teaser-1 .wrap div.img a").vAlign();
	
	$("#main .product-teaser-1 .wrap a.img img, #main .product-teaser-2 .wrap a.img img").parent().css({
		'text-align':'center'
	});
	
	// Bugfix reposition Images after they have been loaded
	$("#main .topseller div.img img, #main .produkt-gruppe-1 div.img img, #main .produkt-gruppe-2 div.img img, #main .produkt-gruppe-3 div.img img, #produktliste .produkt div.img img,#main .product .img img,#produktdetail .img img,#main .product-teaser-1 .wrap a.img img").unbind().load(function(e){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var mh = parseInt((ph - ah) / 2);		
		$(this).css('padding-top', mh);
	}).each(function(){ 
		//IE Fix for Image Load Event
		if ($(this)[0].complete && $(this)[0].naturalWidth !== 0 ) {
			$( this ).trigger('load');
		}
	});
}

function custom_selectboxes() {
	if(($.browser.msie && parseInt($.browser.version) > 6) || !$.browser.msie){
		// Custom select boxes
		$("select.ssel").sSelect({ddMaxHeight: '200px'});
		
		// perform additional actions on custom select boxes
		$(".newListSelected").each(function() {
			// forward error class
			var prev_class = $(this).prev().attr("class");
			prev_class = prev_class.replace("error", "error newListSelected_error");
			$(this).addClass(prev_class);
			
			// add custom class to 1st item in custom select
			$(this).find("ul.newList li:first").addClass("first");
			$(this).find("ul.newList li:last").addClass("last");
		});
	}
}

function custom_checkboxes() {
	// Custom radio buttons and checkboxes
	$(".register input[type=radio], .konto input[type=radio], .konto input[type=checkbox], #produktliste input[type=checkbox], #produktvergleich input[type=checkbox]").prettyCheckboxes();
}

function fancyFsiViewer()
{
	$("a.fsiviewer").fancybox({
		 'overlayShow': 		true
		,'hideOnContentClick': 	false
		,'imageScale': 			true
		,'centerOnScroll': 		true
		,'frameWidth': 		    577
		,'frameHeight':		    475
	});
}

function fancyKaufinfos()
{
	$("a.kaufinfos-link").fancybox({
		 'overlayShow': 		true
		,'hideOnContentClick': 	false
		,'imageScale': 			true
		,'centerOnScroll': 		true
		,'frameWidth': 		    400
		,'frameHeight':		    300
	});
}

function showProductListTopFunctions()
{
	// copy content from bottom to top functions
	$("#productlist-top-functions").children().remove();
	$("#productlist-top-functions").append($("#productlist-bottom-functions").html());

	$("#breadcrumbs-f").css({height:65});
	$("#productlist-top-functions").css({display:'block'});
}

function hideProductListTopFunctions()
{
	$("#breadcrumbs-f").css({height:34});
	$("#productlist-top-functions").css({display:'none'});
}


$.fn.equalHeight = function(substract) {
	var tallest = 0;
	$(this).each(function() {
		var this_height = $(this).height();
		if (this_height > tallest) {
			tallest = this_height;
		} //end if
	});
	if (substract) tallest -= substract;
	$(this).height(tallest);
};


$.fn.tabMe = function() {
	$(this).each(function() {
		var tab_me = $(this);
		
		//init marker
		$(tab_me).find(".tabs a.tab").removeClass("on");
		$(tab_me).find(".tabs a.tab:eq(0)").addClass("on");
		
		//init content
		$(tab_me).find(".tab-content").show();
		$(tab_me).find(".tab-content .tab-container").hide();
		$(tab_me).find(".tab-content .tab-container:eq(0)").show();
		
		//add clicking 
		$(tab_me).find(".tabs a.tab").each(function(num) {
			$(this).click(function() {
				//markers
				$(tab_me).find("a.tab").removeClass("on");
				$(this).addClass("on");
				
				//content
				$(tab_me).find(".tab-content .tab-container").hide();
				$(tab_me).find(".tab-content .tab-container:eq("+num+")").show();
				
				return false;
			}); //end click()
		}); //end each()
	}); //end each
};


var xhrTypes = {productList:0,productDetail:1, productComparison:2,productShowSetVariants:3,productShowSetVariant:4}; 

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var mh = (ph - ah) / 2;
		$(this).css('padding-top', mh);
	});
};

var ttT=null;
$(document).bind("contentResized", function(event){
	clearTimeout(ttT);
	ttT=window.setTimeout(function(){
		// changing #sidebar height for left Navi		
		$("#sidebar").css({height:"auto"});
		var sHeight = $("#sidebar").height();
		$("#col2").css({height:"auto"});
		var cHeight = $("#col2").height();
		
		var mHeight = (sHeight > cHeight)?sHeight:cHeight;
		
		$("#sidebar").css({"min-height":mHeight});
		$("#col2").css({height:mHeight});

		try{
			// correct width of stattpreis
			if($.bowser.msie && parseInt($.browser.version) < 7){
				$(".former em").css({width:$(".former").width()});
			}
		}catch(e){}		
	},500);
});

// custom Ajax loaded trigger
// is used for JS-Correction after Ajax content is loaded

$(document).bind("xhrLoad", function(event, xhrType){
	try{
		if(xhrType==xhrTypes.productList){
			// refreshes the top functions container
//			$("#col2 .functions").children().remove();
//			$("#col2 .functions").replaceWith($("#fktContainer").addClass("functions").attr("id","").show());
			align_images();
		} else if(xhrType==xhrTypes.productDetail){
			fancyFsiViewer();
			fancyKaufinfos();
			$("input.spinner").JSpinner();
		} 
		else if(xhrType==xhrTypes.productShowSetVariants)
		{
			$("#produktset .overlay").fadeTo(10, .7);
			$("#produktset .overlay2").fadeTo(10, .8);
			$("#produktset").height($("#col2").height()-30);
			if ($.browser.msie && $.browser.version < 7.0) {
				$("#produktset .overlay").height($("#col2").height()-30);
				$("#produktset .overlay2").height($("#produktset .data").height());
			} //end if
			$("#produktdetail .passend a").click(function() {
				$("#produktset").fadeIn(200);
				return false;
			});
			$("#produktset a.close").click(function() {
				$("#produktset").fadeOut(200);
				return false;
			});
			$("#produktset button").click(function() {
				$("#produktset").fadeOut(200);
				return false;
			});
			//update only new selct boxes
			if(($.browser.msie && parseInt($.browser.version) > 6) || !$.browser.msie){
				
				
				var len = $(".products div[id^=set-variant-]").length+1;
				$(".products div[id^=set-variant-] .product .props .wrap").each(function(){
					$(this).css("z-index",len);
					len--;
				});
				
				$("#col2 #produktset .product .props .newListSelected ul").css({
					position:"relative",
					height:"auto",
					top:3
				});
				
				window.setTimeout(function(){
					$("select.ssel").each(function(){
					    if(!$(this).next().hasClass("newListSelected")){
					        $(this).sSelect({ddMaxHeight: '200px'});
					    }
					});	
				},250);
			}
			
		} else if(xhrType==xhrTypes.productShowSetVariant){
			if(($.browser.msie && parseInt($.browser.version) > 6) || !$.browser.msie){
				//update only new select boxes
				var len = $(".products div[id^=set-variant-]").length+1;
				$(".products div[id^=set-variant-] .product .props .wrap").each(function(){
					$(this).css("z-index",len);
					len--;
				});
				$("#col2 #produktset .product .props .newListSelected ul").css({
					position:"relative",
					height:"auto",
					top:3
				});
				window.setTimeout(function(){
					$("select.ssel").each(function(){
					    if(!$(this).next().hasClass("newListSelected")){
					        $(this).sSelect({ddMaxHeight: '200px'});
					    }
					});	
				},250);
			}
		}
		$("#produktdetail").css({'z-index':5});
		$("#produktvergleich .img a img,.data .product .img img").unbind().load(function(e){
			var aw = $(this).width();
			var pw = $(this).parent().parent().width();
			var mw = parseInt((pw - aw) / 2);
			$(this).css({position:'relative','left':mw});
		}).each(function(){ 
			//IE Fix for Image Load Event
			if ($(this)[0].complete && $(this)[0].naturalWidth !== 0 ) {
				$( this ).trigger('load');
			}
		});
		if($("#produktdetail .c50l h2 span").length > 0){
			var spnh = $("#produktdetail .c50l h2 span").innerHeight( );
			if(spnh > $("#produktdetail .c50l h2").height()){
				$("#produktdetail .c50l h2").css({
					height:spnh
				});
				$("#produktdetail .c50r .tab-me .tabs").css({
					height:spnh
				});
			}
		}
		$(document).trigger("contentResized");
	}catch(e){}
});




})(jQuery);

function startAjax()
{
	if(typeof(gb) != 'undefined' && gb != null) gb.showLoading();
}

function failedAjax()
{
	if(typeof(gb) != 'undefined' && gb != null) gb.hideLoading();
}

function stopAjax()
{
	if(typeof(gb) != 'undefined' && gb != null) gb.hideLoading();
}

$(function()
{
	$("body").ajaxStart(startAjax)
			 .ajaxStop(stopAjax)
			 .ajaxError(failedAjax)
});


$.ajaxSetup({
	cache: false
});
