﻿$(function () {
    $("body").addClass("jquery");

    // Main navigation styling (overlapping)
    var itemLi = 1;
    $("ul#nav li").each(function () {
        itemLi++;
        var itemLiIndex = 20 - itemLi;
        $(this).css("z-index", itemLiIndex.toString());
    });

    // Footer Directory equal heights
    $("#footer ul#directory > li").equalHeights();

    // Sticky footer styling
    var footerHeight = $("#footer").height();
    var mainPaddingBottom = footerHeight;
    var wrapperMarginBottomCalc = footerHeight + 55;
    var wrapperMarginBottom = -wrapperMarginBottomCalc;
    $("#main").css("paddingBottom", mainPaddingBottom);
    $("#wrapper").css({ marginBottom: wrapperMarginBottom, minHeight: '100%' });

    // Quick Find - collapsing checkbox lists
    $("#quick-find fieldset.expandable legend").click(function () {
        if ($(this).parent().hasClass("open")) {
            $(this).parent().removeClass("open");
        }
        else {
            $(this).parent().addClass("open");
        }
    });
    $("#quick-find fieldset input:checked").parent().parent().parent().addClass("open");

    // Actions - Back to Top link
    $(".top a").click(function () {
        var linkDestination = $(this).attr("href");
        $('html, body').animate({
            scrollTop: $(linkDestination).offset().top
        }, 1000);
        return false;
    });

    // Products hover pop up
    $('#dock .items .item').mouseover(function (e) {
        if ($('body').hasClass('milks')) {
            var popupHeading = $(this).children('h2').html();
        }
        else {
            var popupHeading = $(this).children('h3').html();
        }
        var popupContent = $(this).children('.details').html();
        var popupOffset = $(this).offset();
        var popupOffsetY = popupOffset.top - 110;
        var popupOffsetX = popupOffset.left + 40;
        if (popupContent != null) {
            $('body').append('<div id="dock-popup"><h3>' + popupHeading + '</h3>' + popupContent + '<div class="tip"></div></div>');
        }
        $('#dock-popup').css('top', popupOffsetY);
        $('#dock-popup').css('left', popupOffsetX);
        var popupHeight = $('#dock-popup').height();
        var popupTipOffsetY = popupHeight + 10;
        $('#dock-popup .tip').css('top', popupTipOffsetY);
        $('#dock-popup').fadeIn('slow');
    }).mouseout(function () {
        $('body').children('#dock-popup').fadeOut().remove();
    });

    // Browser discrepancies
    if ($.browser.msie) {
        $(".content-panel#products-listing span.paging ul li:last-child").css("border-right", "0");
    }

    // FAQ's Show/Hide
    $("dl.faq dd").hide();
    $("dl.faq dt").css({ "cursor": "pointer" }).click(function () {
        $(this).next("dd").toggle();
    });

    // Ask a question modal
    $(".contact-us-panel ul.options a").click(function () {
        var path = $(this).attr('href');
        var name = $(this).children("strong").html();
        if (path.indexOf("#") > -1) {
            var pathAnchor = path.substring(path.indexOf("#"));
            $.colorbox({
                iframe: true,
                href: '/ContactUsModal.aspx' + pathAnchor,
                title: 'Ask ' + name,
                opacity: '0.6',
                height: '640px',
                width: '450px',
                onOpen: function () {
                    $("#cboxTitle").css("width", "430px");
                },
                onComplete: function () {
                    $(".msg-error").watch("display,visibility", function () {
                        $.colorbox.resize();
                    });
                },
                onClosed: function () {
                    $(".msg-error").hide();
                }
            });
        }
        return false;
    });

    $(".content-panel#photo-competition ul#entries li a.thumbnail").colorbox({
        maxWidth: "500px",
        maxHegith: "500px",
        scalePhotos: true,
        onOpen: function () {
            $("#colorbox").addClass("photo-competition");
        }
    });
});

window.scrollTo = function (x, y) {
    return true;
}

function getCookie(cookieNameToCollect) {
    var allCookies = document.cookie.split(";");
    var tempCookie;
    var cookieName = '';
    var cookieValue = '';
    for (var i = 0; i < allCookies.length; i++) {
        tempCookie = allCookies[i].split("=");
        cookieName = tempCookie[0].replace(/^\s+|\s+$/g, '');
        if (cookieNameToCollect == cookieName) {
            cookieValue = tempCookie[1].replace(/^\s+|\s+$/g, '');
        }
    }
    return cookieValue;
}

function submitOnEnter(e, button) {
    var k = (window.event) ? e.keyCode : e.which;
    if (k != 13) return true;

    var o = document.getElementById(button);
    if (o != null) {
        if (o.tagName == "A") {
            eval(o.href.replace("javascript:", "").replace(/%20/g," "));
            return false;
        }

        if (o.tagName == "INPUT") {
            if (o.click) o.click();
            if (o.onClick) o.onClick();
            return false;
        }
    }
}





