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"] + "=" + encodeURI(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);
}

var tabs = {
  hasResults: false,
  wobble: function() {
    var t = $j("#tabs").get(0).offsetTop;
    $j("#tabs").animate({top: t+10}, 200, null, function() {$j("#tabs").animate({top: t-10}, 200, null, function() {$j("#tabs").animate({top: t}, 200) }) } );
  },
  change: function(tabid, newSearch) {
    $j("#tabs").removeClass();
    $j("#content").removeClass();
    if (newSearch) tabs.hasResults = false;
    switch (tabid) {
      case 1: $j("#tabs").addClass("prodstab"); $j("#content").addClass("prodstab"); break;
      case 2: if (!tabs.hasResults) {
        $j("#tabs").addClass("searchtab");
        $j("#content").addClass("searchtab");
      }
      else {
        $j("#tabs").addClass("resultstab");
        $j("#content").addClass("resultstab");
      }
      break;
      default: $j("#tabs").addClass("mintab"); $j("#content").addClass("mintab"); break;
    }
    
    if (typeof(ie6tabsfix) != "undefined")
      ie6tabsfix(tabid, tabs.hasResults);
      
    tabs.wobble();
  }
}

var categories = (function() {
  var self = this;
  
  function inArray(needle, haystack) {
    var length = haystack.length;
    for (var i=0; i<length; i++) {
      if (needle == haystack[i]) {
        return i;
      }
    }
    
    return false;
  }
  
  function init() {
    $j("#categorylist span.heart").click(self.toggle);
    var state = getState(), id;
    var len = state.length;
    for (var i=0; i<len; i++) {
      id = state[i];
      $j('#' + id).html('▾').next().next().css('display', 'block');
    }
    
    var scroller = $j('#categorylist').get(0);
    if ((typeof(scroller) != 'undefined') && scroller) {
      scroller.scrollTop = sessionStorage.categoryScrollPos || 0; 
      $j("#categorylist").scroll(saveScrollPos);
    }
  }
  
  function saveScrollPos() {
    sessionStorage.categoryScrollPos = $j('#categorylist').get(0).scrollTop;
  }
  
  function getState() {
    var state = sessionStorage.categoryState;
    if ((typeof(state) == 'undefined') || !state) {
      return [];
    }
    return (state === '')?[]:state.split(',');
  }
  
  function saveState(state) {
    sessionStorage.categoryState = state.toString();
  }
  
  this.toggle = function(e) {
    var state = getState();
    
    e = e || window.event;
    var expanded = ($j(e.target).html() === '▸');
    if (expanded) {
      state.push(e.target.id);
    }
    else {
      var idx = inArray(e.target.id, state);
      if (idx !== false) {
        state.splice(idx, 1);
      }
    }
    saveState(state);
    $j(e.target).html($j(e.target).html() == '▸'?'▾':'▸').next().next().toggle();
    return false;
  }
  
  $j(document).ready(init);
  
  return this;
})();

if (typeof(jQuery) != "undefined")
{
  
  showHelpWin = function(el) {
    $j("#helpwin").stop().css("display", "block").css("top", (el.offsetTop + 90) + "px").fadeTo(500, 1);
  }

  $j(document).ready( function() {
      $j("#quicksearch").css('background', 'none').focus(function() { $j("#quicksearch").css('background', '#fff')}).blur($j("#quicksearch").css('background', 'none'));
      $j("a").focus(function() { this.blur(); });
      $j("img.help").click(function() { showHelpWin(this); })
      $j("img.help").mouseout(function() { $j("#helpwin").stop().fadeTo(500, 0); })
      
      if (document.body.offsetHeight >= 900) {
        $j("#page").css("top", "114px");
        $j("#logo").css("top", "78px");
        $j("#tagline").css("top", "90px");
      }
      
      $j('#quicksearch').focus(function() { tabs.change(2, true); }); 
      
      $j(".newwindow").attr("target", "_blank");
      
    }
  );
}
