$(document).ready(function() {

    $(".dontShowMeFromBeginning").hide();
    $("#manuSku").hide();
	
	
    /*-------------------*/
    /* FILL SEARCH INPUT */
    /*-------------------*/
	
	var txt_search_default_text = "Deine Suche / Artikel-Nr.";
	$("#txt_search").val(txt_search_default_text);
	
	$("#txt_search").change(function(){	
		if(trim($("#txt_search").val()) == ""){
			$("#txt_search").val(txt_search_default_text);
		}
	});
	
	$("#txt_search").click(function(){
		if(trim($("#txt_search").val()) == txt_search_default_text){
			$("#txt_search").val("");
		}	
	});
	
	$("#txt_search").focus(function(){
								//	alert(trim($("#txt_search").val()) + " == " +  txt_search_default_text)
		if(trim($("#txt_search").val()) == txt_search_default_text){
			$("#txt_search").val("");
		}	
	});
	$("#txt_search").blur(function(){	
		if(trim($("#txt_search").val()) == ""){
			$("#txt_search").val(txt_search_default_text);
		}
	});
	


    /*-------------*/
    /* GET BROWSER */
    /*-------------*/

    function getBrowser() {
        var name = navigator.appName;
        var vers = navigator.appVersion;
        var ieversion = vers.substr(vers.indexOf("MSIE") + 5, 1);
        var safari = vers.indexOf("Safari");
        var chrome = vers.indexOf("Chrome");

        if (name == "Microsoft Internet Explorer") {
            if (ieversion == "6") return "MSIE6";
            else if (ieversion == "7") return "MSIE7";
            else if (ieversion == "8") return "MSIE8";
            else return "MSIE";
        } else if (name == "Netscape" && safari != -1 && chrome != -1) {
            return "Chrome";
        } else if (name == "Netscape" && safari != -1 && chrome == -1) {
            return "Safari";
        } else if (name == "Netscape" && safari == -1 && chrome == -1) {
            return "Firefox";
        } else if (name == "Opera") {
            return "Opera";
        } else {
            return "other";
        }
    }

    var browser = getBrowser();

    window.is_ie6 = (browser == "MSIE6") ? true : false;
    window.is_firefox = (browser == "Firefox") ? true : false;




    /*------------------------------*/
    /* DETAILS: Preview & Highscale */
    /*------------------------------*/

    $("#productDetailDescriptionBTN").click(function() { $("#productDetailDescription").slideToggle(250); });




    /*------------------------------*/
    /*   AGB und Widerruf Overlay   */
    /*------------------------------*/

    $("#agbOverlayClick").click(function() {
        w = (($(document).width() - $("#wrapper").width()) / 2) + 50;
        $("#agbOverlay").css({ display: "block", left: w });  //, width:"800px", margin:"0 auto"
        $("#fixedPageOverlay").css({ display: "block" });
    });
    $("#widerrufOverlayClick").click(function() {
        w = (($(document).width() - $("#wrapper").width()) / 2) + 50;
        $("#widerrufOverlay").css({ display: "block", left: w });  //, width:"800px", margin:"0 auto"
        $("#fixedPageOverlayWiderruf").css({ display: "block" });
    });
    $("#fixedPageOverlay, #fixedPageOverlayWiderruf, #agbOverlay, #widerrufOverlay").click(function() {
        $("#fixedPageOverlay, #fixedPageOverlayWiderruf, #agbOverlay, #widerrufOverlay").css({ display: "none" });
    });




    /*--------------------------------*/
    /* PERSONAL DATA: change Password */
    /*--------------------------------*/

    $("#changePasswordLabel").click(function() {
        $("#changePassword").slideToggle(200);
    });




    $(".catalogDetailsText").css({ "display": "none" });
    $(".catalogueToggleImg").click(function() {
        $(".catalogDetailsText").slideToggle(250);

        var url_plus = "images/icons/toggle_plus.jpg";
        var url_minus = "images/icons/toggle_minus.jpg";

        var src = $(this).attr("src");
        var toggler = src.indexOf(url_plus);

        if (toggler != -1) {
            $(this).attr("src", window.ycURL + url_minus);
        } else {
            $(this).attr("src", window.ycURL + url_plus);
        }
    });

















    /********** FUNCTIONS ***********/

    function resizeHighscaleImage() {

        var paddingLeft = 20; //px
        var paddingRight = 20; //px
        var paddingTop = 20; //px
        var paddingBottom = 50; //px
        //	var minwidth = 100; //px
        //	var minheight = 100; //px

        var winwidth = $(window).width() - (paddingLeft + paddingRight);
        var winheight = $(window).height() - (paddingTop + paddingBottom);
        var imgwidth = $("#ycHighscaleImage").width();
        var imgheight = $("#ycHighscaleImage").height();

        var tmpwinfactor = winwidth / winheight;
        var tmpimgfactor = imgwidth / imgheight;

        if (tmpwinfactor < tmpimgfactor) {
            var newimgwidth = Math.round(winwidth);
            var newimgheight = Math.round((newimgwidth / imgwidth) * imgheight);
        } else {
            var newimgheight = Math.round(winheight);
            var newimgwidth = Math.round((newimgheight / imgheight) * imgwidth);
        }

        if (newimgwidth > winwidth || newimgheight > winheight) {
            newimgwidth = imgwidth;
            newimgheight = imgheight;
        }

        var top = ((winheight - newimgheight) / 2) + paddingTop;
        var left = ((winwidth - newimgwidth) / 2) + paddingLeft;

        $("#ycHighscaleImage").width(newimgwidth);
        $("#ycHighscaleImage").height(newimgheight);


        if (window.is_ie6 === false) {
            $("#ycHighscaleImageContainer").css({ "position": "fixed", "top": top + "px", "left": left + "px", "width": newimgwidth + "px", "height": newimgheight + "px" });
        } else {
            $("#ycHighscaleImageContainer").css({ "position": "absolute", "top": top + "px", "left": left + "px", "width": newimgwidth + "px", "height": newimgheight + "px" });
        }
    }




    /*--------------*/
    /* Fisheye-Menu */
    /*-------
     -------*/

    function startFisheye() {
    $("#fisheyeImageList").tawPreloadImages({
    "css_background_class": "tawImagePreloaderContainer",
    "css_background_class_on_error": "tawImagePreloaderContainerOnError",
    "image_fade_in_time": 0,
    "substractPadding": true
    }, function() {
    $("#fisheyeImageList").tawFisheye({
    duration: 300,
    startWidth: "60px",
    startHeight: "60px",
    endWidth: "100px",
    endHeight: "100px"
    });
    });
    }

    startFisheye();
    
   

    var ycImgUrl = window.ycURL;

    $("#fisheyeArrowLeft").mouseover(function() {
    $("img", this).attr("src", ycImgUrl + "/images/fisheye_arrow_lft_hover.jpg");
    }).mouseout(function() {
    $("img", this).attr("src", ycImgUrl + "/images/fisheye_arrow_lft.jpg");
    });

    $("#fisheyeArrowRight").mouseover(function() {
    $("img", this).attr("src", ycImgUrl + "/images/fisheye_arrow_rgt_hover.jpg");
    }).mouseout(function() {
    $("img", this).attr("src", ycImgUrl + "/images/fisheye_arrow_rgt.jpg");
    });

    

    $("#fisheyeArrowLeft").click(function() {
    $.post("ProductCrossNavigator.aspx", { manufacturerid: document.getElementById('fisheyeImageList').getAttribute("manufacturerid"), position: Number(document.getElementById('fisheyeImageList').getAttribute("position")) - 8 }, function(data) {
    $("#ImageListContainer").replaceWith('<div id="ImageListContainer">' + data + '</div>');
    startFisheye();
    });
    });

    $("#fisheyeArrowRight").click(function() {
    $.post("ProductCrossNavigator.aspx", { manufacturerid: document.getElementById('fisheyeImageList').getAttribute("manufacturerid"), position: Number(document.getElementById('fisheyeImageList').getAttribute("position")) + 8 }, function(data) {
    $("#ImageListContainer").replaceWith('<div id="ImageListContainer">' + data + '</div>');
    startFisheye();
    });
    });





    /*--------------*/
    /* Detail-Image */
    /*--------------*/

    //$("#show_highscaled a").text("loading Zoom...");	


    window.url_maxzoomcorner_green = window.ycURL + "images/maxzoomcorner_green.png";
    window.url_maxzoomcorner_red = window.ycURL + "images/maxzoomcorner_red.png";

    // ****************************************************************************************************************************	
    // ****************************************************************************************************************************

    /*

	
    function getScrollPosition() {
    var scrOfX = 0, scrOfY = 0;
		
		if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
    }
		
		return [ scrOfX, scrOfY ];
    }
    */
    // ****************************************************************************************************************************	
    // ****************************************************************************************************************************

    function tawLoadMediumSizeImage() {
        $(".jqzoom").tawPreloadImages({
            "css_background_class": "tawDetailImagePreloaderContainer",
            "css_background_class_on_error": "tawDetailImagePreloaderContainerOnError",
            "image_fade_in_time": 250,
            "substractPadding": true
        });
    }

    // ****************************************************************************************************************************
    // ****************************************************************************************************************************

    function tawLoadHighscaleImage() {

        $("#ycHighscaleImageContainer").css({"display": "none","visibility":"hidden" });

        tawRemoveHighscaleEventHandlers();

        $("#ycHighscaleImageContainer").tawPreloadImages({
            "css_background_class": "tawDetailImagePreloaderContainer",
            "css_background_class_on_error": "tawDetailImagePreloaderContainerOnError",
            "image_fade_in_time": 250,
            "substractPadding": true
        }, function() {
            $("#show_highscaled a").text("Zoom");
            tawResetHighscaleImageSize()
            tawHideHighscaleContainer();
            tawAddHighscaleEventHandlers();
            resizeHighscaleImage();
        });
    }

    function tawRemoveHighscaleEventHandlers() {
        $("#show_highscaled, .jqzoom, #ycHighscaleMaxZoomCorner, .ycHighscaleImageClose, #ycHighscaleImage").unbind("click");
    }

    function tawResetHighscaleImageSize() {
        $("#ycHighscaleImage").width("auto").height("auto");
        var newWidth = $("#ycHighscaleImage").width();
        var newHeight = $("#ycHighscaleImage").height();
        $("#ycHighscaleImageContainer").width(newWidth + "px").height(newHeight + "px");
        if (window.is_firefox) $("#ycHighscaleImageContainer").css("paddingBottom", "29px");
    }

    function tawHideHighscaleContainer() {
        $("#ycHighscaleImageContainer, #ycHighscaleImageContainer img").css({ "display": "block", "visibility": "hidden" });
    }

    function tawShowHighscaleContainer() {
        tawInitWindowResizeHandler();
        $("#ycHighscaleImageContainer, #ycHighscaleImageContainer img").css({ "display": "block", "visibility": "visible", "opacity": "1" });
        $("#ycHighscaleImageContainer").hide().fadeIn(250);
        $("#ycHighscaleMaxZoomCorner").attr("src", window.url_maxzoomcorner_green);
        $("#ycHighscaleMaxZoomCorner").css({ "position": "absolute", "right": "0" });

        if (window.is_ie6 == true) $("select").hide();
    }



    // ****************************************************************************************************************************	
    // ****************************************************************************************************************************

    function tawAddHighscaleEventHandlers() {
        tawRemoveHighscaleEventHandlers();

        if (window.is_ie6 == true) $("select").show();

        $(".ycHighscaleImageClose, #ycHighscaleImage").click(function() {
            if (window.is_ie6 == true) $("select").show();
            $("#ycHighscaleImageContainer").fadeOut(250);
        });

        $("#show_highscaled, .jqzoom").click(function() {
            tawShowHighscaleContainer();
            $("#ycHighscaleImageContainer").fadeIn(250);
        });

        $("#ycHighscaleMaxZoomCorner").click(function() {

            if ($(this).attr("src") == window.url_maxzoomcorner_green) {
                $(this).attr("src", window.url_maxzoomcorner_red);

                tawUnbindWindowResizeHandler();
                tawResetHighscaleImageSize();

                var tmp_left = Math.round((($(window).width() - $("#ycHighscaleImage").width()) / 2));
                if (tmp_left < 0) tmp_left = 0;

                $("#ycHighscaleImageContainer").css({ "position": "absolute", "left": tmp_left + "px" })
                window.scrollTo(0, 0)

            } else {
                $(this).attr("src", window.url_maxzoomcorner_green);
                tawInitWindowResizeHandler();
                resizeHighscaleImage();
            }
        });
    }





    // ****************************************************************************************************************************	
    // ****************************************************************************************************************************


    function tawInitWindowResizeHandler() {
        var resizeTimer = null;
        $(window).bind('resize', function() {
            if (resizeTimer) clearTimeout(resizeTimer);
            resizeTimer = setTimeout(resizeHighscaleImage, 100);
        });
    }

    function tawUnbindWindowResizeHandler() {
        $(window).unbind('resize');
    }


    // ****************************************************************************************************************************
    // ****************************************************************************************************************************

    tawInitWindowResizeHandler();
    tawLoadMediumSizeImage();
    tawLoadHighscaleImage();


    // ****************************************************************************************************************************
    // ****************************************************************************************************************************
    // ****************************************************************************************************************************
    // ****************************************************************************************************************************	


    /*---------------*/
    /* Detail-Thumbs */
    /*---------------*/

    $("#productPreviewThumbs a").click(function() {
        $(".jqzoom img").attr("src", $(this).attr("href"));
        $(".jqzoom img").attr("jqimg", $(this).attr("rel"));
        $("#ycHighscaleImage").attr("src", $(this).attr("rel"));

        el = this;

        $(".jqzoom").tawPreloadImages({
            "css_background_class": "tawImagePreloaderContainer",
            "css_background_class_on_error": "tawImagePreloaderContainerOnError",
            "image_fade_in_time": 250,
            "substractPadding": true
        }, function() {
            $("#productPreviewThumbs a").removeClass("selectedThumbnail");
            $(el).addClass("selectedThumbnail");
            tawLoadHighscaleImage();
        });
        return false;
    });


    // ***************************************************************************************************
    // ***************************************************************************************************
    // ***************************************************************************************************
    // ***************************************************************************************************
    /*---------------------*/
    /* CATALOGUE MAGNIFIER */
    /*---------------------*/


    $(".catalogueMagnifier").tawImagePreview({
        "targetContainerId": "magnifierPopup",
        "targetImageContainerId": "magnifierPopupImage",
        "targetImageContainerErrorClass": "magnifierPopupImageOnError",
        "targetImageContainerErrorMessage": "No Image available<br /><br />Kein Bild vorhanden",
        "targetDescriptionId": "magnifierPopupSizes",
        "max_width": "300px",
        "max_height": "250px",
        "offsetRight": "25px",
        "offsetLeft": "155px",
        "offsetTop": "18px",
        "offsetBottom": "18px",
        "float": "dynamic",
        "substractCssSize": false,
        "delay": 250
    });






	//
	//	SocialBookmarks
	//	
	
	var sbDelay;
	var sbBoxDelay;
	
	$(".socialbookmark .sbBar").mouseover(function(){
		sbDelay = setTimeout(function(){ $(".socialbookmark .sbBookings").show(); }, 300);		
	}).mouseout(function(){
		clearTimeout(sbDelay);
	});
	
	$(".socialbookmark .sbBookings").mouseover(function(){	
		clearTimeout(sbBoxDelay);
	}).mouseout(function(){
		sbBoxDelay = setTimeout(function(){ $(".socialbookmark .sbBookings").fadeOut(300); }, 500);	
	});
	
	$(".socialbookmark .sbClose").click(function(){
		$(".socialbookmark .sbBookings").hide();
	});


	window.meta_keywords = encodeURIComponent($("meta[http-equiv=keywords]").attr("content"));
	window.meta_description = encodeURIComponent($("meta[http-equiv=description]").attr("content"));

	function trim (str) {
		return str.replace (/^\s+/, '').replace (/\s+$/, '');
	}


});
