/* Universal JS */
var $body;
$(document).ready(function() {
    $('#logo-servicenet').click(function() {
        window.location = $(this).children('a').attr('href');
    });

    if($('#product-parts').length) {
        var $docCon = $("#documentsContainer");
        var docConH = 0;
 
        $docCon.children().each(function() {
	  	docConH += $(this).outerHeight();
	  });
	  if(docConH > 100) {
		$docCon.find('a.emailDownload').css('margin-right', $.browser.msie && $.browser.version < 7 ? '5px' : '0');
            $docCon.jScrollPane({ showArrows: true });
        }
    }

    $body = $('body');

    $body.removeClass('noJS');

    $('nav ul ul li:last-child').addClass('last');

    if ($.browser.msie) {
        $("#nav > ul > li").hover(function() {
            $(this).find("ul").css("display", "block");
        }
        , function() {
            $(this).find("ul").css("display", "none");
        });
        if ($.browser.version < 7) {
            $(window).load(function() {
                $("#nav > ul").css('background', 'none');
            });
        }
    }

    $('input[type="text"], input[type="password"]').live('keydown', function(e) {
        var $this = $(this);
	    if (e.keyCode === 13 && !$this.closest('#tableBMD').length) {
            $this.closest('form').submit();
        }
    });

    if ($.browser.msie && $.browser.version < 8) {
        $('a.searchResultsProductList-sml, a.productbox-sml').live('click', function() {
            window.location = $(this).attr('href');
        });
    }

    if ($('#OptionSelectBrand').length) {
        $('#OptionSelectBrand option.' + $body.attr('class')).attr('selected', 'selected');
        $('#OptionSelectBrand').change();
    }

    if($('td.currency-amt').length) {
        $('td.currency-amt').each(function() {
	  	var $this = $(this);
		$this.css('text-align', 'right');

		var padding = $this.width() < 70 ? '0' : '20%';

		$this.html('<span style="display: inline-block; padding-right: '+padding+';">'+$this.text()+'</span>');
        });
    }

    pdpsJS[$body.attr('id')]();
});

/* Page-Specific JS */
var pdpsJS = {
    'cchome': function() {
        prodHiTabs();
    },
    'qobhome': function() {
        prodHiTabs();
    },
    'pdad': function() {
        var $productParts = $('#product-parts');
        $productParts.add('#parts-order-management').tabs();
        //$('table tr:nth-child(even)').addClass('even');

        if ($.browser.msie && $.browser.version < 8) {
            var $thead = $('tr.thead', $productParts);
            $thead.each(function() {
                var $this = $(this);

                $('th:first', this).addClass('first');
                $('th:last', this).addClass('last');
            });
        }
    },
    'checkout': function() {
    }
};

var $prodHi, $prodHiTabs;
function prodHiTabs() {
    $prodHi = $('#product-highlights');
    $prodHiTabs = $('ul li', $prodHi);
    if ($prodHiTabs.length < 5) {
        $prodHiTabs.width(660/$prodHiTabs.length);
    }

    var $phTabs = $('#product-highlights a');
    $prodHi.tabs({
        select: function(event, ui) {
            $phTabs.filter('.ui-state-prev').removeClass('ui-state-prev');
            $phTabs.eq($phTabs.index(ui.tab) - 1).addClass('ui-state-prev');
        }
    });
}

$(document).ready(function() {
    if($('#find-number-brands').length) {
        $('#find-number-brands li').click(function() {
            var $this = $(this);
            
            $this.siblings().css('text-decoration','none');
            $this.css('text-decoration','underline');
            
            var brand = $this.attr('id').split('find-number-')[1];
            
            $('article.number-info').addClass('conceal');
            $('#number-info-'+brand).removeClass('conceal');
        });
	  var brandAssoc = {
	  	'dewalt': 0,
		'bnd': 1,
		'delta': 2
	  }
	  $('#find-number-brands li:eq('+brandAssoc[$body.attr('class')]+')').click();
    }

    // this initialises the demo scollpanes on the page.
    try {
        var $partsListScrollPane = $('#PartsListScrollPane');
        if($partsListScrollPane.length && $partsListScrollPane.find('tr').length >= 9) {
           $partsListScrollPane.height(400);
           $partsListScrollPane.jScrollPane({ showArrows: true });
        }
        $('#PartsDiagramScrollPane').jScrollPane({ showArrows: true });
        $('#YourPartsScrollPane').jScrollPane({ showArrows: true });
        $('#servicenet-news ul').jScrollPane({ showArrows: true });
        $('#PartsListScrollPane.table-scroll').jScrollPane({ showArrows: true });
    }
    catch (e) { }

    // this allows you to click a link to add content to #pane4 and shows how to
    // reinitialise the scrollbars when you have done this.


    if ($('#table-add-to-review tbody').children.length !== 0) {
        $('.event-used-parts').removeClass('conceal');
    }
    if ($('#AuthorizedAccountNumber').attr('type') === "hidden") {
        $('div.event-user-authorized').removeClass('conceal');
    }

    /*$.getJSON(
    "/Products/LoadProductsFromCategory",
    {
    "categoryId": "7"
    },
    function(data) {
    $('#productResults').append(data.OfflineModelsHtml);
    }
    );*/

    /*$.getJSON(
    "/Products/LoadBrowseCategories",
    {
    "categoryId": "7",
    "reverse": "false"
    },
    function(data) {
    }
    );*/

    pBrowserInit();
});

var $pBrowser;
function pBrowserInit() {
    $pBrowser = $('#pbrowser-content');
    if ($pBrowser.length) {
        var taxMode = $('#taxonomy-desc').length;
        
        var pLv = 0;
        var $pCat = $('#pbrowser-cat');

        if (!uniLoad) {
            loadingMsg();
        }
        if (!$('#pbrowser-selectable').length && location.hash === ""
            || $('#pbrowser-selectable').length && $('#warrantyFeeCategoryId').length && $('#warrantyFeeCategoryId').val() === "" && $('#warrantyFeeProductId').val() === ""
            || $('#pbrowser-selectable').length && $('#taxCategoryId').length && $('#taxCategoryId').val() === "" && $('#taxProductId').val() === "") {
            $.getJSON( // Load top level of Product Browser
                    BASE_URL + "Products/LoadBrowseCategories",
                    {
                        "categoryId": "",
                        "reverse": "false"
                    },
                    function(data) {
                        loadingMsg();

                        if (!taxMode) {
                            $.each(data, function(i) {
                                if (this.isActive === false || this.isViewable === false) {
						data[i] = null;
                                }
                            });
                        }

                        var pCatHTML = [];
                        $.each(data, function(i) {
                            if(data[i]) {
				        if ($('#pbrowser-selectable').length) {
                                    pCatHTML.push(['<li><a href="#" catid="', this.Id, '" parentid="', this.ParentCategoryId, '" catchildren="', this.numberOfChildren, '" catactive="', this.isActive, '" catviewable="', this.isViewable, '" catfriendly="', this.FriendlyDescription, '" catincludechildren="', this.includeChildren, '">', this.Description, ' <span>&nbsp;</span></a></li>'].join(''));
                                } else {
                                    pCatHTML.push(['<li><a href="#" catid="', this.Id, '" parentid="', this.ParentCategoryId, '" catchildren="', this.numberOfChildren, '" catactive="', this.isActive, '" catviewable="', this.isViewable, '" catfriendly="', this.FriendlyDescription, '" catincludechildren="', this.includeChildren, '">', this.FriendlyDescription, '</a></li>'].join(''));
                                }
				    }
                        });
                        $pCat.append(pCatHTML.join(''));

                        $('ul', $pBrowser).jScrollPane({
                            dragMinHeight: 45,
                            dragMaxHeight: 45
                        });

                        $pBrowser.css({
                            'height': $pBrowser.parent().height(),
                            'width': $pBrowser.parent().width() - 1
                        });
                        $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 224).css('background', 'none');
                        if (taxMode) {
                            $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 207).css('background', 'none');
                        }
                        $pCat.width('100%');
                    }
                );
        } else {
            /*pLv = 2;
            if (taxMode) {
                pLv = 3;
            }*/

            var pBrowserDeep = $('#taxCategoryId').length ? $('#taxCategoryId').val() : $('#warrantyFeeCategoryId').val();
            if(!$('#pbrowser-selectable').length && location.hash !== "") {
                pBrowserDeep = location.hash.split('#')[1];
            }
            $.getJSON(
                    BASE_URL + "Products/LoadBrowseCategories",
                    {
                        "categoryId": pBrowserDeep,
                        "reverse": "true"
                    },
                    function(data) {
                        loadingMsg();

                        if (!$('#taxonomy-desc').length) {
                            $.each(data.siblings, function(i) {
                                if (this.isActive === false || this.isViewable === false) {
                                    data.siblings[i] = null;
                                }
                            });
                        }
                        
                        $.each(data.parentHierarchy, function(i) {
                            var pNavHTML = ['<ul id="pbrowser-nav', i, '" class="pbrowser-nav">', '<li class="active" catid="', this.Id, '" parentid="', this.ParentCategoryId, '" catchildren="', this.children.length, '">', this.FriendlyDescription, '</li>'];

                            $.each(this.siblings, function() {
                                if (this.isActive !== false || this.isViewable !== false) {
                                    var navItem = ['<li catid="', this.Id, '">', this.FriendlyDescription, '</li>'];

                                    pNavHTML.push(navItem.join(''));
                                }
                            });
                            pNavHTML.push('</ul>');

                            $pBrowser.prepend(pNavHTML.join(''));
                        });

                        if ($('#pbrowser-selectable').length) {
                            var pCatHTML = ['<li><a href="#" catid="', data.Id, '" parentid="', data.ParentCategoryId, '" catchildren="', data.children.length, '" catactive="', data.isActive, '" catviewable="', data.isViewable, '" catfriendly="', data.FriendlyDescription, '" catincludechildren="', data.includeChildren, '">', data.Description, ' <span>&nbsp;</span></a></li>'];
                        } else {
                            var pCatHTML = ['<li><a href="#" catid="', data.Id, '" parentid="', data.ParentCategoryId, '" catchildren="', data.children.length, '" catactive="', data.isActive, '" catviewable="', data.isViewable, '" catfriendly="', data.FriendlyDescription, '" catincludechildren="', data.includeChildren, '">', data.FriendlyDescription, '</a></li>'];
                        }

				$.each(data.siblings, function(i) {
                            if(data.siblings[i]) {
		   	              if ($('#pbrowser-selectable').length) {
                                    pCatHTML.push(['<li><a href="#" catid="', this.Id, '" parentid="', this.ParentCategoryId, '" catchildren="', this.numberOfChildren, '" catactive="', this.isActive, '" catviewable="', this.isViewable, '" catfriendly="', this.FriendlyDescription, '" catincludechildren="', this.includeChildren, '">', this.Description, ' <span>&nbsp;</span></a></li>'].join(''));
                                } else {
                                    pCatHTML.push(['<li><a href="#" catid="', this.Id, '" parentid="', this.ParentCategoryId, '" catchildren="', this.numberOfChildren, '" catactive="', this.isActive, '" catviewable="', this.isViewable, '" catfriendly="', this.FriendlyDescription, '" catincludechildren="', this.includeChildren, '">', this.FriendlyDescription, '</a></li>'].join(''));
                                }
			          }
		  	      });
				$pCat.append(pCatHTML.join(''));

                        $('ul', $pBrowser).jScrollPane({
                            dragMinHeight: 45,
                            dragMaxHeight: 45
                        });

                        $pBrowser.css({
                            'height': $pBrowser.parent().height(),
                            'width': $pBrowser.parent().width() - 1
                        });
                        $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 224).css('background', 'none');
                        if (taxMode) {
                            $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 207).css('background', 'none');
                        }
                        $pCat.width('100%');
                        
                        pLv = $pBrowser.find('ul.nav').length;
                        
				$('a[catid="'+pBrowserDeep+'"]', $pCat).click();
                    }
                );
        }

        $('#product-panel-links a.product-link').live('click', function() {
            if ($('#pbrowser-selectable').length) {
                $('#warrantyFeeProductId').val($.trim($(this).children('strong').text()));

                return false;
            }
        });
        
        $('a span', $pCat).live('mouseenter mouseleave', function() {
            $(this).toggleClass('active');
        });

        $('a span', $pCat).live('click', function() {
            if ($('#productPanel div.jScrollPaneContainer').length) {
                $('#productPanel div.jScrollPaneContainer').remove();
                $('#productPanel').append('<div id="product-panel-links">&nbsp</div>');
            }

            var $this = $(this).parent();

            $('#warrantyFeeCategoryId').val($.trim($this.text()));

            if (pLv <= 1 && $this.attr('catchildren') !== "0" || taxMode && pLv <= 2) {
                pLv++;

                $pCat.fadeOut(function() {
                    $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 224);
                    if (taxMode) {
                        $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 207);
                    }
                    $pCat.width('100%');
                });

                var pNavHTML = [];

                $('a', $pCat).each(function() {
                    var $pCatLink = $(this);
                    var navItem = $pCatLink.text() === $this.text() ?
                            ['<li class="active" catid="', $pCatLink.attr('catid'), '" parentid="', $pCatLink.attr('parentid'), '" catchildren="', this.numberOfChildren, '">', $pCatLink.text(), '</li>']
                            : ['<li catid="', $pCatLink.attr('catid'), '">', $pCatLink.text(), '</li>'];

                    if ($pCatLink.text() === $this.text()) {
                        pNavHTML.unshift(navItem.join(''));
                    } else if ($pCatLink.attr('catchildren') !== '0') {
                        pNavHTML.push(navItem.join(''));
                    }
                });
                pNavHTML.unshift('<ul id="pbrowser-nav', $('ul', $pBrowser).length - 1, '" class="pbrowser-nav">');
                pNavHTML.push('</ul>');

                if (!$('ul.pbrowser-nav', $pBrowser).length) {
                    $pBrowser.prepend(pNavHTML.join(''));
                } else {
                    $('ul.pbrowser-nav', $pBrowser).last().parent().after(pNavHTML.join(''));
                }

                $('ul.pbrowser-nav', $pBrowser).last().jScrollPane({
                    dragMinHeight: 45,
                    dragMaxHeight: 45
                });

                $('ul.pbrowser-nav', $pBrowser).last().parent().hide();

                /*$('ul.pbrowser-nav', $pBrowser).jScrollPane({
                dragMinHeight: 45,
                dragMaxHeight: 45
                });*/

                if (!uniLoad) {
                    loadingMsg();
                }
                $.getJSON(
                    BASE_URL + "Products/LoadBrowseCategories",
                    {
                        "categoryId": $this.attr('catid'),
                        "reverse": "false"
                    },
                    function(data) {
                        loadingMsg();

                        if (!$('#taxonomy-desc').length) {
                            $.each(data.categories, function(i) {
                                if (this.isActive === false || this.isViewable === false) {
                                    data.categories[i] = null;
                                }
                            });
                        }

                        var pCatHTML = [];
                        $.each(data.categories, function(i) {
                            if (data.categories[i]) {
                                if ($('#pbrowser-selectable').length) {
                                    pCatHTML.push(['<li><a href="#" catid="', this.Id, '" parentid="', this.ParentCategoryId, '" catchildren="', this.numberOfChildren, '" catactive="', this.isActive, '" catviewable="', this.isViewable, '" catfriendly="', this.FriendlyDescription, '" catincludechildren="', this.includeChildren, '">', this.Description, ' <span>&nbsp;</span></a></li>'].join(''));
                                } else {
                                    pCatHTML.push(['<li><a href="#" catid="', this.Id, '" parentid="', this.ParentCategoryId, '" catchildren="', this.numberOfChildren, '" catactive="', this.isActive, '" catviewable="', this.isViewable, '" catfriendly="', this.FriendlyDescription, '" catincludechildren="', this.includeChildren, '">', this.FriendlyDescription, '</a></li>'].join(''));
                                }
                            }
                        });

                        $('#pbrowser-cat').parent().remove();

                        $pBrowser.append('<ul id="pbrowser-cat" class="clear"></ul>');

                        $pCat = $('#pbrowser-cat');

                        $pCat.append(pCatHTML.join(''));

                        $pCat.width($pBrowser.width() - $('ul.pbrowser-nav').length * 224).css('background', 'none');
                        if (taxMode) {
                            $pCat.width($pBrowser.width() - $('ul.pbrowser-nav').length * 207).css('background', 'none');
                        }

                        $pCat.jScrollPane({
                            dragMinHeight: 45,
                            dragMaxHeight: 45
                        });

                        $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 224).css('background', 'none');
                        if (taxMode) {
                            $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 207).css('background', 'none');
                        }
                        $pCat.width('100%');

                        $('ul.pbrowser-nav', $pBrowser).last().parent().fadeIn();
                        /*$('ul.pbrowser-nav', $pBrowser).jScrollPane({
                        dragMinHeight: 45,
                        dragMaxHeight: 45
                        });*/
                    }
                );
            } else if (!taxMode) {
                if (!uniLoad) {
                    loadingMsg();
                }
                $.getJSON(
                        "/Products/LoadProductsForCategory",
                        {
                            "categoryId": $this.attr('catid')
                        },
                        function(data) {
                            loadingMsg();

                            var productHTML = [];

                            $.each(data, function(i) {
                                if (this.isAvailableOnline) {
                                    var linkHTML = ['<a class="product-link" href="' + BASE_URL + '/Products/', this.ModelNumber, '" productid="', this.ProductId, '"><strong>', this.ModelNumber, '</strong><img src="', this.ImageURI, '" alt="" /><span class="product-link-desc">', this.ProductDescription, '</span><span class="product-link-text">More Details</span></a>'];
                                    if ((i + 1) % 5 === 0) {
                                        linkHTML[4] = '" style="margin-right: 0;"><strong>';
                                    }
                                    if (this.ImageURI === "" || !this.ImageURI) {
                                        linkHTML[7] = BASE_URL + 'content/images/noimage.gif';
                                    }
                                    productHTML.push(linkHTML.join(''));
                                }                                
                            });

                            $('#product-panel-links').height(400).empty().append(productHTML.join(''));
                            $('#product-panel-links').jScrollPane({
                                dragMinHeight: 45,
                                dragMaxHeight: 45
                            });
                        }
                    );
            }

            return false;
        });

        $('a', $pCat).live('click', function() {
            if ($('#productPanel div.jScrollPaneContainer').length) {
                $('#productPanel div.jScrollPaneContainer').remove();
                $('#productPanel').append('<div id="product-panel-links">&nbsp</div>');
            }

            var $this = $(this);

            if (!taxMode) {
                location.hash = $this.attr('catid');
            }

            $('a', $pCat).removeClass('active');
            $this.addClass('active');

            if ($('#pbrowser-selectable').length) {
                $('#warrantyFeeProductId').val("[none]");
                $('#warrantyFeeCategoryId').val($.trim($this.text()));

                // Populate Taxonomy info box
                $('#taxonomy-desc').text($.trim($this.text()));
                $('#UserFriendlyDesc').val($this.attr('catfriendly'));
                var catInvisible = $this.attr('catviewable') === "true" || $this.attr('catviewable') === true ? false : true;
                var catDeactivate = $this.attr('catactive') === "true" || $this.attr('catactive') === true ? false : true;
                $('#checkbox-viewable').attr('checked', catInvisible);
                $('#checkbox-active').attr('checked', catDeactivate);
                var catIncludeChildren = $this.attr('catincludechildren') === 'false' || $this.attr('catincludechildren') === false ? false : true;
                $('#checkbox-include-children').attr('checked', catIncludeChildren);

                return false;
            }

            if (taxMode && pLv <= 1 || pLv <= 1 && $this.attr('catchildren') !== "0") {
                pLv++;

                $pCat.fadeOut(function() {
                    $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 224);
                    if (taxMode) {
                        $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 207);
                    }
                    $pCat.width('100%');
                });

                var pNavHTML = [];

                $('a', $pCat).each(function() {
                    var $pCatLink = $(this);
                    var navItem = $pCatLink.attr('catid') === $this.attr('catid') ?
                            ['<li class="active" catid="', $pCatLink.attr('catid'), '" parentid="', $pCatLink.attr('parentid'), '" catchildren="', this.numberOfChildren, '">', $pCatLink.text(), '</li>']
                            : ['<li catid="', $pCatLink.attr('catid'), '">', $pCatLink.text(), '</li>'];

                    if ($pCatLink.attr('catid') === $this.attr('catid')) {
                        pNavHTML.unshift(navItem.join(''));
                    } else if ($pCatLink.attr('catchildren') !== '0') {
                        pNavHTML.push(navItem.join(''));
                    }
                });
                pNavHTML.unshift('<ul id="pbrowser-nav', $('ul', $pBrowser).length - 1, '" class="pbrowser-nav">');
                pNavHTML.push('</ul>');

                if (!$('ul.pbrowser-nav', $pBrowser).length) {
                    $pBrowser.prepend(pNavHTML.join(''));
                } else {
                    $('ul.pbrowser-nav', $pBrowser).last().parent().after(pNavHTML.join(''));
                }

                $('ul.pbrowser-nav', $pBrowser).last().jScrollPane({
                    dragMinHeight: 45,
                    dragMaxHeight: 45
                });

                $('ul.pbrowser-nav', $pBrowser).last().parent().hide();

                /*$('ul.pbrowser-nav', $pBrowser).jScrollPane({
                dragMinHeight: 45,
                dragMaxHeight: 45
                });*/

                if (!uniLoad) {
                    loadingMsg();
                }
                $.getJSON(
                    BASE_URL + "Products/LoadBrowseCategories",
                    {
                        "categoryId": $this.attr('catid'),
                        "reverse": "false"
                    },
                    function(data) {
                        loadingMsg();

                        if (!$('#taxonomy-desc').length) {
                            $.each(data.categories, function(i) {
                                if (this.isActive === false || this.isViewable === false) {
                                    data.categories[i] = null;
                                }
                            });
                        }

                        var pCatHTML = [];
                        $.each(data.categories, function(i) {
                            if (data.categories[i]) {
                                if ($('#pbrowser-selectable').length) {
                                    pCatHTML.push(['<li><a href="#" catid="', this.Id, '" parentid="', this.ParentCategoryId, '" catchildren="', this.numberOfChildren, '" catactive="', this.isActive, '" catviewable="', this.isViewable, '" catfriendly="', this.FriendlyDescription, '" catincludechildren="', this.includeChildren, '">', this.Description, ' <span>&nbsp;</span></a></li>'].join(''));
                                } else {
                                    pCatHTML.push(['<li><a href="#" catid="', this.Id, '" parentid="', this.ParentCategoryId, '" catchildren="', this.numberOfChildren, '" catactive="', this.isActive, '" catviewable="', this.isViewable, '" catfriendly="', this.FriendlyDescription, '" catincludechildren="', this.includeChildren, '">', this.FriendlyDescription, '</a></li>'].join(''));
                                }
                            }
                        });

                        $('#pbrowser-cat').parent().remove();

                        $pBrowser.append('<ul id="pbrowser-cat" class="clear"></ul>');

                        $pCat = $('#pbrowser-cat');

                        $pCat.append(pCatHTML.join(''));

                        $pCat.width($pBrowser.width() - $('ul.pbrowser-nav').length * 224).css('background', 'none');
                        if (taxMode) {
                            $pCat.width($pBrowser.width() - $('ul.pbrowser-nav').length * 207).css('background', 'none');
                        }

                        $pCat.jScrollPane({
                            dragMinHeight: 45,
                            dragMaxHeight: 45
                        });

                        $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 224).css('background', 'none');
                        if (taxMode) {
                            $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 207).css('background', 'none');
                        }
                        $pCat.width('100%');

                        $('ul.pbrowser-nav', $pBrowser).last().parent().fadeIn();
                        /*$('ul.pbrowser-nav', $pBrowser).jScrollPane({
                        dragMinHeight: 45,
                        dragMaxHeight: 45
                        });*/
                    }
                );
            } else {
                if (!uniLoad) {
                    loadingMsg();
                }
                $.getJSON(
                        "/Products/LoadProductsForCategory",
                        {
                            "categoryId": $this.attr('catid')
                        },
                        function(data) {
                            loadingMsg();

                            var productHTML = [];

                            $.each(data, function(i) {
                                if (this.isAvailableOnline) {
                                    var linkHTML = ['<a class="product-link" href="/Products/Detail/', this.ModelNumber, '" productid="', this.ProductId, '"><strong>', this.ModelNumber, '</strong><img src="', this.ImageURI, '" alt="" /><span class="product-link-desc">', this.ProductDescription, '</span><span class="product-link-text">More Details</span></a>'];
                                    if ((i + 1) % 5 === 0) {
                                        linkHTML[4] = ' style="margin-right: 0;"><strong>';
                                    }
                                    if (this.ImageURI === "") {
                                        linkHTML[7] = BASE_URL + 'content/images/noimage.gif';
                                    }
                                    productHTML.push(linkHTML.join(''));
                                }                                
                            });

                            $('#product-panel-links').height(400).empty().append(productHTML.join(''));
                            $('#product-panel-links').jScrollPane({
                                dragMinHeight: 45,
                                dragMaxHeight: 45
                            });
                        }
                    );
            }

            return false;
        });

        $('ul.pbrowser-nav li', $pBrowser).live('click', function() {
            if($('#productPanel div.jScrollPaneContainer').length) {
                $('#productPanel div.jScrollPaneContainer').remove();
                $('#productPanel').append('<div id="product-panel-links">&nbsp</div>');
            }
            
            var $this = $(this);
            
            if(!taxMode) {
                location.hash = $this.attr('catid');
            }

            $this.siblings('.active').removeClass('active');
            $this.addClass('active');

            if ($this.parent().parent().index() !== $pBrowser.children('div').length - 1) {
                $this.parent().parent().nextAll().not(':last').fadeOut(function() {
                    $(this).remove();
                });
                pLv = $this.parent().parent().index() + 1;
            }

            $pCat.fadeOut(function() {
                $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 224);
                if (taxMode) {
                    $pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 207);
                }
                $pCat.width('100%');

			$.getJSON(
				BASE_URL + "Products/LoadBrowseCategories",
				{
					"categoryId": $this.attr('catid'),
					"reverse": "false"
				},
				function(data) {
					loadingMsg();

					if (!$('#taxonomy-desc').length) {
						$.each(data.categories, function(i) {
							if (this.isActive === false || this.isViewable === false) {
								data.categories[i] = null;
							}
						});
					}
					
					var pCatHTML = [];
					$.each(data.categories, function(i) {
				if(data.categories[i]) {
							if ($('#pbrowser-selectable').length) {
								pCatHTML.push(['<li><a href="#" catid="', this.Id, '" parentid="', this.ParentCategoryId, '" catchildren="', this.numberOfChildren, '" catactive="', this.isActive, '" catviewable="', this.isViewable, '" catfriendly="', this.FriendlyDescription, '" catincludechildren="', this.includeChildren, '">', this.Description, ' <span>&nbsp;</span></a></li>'].join(''));
							} else {
								pCatHTML.push(['<li><a href="#" catid="', this.Id, '" parentid="', this.ParentCategoryId, '" catchildren="', this.numberOfChildren, '" catactive="', this.isActive, '" catviewable="', this.isViewable, '" catfriendly="', this.FriendlyDescription, '" catincludechildren="', this.includeChildren, '">', this.FriendlyDescription, '</a></li>'].join(''));
							}
				  }
					});
					
					$('#pbrowser-cat').parent().remove();
						
					$pBrowser.append('<ul id="pbrowser-cat" class="clear"></ul>');
					
					$pCat = $('#pbrowser-cat');
					
					$pCat.append(pCatHTML.join(''));
					
					$pCat.width($pBrowser.width() - $('ul.pbrowser-nav').length * 224).css('background', 'none');
					if (taxMode) {
						$pCat.width($pBrowser.width() - $('ul.pbrowser-nav').length * 207).css('background', 'none');
					}
					
					$pCat.jScrollPane({
						dragMinHeight: 45,
						dragMaxHeight: 45
					});
					
					$pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 224).css('background', 'none');
					if (taxMode) {
						$pCat.parent().width($pBrowser.width() - $('ul.pbrowser-nav').length * 207).css('background', 'none');
					}
					$pCat.width('100%');
					
					$('ul.pbrowser-nav', $pBrowser).last().parent().fadeIn();
				}
			);
		});

            if (!uniLoad) {
                loadingMsg();
            }
        });

        $('#save-taxonomy').click(function() {
            var $alteredCat = $('#pbrowser-cat a.active');
            alertBox('Saving', 'Please await confirmation.');
            $.post("/Products/SaveProductCategory", {
                "categoryId": $('#pbrowser-cat a.active').attr('catid'),
                "FriendlyDescription": $('#UserFriendlyDesc').val(),
                "isVisible": !$('#checkbox-viewable').is(':checked'),
                "isActive": !$('#checkbox-active').is(':checked'),
                "includeChildren": $('#checkbox-include-children').is(':checked')
            }, function() {
                alertBox('Saved', 'Taxonomy updates are now live.');
                
                $alteredCat.attr({
                    "catfriendly": $('#UserFriendlyDesc').val(),
                    "catviewable": !$('#checkbox-viewable').is(':checked'),
                    "catactive": !$('#checkbox-active').is(':checked'),
                    "catincludechildren": $('#checkbox-include-children').is(':checked')
                });
            });

            return false;
        });
    }
}

var $modalKeyHooks = null,
    $pagingLinks = null,
    firstModalOpen = true;
function serviceModal(id, modWidth, modHeight) {
    if (!$('#modal').length) {
        $body.append('<div id="scrim"></div>');
        var $modalContent = $('#' + id);
        $modalContent.wrap('<div id="modal" class="modal-content"><div id="modal-window"></div></div>');
        var $modal = $('#modal');
        $modal.prepend('<span id="modal-close" onclick="CloseServiceModal()">Close</span>');
        if (!modWidth) {
            var modWidth = $modalContent.outerWidth();
        }
        if (!modHeight) {
            var modHeight = $modalContent.outerHeight();
        }
        $modal.width(modWidth);
        $modal.height(modHeight);
        $modalContent.removeClass('modal-content');

        
        // Fix for ie6
        if ($.browser.msie && $.browser.version <= 6) {
        	$('#scrim').css('top', $(document).scrollTop() + 'px');

        	$modal.css({
        		'top': $(document).scrollTop() + $('#scrim').height() / 2 - $modal.outerHeight() / 2 + 'px',
        		'left': $body.width() / 2 - $modal.outerWidth() / 2 + 'px'
        	});

        	$('#main select.overlapped-element').hide();
        }

        else {

            $modal.css({
                'top': $body.height() / 2 - $modal.outerHeight() / 2 + 'px',
                'left': $body.width() / 2 - $modal.outerWidth() / 2 + 'px'
            });
        }
        
        
        if (id === 'modal-dragagram') {
            $modal.css({
                'top': '25px',
                'right': '25px',
                'bottom': '25px',
                'left': '25px',
                'width': 'auto',
                'height': 'auto'
            });
            $('#modal-window').height($modal.height());
            $('#modal-window').width($modal.width());
        }
        
        if (id === 'modal-select-type') {
            $('#modal-select-type').css('display','block');
        }
        
        $modalKeyHooks = $('#savedCustomers, #search-tool');
        if($modalKeyHooks.length) {
            if(firstModalOpen) {
                firstModalOpen = false;
                
                /*$modalKeyHooks.keydown(function(e) {
                    if(e.keyCode === 13) {
                        $(this).click();
                    }
                    return false;
                });*/
                
                $pagingLinks = $('#modal-window caption a');
                $pagingLinks.live('focus', function() {
                    $(this).addClass('focus');
                });
                $pagingLinks.live('blur', function() {
                    $(this).removeClass('focus');
                });
                $pagingLinks.eq(0).focus();
                
                $(document).keydown(modalKeys);
                $('#ModelNumber').blur();
            } else {
                $pagingLinks = $('#modal-window caption a');
                $pagingLinks.eq(0).focus();
                
                $(document).keydown(modalKeys);
                $('#ModelNumber').blur();
            }
        } 
    }
}

function modalKeys(e) {
    // direction... 38 = up, 40 = down, 13 = enter, 27 = esc, 32 = space
    if(e.keyCode === 38 || e.keyCode === 40) {
        var $modalTableRows = $('#modal-window tr:not(.thead)'),
            $mtrActive = $modalTableRows.filter('.active');
        
        if(!$mtrActive.length) {
            $modalTableRows.eq(0).addClass('active');
        } else {
            $mtrActive.removeClass('active');
            var $mtrNext = e.keyCode === 40 ? $mtrActive.next() : $mtrActive.prev();
            $mtrNext.addClass('active');
            $mtrActive = $mtrNext;
        }
        return false;
    } else if(e.keyCode === 13) {
        $('#modal-window tr.active').click();
        
        $modalKeyHooks.focus();
        
        return false;
    } else if(e.keyCode === 27) {
        $('#modal-close').click();
        
        $modalKeyHooks.focus();
        
        return false;
    } else if(e.keyCode === 32) {
        var $pagLinksFocus = $pagingLinks.filter('.focus');
        $pagLinksFocus.filter('.focus').click();
        
        return false;
    }
}
