/*

@requires: YAHOO.dom,YAHOO.event,YAHOO.util

*/

if (typeof DrillSpot == "undefined")
{
    var DrillSpot = { };
}

var JSONRequest = 
{
    _objects : [],

    go : function(url,callback)
    {
        this.clear();

        var obj = document.createElement("script");
        obj.setAttribute("type", "text/javascript");
        obj.setAttribute("src", url + "&callback=" + callback);
        
        this._objects.push(obj);
        document.body.appendChild(obj);
    },

    clear : function()
    {
        for (var i = 0; i < this._objects.length; i++)
            document.body.removeChild(this._objects[i]);
        this._objects = [];
    }
};


YAHOO.util.Event.onDOMReady(function(e) { DrillSpot.init(); });



DrillSpot.init = function() {
    DrillSpot.login.init();
    DrillSpot.menu.buildMenu('main_menu');

//    if (document.location.href.indexOf('/go/cart') > 0) {
//        
//    }

};

DrillSpot.Dom = 
{
    getWidth: function(el)
    {
        if (!el) return null;        
        return parseInt(YAHOO.util.Dom.getStyle(el, 'width'));
    },
    getHeight: function(el)
    {
        if (!el) return null;
        return parseInt(YAHOO.util.Dom.getStyle(el, 'height'));
    },
    getX: function(el)
    {
        if (!el) return null;
        return parseInt(YAHOO.util.Dom.getStyle(el, 'left'));
    },
    getY: function(el)
    {
        if (!el) return null;
        return parseInt(YAHOO.util.Dom.getStyle(el, 'top'));
    },
    setWidth: function(el,w)
    {
        if (!el) return;
        YAHOO.util.Dom.setStyle(el, 'width', w + 'px');
    },
    setHeight: function(el,h)
    {
        if (!el) return;
        YAHOO.util.Dom.setStyle(el, 'height', h + 'px');
    },
    setX: function(el,x)
    {
        if (!el) return;
        YAHOO.util.Dom.setStyle(el, 'left', x + 'px');
    },
    setY: function(el,y)
    {
        if (!el) return;
        YAHOO.util.Dom.setStyle(el, 'top', y + 'px');
    },
    setXY: function(el,x,y)
    {
        this.setX(el,x);
        this.setY(el,y);
    },
    setZ: function(el,z)
    {
        if (!el) return;
        YAHOO.util.Dom.setStyle(el, 'zIndex', z);
    },
    fade: function(el,f)
    {
        if (!el) return;
        YAHOO.util.Dom.setStyle(el, 'opacity', f);
    },
    show: function(el)
    {
        if (!el) return;
        YAHOO.util.Dom.setStyle(el, 'display', 'block');
    },
    hide: function(el)
    {
        if (!el) return;
        YAHOO.util.Dom.setStyle(el, 'display', 'none');
    },
    invisible: function(el)
    {
        if (!el) return;
        YAHOO.util.Dom.setStyle(el, 'visibility', 'hidden');
    },
    visible: function(el)
    {
        if (!el) return;
        YAHOO.util.Dom.setStyle(el, 'visibility', 'visible');
    },
    hideClass: function(el)
    {
        this.setClass(el,'');
    },
    setClass: function(el,cls)
    {
        if (typeof el == 'string') el = document.getElementById(el);
        if (!el) return;
        el.className = cls;
    },
    getCenterX: function(el)
    {
        var w = YAHOO.util.Dom.getViewportWidth();
        var eW = DrillSpot.Dom.getWidth(el);
        
        if (eW == 0) eW = w / 2;
        
        return Math.round((w - eW) / 2);
    },
    getCenterY: function(el,eH)
    {
        eH = eH | DrillSpot.Dom.getHeight(el);
    
        var sy = YAHOO.util.Dom.getDocumentScrollTop();
        var h = YAHOO.util.Dom.getViewportHeight();
        
        return Math.round((h - eH) / 2) + sy;
    },
    isDisplayed : function(el)
    {
        var x = YAHOO.util.Dom.getStyle(el,'display');
        if (x == 'block') return true;
        else return false;
    }
};

DrillSpot.Event =
{
    execAction: function(el)
    {
        if (!el) return;
        
        if (el.href) document.location.href = el.href;
        else if (el.click) el.click();
    },
    execEvent: function(e)
    {
        if (!e) return;
        
        if (e.target) DrillSpot.Event.execAction(e.target);
        else if (e.srcElement) DrillSpot.Event.execAction(e.srcElement);
    }
};

DrillSpot.ui = 
{
    select: function(el)
    {
        DrillSpot.Dom.setClass(el,'selected');
    },
    selectTab: function(btnID,tabID)
    {
        DrillSpot.Dom.hideClass(this.selectedButton);
        DrillSpot.Dom.hide(this.selectedTab);
        
        this.selectedButton = document.getElementById(btnID);
        this.selectedTab = document.getElementById(tabID);
        
        this.select(this.selectedButton);
        DrillSpot.Dom.show(this.selectedTab);
    },
    toggle: function(el)
    {
        var x = YAHOO.util.Dom.getStyle(el,'display');
        if (x == 'block') DrillSpot.Dom.hide(el);
        else DrillSpot.Dom.show(el);
    },
    setImage: function(img,src)
    {
        if (typeof img == 'string') img = document.getElementById(img);
        if (!img) return;
        
        img.oldSrc = img.src;
        
        var newImg = new Image();
        newImg.src = src;
        
        YAHOO.util.Event.addListener(newImg, "load", 
            function(e)
            {
                img.src = src;
            });
        
        var mOut = function(e)
        {
            if (!this.oldSrc) return;
            this.src = this.oldSrc;
        };
        
        YAHOO.util.Event.removeListener(img, "mouseout", mOut);
        YAHOO.util.Event.addListener(img, "mouseout", mOut);
    },
    renderError: function(msg)
    {
        var msg_str = '';
        for (var i = 0; i < msg.length; i++)
            msg_str += '\t - ' + msg[i] + '\n';
            
        alert('Please enter:\n' + msg_str);
    },
    clickConfirm: function(e)
    {
        var sure = confirm('Are you sure?');
        if (!sure) 
        {
            YAHOO.util.Event.preventDefault(e);
            return false;
        }
        return true;
    },
    initPrompt: function()
    {
        this.promptPanel = new YAHOO.widget.Panel('confirmPanel', { width:"320px", visible:false, constraintoviewport:true, fixedcenter: true });
        this.promptPanel.render();
    },
    showPrompt: function(e)
    {
        YAHOO.util.Event.preventDefault(e);
        
        var yes = document.getElementById('confirmPanel_yes');
        
        var p = this.promptPanel;
        
        var action = e.srcElement || e.target;
        
        var yesFn = function(ev)
        {
            p.hide();
            DrillSpot.Event.execAction(action);
        };
        YAHOO.util.Event.purgeElement(yes);
        YAHOO.util.Event.addListener(yes, 'click', yesFn);
        
        var no = document.getElementById('confirmPanel_no');
        
        var noFn = function(ev)
        {
            p.hide();
        };
        YAHOO.util.Event.purgeElement(no);
        YAHOO.util.Event.addListener(no, 'click', noFn);

        p.show();
    },
    fadeIn : function(element,opacity)
    {
        opacity = opacity || 0.1;
    
        if (opacity > 1) return;
        
        DrillSpot.Dom.fade(element, opacity);
        opacity += 0.1;
        
        window.setTimeout(function() { DrillSpot.ui.fadeIn(element,opacity); }, 20);
    },
    fadeImageIn: function(id,opacity)
    {
        opacity = opacity || 0.1;
    
        if (opacity > 1) return;

        var img = document.getElementById(id);
        if (!img) return;
        
        DrillSpot.Dom.fade(img, opacity);
        opacity += 0.1;
        
        window.setTimeout("DrillSpot.ui.fadeImageIn('" + id + "'," + opacity + ")", 20);
    },
    showBackDrop: function()
    {
        var w = YAHOO.util.Dom.getDocumentWidth();
        var h = YAHOO.util.Dom.getDocumentHeight();
        
        var bg = document.getElementById('backdrop');
        DrillSpot.Dom.setWidth(bg,w);
        DrillSpot.Dom.setHeight(bg,h);
        DrillSpot.Dom.show(bg);
        DrillSpot.Dom.fade(bg,0.7);
    },
    hideBackDrop: function()
    {
        var bg = document.getElementById('backdrop');
        DrillSpot.Dom.hide(bg);
    }
};

DrillSpot.util =
{
    getUrlParam: function(strParamName)
    {
        var strReturn = "";
        var strHref = window.location.href;
        if ( strHref.indexOf("?") > -1 )
        {
            var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
            var aQueryString = strQueryString.split("&");
            for ( var iParam = 0; iParam < aQueryString.length; iParam++ )
            {
                if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 )
                {
                    var aParam = aQueryString[iParam].split("=");
                    strReturn = aParam[1];
                    break;
                }
            }
        }
        return unescape(strReturn);
    },
        
    bind : function(input, button) {            
        this.onEnter(input, function() {        
            var b = YAHOO.util.Dom.get(button);  
            if (b) document.location.href = b.href;        
        });                  
    },
    
    onEnter : function(input, callback) {    
        YAHOO.util.Event.on(input, "keypress", 
            function(e) {    
                if (e.which == 13 || e.keyCode == 13) {
                    YAHOO.util.Event.preventDefault(e);  
                    callback(e);
                }
            });      
    },    
    
    formatCurrency : function(value)
    {
        value = Math.round(value * 100) / 100;
    
        var str = (value == 0) ? '0.00' : value.toString();
        
        if (str.indexOf('.') < 0) str += '.00';
        
        var dec = str.substring(str.indexOf('.') + 1, str.length);
        if (dec.length == 1) dec += '0';
        
        var num = str.substring(0, str.indexOf('.'));
        
        var numf = '';
        for (i = num.length - 1; i >= 0; i--)
        {
            numf = num.charAt(i) + numf;
            if ((num.length - i) % 3 == 0 && i != 0) numf = ',' + numf;
        }
        
        num = numf + '.' + dec;
        
        return '$' + num;
    },
    testCookie : function()
    {
        document.cookie += "hsh=hsh;";
        var hsh = DrillSpot.util.readCookie('hsh');
        if (hsh) window.location = document.referrer;
        else alert('You do not have cookies enabled! Please follow instructions below.');
    },
    readCookie : function(name) 
    {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0; i < ca.length; i++) 
        {
	        var c = ca[i];
	        while (c.charAt(0)==' ') c = c.substring(1,c.length);
	        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }
};

DrillSpot.login =
{
    _formXID : 'loginX',
    _loginFail : null,
    _regFail : null,

    showNew : function()
    {
        DrillSpot.Dom.hide('login_Existing');
        DrillSpot.Dom.show('login_New');
    },
    showExisting : function()
    {
        DrillSpot.Dom.show('login_Existing');
        DrillSpot.Dom.hide('login_New');
    },
    close : function()
    {
        DrillSpot.ui.hideBackDrop();
        
        var login = document.getElementById('login_form');
        DrillSpot.Dom.hide(login);
        DrillSpot.Dom.hide(this._loginFail);
        DrillSpot.Dom.hide(this._regFail);
    },
    show : function(r)
    {
        DrillSpot.ui.showBackDrop();
        
        var sx = Math.floor(YAHOO.util.Dom.getDocumentScrollTop());

        var login = document.getElementById('login_form');
        var x = DrillSpot.Dom.getCenterX(login);
        var y = DrillSpot.Dom.getCenterY(login,350);

        if (r)
        {
            var reff = document.getElementById('ctl00_ccLogin_ccLoginReff');
            if (reff) reff.value = r;
        }

        DrillSpot.Dom.setXY(login,x,y);
        DrillSpot.Dom.setZ(login,100);
        DrillSpot.Dom.show(login);
    },
    init : function()
    {
        this._loginFail = document.getElementById('ctl00_ccLogin_ccLoginFail');
        this._regFail = document.getElementById('ctl00_ccLogin_ccRegisterFail');
        
        if (this._regFail || this._loginFail) DrillSpot.login.show();
    }
};