// display greet box when document is ready
jQuery(document).ready(function($){
  // set default referrer if none found
  var ref = "default";
  if(document.referrer){
    ref = document.referrer;
    m = ref.match(/^http:\/\/[^\/]+/i);
    if(m){
      ref = m;
    }
  }
  // send request
  $.ajax({
    type: "GET",
    url: "index.php",
    data: "wpgb_public_action=query&visit_delta="+wpgb_get_delta(ref)+"&referrer="+encodeURIComponent(document.referrer)+"&url="+encodeURIComponent(document.location)+"&title="+encodeURIComponent(document.title),
    success: function(resp) {
      if(resp != ''){
        // show greeting message
        $("#greet_block").hide().html(resp).fadeIn("def");
        // bind close action
        $("#greet_block_close").click(function(){
          $("#greet_block").fadeOut("def");
        });
        // bind search action (if any)
        $("#greet_search_link").click(function(){
          action = $(this).attr("action");
          if(action == "show"){
            $("#greet_search_results").slideDown();
            $(this).attr("action","hide");
            $(this).html(String.fromCharCode(171) + " Hide related posts");
            $("#greet_search_text").html("Posts relating to");
            $("#greet_search").css("font-size","14px");
          }
          else if(action == "hide"){
            $("#greet_search_results").slideUp("fast");
            $(this).attr("action","show");
            $(this).html("See posts relating to your search " + String.fromCharCode(187));
            $("#greet_search_text").html("You were searching for");
            $("#greet_search").css("font-size","11px");
          }
        });
      }
    }
  });
  // set cookie
  wpgb_set_cookie("wpgb_visit_last-"+ref,new Date(),wpgb_cookie_exp);
});

