﻿/* <gslb_version version="V0.0.0/BL0000A" /> */
/* Copyright © GalaSoft Laurent Bugnion 2007 */

// Last Base Level BL0000

// using gslb.Object
// using gslb.PositionedBase
// using gslb.CssLength
// using gslb.PositionedNode
// using gslb.FadedNode
// using gslb.PopUp

if (typeof(google) != "undefined"
    && google)
{
    google.load("search", "1");
}

// Create namespace
if ( !window.gslb )
{
  window.gslb = {};
}

if ( !gslb.PositionedNode )
{
  throw "Error in gslb.GoogleSearch: gslb.PositionedNode not found";
}
if ( !gslb.FadedNode )
{
  throw "Error in gslb.GoogleSearch: gslb.FadedNode not found";
}

gslb.GoogleSearch = function(nodeContainer, node,
    useWebSearch, labelWebSearch, restrictionWebSearch,
    useBlogSearch, labelBlogSearch, restrictionBlogSearch) 
{
  this.popUp = null;
  this.node = node;
  this.useWebSearch = useWebSearch;
  this.labelWebSearch = labelWebSearch;
  this.restrictionWebSearch = restrictionWebSearch;
  this.useBlogSearch = useBlogSearch;
  this.labelBlogSearch = labelBlogSearch;
  this.restrictionBlogSearch = restrictionBlogSearch;

  this.searchControl = null;

  gslb.GoogleSearch.instances[gslb.GoogleSearch.instances.length] = this;
}

gslb.GoogleSearch.instances = new Array();

gslb.GoogleSearch.onLoad = function()
{
  if (typeof(google) == "undefined"
      || !google)
  {
    return;
  }

  for ( var index = 0; index < gslb.GoogleSearch.instances.length; index++ )
  {
    // Create a search control
    var searchControl = new google.search.SearchControl();

    // Add in a full set of searchers
    if (gslb.GoogleSearch.instances[index].useWebSearch)
    {
      var webSearch = new google.search.WebSearch();
      webSearch.setUserDefinedLabel(gslb.GoogleSearch.instances[index].labelWebSearch);
      webSearch.setSiteRestriction(gslb.GoogleSearch.instances[index].restrictionWebSearch);
      searchControl.addSearcher(webSearch);
    }

    if (gslb.GoogleSearch.instances[index].useBlogSearch)
    {
      var blogSearch = new google.search.BlogSearch();
      blogSearch.setUserDefinedLabel(gslb.GoogleSearch.instances[index].labelBlogSearch);
      blogSearch.setSiteRestriction(gslb.GoogleSearch.instances[index].restrictionBlogSearch);
      searchControl.addSearcher(blogSearch);
    }

    // Tell the searcher to draw itself and tell it where to attach
    searchControl.draw(gslb.GoogleSearch.instances[index].node);
  }
}

gslb.GoogleSearch.prototype.open = function() 
{
  if (!this.popUp)
  {
    this.popUp = new gslb.PopUp(nodeContainer,
    {
      mustFade: true,
      pinX: true,
      pinWhenOpenY: true,
      pinWhenClosedY: false,
      startClosed: true
    });
  }

  this.popUp.open();
}

gslb.GoogleSearch.prototype.close = function()
{
  this.popUp.close();
}

gslb.GoogleSearch.prototype.minimize = function()
{
  this.popUp.minimize();
}

if (typeof(google) != "undefined"
    && google)
{
    google.setOnLoadCallback(gslb.GoogleSearch.onLoad);
}
