if (typeof(jQuery) != "undefined")
  var $j = jQuery.noConflict();

$ = function() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

String.prototype.trim = function()
{
  a = this.replace(/^\s+/, '');
  return a.replace(/\s+$/, '');
}

function Ajax(URL, responseHandler, data)
{

  var me = this;
  this.data = data;
  this.xmlhttp = false;
  this.CreateXmlHttp = CreateXmlHttp;
  this.URL = URL
  this.responseHandler = responseHandler;
  this.defaultHandler = defaultHandler;
  this.responseXML = responseXML;
  this.responseText = "";
  this.post = Post;

  function responseXML()
  {
    var xmldoc = false;

    if (this.xmlhttp.responseText)
    {
      if (window.ActiveXObject)
      {
        xmldoc = new ActiveXObject("Microsoft.XMLDOM");
        xmldoc.async = "false";
        xmldoc.loadXML(this.xmlhttp.responseText);
      }
      else
      {
        var parser = new DOMParser();
        xmldoc = parser.parseFromString(this.xmlhttp.responseText, "text/xml");
      }
    }

    return xmldoc;
  }


  function defaultHandler()
  {
    if (this.xmlhttp && (this.xmlhttp.readyState==4) && this.responseHandler)
    {
      this.responseText = this.xmlhttp.responseText;
      this.responseHandler(this, this.data);
    }
  }


  function CreateXmlHttp()
  {
    try
    {
      this.xmlhttp = new XMLHttpRequest();
    }
    catch (trymicrosoft)
    {
      try
      {
        this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (othermicrosoft)
      {
        try
        {
          this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (failed)
        {
          this.xmlhttp = false;
        }
      }
    }
  }

  function Post(parameterList)
  {
    if (this.xmlhttp)
    {
      paramStr = "";
      for (var i = 0; i<parameterList.length;i++)
        paramStr += parameterList[i]["name"] + "=" + escape(parameterList[i]["value"]) + "&";
      paramStr = paramStr.substring(0, paramStr.length-1);

      if (this.xmlhttp.overrideMimeType) this.xmlhttp.overrideMimeType('text/html');

      this.xmlhttp.open("POST", this.URL, true);
      this.xmlhttp.onreadystatechange = function() { me.defaultHandler(); };
      this.xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
      this.xmlhttp.setRequestHeader("Content-length", paramStr.length);
      this.xmlhttp.setRequestHeader("Connection", "close");
      this.xmlhttp.send(paramStr);
    }
  }

  this.CreateXmlHttp();
}

function ajaxpost(aURL, aHandler, params, data)
{
  var ajax = new Ajax(aURL, aHandler, data)
  ajax.post(params);
}

function PageInitCommon() {
  $j("input.noautocomplete").attr("autocomplete", "off");
}

$j(document).ready(PageInitCommon);

var startButton = (function() {
  var visible = false
  var self = this;
  var hintShowing = false;
  var hintTimeoutHandle = null;
  
  this.showHint = function() {
    $j("#hintbubble").fadeIn("slow").click(hideHint);
    hintShowing = true;
    hintTimeoutHandle = setTimeout(function() { hideHint(); }, 5000);
  };
  
  function hideHint() {
    $j("#hintbubble").fadeOut("slow");
    hintShowing = false;
    hintTimeoutHandle = null;
  }
  
  function showMenu() {
    if (hintShowing) {
      hintShowing = false;
      clearTimeout(hintTimeoutHandle);
      hintTimeoutHandle = null;
      $j("#hintbubble").css('display', 'none');
    }
    $j("#menu").toggle("slide", {direction: "up"}, 200);
    visible = !visible;
    if (visible)
      $j(document).bind("click", null, hideMenu);
    else
      $j(document).unbind("click", hideMenu);
    
    return false;
  }
  
  var hideMenu = function(e) {
    e = e||window.event;
    var o = $j(e.target);
    
    if (!(o.is('#menu') || o.is("#startbutton")  || o.is("#startbutton a") || o.parents().is('#menu'))) {
      return showMenu();
    }
  }
  
  function init() {
    $j("#startbutton a").click(showMenu);
    $j("#taskbar a.products").click(showMenu);
  }
  
  $j(document).ready(init);
  
  return this;  
})();
