
function getDivByTitle (title)
{
  var e=document.getElementsByTagName('div');
  for (var i=0;i<e.length;i++)
    if (e[i].title==title) return e[i];
  return null;
}

google.load('search', '1');

function resultCallback(res)
{
    if (res.width > 900 || res.height > 900)
    {
        alert("Sorry, but due to the popularity of our website, we do not process images larger than 900 pixels in width or hight.");
        return true;
    }
    current_field.value = res.url;
    close_popup();
    return false;
}

/*
var resized = false;
function searchDoneCallback(searcher)
{
    if (!resized)
    {
        search_div.style.height = search_div.scrollHeight + 'px';
        resized = true;
    }
}
*/

function se(form,div) {
  results = div;

  // create a left, right search control
  // create a custom search form
  this.control = new google.search.SearchControl();
  this.searchForm = new google.search.SearchForm(true, form);
  this.searchForm.input.focus();

  // bind clear and submit functions
  this.searchForm.setOnSubmitCallback(this, se.prototype.onSubmit);
  this.searchForm.setOnClearCallback(this, se.prototype.onClear);

  // set up for large result sets
  this.control.setResultSetSize(GSearch.LARGE_RESULTSET);
  
  // set up link to hijack 
  this.control.setOnKeepCallback(this,resultCallback, "<a> </a>");
  /*this.control.setSearchCompleteCallback(this,searchDoneCallback);*/
  
  this.drawOptions = new google.search.DrawOptions();
  this.drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
  
  var searcher = new google.search.ImageSearch()
  searcher.setRestriction(google.search.ImageSearch.RESTRICT_FILETYPE,google.search.ImageSearch.FILETYPE_JPG);
  searcher.setRestriction(google.search.ImageSearch.RESTRICT_IMAGESIZE,google.search.ImageSearch.IMAGESIZE_MEDIUM);
  this.control.addSearcher(searcher);
  this.control.setNoResultsString("No Results");
  this.control.draw(results,this.drawOptions);

  // execute a starter search
  if (window.lastSearch != null)
    this.searchForm.execute(window.lastSearch);
}

se.prototype.onSubmit = function(form) {
  var q = form.input.value;
  if (q && q!= "") {
    this.control.draw(results,this.drawOptions);
    this.control.execute(q);
  }
  
  return false;
}

var results = null;
var timer = null;



// when the form fires a clear, call the left and right control
se.prototype.onClear = function(form) {
  this.control.clearAllResults();
  form.input.value = "";
  return false;
}

var current_field = null;
var popup_div = null;
var bg = null;
var search_div = null;

function image_search_popup(field)
{
  current_field = field;
  
  if (popup_div != null)
  {
      if (bg != null) bg.style.display = 'inline';
      popup_div.style.display = 'inline';
      return;
  }
  
  popup_div = document.createElement('div');
  popup_div.id = "popup";
  popup_div.onClick = "close_popup();";
  
  if (/MSIE/.test(navigator.userAgent))
  {
    bg = document.createElement('div');
    bg.id="popup";
    bg.style.filter = 
      "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='../common/75w.png');";
    document.body.appendChild(bg);
  }
  else
  {
    popup_div.style.backgroundImage = 'url("../common/75w.png")';
    popup_div.style.backgroundRepeat = 'repeat';
  }
  
  var top = document.createElement('div');
  top.id = 'top';
  top.innerHTML = "<a href='javascript:close_popup();' style='float:right; text-decoration: none; color: #111111;'>close</a>";
  
  //var message = document.createElement('div');
  //message.innertHTML = "";
  
  document.body.appendChild(popup_div);
  
  search_div = document.createElement('div');
  search_div.id = "search";
  popup_div.appendChild(search_div);
  search_div.appendChild(top);
  
  var search_box,results,clear;
  
  clear = document.createElement('div');
  search_box = document.createElement('div');
  results = document.createElement('div');
  
  search_box.style.width="40%";
  results.style.width="100%";
  clear.style.clear = 'both';
  
  search_div.appendChild(search_box);
  search_div.appendChild(results);
  search_div.appendChild(clear)
  
  new se(search_box,results);
}

function remove_element(e)
{
    if (e!=null)
    {
        e.innerHTML="";
        document.body.removeChild(e);
    }
}

function close_popup()
{
    popup_div.style.display = 'none';
    if (bg) bg.style.display = 'none';
}