$(function(){
	
	for(i = 0; i <= $(".layer_window").length-1; i++) {
		$(".layer_window:eq("+i+")").hide();
	}
	
	$(".navilink").hover(
	function() {
		showHover($(this));
	},
	function() {
		hideHover($(this));
	});
	
	hidesearchlayer = true;
	
	$("#warenkorb, #warenkorbinhalt").hover(
	function() {
		showWarenkorb();
	},
	function() {
		hideWarenkorb();
	});
	
	lastactivelink = null;
	
	$("input[name='pQuery']").click(function() {
		if ($(this).val() == "Begriff / Artikelnummer") {
			$(this).val("");			
		}
	});
	
	$("input[name='pQuery']").blur(function() {
		if ($(this).val() == "") {
			$(this).val("Begriff / Artikelnummer");
		}
	});
	
	$("input[name='email']").click(function() {
		if ($(this).val() == "E-Mail Adresse eingeben") {
			$(this).val("");			
		}
	});
	
	$("input[name='email']").blur(function() {
		if ($(this).val() == "") {
			$(this).val("E-Mail Adresse eingeben");
		}
	});
	
	$("input[name='pstrArtnr']").click(function() {
		if ($(this).val() == "Artikelnummer eingeben") {
			$(this).val("");			
		}
	});
	
	$("input[name='pstrArtnr']").blur(function() {
		if ($(this).val() == "") {
			$(this).val("Artikelnummer eingeben");
		}
	});
	
	// Bestellstrecke
	
	$(".sb").selectbox();
	
	$(".sbsize1 .jquery-selectbox").css("width", "131px");
	$(".sbsize1 .jquery-selectbox .jquery-selectbox-list").css("width", "126px");
	$(".sbsize2 .jquery-selectbox").css("width", "102px");
	$(".sbsize2 .jquery-selectbox .jquery-selectbox-list").css("width", "97px");
	$(".sbsize3 .jquery-selectbox").css("width", "55px");
	$(".sbsize3 .jquery-selectbox .jquery-selectbox-list").css("width", "50px");
	$(".sbsize4 .jquery-selectbox").css("width", "109px");
	$(".sbsize4 .jquery-selectbox .jquery-selectbox-list").css("width", "104px");

	
    $('.jqcarouselh').jcarousel({vertical: false, scroll: 2});
	
	lastimageh = null;
	
	$('div.jcarousel-prev-horizontal, div.jcarousel-next-horizontal').click(function() {
		if (lastimageh != null) {
			lastimageh.css('border', '5px solid #FFFFFF');
		}
	});
	
	$('.jqcarouselh li').click(function() {
		if (lastimageh != null) {
			lastimageh.css('border', '5px solid #FFFFFF');
		}
		lastimageh = $(this).children('img');
		lastimageh.css('border', '5px solid #ffbc39');
	});
		
	$("#kdnnrnh").click(function() {
		if($("#bs_info_kknr").css("display") != "none") {
			$("#bs_info_kknr").hide();
		}else {
			$("#bs_info_kknr").show();
		}
	});
	
	$("input[name='tt'], input[name='mm'], input[name='jjjj']").click(function() {
		
		switch($(this).attr("name")) {
			case "tt": {
				if($(this).val() == "TT") {
					$(this).val("");
				}
				break;
			};
			case "mm": {
				if($(this).val() == "MM") {
					$(this).val("");
				}
				break;
			};
			case "jjjj": {
				if($(this).val() == "JJJJ") {
					$(this).val("");
				}
				break;
			}
		}
	});
	
	$("input[name='tt'], input[name='mm'], input[name='jjjj']").blur(function() {
		switch($(this).attr("name")) {
			case "tt": {
				if ($(this).val() == "") {
					$(this).val("TT");
				}
				break;
			};
			case "mm": {
				if ($(this).val() == "") {
					$(this).val("MM");
				}
				break;
			};
			case "jjjj": {
				if ($(this).val() == "") {
					$(this).val("JJJJ");
				}
				break;
			}
		}	
	});
	
	$(".bs_save").click(function() {
		switch($(this).parent("div").parent("div").attr("id")) {
			case "rechnungsanschrift": {
				type = "ra";
				break;
			}
			case "lieferanschrift": {
				type = "la";
				break;
			}
		}
		rla_edit = $(this).parent("div");
		rla_show = $("#"+type+"_show");
		rla_edit.hide();
		rla_show.show();
		if(rla_edit.children("#"+type+"_frau").attr("checked") == true) {
			rla_show.children(".rightrow:eq(0)").html("Frau");
		}else if(rla_edit.children("#"+type+"_herr").attr("checked")) {
			rla_show.children(".rightrow:eq(0)").html("Herr");
		} else {
			rla_show.children(".rightrow:eq(0)").html("");
		}
		rla_show.children(".rightrow:eq(2)").html(rla_edit.children("input[name='"+type+"_nachname']").val());
		rla_show.children(".rightrow:eq(1)").html(rla_edit.children("input[name='"+type+"_vorname']").val());
		rla_show.children(".rightrow:eq(3)").html(rla_edit.children("input[name='"+type+"_firma']").val());
		rla_show.children(".rightrow:eq(4)").html(rla_edit.children("input[name='"+type+"_str']").val());
		rla_show.children(".rightrow:eq(5)").html(rla_edit.children("input[name='"+type+"_nr']").val());
		rla_show.children(".rightrow:eq(6)").html(rla_edit.children("input[name='"+type+"_plz']").val());
		rla_show.children(".rightrow:eq(7)").html(rla_edit.children("input[name='"+type+"_ort']").val());
	});
	
	$("#lart_terminwahl").hide();
	$("#lart_feierterminwahl").hide();
	
	$("#latermine input[type='radio']").click(function() {
		$("#lart_terminwahl").hide();
		$("#lart_feierterminwahl").hide();
		switch($(this).attr("id")) {
		  case "lart_termin": {
			  $("#lart_terminwahl").show();
			  break;
		  }
		  case "lart_feierabend": {
			  $("#lart_feierterminwahl").show();
			  break;
		  }
		}
	});
	
});

function showWarenkorb() {
	$("#warenkorbinhalt").css("display", "block");
}

function hideWarenkorb() {
	$("#warenkorbinhalt").css("display", "none");
}

function showHover(link) {
	if(link != null) {
		link.children("img").attr("src", farbverzeichnis + link.children("img").attr("name")+"_hover.jpg");
	}
}

function hideHover(link) {
	if(link != null && !link.attr("sel")) {
		link.children("img").attr("src", farbverzeichnis + link.children("img").attr("name")+".jpg");
	}
}

$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
};

var lastimagev = null;

function doZoomLink(content) {
	$("#layer_background").css("width", $(document).width()+"px");
	$("#layer_background").css("height", $(document).height()+"px");
	$("#layer_background").fadeTo(0, 0.0);
	$("#layer_background").show();
	$("#layer_background").fadeTo("slow", 0.5);
	$("#layer_zoom").html(content);
	$("#layer_zoom").centerInClient();
	$("#layer_zoom").fadeIn("slow");
	
	$('.jqcarouselv').jcarousel({vertical: true, scroll: 2});
	
	lastimagev = null;
	
	$('div.jcarousel-prev-vertical, div.jcarousel-next-vertical').click(function() {
		if (lastimagev != null) {
			lastimagev.css('border', '2px solid #FFFFFF');
		}
	});
	
	$('.jqcarouselv li').click(function() {
		if (lastimagev != null) {
			lastimagev.css('border', '2px solid #FFFFFF');
		}
		lastimagev = $(this).children('img');
		lastimagev.css('border', '2px solid #ffbc39');
		$("iframe[name='zoom']").attr("src", lastimagev.attr("link"));
	});
	$(".layer_close, #layer_background").click(function() {
		$("#layer_zoom").fadeOut("slow");
		$("#layer_background").fadeOut("slow");
	});
}

function doBigLink(content) {
	$("#layer_grossansicht").html(content);
	$("#layer_grossansicht").centerInClient();
	$("#layer_background").css("width", $(document).width()+"px");
	$("#layer_background").css("height", $(document).height()+"px");
	$("#layer_background").css("opacity", 0.5);
	$("#layer_background").show();
	$("#layer_grossansicht").show();
	window.focus();
	
	$('.jqcarouselv').jcarousel({vertical: true, scroll: 2});
	
	lastimagev = null;
	
	$('div.jcarousel-prev-vertical, div.jcarousel-next-vertical').click(function() {
		if (lastimagev != null) {
			lastimagev.css('border', '2px solid #FFFFFF');
		}
	});
	
	$('.jqcarouselv li').click(function() {
		if (lastimagev != null) {
			lastimagev.css('border', '2px solid #FFFFFF');
		}
		lastimagev = $(this).children('img');
		lastimagev.css('border', '2px solid #ffbc39');
		$("#grossbild img").attr("src", lastimagev.attr("link"));
	});
	
	$(".layer_close, #layer_background").click(function() {
		$("#layer_grossansicht").fadeOut("slow");
		$("#layer_background").fadeOut("slow");
	});
}

function doMassTabellenLink(content) {
	$("#layer_background").css("width", $(document).width()+"px");
	$("#layer_background").css("height", $(document).height()+"px");
	$("#layer_background").fadeTo(0, 0.0);
	$("#layer_background").show();
	$("#layer_background").fadeTo("slow", 0.5);
	$("#layer_masstabellen").html(content);
	$("#layer_masstabellen").centerInClient();
	$("#layer_masstabellen").fadeIn("slow");
	
	$(".layer_close, #layer_background").click(function() {
		$("#layer_masstabellen").fadeOut("slow");
		$("#layer_background").fadeOut("slow");
	});
}
