
// we want to make sure that the hover state images don't need to load on the actual hover
var preload = [
    "/img/global/sitebar/link-hover-l.gif",
    "/img/global/sitebar/link-hover-r.gif",
    "/img/global/sitebar/dropdown-top.png",
    "/img/global/sitebar/dropdown-bottom.png",
    "/img/global/th3/subitem-bottom-border.gif"
];
setTimeout(function() {
    preload.each(function(img) {
        new Element('img', { 'src': (window.CRS) ? '/static'+img:img });
    });
}, 10);

document.observe('dom:loaded', function(e) {
    $$('ol#sitenav > li').each(function(el) {
        new Tablet.HoverElement(el, {hasActive:true, cssHover:true});
    });
    if ($('subnav')) {
        $$('ol#subnav li.hoverparent').each(function(el) {
            new Tablet.HoverElement(el);
        });
    }
    // this search thing is no longer here
    // var sitebar = $('sitebar');
    // if (sitebar) {
    //     var frm = sitebar.down('form.findlocation');
    //     if (frm) {
    //         var npt = frm.down('[name=ftl]');
    //         
    //         if (npt) {
    //             var sbmt = npt.next('input.hoverelement');
    //             if (sbmt) {
    //                 npt.observe('focus', function(e) {
    //                     sbmt.activate();
    //                 });
    //                 
    //                 npt.observe('blur', function(e) {
    //                     sbmt.deactivate();
    //                 });
    //             }
    //         }
    //     }
    // }
});

document.observe('dom:loaded', function(e) {
    new Tablet.Search.TextInput('finddestination', {
        url:'/javascript/LocationComplete',
        completer:'destinationcomplete',
        select:false
    });
});
Tablet = Tablet || {};

Tablet.Ajaxificator = Class.create({
    initialize: function(link) {
        this.link = $(link);
        this.options = Object.extend({
            auth: false,
            handle401:false,
            beforeStart: function() {},
            ajaxOptions: {}
        }, arguments[1] || {});

        this.initializeEvents();
    },
    initializeEvents: function() {
        Event.observe(this.link, 'click', this.clickHandler.bind(this));
    },
    unloadEvents: function() {
        Event.stopObserving(this.link, 'click');
    },
    clickHandler: function(event) {
        this.options.beforeStart.apply(this, []);
        if (!this.options.auth) {
            this.ajaxHandler();
        } else {
            this.authHandler();
        }
        event.stop();
    },
    deniedHandler: function(resp) {
        var message = '';
        if (resp.headerJSON && resp.headerJSON.message) {
            message = resp.headerJSON.message;
        }
        // check for message back from server
        
        Tablet.EnterSite.pane.page('loginForm', {
            onShow:function() {
                var action = {action:this.ajaxHandler.bind(this), args:[this]};
                Tablet.EnterSite.setManeuver(null, action); // sending null will create one automatically
            }.bind(this),
            message:message
        });
    },
    ajaxHandler: function() {
        var respFunctions = {};
        if (this.options.handle401) {
            respFunctions = {
                on401:this.deniedHandler.bind(this),
                on403:this.deniedHandler.bind(this)
            };
        }
        var options = Object.extend(respFunctions, this.options.ajaxOptions);
        new Ajax.Request(
            this.link.href, 
            options
        );
    },
    authHandler: function() {
        // man there's a lot of binding going on here
        Tablet.EnterSite.isRecognized({
            onSuccess:this.ajaxHandler.bind(this),
            onDenied:function(resp) {
                var message = '';
                // check for message back from server
                
                Tablet.EnterSite.pane.page('loginForm', {
                    onShow:function() {
                        var action = {action:this.ajaxHandler.bind(this), args:[this]};
                        Tablet.EnterSite.setManeuver(null, action); // sending null will create one automatically
                    }.bind(this)
                });
            }.bind(this)
        });
    }
});

function switchProductDetailTab(code,hcolor){
    if(!hcolor) hcolor='#E5E5E5'
    nav = document.getElementById('document');
    tab = document.getElementById('tab_'+code);
    tabs = $(document).getElementsByClassName('tabs');
    block = document.getElementById('tabBlock_'+code);
    blocks = $(document).getElementsByClassName('tabBlocks');
    for(i=0;i<tabs.length;i++){
        tabs[i].style.backgroundColor='#E5E5E5';
    }
    for(i=0;i<blocks.length;i++){
        blocks[i].style.display='none';
    }
    tab.style.backgroundColor=hcolor;
    block.style.display='block'
}

Event.onReady(function() {
    if($('ShopDetail')){
        $A($('ShopDetail').getElementsByClassName('tabBlocks')).each(
            function(e){if(e.offsetHeight<261){e.style.height='261px'}
        })
    }                          
})