/**
 * jGoogle v0.0.4 - jQuery wrapper for google search API
 *   http://n.efario.us/jquery/jGoogle/
 *   
 * Copyright (c) 2008 Paul Bringetto (http://n.efario.us)
 * licensed under http://www.gnu.org/licenses/gpl-3.0.txt.
 *
 */
 (function($){  
  $.fn.jGoogle = function(options) {  
   var defaults = {
      keeperElement: "<span>Insert Into Thread</span>",
      cssControl: true,
      callbackKeep: function() { return false; },
      callbackComplete: function() { return false; },
      callbackStarting: function() { return false; }
    };
    var options = $.extend(defaults, options);
    return this.each(function() { 
      this.ctrl = new GSearchControl();
      var searcherOptions = new GsearcherOptions();
      searcherOptions.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
      switch(options.searchType) {
        case "videoSearch":
          this.video = new GvideoSearch();
          this.ctrl.addSearcher(this.video, searcherOptions);
          break; 
        case "bookSearch":
          this.books = new GbookSearch();
          this.books.setRestriction(GSearch.RESTRICT_TYPE, GbookSearch.TYPE_ALL_BOOKS);
          this.ctrl.addSearcher(this.books, searcherOptions);
          break;    
        default:
          this.imgs = new GimageSearch();
          this.imgs.setRestriction(GimageSearch.RESTRICT_FILETYPE,GimageSearch.FILETYPE_JPG);
          this.imgs.setRestriction(GimageSearch.RESTRICT_COLORIZATION,GimageSearch.COLORIZATION_COLOR);
          this.ctrl.addSearcher(this.imgs, searcherOptions);
        }
      this.ctrl.setSearchCompleteCallback(this, ready);
      this.ctrl.setOnKeepCallback(this, keep, options.keeperElement);
      this.ctrl.setSearchStartingCallback(this, start);
      this.ctrl.setResultSetSize(GSearch.LARGE_RESULTSET);
      this.ctrl.setLinkTarget(GSearch.LINK_TARGET_PARENT);
      this.ctrl.draw(this);
      this.ctrl.execute(options.searchString);
      function start() { 
       //
      }
      function ready() { 
	    var e;
        switch(options.searchType) {
          case "bookSearch":
            e = ".gs-row-2 > a";
            break;
          default:
            e = ".gs-image-box > a";
        }
        $(e).each(function(i, el) {
	      var url = $(el).attr("href");
          $(el).attr({ href: "javascript: void(0)" }); 
          $(el).click(function() { 
            options.callbackKeep(url);
          });
        });
      }
      function keep(a) { 
        options.callbackKeep(a.url);
      }
    });
  };  
})(jQuery);



