
(function(){
    var timeout         = 500; //500ms = half a second
    var closetimer		= 0;
    var ddmenuitem      = 0;

    function jsddm_close(){
        if(ddmenuitem){
            ddmenuitem.css('visibility', 'hidden');
            ddmenuitem.parent().removeClass('hover');
        }
    }

    $('#productLanding>li,#productNavStrip2>li').hover(
        function () {
            //cancel timer
            if(closetimer) {
                window.clearTimeout(closetimer);
                closetimer = null;
            }
            //close menu
            jsddm_close();
            ddmenuitem = $(this).find('div').eq(0).css('visibility', 'visible');
            $(this).addClass('hover');
            //$(this).find('div').eq(0).fadeIn('slow');
        },
        function () {
            closetimer = window.setTimeout(jsddm_close, timeout);
        }
    );
    
    //if we click anywhere else, close the menu
    $(document).click(jsddm_close);
    
    //make our js-friendly urls.
    $('#productLanding a,#productNavStrip2 a,#flavors a').each(function(){
        this.href = this.href.replace(/(detail.aspx\/.*?)\/(.*)/,'$1#/$2');
    });
    

    //poll the url hash and show the appropriate product .. if we're on a product page (have 'data' available)
    if(window.data){
    
        var currentHash;
        
         
                
        setInterval(function(){
            
            if(currentHash != location.hash){
                var family = data.Name;
                
                var hash = location.hash.replace('#/','');
                
                // if, for example, we are on /smallmighty, length will be 0, if we are on /smallmighty/wildfresh, length will be 1
                var pdata = hash.length > 0 ? $.grep(data.Scents, function(n){
                    return n.Name == hash;
                })[0] : data.Scents[0];
                
                var product = pdata.Name;
                
                //fix/hack: powders do not have individual product pages..
                if(family == 'powders') product = family;
                
                //<update the page title>
                //define html entities to replace with utf-16 versions
                var map = [
                    ['&reg;','\u00AE'],
                    ['&trade;','\u2122'],
                    ['&rsquo;', '\u2019'],
                    ['&amp;', '\u0026'],
                    ['\u00C2',''] //replace the acute 'A' with .. nothing.
                ];
                var t = pdata.PageTitle;
                
                //perform all html entity replacements
                for(var i=0;i<map.length;i++) t = t.split(map[i][0]).join(map[i][1]);
                
                //update the page title.
                document.title = t;
                //</update the page title>
                
                //update the product quote
                $('#productDisplay p').html('<span class="quote_left"><span>&ldquo;</span></span>' + pdata.Quote + '<span class="quote_right"><span>&rdquo;</span></span>');
                
                // not applicable for powders.
                if(family != 'powders'){
                    //update the [large] product image
                    $('#productDisplay').css('background-image','url(' + pdata.ImageUrl + ')');
                    
                    //update the the list of available sizes (oz/loads)
                    $('#sizes ul').html( $.map(pdata.Sizes,function(s){return '<li id="size_' + s + '">' + s + '</li>';}).join('') );
                    
                    //update the display of the selected flavor (scent)
                    $('#flavors li').removeClass('active');
                    $('#flavors li#' + product).addClass('active');
                }
                //keep track of what the currentHash is so we only update product display attributes if we have a new prodoct
                currentHash = location.hash;
            }
            
        }, 100);
        
    }

})();
