﻿(function ($) {
    // Extend jQuery with a method useful for obtaining an element's width and height.
    $.fn.getBounds = function (inner) {
        var $this = $(this);

        if (inner) {
            return { width: $this.width(), height: $this.height() };
        } else {
            return { width: $this.outerWidth(), height: $this.outerHeight() };
        }
    };
} (jQuery));

$(document).ready(function () {
    $("body").css("background-color", "#D8D8D8");
    $("html").css("background-color", "#D8D8D8");

    $('a.Clickable').click(function () {
        if ($(this).parent().attr('class') == '' && $(this).parent().find('ul').length > 0) {
            $('li.selected').find('ul').css({ 'display': 'none' }).parent().removeClass('selected');
            $(this).parent().find('ul').css({ 'display': 'block' });
            $(this).parent().addClass('selected');
        }
        else {
            if ($(this).parent().find('ul').length > 0) {
                $(this).parent().find('ul').css({ 'display': 'none' });
                $(this).parent().removeClass('selected');
            }
        }
    });

    $('a.imageLink').hover(function () {
        if ($(this).find('img').length = 2) {
            $(this).find('img:eq(0)').css({ 'display': 'none' });
            $(this).find('img:eq(1)').css({ 'display': 'block' });
        }

        var id = $(this).attr('id').split('_')[1];
        $('#DepartmentLink_' + id).addClass('selected');
    },
    function () {
        if ($(this).find('img').length = 2) {
            $(this).find('img:eq(0)').css({ 'display': 'block' });
            $(this).find('img:eq(1)').css({ 'display': 'none' });
        }

        var id = $(this).attr('id').split('_')[1];
        $('#DepartmentLink_' + id).removeClass('selected');
    });

    $('a#SketchImageLink').hover(function () {
        $('a#SketchFooterLink').css({ 'text-decoration': 'underline' });
    }, function () {
        $('a#SketchFooterLink').css({ 'text-decoration': 'none' });
    });

    if ($('#HeroImageMenu').length > 0) {
        $('.DepartmentMenu a').hover(function () {
            var id = $(this).attr('id').split('_')[1];
            if ($('#ImageLink_' + id).find('img').length = 2) {
                $('#ImageLink_' + id).find('img:eq(0)').css({ 'display': 'none' });
                $('#ImageLink_' + id).find('img:eq(1)').css({ 'display': 'block' });
            }
        }, function () {
            var id = $(this).attr('id').split('_')[1];
            if ($('#ImageLink_' + id).find('img').length = 2) {
                $('#ImageLink_' + id).find('img:eq(0)').css({ 'display': 'block' });
                $('#ImageLink_' + id).find('img:eq(1)').css({ 'display': 'none' });
            }
        });
    }

    $('#HeroImageLink').hover(function () {
        $('#HeroImageBodytext').stop(true, true).css({ 'display': 'block' })
    }, function () {
        $('#HeroImageBodytext').stop(true, true).css({ 'display': 'none' })
    });

    $("a.lightbox").lightBox();    
});

// 200 character textarea limits
(function () {
    function setLimits() {
        $('textarea.limit-maxlength').each(function () {
            var maxlength = $(this).attr('maxlength');

            if (typeof (maxlength) === 'string') {
                $(this).limit(maxlength);
            }
        });
    }

    Sys.Application.add_load(setLimits);
} ());

(function () {
    function noDblClick() {
        $(".no-dbl-click").click(function () {
            var $this = $(this);

            if ($this.attr("data-dbl-click-disabled") === "true") {
                return false;
            }

            $this.attr("data-dbl-click-disabled", "true");

            window.setTimeout(function() {
                $this.removeAttr("data-dbl-click-disabled");
            }, 5000);
        });
    }

    Sys.Application.add_load(noDblClick);
} ());

function SetMainImage(imageURL, mainImageID, selectedThumbnailID, largeImageWidth, LightBoxImageHeight, showLargeImage, lnkID) {
    var mainImage = document.getElementById(mainImageID);
    if (mainImage != null) {
        mainImage.src = imageURL + "&width=" + largeImageWidth;
        $('.ProductThumbnail').find('img.VeryDarkGreyBorder').addClass('MidGreyBorder').removeClass('VeryDarkGreyBorder');
        var selectedThumbnail = document.getElementById(selectedThumbnailID);
        if (selectedThumbnail != null) {
            selectedThumbnail.className = 'VeryDarkGreyBorder';
        }

        var popupLink = document.getElementById(lnkID);
        if (popupLink != null) {
            if (showLargeImage == 'True') {
                popupLink.href = imageURL + "&height=" + LightBoxImageHeight;
                popupLink.style.display = 'block';
            }
            else {
                popupLink.style.display = 'none';
            }
        }
    }
}

function HighlightErrors() {
    $("table span.Error").each(function () {
        $(this).parent().parent().find('th').addClass('Red');
    });
}

function getPageSize() {
    var xScroll, yScroll; if (window.innerHeight && window.scrollMaxY) { xScroll = window.innerWidth + window.scrollMaxX; yScroll = window.innerHeight + window.scrollMaxY; } else if (document.body.scrollHeight > document.body.offsetHeight) { xScroll = document.body.scrollWidth; yScroll = document.body.scrollHeight; } else { xScroll = document.body.offsetWidth; yScroll = document.body.offsetHeight; }
    var windowWidth, windowHeight; if (self.innerHeight) {
        if (document.documentElement.clientWidth) { windowWidth = document.documentElement.clientWidth; } else { windowWidth = self.innerWidth; }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { windowWidth = document.documentElement.clientWidth; windowHeight = document.documentElement.clientHeight; } else if (document.body) { windowWidth = document.body.clientWidth; windowHeight = document.body.clientHeight; }
    if (yScroll < windowHeight) { pageHeight = windowHeight; } else { pageHeight = yScroll; }
    if (xScroll < windowWidth) { pageWidth = xScroll; } else { pageWidth = windowWidth; }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight); return arrayPageSize;
};

function DoClick(buttonName, buttonId, e) {
    //the purpose of this function is to allow the enter key to 
    //point to the correct button to click.
    var key;
    if (window.event) {
        key = window.event.keyCode;     //IE
    }
    else {
        key = e.which;      //firefox
    }

    if (key == 13) {
        var ieVersion = getInternetExplorerVersion();
        //Get the button the user wants to have clicked
        var btn = document.getElementById(buttonName);
        if (btn != null) { //If we find the button click it
            if (ieVersion > -1) {
                btn.click();
                event.keyCode = 0
            }
            else {
                e.preventDefault();
                __doPostBack(buttonId, '');
                return false;
            }
        }
    }
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}


