function strip(a, str) {
  var b = new Array();
  for(var i=0; i<a.length; i++) {
    if(a[i] != str) {
      b.push(a[i]);
    }
  }
  return b;
}

function saveSearch(search, url, length) {
  if(search != '' && url != '') {
    var tmp = null;
    var history = null;
    var historyURLs = null;
    
    if(Cookie.get('searchHistory') != null) {
      Cookie.set('searchHistory', strip(Cookie.get('searchHistory').split('|'), search).join('|'));
      Cookie.set('searchHistoryURLs', strip(Cookie.get('searchHistoryURLs').split('|'), url).join('|'));
      
      if(Cookie.get('searchHistory').split('|').length>=length) {
        tmp = Cookie.get('searchHistory').split('|');
        history = tmp.slice(0, length-1).join('|');
      
        tmp = Cookie.get('searchHistoryURLs').split('|');
        historyURLs = tmp.slice(0, length-1).join('|');
      }
      else {
        history = Cookie.get('searchHistory');
        historyURLs = Cookie.get('searchHistoryURLs');
      }
    }
    
    if(history == null) {
      history = search;
      historyURLs = url;
    }
    else {
      history = search + "|" + history;
      historyURLs = url + "|" + historyURLs;
    }
    
    Cookie.set('searchHistory', history);
    Cookie.set('searchHistoryURLs', historyURLs);
  }
}

function drawHistory(searchDiv) {
  if(Cookie.get('searchHistory') != null) {
    var history_a = Cookie.get('searchHistory').split("|");
    var historyURLs_a = Cookie.get('searchHistoryURLs').split("|");
  
    document.write("<div id=\"search-items\">");
    for(var i = 0; i < history_a.length; i++) {
      if(i%2 == 0) {
        document.write("<div id=\"search-item\" class=\"search-item-odd\">");
      }
      else {
        document.write("<div id=\"search-item\" class=\"search-item-even\">");
      }
      document.write("<a href=\"/search/" + historyURLs_a[i] + "\">" + history_a[i] + "</a>");
      document.write("</div>");
    }
    document.write("</div>");
  }
  else {
    if(searchDiv != null) {
      $(searchDiv).hide();
    }
  }
}