/*

search suggest box
@requires: DrillSpot, YAHOO

*/

DrillSpot.search.SuggestKey = function(keyCode) {

    

};

DrillSpot.search.Suggest = {

    _URL : 'http://216.24.138.210/Suggest',
    
    init : function()
    {
        YAHOO.util.Event.addListener(document.body, 'click', DrillSpot.search.Suggest.release);
    
        var queryBox = document.getElementById('searchQuery');
            
        YAHOO.util.Event.addListener(queryBox, 'keyup', 
            function(e)
            {
                if (e.keyCode == 40 || e.keyCode == 38)
                {
                    DrillSpot.search.Suggest.panel.scroll((e.keyCode == 40) ? 1 : -1);
                }
                else if (e.keyCode == 27 || e.keyCode == 13)
                {
                    DrillSpot.search.Suggest.panel.close();
                }
                else if (e.keyCode == 37 || e.keyCode == 39 || e.keyCode == 35 || e.keyCode == 36)
                {
                }
                else
                {
                    DrillSpot.search.Suggest.suggest(this.value);
                }
            });

        this.panel = new DrillSpot.search.SuggestPanel(queryBox);
    },
    
    release : function(e)
    {
        var x = e.clientX;
        var y = e.clientY;
        var r = YAHOO.util.Dom.getRegion(DrillSpot.search.Suggest.panel.container);
        
        if (r)
        {
            if (x < r.left || x > r.right || y < r.top || y > r.bottom)
            {
                DrillSpot.search.Suggest.panel.close();
                YAHOO.util.Event.preventDefault(e);
                return false;
            }
        }
        
        return true;
    },
    
    suggest : function(phrase)
    {
        if (!phrase || phrase.length == 0)
        {
            this.panel.close();
            return;
        }

        phrase = phrase.toLowerCase();

        JSONRequest.go(this._URL + "?phrase=" + escape(phrase), "DrillSpot.search.Suggest.render");
    },
    
    render : function(list)
    {
        if (list && list.length > 0)
            this.panel.render(list);
        else
            this.panel.close();
    }
};



DrillSpot.search.SuggestPanel = function(queryBox)  {

    this.queryBox = queryBox;

    var r = YAHOO.util.Dom.getRegion(this.queryBox);

    this.container = document.createElement('div');
    this.container.style.display = 'none';
    this.container.style.position = 'absolute';
    this.container.style.top = r.bottom + 'px';  
    this.container.style.left = r.left + 'px';

    this.panel = document.createElement('div');
    this.panel.className = 'suggest_panel';
    
    var t =  document.createElement('table');
    t.setAttribute('cellspacing','0');
    t.setAttribute('cellpadding','0');
    
    this.panel.list = document.createElement('tbody');
    this.panel.list.setAttribute('cellspacing','0');
    
    t.appendChild(this.panel.list);
    
    this.panel.appendChild(t);
    this.container.appendChild(this.panel);

    var href = document.location.href;
    if (href.indexOf('/checkout/') == -1 && href.indexOf('/submitpayment/') == -1)
    {
        if (YAHOO.env.ie)
            document.body.childNodes[0].appendChild(this.container);
        else
            document.body.appendChild(this.container);
    }
    
    this.currentIndex = -1;
    this.active = true;
};

DrillSpot.search.SuggestPanel.prototype =  {

    close : function()
    {
        DrillSpot.Dom.hide(this.container);
    },
    
    clear : function()
    {
        this.currentIndex = -1;
        var l = this.panel.list;
        while (l.childNodes.length > 0)
            l.removeChild(l.childNodes[0]);
    },
    
    render : function(list)
    {
        this.clear();

        var me = this;
        
        for (var i = 0; i < list.length; i++) {
        
            var tr = document.createElement('tr');
            tr.className = 'sugg_pan_row';
            tr.index = i;
            
            var td_tag = document.createElement('td');
            td_tag.innerHTML = list[i].t;
            tr.appendChild(td_tag);
            tr.tag = list[i].t;
            tr.tagClean = tr.tag.replace(/<\/?[^>]+>/g, '');
            
            var td_num = document.createElement('td');
            td_num.innerHTML = '(' + list[i].n + ')';
            td_num.className = 'sugg_pan_num';
            tr.appendChild(td_num);
            
            YAHOO.util.Event.addListener(tr, 'click', 
                function(e)
                {
                    me.close();
                    DrillSpot.search.search(this.tagClean);
                });
            
            this.panel.list.appendChild(tr);
        }
        
        DrillSpot.Dom.show(this.container);
    },
    
    scroll : function(adv)
    {
        var next = this.currentIndex + adv;
    
        if (next < 0 || next >= this.panel.list.childNodes.length) return;
 
        this.select(next);
    },

    deselect : function(index)
    {
        if (index >= 0)
            this.panel.list.childNodes[index].className = 'sugg_pan_row';
    },

    select : function(index)
    {
        this.deselect(this.currentIndex);
        
        var x = this.panel.list.childNodes[index];
        
        x.className = 'sugg_pan_sel';
        this.queryBox.value = x.tagClean;
        
        this.currentIndex = index;
        this.queryBox.focus();
    }
};


YAHOO.util.Event.onDOMReady(function(e) { DrillSpot.search.Suggest.init(); });
