/*
 * Copyright 2008 Jeffery Palm.
 */

var STYLESHEET_ID = "_stylesheet";
var backgroundDiv = 0;
var modalDialogDiv = 0;
var isSafari = false;
var isMoz = false;
var isIE = false;

function main() {
  browserCheck();

  // Search for all forms near text with 'search' or something close
  var fs = document.forms;
  if (fs <= 0) {
    alert('There are no search boxes');
    return;
  }

  // Show the dialog
  showForm(findForm(fs));
 
}

function browserCheck() {
  if (navigator.userAgent.indexOf("Safari") > 0) {
    isSafari = true;
    isMoz = false;
    isIE = false;
  } else if (navigator.product == "Gecko") {
    isSafari = false;
    isMoz = true;
    isIE = false;
  } else {
    isSafari = false;
    isMoz = false;
    isIE = true;
  }
}

/**
 * Shows the form and displays a modal dialog.
 * @param f Form to show
 */
function showForm(f) {
  if (!backgroundDiv) {
    backgroundDiv                        = $n("div",document.body);
    backgroundDiv.style.position         = "absolute";
    backgroundDiv.style.top              = "0px";
    backgroundDiv.style.left             = "0px";
    backgroundDiv.style.height           = "100%";
    backgroundDiv.style.width            = "100%";
    backgroundDiv.style.backgroundColor  = "gray";
    backgroundDiv.style.opacity          = ".7";
    backgroundDiv.style.filter           = "alpha(opacity=70)";
    backgroundDiv.style.zIndexw          = "5";
  }

  if (!modalDialogDiv) {
    var WIDTH = 40;
    var MARGIN = (100 - WIDTH) / 2;
    var HEIGHT = 300;
    modalDialogDiv                         = $n("div",document.body);
    modalDialogDiv.style.position          = "absolute";
    modalDialogDiv.style.backgroundColor   = "white";
    modalDialogDiv.style.border            = "1px solid black";
    modalDialogDiv.style.top               = "100px";
    modalDialogDiv.style.width             = WIDTH + "%";
    modalDialogDiv.style.marginLeft        = MARGIN + "%";
    modalDialogDiv.style.marginRight       = MARGIN + "%";
    modalDialogDiv.style.zIndex            = "10";
    modalDialogDiv.style.padding           = "20px";
    modalDialogDiv.style.maxHeight         = "60%";
    modalDialogDiv.style.minHeight         = "30%";

  }

  while (modalDialogDiv.childNodes.length > 0) {
    modalDialogDiv.removeChild(modalDialogDiv.firstChild);
  }

  var title = $n("div",modalDialogDiv);
  title.innerHTML = "Search";
  
  // We can't use a real form, because that will submit the page
  var formDiv = $n("div",modalDialogDiv);
  formDiv.backgroundColor = "#eee";
  formDiv.padding = "35px 0px 40px 50px";

  var text = $n("input",formDiv);
  text.type = "text";
  text.size = "45";

  addKeyListener(text,pressEnterFunction(f,text));

  var submit = $n("input",formDiv);
  submit.type = "submit";
  submit.value = "Search";
  addClickListener(submit,function(e) {return submitForm(f,text.value);});

  for (var i=0; i<5; i++) br(modalDialogDiv);

  var closeLink = $n("a",modalDialogDiv);
  closeLink.innerHTML = "close";
  closeLink.href = "#";
  closeLink.addEventListener("click",closeModalDialog,true);
  closeLink.style.position = "relative";
  closeLink.style.left = "10px";
  closeLink.style.bottom = "10px";

  addCSS(title,text,submit,closeLink,submit);
  text.focus();
  show(backgroundDiv);
  show(modalDialogDiv);

  return modalDialogDiv;
}

function addKeyListener(element, listener) {
  if (isSafari)
    element.addEventListener("keydown",listener,false);
  else if (isMoz)
    element.addEventListener("keypress",listener,false);
  else
    element.attachEvent("onkeydown",listener);
}

function addClickListener(element, listener) {
  if (isSafari)
    element.addEventListener("click",listener,true);
  else if (isMoz)
    element.addEventListener("click",listener,true);
  else
    element.attachEvent("onclick",listener);
}

function addCSS(title,text,submit,closeLink,submit) {

  var titleStyle = {
  marginTop: "0px",
  marginRight: "0px",
  marginLeft: "0px",
  paddingRight: "0px",
  paddingBottom: "0px",
  paddingLeft: "0px",
  fontFamily: "Helvetica",
  fontStyle: "normal",
  fontVariant: "normal",
  fontWeight: "bold",
  fontSize: "34px",
  lineHeight: "normal",
  fontSizeAdjust: "none",
  fontStretch: "normal",
  paddingTop: "18px",
  color: "#3e4d5c",
  marginBottom: "25px"
  };
  setStyle(title,titleStyle);

  var textStyle = {
  border: "1px solid #cccccc",
  padding: "5px",
  fontFamily: "Lucida Grande",
  fontStyle: "normal",
  fontVariant: "normal",
  fontWeight: "normal",
  fontSize: "16px",
  lineHeight: "normal",
  fontSizeAdjust: "none",
  fontStretch: "normal",
  backgroundColor: "#ffffff",
  backgroundAttachment: "scroll",
  backgroundXPosition: "center",
  backgroundYPosition: "top"
  };
  setStyle(text,textStyle);

  var closeLinkStyle = {
  fontFamily: "Lucida Grande",
  fontSize: "12px",
  };
  setStyle(closeLink,closeLinkStyle);

  var submitStyle = {
  fontFamily: "Lucida Grande",
  fontSize: "14px",
  marginTop: "20px",
  marginBottom: "20px"
  };
  setStyle(submit,submitStyle);
}


function setStyle(node,attrs) {
  for (var key in attrs) {
    if ((typeof attrs[key]) == "string") {
      eval("try {node.style." + key + " = '" + attrs[key] + "';} catch (e){}");
    }
  }
}

function pressEnterFunction(f,text) {
  return function(e) {
    if (!e) e = window.event;
    if (e.keyCode && e.keyCode == "13") {
      submitForm(f,text.value);
    }
    return false;
  }
}

function submitForm(f,text) {
  var inputs = f.getElementsByTagName("input");
  var set = false;
  for (var i=0; i<inputs.length; i++) {
    var input = inputs[i];
    if (input.type != "text") continue;
    input.value = text;
    set = true;
    break;
  }
  closeModalDialog();
  if (set) f.submit();
  return false;
}

/**
 * Closes the main dialog.
 */
function closeModalDialog(e) {
  close(backgroundDiv);
  close(modalDialogDiv);
}

/**
 * Closes all element arguments passed in.
 */
function close() {
  for (var i=0; i<arguments.length; i++) {
    if (arguments[i]) arguments[i].style.display = "none";
  }
}

/**
 * Shows all element arguments passed in.
 */
function show() {
  for (var i=0; i<arguments.length; i++) {
    if (arguments[i]) arguments[i].style.display = "";
  }
}

/**
 * Returns the most likely form to be a search form
 * @param fs Array(Form)
 */
function findForm(fs) {
  // If we have just one form, assume it's the search form
  if (fs > 1) {
    searchForm = fs[0];
  } 

  // Otherwise try to determine which form is the search form
  // Keep a counter of the maximum weight (i.e. most likely
  // to be a search form, and current search form
  else {
    var maxWeight = -1;
    var maxForm;
    for (var i=0; i<fs.length; i++) {
      var f = fs[i];
      var w = weightForm(f);
      if (w>maxWeight) {
        maxWeight = w;
        maxForm = f;
      }
    }
    searchForm = maxForm;
  }

  return searchForm;
}

/**
 * Returns the weight of a form
 * @param f Form
 */
function weightForm(f) {
  
  return 1;
}

function $n(tag,on) {
	var e = document.createElement(tag);
	if (on) on.appendChild(e);
  if (arguments.length > 2) setId(e,arguments[2]);
	return e;
}

function $t(text,on) {
	var e = document.createTextNode(text);
	if (on) on.appendChild(e);
	return e;
}

function br(s) {
  $n("br",s);
}

main();

