
$().ready(function(){
  s_type = $(document).getUrlParam("search_type");
  if (s_type == 'p') {
    s_text = $(document).getUrlParam("usr_name") || $(document).getUrlParam("search_text");;
		
  }
  if (s_type == 'b') s_text = $(document).getUrlParam("search_text");
  if (s_type) {
    $('#search_text').val(s_text);
    $('#search_type').val(s_type);
  }
});

function dosearch() {
  search_type = $('#search_type').val();
  search_text = $('#search_text').val();
    
  if (search_type == 'p') {
    location.href = "/browse.php?search_type=p&keywords=&usr_name=" + search_text;
  }
  else if (search_type == 'a') {
    location.href = "/browse.php?search_type=a&usr_name=&keywords=" + search_text;
  }
  else if (search_type == 'b') {
    if (search_text.length < 4) {
      if ($('#search_notice').length == 0) {
        $('#header').prepend('<div id="search_notice" style="position:absolute; background-color:#333; padding:10px 20px; white-space:nowrap; cursor:pointer; display:none;" onclick="$(this).fadeOut(1000)">NOTICE: minimum 4 characters required</div>');
      }
      offs = $('#search_text').offset();
      $('#search_notice').css('left', offs.left).css('top', offs.top-35).fadeIn(1000);
      $('#search_text').focus(function(){
        $('#search_notice').fadeOut(1000);
      }).keydown(function(){
        $('#search_notice').fadeOut(1000);
      });			
      return false;
    }
    location.href = "/search_t.php?search_type=b&search_text=" + search_text;
  }
  else if (search_type == 'w') {
    window.open("http://google.com/search?q=" + search_text);
  }
  return false;
}

function get_win_size() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == "number" ) {
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}

function show_alert() {
  win_size = get_win_size();
  aleft = Math.round((win_size[0] - $("#alert_msg").width()) / 2);
  atop = Math.round((win_size[1] - $("#alert_msg").height()) / 2);
  $("#alert_msg").css({left: aleft, top: atop});
  $("#alert_msg").fadeIn("slow");
  $("#alert_msg").click(function(){$(this).fadeOut("slow");});
  window.onscroll = function(){$("#alert_msg").fadeOut("slow");}
}

function my_msgbox(text) {
  if ($('#my_msgbox').length == 0) {
    msgbox_html = 
      '<div id="my_msgbox" style="padding: 20px 40px; width:400px; position:absolute; left:300px; top:200px; background-color:black; border:4px solid white; color:white; text-align:left; cursor:pointer; display:none; z-index:100">' +
      '<div id="my_msgbox_text" style="font-weight:bold; font-size:120%; text-align:center;"></div>' +
      '</div>';
    $('body').append(msgbox_html);
  }
  $('#my_msgbox').hide();
  $('#my_msgbox_text').html(text);
	
  pos_x = Math.round((($(window).width() - $("#my_msgbox").width()) / 2) + (window.scrollX || window.pageXOffset));
  pos_y = Math.round((($(window).height() - $("#my_msgbox").height()) / 2) + (window.scrollY || window.pageYOffset));
	
  window.onscroll = function(){$("#my_msgbox").fadeOut("slow");}
  $('#my_msgbox').css({left: pos_x, top: pos_y})
  .click(function(){$(this).fadeOut("slow");})
  .fadeIn('slow');
}

