// ON LOAD
$(document).ready(function(){

$(".lst").bind("focus", function(e){
  var currentTime = new Date();
  thisKeystroke = currentTime.getTime();
});

$(".lst").bind("keypress", function(e){
  var currentTime = new Date();
  lastKeystroke = thisKeystroke;
  thisKeystroke = currentTime.getTime();
  timeSinceLast = thisKeystroke - lastKeystroke;
  if (timeSinceLast < 600 && searchEvent != null) {
    clearTimeout(searchEvent);
  }
  searchEvent = setTimeout("updateResults();", 600);
});

$("td.cur").click(function(){
	if ($(window).scrollTop() == $(document).height() - $(window).height()){
	  loadMore();
	}
});

start = 0;

$('<div id="tempresults" class="moreloaded"></div>').insertAfter("#res");


var resultinfo = $("div#ssb p").html();
var resultinfo2 = resultinfo.split('about ')[1];
var resultinfo3 = resultinfo2.split('. ')[0];
var resultinfo4 = resultinfo3.split(' for ')[0] + " results for " + resultinfo3.split(' for ')[1];

$("div#ssb p").html(resultinfo4);
$("#gbar b.gb1").addClass("desel");

	$("input.lst").focus(function() {
		$("input.lsb").stop();
		$("input.lsb").animate({opacity: "1"}, 200);
	});
	
	$("input.lst").blur(function() {
		$("input.lsb").stop();
		$("input.lsb").animate({opacity: "0"}, 400);
	});
	

	$("#gbar").hover(function() {
		$("#gbar a.gb1").stop();
		$("#gbar a.gb1").fadeTo(200,1);
		
		$("#gbar a.gb3").stop();
		$("#gbar a.gb3").fadeTo(200,1);
		
		$("td.nobr.xsm a").stop();
		$("td.nobr.xsm a").fadeTo(200,1);
		$("#gbar b.gb1").removeClass("desel");
		
	}, function() {
	});

	$("table#sft.ts").hover(function() {
		$("#gbar a.gb1").stop();
		$("#gbar a.gb1").fadeTo(200,1);
		
		$("#gbar a.gb3").stop();
		$("#gbar a.gb3").fadeTo(200,1);
		
		$("td.nobr.xsm a").stop();
		$("td.nobr.xsm a").fadeTo(200,1);
		
		$("#gbar b.gb1").removeClass("desel");
		
	}, function() {
		$("#gbar a.gb1").stop();
		$("#gbar a.gb1").fadeTo(200,0);
		
		$("#gbar a.gb3").stop();
		$("#gbar a.gb3").fadeTo(200,0);
		
		$("td.nobr.xsm a").stop();
		$("td.nobr.xsm a").fadeTo(200,0);
		
        $("#gbar b.gb1").addClass("desel");
      
	});
	

	$("li.g").hover(function() {
		$(this).children("div").children("cite").stop();
		$(this).children("div").children("cite").animate({opacity: "1"}, 200);
		$(this).children("div").children("span").stop();
		$(this).children("div").children("span").animate({opacity: "1"}, 200);
	}, function() {
		$(this).children("div").children("cite").stop();
		$(this).children("div").children("cite").animate({opacity: "0"}, 200);
		$(this).children("div").children("span").stop();
		$(this).children("div").children("span").animate({opacity: "0"}, 200);
	});

});

function loadMore() {
  start += 10;
  var nextlink = $("#nav tr td:last a").attr('href');
  var currenturl = window.location;
  url = currenturl + '&start=' + start;
  $("#tempresults").load(url + ' #res');
  $("#tempresults").attr('id', 'oldtemp');
  $('<div id="tempresults" class="moreloaded"></div>').insertAfter(".moreloaded:last");
}

function updateResults() {
  var url = '/search?' + $('#tsf').serialize();
  $.get(
    url,
    {},
    function(data, textStatus) {
      $("#res").replaceWith($("#res", data));
    }
  );
}