/******************************************************************************
 ** Google CSE
 **
 ** Documentation:
 ** 
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("search", "1");
    </script>
 ** Include the above script in any page that uses google cse
 ** 
 ** 
 ** 

<div id="cse" style="width: 100%;">Loading</div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript"> 
  google.load('search', '1', {language : 'en'});
  google.setOnLoadCallback(function() {
    var customSearchControl = new google.search.CustomSearchControl('004983444642968987554:skgjyzjfuh0');
    customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
    customSearchControl.draw('cse');
  }, true);
</script>
<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />
    API KEY : AIzaSyA5xtyg_gNWCI_UUQ_g_aJGZPKBZwQyZbI 
 *****************************************************************************/
(function ($) { // protect the namespace


$.widget( "ui.googleCSE", { 

    options: {
        query:"",
        googleResultsCount: 8,
        startingCallback: undefined,
        completeCallback: undefined,
        googleSearchCX: "", //<% $Ruby::Core::Config{GOOGLE_SEARCH_CX} |a %>
        customSearchControl: ""
    }, // options
    _parseURLVars: function(){

        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },

    _create: function () {
        var self=this;
    	var options = this.options;
        console.log("GoogleCSE options:", options);

        var googleResultsCount = options.googleResultsCount;
        googleResultsCount = google.search.Search.FILTERED_CSE_RESULTSET;
//SETTING UP SEARCH
        function OnLoad(){
            console.log("GoogleCSE onload options:", options);
        // Create a custom search control that uses a CSE restricted to code.google.com
        self.customSearchControl = new google.search.CustomSearchControl(options.googleSearchCX);
        self.customSearchControl.setResultSetSize(options.googleResultsCount);
        if(options.startingCallback != undefined){
            self.customSearchControl.setSearchStartingCallback(this, function(searchControl, searcher, query){options.startingCallback(searchControl, searcher, query)});
        }
        if(options.completeCallback != undefined){
            self.customSearchControl.setSearchCompleteCallback(this, function(searchControl, searcher){options.completeCallback(searchControl, searcher)});
        }

        // Draw the control in content div
        self.customSearchControl.draw('cse-content');
        self.customSearchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);

            // run a query on load
            //check if query is defined
            var gargs = self._parseURLVars();
            if(typeof gargs["q"] != 'undefined'){
                var query_string = decodeURI(gargs["q"]);
                self.customSearchControl.execute(query_string);
            }

            //* TODO it's a hack here, we need an onLoad function for analytics
            if(typeof pageTrack != "undefined" && pageTracker == null){
                //pageTracker = _gat._getTracker(GOOGLE_ANALYTICS);
                console.log("page tracker", pageTracker);
            }
        }
        if(typeof google == "undefined"){
            google.setOnLoadCallback(OnLoad);
        }
        else{
            OnLoad();
        }
        return;   
    }, // init
    lookup:function(query){
        var self=this;
        self.customSearchControl.execute(query);
    },
    destroy: function () { 
	
	    $.Widget.prototype.destroy.apply( this, arguments );
    }
}); // $.widget

})(jQuery); // function ($)

