﻿(function ($) {
    $.fn.getHiddenDimensions = function (boolOuter) {
        var $item = this;
        var props = { position: 'absolute', visibility: 'hidden', display: 'block' };
        var dim = { 'w': 0, 'h': 0 };
        var $hiddenParents = $item.parents().andSelf().not(':visible');

        var oldProps = [];
        $hiddenParents.each(function () {
            var old = {};

            for (var name in props) {
                old[name] = this.style[name];
                this.style[name] = props[name];
            }

            oldProps.push(old);
        });

        dim.w = (boolOuter === true) ? $item.outerWidth() : $item.width();
        dim.h = (boolOuter === true) ? $item.outerHeight() : $item.height();

        $hiddenParents.each(function (i) {
            var old = oldProps[i];
            for (var name in props) {
                this.style[name] = old[name];
            }
        });

        return dim;
    }
} (jQuery));

$(document).ready(function () {
    //breadcrumbs highlight last link
    $
    $('.mastBreadcrumbs a').eq($('.mastBreadcrumbs a').size() - 2).addClass('active');

    //form clear
    var $field = $('.search');
    var $textField = $('.search .textBox');
    $field.live('click', function () {
        $('.lbl', this).fadeOut(0);
        $('.textBox', this).focus();
    });
    $textField.live('blur', function () {
        if (this.value == "" || this.value == " ") {
            $(this).prev('.lbl').fadeIn(0);
        }
    });

    $textField.live('focus', function () {
        $(this).parent('.search').find('.lbl').fadeOut(0);
    });

    if ($textField.val()) {
        $('.lbl').fadeOut(0);
    };


    $topNavLi = $('.header li');

    var $dldd = $('.dealerList');
    var $dlLi = $('.mastHead li:first');
    $dldd.appendTo($dlLi);

    $dlLi.hover(function () {
        if ($.support.opacity) {
            $dldd.stop().fadeIn(300, function () { $(this).css('opacity', '1') });
        } else {
            $dldd.fadeIn(0);
        }
    }, function () {
        if ($.support.opacity) {
            $dldd.stop().fadeOut(300);
        } else {
            $dldd.fadeOut(0);
        }
    });

    generateShadows();

    function generateShadows() {
        $('.dropDown').each(function () {
            var curWidth = 0;
            $('.dropDownContainer > *', this).each(function () {
                var thisWidth = $(this).getHiddenDimensions(true);
                curWidth += thisWidth.w;
            });
            $('.dropDownContainer', this).css('width', curWidth + 15);
        });

        $('.dropDownContainer').each(function () {
            $(this).prepend('<div class="topNav_left"></div><div class="topNav_right"></div>').after('<div class="topNav_bottom_wrap group"><div class="topNav_bottomLeft"></div><div class="topNav_bottomRight"></div><div class="topNav_bottom"></div></div>').addClass('group');
            var curHeight = $(this).getHiddenDimensions(true);
            $('.topNav_left, .topNav_right', this).css('height', curHeight.h + 12);
            if ($.browser.msie && $.browser.version.substr(0, 1) <= 7) {
                $('.topNav_bottom').css({ 'width': $(this).css('width'), 'float': 'left' });
            }
        });
    };

    $topNavLi.hover(function () {
        if ($.support.opacity) {
            $('.dropDown', this).stop().fadeIn(300, function () { $(this).css('opacity', '1') });
        } else {
            $('.dropDown', this).fadeIn(0);
        }
    }, function () {
        if ($.support.opacity) {
            $('.dropDown', this).stop().fadeOut(300);
        } else {
            $('.dropDown', this).fadeOut(0);
        }
    });

    $('.printIcn').click(function () {
        window.print();
    });




    //Create onstate based on url
    var currentURL = window.location.href;
    var headerLinks = $('.header > ul > li');

    if (currentURL.indexOf('/Products') > -1) {
        headerLinks.eq(0).addClass('currentNode');
    }

    if (currentURL.indexOf('/SmartSecurity') > -1) {
        headerLinks.eq(1).addClass('currentNode');
    }

    if (currentURL.indexOf('/How-To-Choose') > -1) {
        headerLinks.eq(2).addClass('currentNode');
    }

    if (currentURL.indexOf('/Customer-Support') > -1) {
        headerLinks.eq(3).addClass('currentNode');
    }

    if (currentURL.indexOf('/Trade-Professionals') > -1) {
        headerLinks.eq(4).addClass('currentNode');
    }




});



