﻿
DrillSpot.search =  {

    INITIAL_VALUE : "Search Products",
    QUERY_ID : "searchQuery",
    BUTTON_ID : "searchGoButton",

    init : function() {    
        
        this.url = "/go/search/?q=";
        if (document.location.href.indexOf('localhost') > 0) 
            this.url = '/Drillspot' + this.url;

        var me = this;

        this.query = document.getElementById('searchQuery');
        
        YAHOO.util.Event.on(this.query, "click", function() { me.clear(); });
        YAHOO.util.Event.on("searchGoButton", "click", function() { me.search(); });        
        DrillSpot.util.onEnter("searchQuery", function() { me.search(); });

        var q = DrillSpot.util.getUrlParam('q');
        if (q) this.query.value = unescape(q.replace(/\+/g, " "));
        
    }, 
    
    clear : function() {    
        if (this.query.value == this.INITIAL_VALUE)
            this.query.value = "";    
    },

    search: function(q) {

        q = q || this.query.value;    
        q = escape(q);
        q = q.replace(/ /g, '+');

        if (q.length > 0 && q != this.INITIAL_VALUE) 
            document.location.href = this.url + q;
    }
};

YAHOO.util.Event.onDOMReady(function(e) { DrillSpot.search.init(); });
