//### object userinput
function UserInput ()
{
	var		me = this;
	this.offset = 0;
	this.allowed = true;
	
	$("#left").hide ();
	
	//# initialize
	document.onkeydown = function(e) { 
	
	  var   active = document.activeElement;
	  me.allowed = (active.nodeName !== "TEXTAREA" && active.nodeName !== "INPUT");
	
		me.handle(e);
	};
	
//### set if allowed or not
	this.shouldHandleEvents = function(fl)
	{
		this.allowed = fl;
	}
	
//### get current tite
	this.getCurrentTitle = function()
	{
	  return $(".post h1").text();
	}
	
//### jump to previous
	this.showPrevious  = function ()
	{
	  this.offset --;
	  if (this.offset == 0) {
	     $("#left").hide ();
	  }
	  
      $.ajax({    url: "/request.php", 
 			type: "POST",
         data: "offset="+this.offset,
         success: function(data){
        		$("#items").html(data);
   			$.sticky(me.getCurrentTitle());

        		document.title = me.getCurrentTitle() + " | corefault";
         }
        });
	}
	
//### jump to next
	this.showNext = function ()
	{
	  this.offset ++;
	  if (this.offset > 0) {
	     $("#left").show ();
	  }
	  $.ajax({    url: "/request.php", 
 			type: "POST",
         data: "offset="+this.offset,
         success: function(data){
        		$("#items").html(data);
   			$.sticky(me.getCurrentTitle());
        		document.title = me.getCurrentTitle() + " | corefault";
         }
        });
	}
	
//## handle
	this.handle = function (e)
	{
		if (!this.allowed) {
			return;
		}
		
		if (! e) {
		 e = window.event;
		}
		var code = e.charCode ? e.charCode : e.keyCode;
		if (! e.shiftKey && ! e.ctrlKey && ! e.altKey && ! e.metaKey) {
	   //# navigation
		  if (code==74) { //# next "J"
		    this.showNext ();
		  }
		  else if (code == 75) { // prev "K"
		    this.showPrevious ();
		  }
	 	}
	}
}

