// cookie variables
var COOKIE_LAST_SEARCH = "last_search";
var COOKIE_SCOPE = "scope";
var COOKIE_PROD_TYPE = "product_type";
var COOKIE_FLUSH_DET = "flushometer_details";
var COOKIE_PROJ_TYPE = "project_type";
var COOKIE_INSTALL = "install_location";
var options = { path: "/", expires: 9999 };
function setMainSearchOption() {
    if ($.cookie(COOKIE_SCOPE)) {
        if ($.cookie(COOKIE_SCOPE) == "Products Only") {
            $("#main1").attr("checked","checked");
            $('.productType').show();
            $('.projectType').show();
            $('.installLocation').show();
            /*if ($('.productType select option:selected').val() == "Flushometers") {
                $('.flushometerDetails').show();
                setFlushDetails();
            }*/    
        } else {
            $("#main2").attr("checked","checked");
            $('.productType').hide();
            $('.projectType').hide();
            $('.installLocation').hide();
            $('.flushometerDetails').hide();
        }
    } else {
        // no cookie, establish main options
        $("#main2").attr("checked","checked");
        $.cookie(COOKIE_SCOPE, "Entire Website", options);
    }    
}
function setProductTypeOption() {
    if ($.cookie(COOKIE_PROD_TYPE)) {
        $('.productType select option').each(function() {
            if ($(this).val() == $.cookie(COOKIE_PROD_TYPE)) {
                $(this).attr("selected","selected");
                if ($(this).val() == "Flushometers" && $.cookie(COOKIE_SCOPE) != "Entire Website") {
                    $('fieldset.flushometerDetails').show();
                    setFlushDetails();
                }
            }    
        });
    }        
}
function setProjectTypeOption() {
    if ($.cookie(COOKIE_PROJ_TYPE)) {
        $('.projectType select option').each(function() {
            if ($(this).val() == $.cookie(COOKIE_PROJ_TYPE)) {
                $(this).attr("selected","selected");
            }    
        });    
    }
}
function setFlushDetails() {
    
    if ($.cookie(COOKIE_FLUSH_DET)) {
        var myDetails = $.cookie(COOKIE_FLUSH_DET).split(",");
        $('.flushometerDetails input').each(function() {
            var myValue = $(this).val();
            if (jQuery.inArray(myValue, myDetails) > -1) {
                $(this).attr("checked","checked");                                           
            }
        });
    }    
}
function setInstallLocations() {
    if ($.cookie(COOKIE_INSTALL)) {
        var myLocations = $.cookie(COOKIE_INSTALL).split(",");
        $('.installLocation input').each(function() {
            var myLocation = $(this).val();
            if (jQuery.inArray(myLocation, myLocations) > -1) {
                $(this).attr("checked","checked");    
            }
        });
    }
}
function setSearchField() {
    $('#searchField').val("Search");        
}
function removeCurrentClass() {
    $('.pictureGroup li a').each(function() {
        $(this).removeClass("current");                  
    });
}
function switchTabs(id) {
    $('.productTabs ul li a').each(function() {
        $(this).removeClass("current");
        $(this).parent().css("z-index","1");                                        
    });
    $('.productTabInfo').each(function() {
        $(this).hide();                                    
    });
    
    switch (id) {
        case "technicalDownloadsTab":
        $('#productTechnicalDownloads').show();
        break;
        
        case "productFeaturesTab":
        $('#productFeatures').show();
        break;
        
        case "relatedVideosTab":
        $('#productRelatedVideos').show();
        break;
        
        case "youMightAlsoLikeTab":
        $('#productsYouMightAlsoLike').show();
        break;
    }
        
}
function updateTimeline(arg) {
    
    // update left nav
    $('.sideNav ul li.sub ul li').each(function() {
        $(this).removeClass("on");
        if ($(this).children("a").attr("href") == arg) {
            $(this).addClass("on");
        }
    });    
    
    // update timeline nav
    $('.timelineNav .ui-tabs-nav li').each(function() {
                
        var thisLocation = $(this).children("a").attr("href");
        var thisShortLocation = thisLocation.substring(0,thisLocation.length-3);
        if (thisShortLocation == arg) {
            $(this).addClass("ui-tabs-selected");    
        } else {
            $(this).removeClass("ui-tabs-selected");
        }
    });
    
    // update timeline description
    
    $('.timelineDiv').each(function() {
                                    
        var thisPanelID = $(this).attr("id");
        var thisPanelDescription = thisPanelID.substring(0,thisPanelID.length-3);
        if (thisPanelDescription == arg.substring(1,arg.length)) {
            $(this).removeClass("ui-tabs-hide");
        } else {
            $(this).addClass("ui-tabs-hide");
        }
    });
    
}
    
    
// Lightbox
// calculate viewport & offset dimensions
function viewPortHeight() { 
    return self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; }
function viewPortWidth() { 
    return self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;}
function scrollOffsetHeight() { 
    return self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;}
function scrollOffsetWidth() { 
    return self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;}
function documentHeight() {
    var myHeight = 0;
    if (document.documentElement) {
        myHeight = document.documentElement.scrollHeight;
    } else if (document.body) {
        myHeight = document.documentElement.scrollHeight;
    }
    return myHeight;
}
function showbox(el) {
    var oHeight = documentHeight();
    var oWidth = viewPortWidth();
    $("#overlay").css("height",oHeight);
    
    // hide select dropdowns for ie6
    $("select").css("visibility","hidden");
    
    // setup positioning for lightbox (centered)    
    var leftPos = ( Math.round(viewPortWidth()/2) + scrollOffsetWidth() - Math.round($(el).width()/2) ); 
    var topPos = ( Math.round(viewPortHeight()/2) + scrollOffsetHeight() - Math.round($(el).height()/2) );
    $(el).css("left",leftPos);
    $(el).css("top",topPos);
        
    // setup positioning for the overlay (centered) 
    // $("#overlay").css("left",Math.round(viewPortWidth()/2) + scrollOffsetWidth() - Math.round($(el).width()/2 + 14));
    // $("#overlay").css("top",Math.round(viewPortHeight()/2) + scrollOffsetHeight() - Math.round($(el).height()/2 + 14));    
    $("#overlay").css("left",0);
    $("#overlay").css("top",0);    
    
    // show overlay 
    $("#overlay").show();
        
    // show lightbox
    $(el).show();
}
function hidebox(el) {
    // hide lightbox
    $(el).hide();
    $("#overlay").hide();
}
$(function() {
    $('.sideNav ul li ul li a').click(function() {
        if ($('.sideNav ul li ul li.last').hasClass('on')) {
            $('.sideNav ul li ul li.last.on').attr("style","background: url(/images/backgrounds/innerNav.ul.li.ul.li.last.hover.png) 0 bottom no-repeat !important");
        } else {
            $('.sideNav ul li ul li.last').attr("style","");
        }
    });
    
    // specifically for Our Heritage page
    if ($('#ourHeritage').length > 0) {
        $('a.timeline').each(function() {
            $(this).click(function() {
                if ($(this).attr("href") == "#timeline2001_sg"     || $(this).attr("href") == "#timeline2001_pi" || $(this).attr("href") == "#timeline2001_pc") {
                    $('.sideNav ul li ul li.last').attr("style","background: url(/images/backgrounds/innerNav.ul.li.ul.li.last.hover.png) 0 bottom no-repeat !important");
                } else {
                    $('.sideNav ul li ul li.last').attr("style","");
                }
            });
        });
    }
});
function highlightAttribute(arg) {
    $(arg).each(function() {
        $(arg).addClass("highlightAttribute");                       
    });
}
function removeHighlight(arg) {
    $(arg).each(function() {
        $(arg).removeClass("highlightAttribute");                       
    });
}
$(document).ready(function() {
    setSearchField();
    setMainSearchOption();
    setProductTypeOption();
    setProjectTypeOption();
    setInstallLocations();
                           
    // safari fixes
    if ($.browser.safari) {
        $('#moreInfo').css("left","75.9%");    
        
        $('#searchFilters a#searchFiltersClose').css("right","0").css("top","0");
        $('.main ul li').css("padding-bottom","3px").css("padding-top","3px");
        $('.installLocation ul li').css("padding-bottom","3px").css("padding-top","3px");
        $('.sideNav ul li a').css("background-position","10px 6px");
        $('ul.guides li a').css("padding","4px 0 2px 0");
    }
    
    $('h1').click(function() {
        window.location.href = "/Home.aspx";                           
    });
    
    $('#header #topMenu ul li a span').each(function() {
        $(this).pngfix();                                             
    });
    $('#header #topMenu ul li a').each(function() {
        $(this).mouseover(function() {
            $(this).children("span").css("margin-top","-11px");
        });    
        $(this).mouseout(function() {
            $(this).children("span").css("margin-top","0");
        });                                            
    });
    
    $('#header #nav').pngfix();
    $('#header #nav ul li a span').each(function() {
        $(this).pngfix();
    });
    $("#header #nav ul li a").each(function() {
        if (!($(this).hasClass("selected"))) {
            $(this).mouseover(function() {
                $(this).children("span").css("margin-top","-54px");
            });
            $(this).mouseout(function() {
                $(this).children("span").css("margin-top","0");
            });
        }
    });
    
    $('#searchFilters .searchFiltersSide').pngfix(); 
    $('#searchFilters .searchFiltersBottom').pngfix();
    
    $('#content').pngfix();
    
    $('#footer').pngfix();
    $('#footer ul li a span').each(function() {
        $(this).pngfix();    
    });
    $("#footer ul li a").each(function() {
        $(this).mouseover(function() {
            $(this).children("span").css("margin-top","-10px");
        });
        $(this).mouseout(function() {
            $(this).children("span").css("margin-top","0");
        });
    });
    
    $('#searchField').click(function() {
        if ($(this).val() == "Search") {
            //if ($.cookie(COOKIE_LAST_SEARCH) && ($.cookie(COOKIE_LAST_SEARCH) != "") && ($.cookie(COOKIE_LAST_SEARCH) != "null")) {
            //    $(this).val($.cookie(COOKIE_LAST_SEARCH));
            //} else {
                $(this).val("");
            //}
        //} else {
        //    $(this).val($.cookie(COOKIE_LAST_SEARCH));    
        }
        // JRK $('#searchFilters').show();
    });
    
    $('#searchField').change(function() {
        $.cookie(COOKIE_LAST_SEARCH, $(this).val(), options);                                  
    });
    
    // JRK 12-11-2009 duplicate caching functionality for search terms for the Site Search page.
    /* JRK Remove 12-30-2009
    $('#searchboxquery').click(function() {
        if ($.cookie(COOKIE_LAST_SEARCH) && ($.cookie(COOKIE_LAST_SEARCH) != "") && ($.cookie(COOKIE_LAST_SEARCH) != "null")) {
            $(this).val($.cookie(COOKIE_LAST_SEARCH));
        } else {
            $(this).val("");
        }
    });
    $('#searchboxquery').change(function() {
        $.cookie(COOKIE_LAST_SEARCH, $(this).val(), options);
    });
    */

    $('.main input').click(function()    {
    
        // determine what happens when an item is checked
        $('.main input:checked').each(function() {
            if ($(this).val() == "Entire Website") {
                $('.productType').hide();
                $('.projectType').hide();
                $('.installLocation').hide();
                $('.flushometerDetails').hide();
            } else {
                $('.productType').show();
                $('.projectType').show();
                $('.installLocation').show();
                if ($('.productType select option:selected').val() == "Flushometers") {
                    $('.flushometerDetails').show();
                    setFlushDetails();
                }
            }
            // set the cookie value
            $.cookie(COOKIE_SCOPE, $(this).val(), options);
        });    
    });
    
    $('.productType select').change(function() {
        $('.productType select option:selected').each(function() {
            if ($(this).val() == "Flushometers") {
                $('fieldset.flushometerDetails').show();
            } else {
                $('fieldset.flushometerDetails').hide();
            }
            
            $.cookie(COOKIE_PROD_TYPE, $(this).val(), options);
        });
    });
    
    if ($.cookie(COOKIE_FLUSH_DET)) {
        var flushDetails = $.cookie(COOKIE_FLUSH_DET).split(",");    
    } else {
        var flushDetails = new Array();
    }
    
    $('.flushometerDetails input').each(function() {
        $(this).click(function() {
            if (($(this).is(":checked"))) {
                // push to array
                flushDetails.push($(this).val());
            } else {
                // find element index
                var i = jQuery.inArray($(this).val(), flushDetails);
                // remove it from array
                flushDetails.splice(i,1);
            }
            $.cookie(COOKIE_FLUSH_DET, flushDetails, options);
        });
    });
    
    $('.projectType select').change(function() {
        $.cookie(COOKIE_PROJ_TYPE, $(this).val(), options);                                         
    });
    
    if ($.cookie(COOKIE_INSTALL)) {
        var locations = $.cookie(COOKIE_INSTALL).split(",");    
    } else {
        var locations = new Array();
    }
    
    $('.installLocation input').each(function() {
        $(this).click(function() {
            if (($(this).is(":checked"))) {
                locations.push($(this).val());    
            } else {
                var i = jQuery.inArray($(this).val(), locations);
                locations.splice(i, 1);
            }
            $.cookie(COOKIE_INSTALL, locations, options);
        });
    });
    
    $('#searchFiltersClose').click(function() {
        $('#searchFilters').hide();
        $('#searchField').val("");
    });
    
    $('input.searchBtn').each(function() {
        $(this).mouseover(function() {
            $(this).css("background-position","0 -21px");                           
        });
        $(this).mouseout(function() {
            $(this).css("background-position","0 0");                      
        });
    });
    
    /***** Individual Products page *****/
    
    $('.products .product').each(function() {
        $(this).mouseenter(function() {
            $(this).addClass("productHover");                                
        });
        $(this).mouseleave(function() {
            $(this).removeClass("productHover");                            
        });
    });
    $('.products .product a.addToCompare').each(function() {
        $(this).click(function() {
            // hide this link
            $(this).hide();
            // show the go to compare link
            $(this).next().show();
            $(this).next().next().show();
        });
    });
    $('.products .product a.compareSelected').each(function() {
        $(this).click(function() {
            // hide this button and the next button
            $(this).hide();
            $(this).next().hide();
            // show Add to Compare button
            $(this).prev().show();
            
            if ($.browser.msie && $.browser.version < 7) {
                $(this).parent().css("margin-bottom","0px");    
            }
        });
    });
    
    
    /***** Product Detail page *****/
    
    $('.productLeftCol .options a.addToCompare').each(function() {
        $(this).click(function() {
            // hide this link
            $(this).hide();
            // show the go to compare links
            $(this).next().show();
            $(this).next().next().show();
            
            /*
            if ($.browser.msie && $.browser.version < 7) {
                $(this).parent().parent().css("margin-bottom","-20px");    
            } */
        });
    });
    $('.productLeftCol .options a.compareSelected').each(function() {
        $(this).click(function() {
            //hide this button and the next button
            $(this).hide();
            $(this).prev().hide();
            // show Add to Compare button
            $(this).prev().prev().show();
            
            /*
            if ($.browser.msie && $.browser.version < 7) {
                $(this).parent().css("margin-bottom","0px");    
            } */
        });
    });
    $('.productLeftCol .pictureGroup li a').each(function() {
        $(this).click(function() {
            removeCurrentClass();
            $(this).addClass("current");
            
            var thumbnail = $(this).children("img").attr("src");
            var newImagePath = thumbnail.substr(0,thumbnail.length-7);
            var newMediumImagePath = newImagePath + ".med.jpg";
            var newLargeImagePath = newImagePath + ".large.jpg";
            
            $('#mainImageMedium').attr("src", newMediumImagePath);
            $('#mainImageLarge').attr("href", newLargeImagePath);
        });
    });
    $('.productRightCol .productTabs ul li a').each(function() {
        $(this).click(function() {
            switchTabs($(this).attr("id"));
            $(this).addClass("current");
            $(this).parent().css("z-index","2");
        });
    });
    
    $('.relatedProduct .productImageLink').each(function() {
        $(this).mouseover(function() { 
            $(this).next().children("a").css("color","#2894da");
            $(this).css("background-position","0 bottom");
        });
        $(this).mouseout(function() { 
            $(this).next().children("a").css("color","#414141");
            $(this).css("background-position","0 0");
        });                                                       
    });
    $('.relatedProduct h3 a').each(function() {
        $(this).mouseover(function() { 
            $(this).parent().prev().css("background-position","0 bottom");
            $(this).css("color","#2894da");
        });
        $(this).mouseout(function() { 
            $(this).parent().prev().css("background-position","0 0");
            $(this).css("color","#414141");
        });                                                       
    });
    
    
    /***** Product Family page *****/
    
    $('.productFamily').each(function() {
        $(this).mouseenter(function() {
            $(this).addClass("productFamilyHover");                                
        });
        $(this).mouseleave(function() {
            $(this).removeClass("productFamilyHover");                            
        });
    });
    $('.productFamily img').each(function() {
        $(this).pngfix();
    });
    
    
    /***** Our Heritage page *****/
    
    $('#ourHeritage').css("visibility","visible");
    
    $('.timelineNav div a.next').click(function() {
        if (!($('.timelineNav .ui-tabs-nav li:last').hasClass('ui-tabs-selected'))) {                                                        
            
            // make next li element the selected one
            $('.timelineNav .ui-tabs-nav .ui-tabs-selected').removeClass('ui-tabs-selected').next().addClass('ui-tabs-selected');    
            
            // switch out timelines
            $('#sloanAtAGlance .timeline .ui-tabs-panel').each(function() {
                if (!($(this).hasClass('ui-tabs-hide'))) {
                    $(this).addClass('ui-tabs-hide').next().removeClass('ui-tabs-hide');
                    return false;
                }
            });
            $('#productInnovations .timeline .ui-tabs-panel').each(function() {
                if (!($(this).hasClass('ui-tabs-hide'))) {
                    $(this).addClass('ui-tabs-hide').next().removeClass('ui-tabs-hide');
                    return false;
                }
            });
            $('#peopleAndCulture .timeline .ui-tabs-panel').each(function() {
                if (!($(this).hasClass('ui-tabs-hide'))) {
                    $(this).addClass('ui-tabs-hide').next().removeClass('ui-tabs-hide');
                    return false;
                }
            });
            
            // update left side nav
            $('.sideNav ul li.sub ul li.on').removeClass('on').next().addClass('on');
            
            
            if ($('.sideNav ul li ul li.last').hasClass('on')) { 
                $('.sideNav ul li ul li.last.on').attr("style","background: url(/images/backgrounds/innerNav.ul.li.ul.li.last.hover.png) 0 bottom no-repeat !important");
            } else {
                $('.sideNav ul li ul li.last').attr("style","");    
            }
        }
    });
    $('.timelineNav div a.prev').click(function() {
        // check to see if the first element has the selected class, if not, then click through timeline
        if (!($('.timelineNav .ui-tabs-nav li:first').hasClass('ui-tabs-selected'))) {                                                    
            
            // make previous li element the selected one
            $('.timelineNav .ui-tabs-nav .ui-tabs-selected').removeClass('ui-tabs-selected').prev().addClass('ui-tabs-selected');
        
            // switch out timelines
            $('#sloanAtAGlance .timeline .ui-tabs-panel').each(function() {
                if (!($(this).hasClass('ui-tabs-hide'))) {
                    $(this).addClass('ui-tabs-hide').prev().removeClass('ui-tabs-hide');
                    return false;
                }
            });
            $('#productInnovations .timeline .ui-tabs-panel').each(function() {
                if (!($(this).hasClass('ui-tabs-hide'))) {
                    $(this).addClass('ui-tabs-hide').prev().removeClass('ui-tabs-hide');
                    return false;
                }
            });
            $('#peopleAndCulture .timeline .ui-tabs-panel').each(function() {
                if (!($(this).hasClass('ui-tabs-hide'))) {
                    $(this).addClass('ui-tabs-hide').prev().removeClass('ui-tabs-hide');
                    return false;
                }
            });
            
            // update left side nav
            $('.sideNav ul li.sub ul li.on').removeClass('on').prev().addClass('on'); 
            $('.sideNav ul li ul li.last').attr("style",""); 
        }
    });
    
    //$('.timelineNav ul li a').each(function() {
    $('.timelineNav ul li a').click(function() {
        // update the nav on the left hand side
        var t = $(this).attr("href");
        var x = t.substring(0,t.length-3);
        
        // remove all selected classes from li's in each panel
        $('.timelineNav ul li').each(function() {
            $(this).removeClass('ui-tabs-selected');                                      
        });
        
        // "select" other li's with similar base href in a tag
        $('.timelineNav ul li a').each(function() {
            var t2 = $(this).attr("href");
            var x2 = t2.substring(0,t2.length-3);
            
            if (x2 == x) {
                $(this).parent().addClass('ui-tabs-selected');
            }
        });
        
        // show correct panel (for each tab)
        $('.timeline .ui-tabs-panel').each(function() {
            var t3 = $(this).attr("id");
            var x3 = '#' + t3.substring(0,t3.length-3);
            
            if (x3 == x) {
                $(this).removeClass('ui-tabs-hide');    
            } else {
                $(this).addClass('ui-tabs-hide');    
            }
        });
        
        // clear out "on" state of each nav item, except one that matches timeID
        $('.sideNav ul li.sub ul li').each(function() {
            $(this).removeClass("on");
            if ($(this).children("a").attr("href") == x) {
                $(this).addClass("on");    
            }
        });
    });
    //});
    
    $('.timelineDivImage img').each(function() {
        $(this).pngfix();                                       
    });
    
    /***** Technical Downloads page *****/
    
    $('#accordion').css("visibility","visible");
    
    $('#accordion div ul li a').click(function() {
        if (!($(this).parent().parent().hasClass("downloadList"))) {
            $(this).toggleClass("open");
            $(this).next().toggleClass("downloadList");
        }
    });    
    
    $('#accordion div ul li ul li a').click(function() {
        if (!($(this).prev().is(":checked"))) {
            $(this).prev().attr("checked","checked");                        
        } else {
            $(this).prev().attr("checked","");
        }
    });
    
    /***** Our Products page *****/
    
    $('.majorProductFamily').each(function() {
        $(this).mouseenter(function() {
            $(this).addClass("over");                                
        });
        $(this).mouseleave(function() {
            $(this).removeClass("over");                            
        });
    });
    
    $('.majorProductFamily img').pngfix();
    $('.majorProductFamily .gradient').pngfix();
    
    /***** Compare page *****/
    
    var thisHeight = 0;
    var tallestHeight = 0;
    
    var compareArray = new Array('.productCompareColumn span','.productCompareColumn .description','.productCompareColumn .manualVsSensorOperated','.productCompareColumn .flushRate','.productCompareColumn .concealedVsExposedValue','.productCompareColumn .powerSupply','.productCompareColumn .finish','.productCompareColumn .dualOrSingleFlush','.productCompareColumn .retrofit','.productCompareColumn .publicVsPrivateSpace');
    
    for (x=0;x<compareArray.length;x++) {
        
        // go through each element to find tallest height
        $(compareArray[x]).each(function() {
            thisHeight = $(this).height();
            if (tallestHeight < thisHeight) {
                tallestHeight = thisHeight;    
            }
        });
        
        // assign tallest height to each element
        $(compareArray[x]).each(function() {
            $(this).css("height",tallestHeight);                                              
        });
        
        // reset values
        thisHeight = 0;
        tallestHeight = 0;
        
    }
    
    $('a.close').each(function() {
        $(this).click(function() {
            $(this).parent().parent().css("display","none");                  
        });
    });
    
    
    $('.productCompareColumn').each(function() {
        $(this).css("visibility","visible");
    });
        
    /***** Dates and Events page *****/
    
    $('.events').css("visibility","visible");
    
    $('a.dateHeader').click(function() {
        if (!($(this).hasClass("sortByDate"))) {
            $('a.eventHeader').removeClass("sortByEvent");
            $(this).addClass("sortByDate");
        }
    });
    
    $('a.eventHeader').click(function() {
        if (!($(this).hasClass("sortByEvent"))) {
            $('a.dateHeader').removeClass("sortByDate");
            $(this).addClass("sortByEvent");
        }
    });      
    
    /***** Fixture Compatibility page *****/
    
    $('.resultsTable td').hover(function() {
        $(this).parent().addClass("rowHover");                                           
    }, function() {
        $(this).parent().removeClass("rowHover");
    });
    
    $('#compatibleFixtures th a').each(function() {
        $(this).click(function() {
            $('#compatibleFixtures th a').each(function() {
                $(this).removeClass("sortByThis");                                    
            });
            $(this).addClass("sortByThis");                       
        });
    });
        
    /***** Our Global Reach page *****/
    
    $('#mapResults th a').each(function() {
        $(this).click(function() {
            $('#mapResults th a').each(function() {
                $(this).removeClass("sortByThis");                                    
            });
            $(this).addClass("sortByThis");                       
        });
    });
    
    
    /***** Search Results page *****/
    
    $('#searchboxquery').click(function() {
        if ($(this).val() == "Search") {
            $(this).val("");
        }
    });
    if ($.browser.mozilla) {
        $('.firstCol .searchbox .searchType ul li label').css("position","relative").css("top","-3px");
    };
    
    /***** Self-Service Troubleshooting page *****/
    
    var w = $('.productAnnotations .pictureGroup li').length * 63;
    $('.productAnnotations .pictureGroup').css('width',w+"px");
    
    $('.productAnnotations .pictureGroup li a').each(function() {
        $(this).click(function() {
            removeCurrentClass();
            $(this).addClass("current");
            
            var thumbnail = $(this).children("img").attr("src");
            var t = thumbnail.substr(0,thumbnail.length-10);
            var i = t + ".jpg";
            var list = $('#' + i.substr(7,3) + "-list");
            
            $('#mainProductAnnotation').attr("src", i);
            
            $('.productAnnotations ol').each(function() {
                $(this).hide();                                          
            });
            list.show();
            
        });
    });
    
    $('.sideNav ul li ul li').each(function() {
        if ($(this).height() > 30) {
            if ($(this).hasClass("on")) {
                $(this).css("background-image","url(/images/backgrounds/innerNav.ul.li.ul.li.hover.twoline.png)");
                $(this).hover(function() {
                    $(this).css("background-image","url(/images/backgrounds/innerNav.ul.li.ul.li.hover.twoline.png)");
                }, function() {
                    $(this).css("background-image","url(/images/backgrounds/innerNav.ul.li.ul.li.hover.twoline.png)");
                });
                $(this).children("a").hover(function() {
                    $(this).attr("style","background-image: none !important");
                
                    if ($.browser.safari) {
                        $(this).children("a").css('padding-bottom','9px');    
                    }
                }, function() {
                    $(this).attr("style","background-image: none !important");
                
                    if ($.browser.safari) {
                        $(this).children("a").css('padding-bottom','9px');    
                    }
                });
            } else {
                $(this).children("a").hover(function() {
                    $(this).attr("style","background-image: url(/images/backgrounds/innerNav.ul.li.ul.li.hover.twoline.png) !important");
                
                    if ($.browser.safari) {
                        $(this).children("a").css('padding-bottom','9px');    
                    }
                }, function() {
                    $(this).attr("style","background-image: none !important");
                
                    if ($.browser.safari) {
                        $(this).children("a").css('padding-bottom','9px');    
                    }
                });
            }
        }
    });
    

    /***** Water Savings Calculator page *****/
    
    $('label.unchecked').click(function() {
        $(this).parent().parent().children("li").each(function() {
            // deselect all checked options
            if ($(this).children("label").hasClass("checked")) {
                $(this).children("label").removeClass("checked");
                $(this).children("label").addClass("unchecked");
                $(this).children("input").attr("checked","");
            }
        });
        // check hidden input
        $(this).prev().attr("checked","checked");
        // display checked background image
        $(this).removeClass("unchecked");
        $(this).addClass("checked");
    });
    
    $('#calculator a.next').click(function() {
        $(this).parent().children(".step").each(function() {
            if (!($(this).is(":hidden"))) {
                $(this).hide();
                $(this).next().show();
                return false;
            }
        });
        
        var m = $(this).next().children("span").css("margin-top");
        var m2 = eval(m.substring(0, m.length-2));
        var m2 = m2 - 11 + "px";
        $(this).next().children("span").css("margin-top",m2);
    });
    
    $('#calculator .progress span').pngfix();

    /***** Where to buy page *****/
    
    $('#zipcodeSearch').click(function() {
        if ($(this).val() == "60131") {
            $(this).val("");
        }
    });
    
    if ($.browser.mozilla) {
        $('.firstCol .whereFilters ul li label').css("position","relative").css("top","-3px");
    };
    
    /***** right side modules *****/
    
    $('.module').each(function() {
                                                    
            $(this).mouseenter(function() {
                $(this).addClass("rollover");
                $(this).children("img.arrow").attr("src","/images/icons/module.arrow.blue.png");
                if ($(this).children("img.moduleIcon").attr("src") != null) {
                    // get original src path
                    var origSrc = $(this).children("img.moduleIcon").attr("src");
                    // replace .jpg with .over.jpg
                    var moduleIconSrc = origSrc.substr(0,origSrc.length-4) + ".over.jpg";
                    // update image path
                    $(this).children("img.moduleIcon").attr("src",moduleIconSrc);    
                }
            });
            $(this).mouseleave(function() {
                $(this).removeClass("rollover");
                $(this).children("img.arrow").attr("src","/images/icons/module.arrow.gray.png");
                if ($(this).children("img.moduleIcon").attr("src") != null) {
                    // get mouseover src path
                    var origSrc = $(this).children("img.moduleIcon").attr("src");
                    // take away .over.jpg and replace with .jpg
                    var moduleIconSrc = origSrc.substr(0,origSrc.length-9) + ".jpg";
                    // return to base image path
                    $(this).children("img.moduleIcon").attr("src",moduleIconSrc);    
                }
            });
            if ($(this).children('#datepicker').length == 0) {
                $(this).children("a").mouseenter(function() {
                    $(this).next().attr("src","/images/icons/module.arrow.hover.png");
                });
                $(this).children("a").mouseleave(function() {
                    $(this).next().attr("src","/images/icons/module.arrow.blue.png");
                });
                $(this).click(function() {
                    // JK - Top Products and others should not link here
                    //window.location.href = $(this).children("a").attr("href");                            
                });
            }
    });
    
    /***** global functions *****/
    if ($.browser.msie && $.browser.version < 7) {
        $('.sideNav ul li.sectionTitle a span').pngfix();    
    }
    $('.sideNav ul li.sectionTitle a').hover(function() {
        $(this).children("span").css("margin-top","-21px");                              
    }, function() {
          $(this).children("span").css("margin-top","0");
    });
    $('.sideNavBtm').pngfix();
    
        if ($('.sideNav ul li ul li.last').hasClass('on')) {
            $('.sideNav ul li ul li.last.on').attr("style","background: url(/images/backgrounds/innerNav.ul.li.ul.li.last.hover.png) 0 bottom no-repeat !important");
        } else {
            $('.sideNav ul li ul li.last').attr("style","");
        }
    
    });
 
// JK 11/8/2009 - Our Global Reach Business Rules
function updateFacilityType(oList1, oList2){
    if(oList1.value == 'US'){
        RemoveItemFromList(FindObj("mapFacility"), 0);
    }else{
        AddItemToListHead(oList2, -1, "Select All")
    }
}
// JK 11/8/2009 - Generic List Functions
function AddItemToListTail(oList, value, text){
   for (var i = 0; i < oList.length; i++) {
      if (oList.options[i].value == value && oList.options[i].text == text)
         return;
   }
   oList.length += 1;
   oList.options[i].value = value;
   oList.options[i].text = text;
}
function AddItemToListHead(oList, value, text){
   for (var i = 0; i < oList.length; i++) {
      if (oList.options[i].value == value && oList.options[i].text == text)
         return;
   }
   oList.length += 1;
   oList.options[i].value = "temp";
   oList.options[i].text = "temp";
   for (var i = oList.length - 1; i > 0; i--) {
      oList.options[i].value =  oList.options[i-1].value
      oList.options[i].text = oList.options[i-1].text
   }
   oList.options[0].value = "-1";
   oList.options[0].text = "Select All";
}
function RemoveItemFromList(oList, index){
   if (index < 0) return;
   for (var i = index; i < oList.length - 1; i++) {
      oList.options[i].value = oList.options[i+1].value;
      oList.options[i].text = oList.options[i+1].text;
      oList.options[i].selected = oList.options[i+1].selected;
   }
   if (oList.length > 0)
      oList.length -= 1;
}
function FindObj(n, d){
  var p,i,x; if(!d) d=document;
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document;
    n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) x=d.all[n];
  if(x) return x;
  for (i=0;i<d.forms.length;i++) {x=d.forms[i][n]; if(x) return x;}
if (d.layers)
  for(i=0;i<d.layers.length;i++) {x=FindObj(n,d.layers[i].document); if(x) return x;}
  if(parseInt(navigator.appVersion) >= 5){
    x=document.getElementsByName(n);
    if (x) {
        if (x.length==1) x = x[0];
        else if (x.length==0) {x = document.getElementById(n);}
    } else {
        x = document.getElementById(n);
    }
  }
  return x;
}