// Namespace the functions to remove possibility of conflict.
var Peach = {

    /// <summary>
    /// Instantializes the cufon font replacement engine.
    /// </summary>
    initializeCufonFonts: function () {
        Cufon.replace('#header #logo a span', {
            color: '-linear-gradient(#FFFFFF, #777777)',
            textShadow: '#000000 1px 1px, #0000000 -1px -1px'
        });
        Cufon.replace('#slogan', {
            color: '-linear-gradient(#FFFFFF, #777777)'
        });

        // Tells Cufon that the DOM is ready and it's OK to start replacing stuff. Needed for Internet Explorer
        Cufon.now();
    },

    /// <summary>
    /// Adds the hover functionality to the menu.
    /// </summary>
    addMenuHover: function () {

        // Header style.
        jQuery("#mainNavigation ul li.topLevel,#mainNavigation ul li.hasChildren").each(function () {

            var $href = jQuery(this).children("a").attr("href");
            $href = $href.substring(0, $href.length - 5);
            if (location.href.indexOf($href) > 0) {

                jQuery(this).children("a").addClass("hover");

                // fix child border.
                jQuery(this).prev("li").children("a").addClass("noBorder");

            }

        });

        jQuery("#mainNavigation ul li.topLevel,#mainNavigation ul li.hasChildren").hover(
         function () {

             if (!jQuery(this).children("a").hasClass("selected")) {

                 // over
                 jQuery(this).children("a").addClass("hover");
                 // fix child border.
                 jQuery(this).prev("li").children("a").addClass("noBorder");
             }

             if (jQuery(this).hasClass("hasChildren")) {

                 jQuery(this).children("ul").slideDown("fast", "linear");
             }

         },
         function () {

             if (!jQuery(this).children("a").hasClass("selected")) {

                 // out
                 jQuery(this).children("a").removeClass("hover");
                 // fix child border.
                 jQuery(this).prev("li").children("a").removeClass("noBorder");

             }

             if (jQuery(this).hasClass("hasChildren")) {

                 jQuery(this).children("ul").slideUp("fast", "linear");
             }

         }
        );
    },

    /// <summary>
    /// Adds the hover functionality to the products.
    /// </summary>
    addProductHover: function () {

        if (this.browser.ie7) {

            jQuery("span.photoList, span.photo, span.photoCross").click(
          function () {
              window.location.replace(jQuery(this).parent("a").attr("href"));
          });
        }

        jQuery("span.photoList").hover(
         function () {
             // over
             jQuery(this).children("img").fadeTo("fast", 0.65);
             jQuery(this).find("span.link").addClass("linkOver");
             jQuery(this).find("h2").addClass("coloured");
         },
         function () {
             // out
             jQuery(this).children("img").fadeTo("fast", 1);
             jQuery(this).find("span.link").removeClass("linkOver");
             jQuery(this).find("h2").removeClass("coloured");
         }
        );

        jQuery("span.photo").hover(
         function () {
             // over
             jQuery(this).children("img").fadeTo("fast", 0.65);
             jQuery(this).find("span.link").addClass("linkOver");
             jQuery(this).find("strong").addClass("coloured");
         },
         function () {
             // out
             jQuery(this).children("img").fadeTo("fast", 1);
             jQuery(this).find("span.link").removeClass("linkOver");
             jQuery(this).find("strong").removeClass("coloured");
         }
        );

        jQuery("span.photoCross").hover(
         function () {
             // over
             jQuery(this).children("img").fadeTo("fast", 0.65);
             jQuery(this).find("strong").addClass("coloured");
         },
         function () {
             // out
             jQuery(this).children("img").fadeTo("fast", 1);
             jQuery(this).find("strong").removeClass("coloured");
         }
        );

        jQuery("#leftMenu a.whereToBuy").hover(
         function () {
             // over
             jQuery(this).fadeTo("fast", 0.65);
         },
         function () {
             // out
             jQuery(this).fadeTo("fast", 1);
         }
        );

        jQuery("span.photoThumbnail").hover(
         function () {
             // over
             var $image = jQuery(this).children("img").attr("src");
             var $index = $image.lastIndexOf("width=") + 6;
             $image = $image.substring(0, $index) + "280";

             jQuery(this).addClass("photoThumbnailHover");
             jQuery("div.productImage img.mainProductImage").attr("src", $image);
         },
         function () {
             // out
             jQuery(this).removeClass("photoThumbnailHover");
         }
        );
    },

    /// <summary>
    /// Adds the 'first-child' css class to the first child of the left menu to allow ie6
    /// to correctly apply the correct css properties.
    /// </summary>
    addFirstChildSelector: function () {

        if (this.browser.ie6) {
            jQuery("#leftMenu ul li:first-child").addClass("first-child");
            jQuery("#contactBodyRight h2:first:child").addClass("first-child");
        }

    },

    /// <summary>
    /// Corrects some minor layout issues in the form control.
    /// </summary>
    fixFormLayout: function () {

        // Hide the form error message if there is no form to render.
        if (jQuery(".contourErrorMessage").text() === "Form could not be loaded, form is either missing or damaged") {
            jQuery(".contourErrorMessage").hide();
        }

        // Add the required indicator key next to the submit button.
        var $requiredKey = "<label class=\"fieldLabel\"><span class=\"contourIndicator\">*</span> Required Fields</label>";

        jQuery("#columnWrapper #contour .contourNavigation").prepend($requiredKey);

        // remove the empty 'p' generated by having an empty title.
        jQuery("#columnWrapper #contactBody p:first-child").remove();

        // Change the button text on the email signup form
        jQuery("#footerRight .emailSignupWrapper #contour .contourNavigation input.contourButton").attr("value", "Sign up");

        // Add hover effect to buttons.
        jQuery("#contour .contourButton").hover(
            function () {
                // over
                jQuery(this).addClass("contourButtonHover");
            },
        function () {
            // out
            jQuery(this).removeClass("contourButtonHover");
        }
        );
    },

    fixProductListLayout: function () {
        this.equalizeHeight("#productListWrapper a");
    },

    /// <summary>
    /// Equalizes the height of a series of elements matching the given selector.
    /// <param name="selector" type ="String">
    /// A selector to be parsed by jQuery.
    /// </param>
    /// </summary>
    equalizeHeight: function (selector) {
        var tallest = 0, height, $selector = jQuery(selector);

        $selector.each(function () {
            var $this = jQuery(this);

            // Check to see if there is an inline height as jQuery().height() 
            // is reporting different values in different browsers.
            if (this.style.height) {

                height = Math.max(this.style.height.split("px")[0], $this.height());

                // Remove the inline style so any dynamically added text does not overflow.
                $this.css("height", "");

            } else {
                height = $this.height();
            }

            // We're rounding here as Firefox adds small percentage to the height.
            tallest = Math.round(Math.max(tallest, height));
        });

        Peach.browser.ie6 ? $selector.css("height", tallest) : $selector.css("min-height", tallest);

//        $selector.each(function () {

//            var $height = jQuery(this).height();

//            if ($height > $tallest) {

//                $tallest = $height;
//            }
//        });

//        $selector.height($tallest);
    },

    /// <summary>
    /// Fixes the slogan top-margin if Osmo Ikon logo is displayed
    /// </summary>
    fixOsmoIkonSlogan: function () {
        if (jQuery("#banner #SubSlogan").html() !== null) {
            jQuery("#slogan").css("paddingTop", "35px");
        }
    },

    /// <summary>
    /// Applies events for Where To Buy
    /// </summary>
    whereToBuyEvents: function () {
        $("#whereToBuyContainer").tabs();


        var $offset = 0;

        jQuery("ul.ListStores li, ul.ListDistributors li").replaceText(/ ,/gi, ",<br/>");

        jQuery("div.StoresAndDistributorsContainer h2 a").click(function () {

        var $this = jQuery(this);

            if (!$this.parent().next(".StoresAndDistributors").is(":visible")) {

                jQuery("div.StoresAndDistributors").slideUp("fast");

                $this.parent().prevAll("div.StoresAndDistributorsContainer h2").each(function () {
                var $that = jQuery(this);
                    if(Peach.browser.ie6 || Peach.browser.ie7 ||Peach.browser.ie8){
                        $offset += ($that.outerHeight(true) - 5);
                    }
                    else{
                        $offset += $that.outerHeight(true);
                    }

                });

                // Scroll to that anchor by setting the body to scroll to the anchor top.
                $("html, body").animate({ scrollTop: $this.closest("div.StoresAndDistributorsContainer").offset().top + $offset }, "fast");
                
                $offset = 0;

                jQuery(this).parent().next("div.StoresAndDistributors").slideDown("fast");

                Peach.equalizeHeight("ul.ListStores li, ul.ListDistributors li");
                
            }
            else {
                jQuery("div.StoresAndDistributors").slideUp("fast");
            }
        });
    },

    emailFormOnBlurEvents: function () {

        var $input = jQuery(".emailsignup input.text");

        $input.attr("title", "enter email");

        $input.focus(function () {

            if ($input.val() === $input.attr("title")) {

                $input.val("");
            }

        });

        $input.blur(function () {
            if ($input.val() === "") {
                $input.val($input.attr("title"));
            }
        });

        $input.blur();

    },

    addFaceBookMetaTags: function () {

        if (fbActive) {
            var meta = jQuery("#fbHiddenInput").val();
            jQuery("head").append("<meta property=\"og:image\" content=\"" + meta + "\" />");
        }

    },

        faqEvents: function(){  
    
        var $offset = 0;
    

        jQuery("#faqWrapper h2").click(function () {

            if (!jQuery(this).next(".questionList").is(":visible")) {

                jQuery(".questionList").slideUp("fast");

                jQuery(this).parent().prevAll("h2").each(function () {

                    var $that = jQuery(this);

                    if(Peach.browser.ie6 || Peach.browser.ie7 ||Peach.browser.ie8){
                        $offset += ($that.outerHeight(true) - 5);
                    }
                    else{
                        $offset += $that.outerHeight(true);
                    }

                });
              
                // Scroll to that anchor by setting the body to scroll to the anchor top.
                $("html, body").animate({ scrollTop: jQuery(this).closest("#faqWrapper").offset().top + $offset }, "fast");
                
                $offset = 0;

                jQuery(this).next(".questionList").slideDown("fast");
                
            }
            else {
                jQuery(".questionList").slideUp("fast");
            }
        });

    },


    browser: {
        /// <summary>
        ///     The peach browser object provides information about the current browser.
        ///     This utilizes the jQuery.support object which does not use browser UA sniffing.
        /// </summary>

        ie6: !jQuery.support.leadingWhitespace && !jQuery.support.boxModel && !window.XMLHttpRequest,

        ie7: !jQuery.support.leadingWhitespace && !jQuery.support.boxModel && !jQuery.support.hrefNormalized,

        ie8: !jQuery.support.leadingWhitespace && jQuery.support.hrefNormalized,

        webkit: !jQuery.support.checkOn
    }
};

/// <summary>
/// Executes the functions when the DOM is fully loaded.
/// </summary>
jQuery(document).ready(function () {
    //Peach.addFaceBookMetaTags();
    Peach.initializeCufonFonts();
    Peach.addMenuHover();
    Peach.addProductHover();
    Peach.fixFormLayout();
    Peach.fixOsmoIkonSlogan();
    Peach.whereToBuyEvents();
    Peach.emailFormOnBlurEvents();
    Peach.faqEvents();
});
/// <summary>
/// Executes the functions when the DOM is fully loaded.
/// </summary>
jQuery(window).load(function () {
    Peach.fixProductListLayout();
});

