function onpreviewhover(el) {
  $(el).fade(1);
}

function onpreviewout(el) {
  $(el).fade(0);
}

function oneqhover(el,eq_id) {
  var preview_el = $(eq_id);
  var pos = $(el).getCoordinates();
  preview_el.setPosition({x:pos.left, y:pos.bottom+5});
  preview_el.fade(1);
}

function oneqout(el,eq_id) {
  var preview_el = $(eq_id);
  preview_el.fade(0);
}

function onauthorhover(el,author_id) {
  var preview_el = $('preview_el_'+author_id);
  var pos = $(el).getCoordinates();
  preview_el.setPosition({x:pos.right+5, y:pos.top});
  preview_el.fade(1);
}

function onauthorout(el,author_id) {
  var preview_el = $('preview_el_'+author_id);
  preview_el.fade(0);
}

function pp_get_cookie(c_name) {
  if (document.cookie.length > 0) {
    c_start = document.cookie.indexOf(c_name + "=");
    if (c_start != -1) {
      c_start = c_start + c_name.length+1;
      c_end = document.cookie.indexOf(";", c_start);
      if (c_end == -1) 
        c_end = document.cookie.length;
      return unescape(document.cookie.substring(c_start, c_end));
    }
  }
  return "";
}

function pp_set_cookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function showrating (hoverStars) {
  if (!document.rating) document.rating = 0;
  activeCount = (hoverStars==null)?document.rating:hoverStars;
  for (i = 1; i <= activeCount; i++) {
    document.getElementById ('star' + i).src = (hoverStars)?'/search/starhover.gif':'/search/star.gif';
  }

  for (i = activeCount + 1; i <= 5; i++) {
    document.getElementById ('star' + i).src = '/search/starempty.gif';
  }
}

function setrating (rating,cat,id) {
  document.rating = rating;
  sendrating(cat,id);
}

function sendrating (cat,id) {
  if (document.rating && document.rating > 0) {
    showrating();
    pp_send_request(get_full_pp_file_path('rate-item.php'), "mode=stars&rating="+document.rating+"&id="+id+"&cat="+cat, pp_rating_done, $('rating_content'));
    pp_set_cookie('s_'+cat+'_'+id,document.rating,365);
  }
}

function pp_rate_photo(item_id, rate_offset, div_id) {
  var cookie_name = "r_"+item_id;
  var cookie_value = pp_get_cookie(cookie_name);
  if (cookie_value == '1' || cookie_value == '-1')
    return;

  pp_send_request(get_full_pp_file_path('rate-item.php'), "id="+item_id+"&offset="+rate_offset, pp_rating_done, $(div_id));

  pp_set_cookie (cookie_name,rate_offset,365);
}

function pp_unserialize(url) {
  var params = url;
  if (url.match(/\?(.+)$/)) {
      params = RegExp.$1;
  }
  var pArray = params.split("&");
  var pHash = {};
  for(var i = 0; i < pArray.length; i++) {
    var temp = pArray[i].split("=");
    pHash[temp[0]] = unescape(temp[1]).replace(/\+/g, ' ');
  }
  return pHash;
}

function pp_get_param(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if (results == null)
    return null;
  else
    return results[1];
}

function get_full_pp_file_path(name) {
  return document.pp_root_path + name;
}

function pp_show_hourglass(hide_element, wait_text) {
  document.pp_ajax_call_pending = true;
  if (hide_element) {
    document.pp_ajax_call_hide_element = hide_element;
    //hide_element.style.display = 'none';
    hide_element.innerHTML=("<div id='hourglass_element'>"+(wait_text ? wait_text : '')+" <img src='" + get_full_pp_file_path('search/hourglass.gif') + "' align='middle' /></div>");
  }
}

function pp_hide_hourglass() {
  if (document.pp_ajax_call_pending) {
    if (document.pp_ajax_call_hide_element) {
      //document.pp_ajax_call_hide_element.style.display = 'inline';
      document.pp_ajax_call_hide_element.innerHTML='';
      document.pp_ajax_call_hide_element = null;
    }
    document.pp_ajax_call_pending = false;
  }
}

function pp_on_request_failure() {
  var error_div = $('error_msg');
  if (error_div) {
    error_div.style.display = 'inline';
    error_div.innerHTML = "Unknown error occurred.";
  }
}

function pp_send_request(url, data, on_complete_function, hide_item, wait_text) {
  document.hide_item = hide_item;
  pp_show_hourglass(hide_item, wait_text);
  var xhr_req = new Request (
                             {
                             url:url,
                                 method:'post',
                                 onSuccess: on_complete_function,
                                 onFailure: pp_on_request_failure,
                                 onException: pp_on_request_failure,
                                 onCancel: pp_on_request_failure,
                                 evalScripts: true
                                 }).send(data);
}

function pp_rating_done(txt, hide_item) {
  document.hide_item.innerHTML = txt;
}

function hide_unwanted_images() {
  
}


