/*-------------------------------------------------------------------------------------	
	*	@copyright:_________2009, Trost GmbH und Co. KG
	*	@link:______________http://www.trost.de
	*	@author:____________Jung v. Matt/Neckar
	*	@function:__________custom functions
	*	@version:___________1.2.6
	* 	@revision:__________$Revision: 13 $
	* 	@lastmodified:______$Date: 2009-03-11 12:03:06 +0100 (Mi, 11 Mrz 2009) $
	*
-------------------------------------------------------------------------------------*/
/*------------------------------------------------
	*	Adding class to html-tag to hide sections 
		on page load without flickering
--------------------------------------------------*/
document.documentElement.className += " tr_JsLoad";

/*------------------------------------------------
	*	Browser sniffing
--------------------------------------------------*/
// Internet Explorer sniffing
var iESniff = jQuery.browser.msie;
var iEVersionSniff = jQuery.browser.version;
// IE6
if(iESniff && iEVersionSniff < 7)var iE6Sniff = true;

/*------------------------------------------------
	*	Fire all Functions which need to be 
		executed or initialized if DOM is loaded
--------------------------------------------------*/
jQuery(function(jQuery) {
				
	// Toggle form value text
	customEffects.tgFmVal();
	
	// Header combo box flyout
	customEffects.hdCoFlyOut();
	
	// Main navigation
	customEffects.mnFlyOut();
	
	// Sub navigation
	customEffects.snFlyOut();
	
	// Target group flyout
	customEffects.tgFlyOut();
	
	// Filter flyout
	customEffects.flFlyOut();
	
	// CSS workarounds
	customEffects.cssStyling();
	
	// Accordion
	jQuery('.accCont, .accNewsCont').simpleAccordion();
	
	// Image carousel
	jQuery("#trImgWheelCont").jCarouselLite({
		auto: 1000,
		speed: 1000,
		scroll: 1,
		easing: "easeOutQuad",
		btnNext: ".jBtnNext",
		btnPrev: ".jBtnPrev"
	});
	
	// Image galery
	if(jQuery(".elBoxSp #trImgGal").length > 0){
		jQuery("#trImgGalCont").jCarouselLiteGal({
			speed: 1000,
			scroll: 1,
			easing: "easeOutQuad",
			btnNext: ".jBtnNext",
			btnPrev: ".jBtnPrev",
			visible: 6
		});
	}
	else {
		jQuery("#trImgGalCont").jCarouselLiteGal({
			speed: 1000,
			scroll: 1,
			easing: "easeOutQuad",
			btnNext: ".jBtnNext",
			btnPrev: ".jBtnPrev",
			visible: 4
		});
	}
	
	// Select box styled
	if(jQuery(".fmSel").length) {
		jQuery(".fmSel").selectbox();
	}
	
	// Checkbox/Radio styled
	if(jQuery(".fmCheckBox" || ".fmRadio").length) {
		jQuery('input[type=checkbox],input[type=radio]').prettyCheckboxes();
	}
	
	// File upload field styled
	if(jQuery(".fmFile").length) {
		jQuery(".fmFile").filestyle({ 
			imageheight : 22,
			imagewidth : 92,
			width : 430
		});
	}
	
	// Resize form text area
	customEffects.fmAreaResize();
	
	// Sitemap equal columns
	$('#smSrd').equalHeights();
	
	// Load event again for delayed loading
	jQuery(function(jQuery) {
		// Automatic content height if page is smaller than viewport BETA (needs to be testet!!)
		if(!iESniff){ /* just for good browsers which can handle the onresize event correct. ie sucks... */
			customEffects.ctAutoHeight();
			onTotalPageLoad.pageResize();
		}
		
		// CSS styling on total page load
		onTotalPageLoad.cssStyling();
	});
	
	//if(iE6Sniff){
		//$(".selectbox-wrapper").prepend("<!--[if lte IE 6.5]><table><tr><td><iframe></iframe><![endif]-->");
		//$(".selectbox-wrapper").append("<!--[if lte IE 6]></td></tr></table><![endif]-->");
		//$(".ieIframe").append("<!--[if lte IE 6.5]><iframe></iframe><![endif]-->");
	//}
	
});


/*------------------------------------------------
	*	custom page effects
--------------------------------------------------*/
var customEffects = {

    /*--------------------------------------------------------------------------------*/
    /*-----|| Toggle Text in input fields on focus  ||--------------------------------*/
    /*--------------------------------------------------------------------------------*/
    tgFmVal: function() {
        // Function call if the text search field has been clicked (focused)
        var defValues = new Array();
        jQuery("input.fmTxtTg").focus(function() {
            if (!defValues[this.id]) {
                defValues[this.id] = this.value;
            }
            if (this.value == defValues[this.id]) {
                this.value = '';
            }
            jQuery(this).blur(function() {
                if (this.value == '') {
                    this.value = defValues[this.id];
                }
            });
        });

    },

    /*--------------------------------------------------------------------------------*/
    /*-----|| Header combo box flyout  ||---------------------------------------------*/
    /*--------------------------------------------------------------------------------*/
    hdCoFlyOut: function() {

        var el, elFo, getBgPosLeft;

        jQuery("#ptLnLogin, #coLnChange").bind("click", function() {
            el = jQuery(this);
            elFo = el.next();

            if (el.attr("id") == "ptLnLogin") {
                getBgPosLeft = -322;
            } else {
                getBgPosLeft = -397;
            }

            // Call global closing if doc is clicked
            hdCoFlyOutClose();

            if (jQuery(".hdComboFo:visible").size() === 0) { // if no flyout is open
                elFo.addClass("foActive").slideDown("fast");
                el.css({ backgroundColor: "#62696f", color: "#FFF", backgroundPosition: getBgPosLeft + "px -44px" });
            } else { // if flyout is already open
                if (elFo.hasClass("foActive")) { // if clicked flyout is open
                    elFo.removeClass("foActive").slideUp("fast", function() {
                        el.css({ backgroundColor: "transparent", color: "#333", backgroundPosition: getBgPosLeft + "px 0px" });
                    });
                } else { // if other flyout is open
                    jQuery(".hdComboFo:visible").removeClass("foActive").slideUp("fast", function() {
                        if (el.attr("id") == "ptLnLogin") {
                            jQuery(".hdComboFo").prev().css({ backgroundColor: "transparent", color: "#333", backgroundPosition: -397 + "px 0px" });
                        } else {
                            jQuery(".hdComboFo").prev().css({ backgroundColor: "transparent", color: "#333", backgroundPosition: -322 + "px 0px" });
                        }
                        elFo.addClass("foActive").slideDown("fast");
                        el.css({ backgroundColor: "#62696f", color: "#FFF", backgroundPosition: getBgPosLeft + "px -44px" });
                    });
                }
            }

            return false;

        });

        // Hide FlyOut if clicked outside
        function hdCoFlyOutClose() {
            jQuery(document).bind("click", function(e) {
                var jQueryclicked = jQuery(e.target); // get the element clicked
                if (!(jQueryclicked.is(".hdComboFo") || jQueryclicked.prev().is("a"))) {
                    elFo.removeClass("foActive").slideUp("fast", function() {
                        el.css({ backgroundColor: "transparent", color: "#333", backgroundPosition: getBgPosLeft + "px 0px" });
                    });
                    jQuery(document).unbind("click");
                }
            });
        }

    },

    /*--------------------------------------------------------------------------------*/
    /*-----|| Main navigation ||------------------------------------------------------*/
    /*--------------------------------------------------------------------------------*/
    mnFlyOut: function() {
        var elId, delayOnLv0, delayOffLv0;

        // First of all, disable the fallback css menu behaviour
        var mnFB = jQuery("#mnFb");
        if (mnFB) {
            mnFB.attr("rel", "alternate stylesheet");
            mnFB.attr("disabled", "disabled");
        }

        // Just for bugfixing IE6 when subnav lays over select box
        if (iE6Sniff) {
            //jQuery("#mnCont .mnFoCont").append("<!--[if lt IE 7]><iframe></iframe><![endif]-->");
        }

        // Here goes the mouseover event
        jQuery("#mnLv0 > li.lv0").bind("mouseenter focus", function() {
            var el = jQuery(this);

            elId = el.attr("id");

            // Gets values for repositioning the menu if screen is smaller
            var lnXPos = jQuery(this).offset(); // X-pos of link relative to left of page
            //var dcWidth = 976;
            var wdWidth = jQuery(window).width(); // Width of window
            var elWidth = 602 + lnXPos.left;

            // If the window is bigger than the width of the subnav
            if (wdWidth < elWidth) {
                el.find("a.lv0").next().css({ left: wdWidth - elWidth + "px" })
            }

            // Init AJAX function to give additional features to each sub item
            customEffects.ajaxFlyOutCall(elId);

            delayOnLv0 = setTimeout(function() {
                if (el.find("a.lv0").next().length) { // if flyout exists
                    el.find("a.lv0").stop().fadeTo("fast", 0.0, function() {
                        el.find("a.lv0").next().stop().slideDown("normal", "easeOutQuad");
                        el.css({ zIndex: 850 });
                    });
                } else {
                    el.find("a.lv0").stop().fadeTo("fast", 0.0);
                }
            }, 200);

        })
        // Hover out, blur mainnav
		.bind("mouseleave", function() {
		    var el = jQuery(this);

		    clearTimeout(delayOnLv0);

		    if (el.find("a.lv0").next().length) { // if flyout exists
		        el.find("a.lv0").next().slideUp("fast", function() {
		            el.find("a.lv0").stop().fadeTo("fast", 1.0);
		            el.css({ zIndex: 800 });
		        });
		    } else {
		        el.find("a.lv0").stop().fadeTo("fast", 1.0);
		    }
		});
    },

    /*--------------------------------------------------------------------------------*/
    /*-----|| Main navigation AJAX call ||--------------------------------------------*/
    /*--------------------------------------------------------------------------------*/
    ajaxFlyOutCall: function(elId) {
        var delayOn;

        jQuery("#" + elId + "").find("a.lv2").bind("mouseenter focus", function() {

            var el = jQuery(this);
            var elSubId = el.attr("id");

            if (elSubId) { // if flayout nav item has an ID the additional information gets visible

                delayOn = setTimeout(function() {

                    var strTab;
                    var strImgTab;
                    var elArray = elSubId.split("_");
                    if (elArray.length > 1) {
                        strTab = elArray[0];
                        strImgTab = elArray[1];
                    } 
                    else {
                        strTab = elArray[0];
                        strImgTab = strTab;
                    }

                    // Fade in loading ani
                    el.append(jQuery("#" + elId + "").find(".mnDetLoader"));
                    jQuery("#" + elId + "").find(".mnDetLoader").fadeIn("normal", function() {
                        jQuery.post("/Desktopmodules/Trost.CSSMenu/Callback.aspx", { tab: strTab, imgId: strImgTab }, function(data) {

                            // if you get response, do something
                            if (data.length > 0) {

                                if (data == "NO_DETAIL") {
                                    jQuery("#" + elId + "").find(".mnDetLoader").hide();
                                    return false;
                                }

                                // Fade out loading ani
                                jQuery("#" + elId + "").find(".mnDetLoader").fadeOut("normal", function() {
                                    jQuery("#" + elId + "").find(".mnFoDetCont").empty();
                                    // insert whole data
                                    jQuery("#" + elId + "").find(".mnFoDetCont").append(data);

                                    // Call it just one time
                                    if (!jQuery("#" + elId + "").find(".mnFoDet").hasClass("lv2Active")) {
                                        // add class of active state
                                        jQuery("#" + elId + "").find(".mnFoDet").addClass("lv2Active");

                                        jQuery("#" + elId + "").find(".mnFoCont").animate({
                                            width: "600px"
                                        }, "fast", function() {
                                            // Fade in detail
                                            jQuery("#" + elId + "").find(".mnFoDet").show();
                                        });
                                    }

                                    jQuery("#" + elId + "").find(".mnFoDetCont").show();

                                });
                            }
                        });
                    });
                }, 1000);

            }

        })
        // Hover out, blur
			.bind("mouseleave", function() {
			    clearTimeout(delayOn);
			});
    },


    /*--------------------------------------------------------------------------------*/
    /*-----|| Sub navigation ||------------------------------------------------------*/
    /*--------------------------------------------------------------------------------*/
    snFlyOut: function() {

        // Inserts a-tag to enable keyboard-functionality
        jQuery("#snLv0 h2").each(function(i) {
            //if(!jQuery("#snLv0 h2:has(.snLv0)").length()){
            if (!jQuery(this).children().hasClass("snLv0")) {
                jQuery(this).wrapInner("<a href='#'></a>");
            }
        });

        // Removes border on last item
        jQuery(".snLv1").each(function(i) {
            jQuery(this).find("li:last").css({ borderBottom: "none" });
        });

        // Show/hide menu box in subnavigation on click
        jQuery("#snLv0 h2 > a:not('.snLv0')").bind("click", function() {
            // Switches classname to change bg-pos
            jQuery(this).parent().toggleClass("current");

            // Toggles slider
            jQuery(this).parent().next(".snLv1").slideToggle("fast");

            return false;
        });
    },


    /*--------------------------------------------------------------------------------*/
    /*-----|| Target group flyout ||--------------------------------------------------*/
    /*--------------------------------------------------------------------------------*/
    tgFlyOut: function() {
        var el, elFo, delayOnLv0, delayOffLv0;
        jQuery("#mnTgCont").bind("mouseenter", function() {
            el = jQuery(this).find("#mnTgHd a");
            elFo = el.parent().next();
            delayOnLv0 = setTimeout(function() {
                el.stop().fadeTo("fast", 0.0, function() { elFo.stop().slideDown("normal"); }).addClass("tgActive");
            }, 200);
        }).bind("mouseleave", function() {
            el = jQuery(this).find("#mnTgHd a");
            elFo = el.parent().next();
            clearTimeout(delayOnLv0);
            elFo.slideUp("fast", function() { el.stop().fadeTo("fast", 1.0).removeClass("tgActive") });
        })
        jQuery("#mnTgHd a").bind("click", function() {
            return false;
        });
    },


    /*--------------------------------------------------------------------------------*/
    /*-----|| Filter flyout ||--------------------------------------------------*/
    /*--------------------------------------------------------------------------------*/
    flFlyOut: function() {
        var el, elFo, elFoHeight;
        var elFlyDown = false;

        jQuery("#lnFilter").bind("click", function() {
            el = jQuery(this);

            if (el.next().is("#lnFoFilter")) {
                elFo = el.next();
            } else {
                elFo = el.next().find("#lnFoFilter");
            }

            // Gets values for repositioning the flyout if screen is smaller
            var elHeight = el.outerHeight();
            var lnYPos = el.offset(); // Y-pos of link relative to left of page
            //lnYPos = lnYPos + elHeight;
            var scrollTop = jQuery(window).scrollTop(); // Vertical scroll position
            var wdHeight = jQuery(window).height(); // Height of window
            wdHeightTotal = wdHeight - scrollTop;
            var elFoHeight = elFo.outerHeight();
            var elFoHeightTotal = elFoHeight + lnYPos.top + elHeight;

            // If the window is bigger than the width of the subnav
            if (wdHeightTotal > elFoHeightTotal) {
                elFlyDown = true;
            }
            // Else get the subnav position by subtracting the real needed element height with the window height
            else {
                elFlyDown = false;
            }

            // Call global closing if doc is clicked
            flFlyOutClose(elFoHeight);


            if (!el.hasClass("flActive")) {
                el.addClass("flActive");
                el.css({ backgroundPosition: "0 -40px", color: "#FFF" });
                if (elFlyDown) { // If y-screen is high enough
                    elFo.slideDown("fast");
                } else {
                    //elFo.css({top:"-33px", height:"0px", display:"block", borderTop:"1px solid #D1D1D1", borderBottom:"2px solid #E64215", overflow:"hidden"});
                    elFo.wrap("<div id='lnFilterOut'></div>");
                    elFo.css({ position: "relative", top: elFoHeight + "px", display: "block", borderTop: "1px solid #D1D1D1", borderBottom: "2px solid #E64215" });
                    jQuery("#lnFilterOut").css({ height: elFoHeight + "px", top: -elFoHeight + "px" });

                    elFo.animate({
                        top: "-=" + (elFoHeight)
                    }, "fast");
                }
            } else {
                el.removeClass("flActive");
                if (elFlyDown) { // If y-screen is high enough
                    elFo.slideUp("fast", function() { el.css({ backgroundPosition: "0 0", color: "#666" }); });
                } else {
                    var cnt = jQuery("#lnFilterOut").contents();
                    elFo.animate({
                        top: "+=" + (elFoHeight)
                    }, "fast", function() { elFo.hide().css({ position: "absolute", top: "40px", display: "none", borderBottom: "1px solid #D1D1D1", borderTop: "2px solid #E64215" }); el.css({ backgroundPosition: "0 0", color: "#666" }).next().replaceWith(cnt) });
                }
            }

            return false;

        });

        // Hide FlyOut if clicked outside
        function flFlyOutClose(elFoHeight) {
            jQuery(document).bind("click", function(e) {
                var jQueryclicked = jQuery(e.target); // get the element clicked

                if (!(jQueryclicked.is("#lnFilter") || jQueryclicked.is("#lnFoFilter") || jQueryclicked.is("#lnFilterOut"))) {
                    el.removeClass("flActive");
                    if (elFlyDown) { // If y-screen is high enough
                        elFo.slideUp("fast", function() { el.css({ backgroundPosition: "0 0", color: "#666" }); });
                    } else {
                        var cnt = jQuery("#lnFilterOut").contents();
                        elFo.animate({
                            top: "+=" + (elFoHeight)
                        }, "fast", function() { elFo.hide().css({ position: "absolute", top: "40px", display: "none", borderBottom: "1px solid #D1D1D1", borderTop: "2px solid #E64215" }); el.css({ backgroundPosition: "0 0", color: "#666" }).next().replaceWith(cnt) });
                    }
                    jQuery(document).unbind("click");
                }

            });

        }
    },


    /*--------------------------------------------------------------------------------*/
    /*-----|| Content auto height BETA ||-------------------------------------------------------*/
    /*--------------------------------------------------------------------------------*/
    ctAutoHeight: function() {
        var wdHeight = jQuery(window).height(); // Height of viewport
        var dcHeight = jQuery("#dcCont").height(); // Height of document
        var hdHeight = jQuery("#hdCont").height(); // Height of header
        var mnHeight = jQuery("#mnCont").height(); // Height of mainnav
        var chHeight = jQuery("#chCont").height(); // Height of content header
        var ctOuterHeight = jQuery("#ctCont").outerHeight(); // Height of content
        var ctInnerHeight = jQuery("#ctCont").height(); // Height of content
        var ctHeight = jQuery("#ctCont .ct").height(); // Height of content
        var ftHeight = jQuery("#ftCont").height(); // Height of footer
        var ctTotalHeight = hdHeight + mnHeight + chHeight + ctHeight + (ctOuterHeight - ctInnerHeight) + ftHeight - 6;

        if (dcHeight < wdHeight) {
            var restHeight = wdHeight - dcHeight;
            jQuery("#ctCont").css({ height: ctInnerHeight + restHeight + "px" });
        } else if (dcHeight > wdHeight && ctTotalHeight < wdHeight) {
            var restHeight = dcHeight - wdHeight;
            jQuery("#ctCont").css({ height: ctInnerHeight - restHeight + "px" });
        } else {
            jQuery("#ctCont").css({ height: "auto" });
        }

    },

    /*--------------------------------------------------------------------------------*/
    /*-----|| Resize form text area ||-------------------------------------------------------*/
    /*--------------------------------------------------------------------------------*/
    fmAreaResize: function() {
        jQuery(".lnResize").bind("click focus", function() {
            var el = jQuery(this);
            var targetEl = el.parent().next().find(".fmArea");
            if (targetEl.height() <= 500) {
                targetEl.animate({
                    height: "+=100px"
                }, "fast");
            } else {
                el.css({ color: "#999999" });
            }
            return false;
        });

    },


    /*--------------------------------------------------------------------------------*/
    /*-----|| CSS workarounds ||-------------------------------------------------------*/
    /*--------------------------------------------------------------------------------*/
    cssStyling: function() {
        jQuery(".mnLv2").each(function(e) {
            jQuery(this).find("h3:first").css({ paddingTop: "0px", borderTop: "none" });
        });

        jQuery(".accNewsCont .accDdCont:last").css({ borderBottom: "none" });

        if (iE6Sniff) {
            jQuery(".fmBtn input").bind("mouseenter", function() {
                if (jQuery(this).parent().parent().hasClass("btnGrey")) {
                    jQuery(this).css({ backgroundPosition: "0 -100px", color: "#FFFFFF" });
                } else {
                    jQuery(this).css({ backgroundPosition: "0 -20px", color: "#FFFFFF" });
                }
            }).bind("mouseleave", function() {
                if (jQuery(this).parent().parent().hasClass("btnGrey")) {
                    jQuery(this).css({ backgroundPosition: "0 -80px", color: "#fce9e3" });
                } else {
                    jQuery(this).css({ backgroundPosition: "0 0", color: "#fce9e3" });
                }
            });
        }
    }
}

/*------------------------------------------------
	*	custom page effects
--------------------------------------------------*/
var onTotalPageLoad = {
	/*--------------------------------------------------------------------------------*/
	/*-----|| Resize browser ||-------------------------------------------------------*/
	/*--------------------------------------------------------------------------------*/
	pageResize:function () {
		var currheight; 
		window.onresize = function() {
			if(currheight != document.documentElement.clientHeight){
				customEffects.ctAutoHeight();
			}
		}
	},
	
	/*--------------------------------------------------------------------------------*/
	/*-----|| CSS workarounds ||-------------------------------------------------------*/
	/*--------------------------------------------------------------------------------*/
	cssStyling:function () {
		if(iE6Sniff){
			jQuery(".selectbox-wrapper ul li").bind("mouseenter", function(){
				jQuery(this).addClass("selBoxHover");
			}).bind("mouseleave", function(){
				jQuery(this).removeClass("selBoxHover");
			});
			
		}
	}
}
