// Thanks to:
// http://www.catswhocode.com/blog/using-keyboard-shortcuts-in-javascript
//http://web-kreation.com/demos/Sliding_login_panel_jquery/#
//http://api.jquery.com/keypress/
//Based on: http://wordpress.org/extend/plugins/prev-next-keyboard-navigation/
var xHelped = 2;
jQuery(function() {
	jQuery(document).keypress(function(e) {
		var f=false, b=false;
		
		if (e.target.tagName.match(/input|textarea/i)) return;
		switch (e.which) {
			case 75:
			case 107:
				if (jQuery('.post').length < 2) return;
				else {
				b=true; }
				break;
			case 74:
			case 106:
				if (jQuery('.post').length < 2) return;
				else {
				f=true; }
				break;
			case 71:
			case 103:
				if (jQuery('.post').length > 2) return;
				else {
				$('html, body').animate({ scrollTop: $("#idc-container-parent").offset().top}); }
				break;
			case 78:
			case 110:
				GoToComments;
				break;

			case 89:
			case 121:
				if (jQuery('.post').length > 2) return;
				else {
				$('html, body').animate({ scrollTop: $("#content").offset().top});
				}
				break;
			case 85:
			case 117:
				$('html, body').animate({ scrollTop: $("#header").offset().top});
				break;
				
			case 70:
			case 102:
				contentWidth = $('#content').width();
				if (contentWidth < 960) {
				$('#sidebar').fadeOut('fast');
				$('#content').width(960);
				$('.post').width(960);
				}
				else {
				$('#content').width(620);
				$('.post').width(540);
				$('#sidebar').fadeIn('fast');
				}
				break;
			case 104:
				if(xHelped % 2 == 0) {
				$("div#xhot_short").slideDown("slow");
				$('html, body').animate({ scrollTop: $("#xhot_panel").offset().top});
				$("#xhot_open").hide();
				$("#xhot_close").show();
				xHelped++;
				} else {
				$("div#xhot_short").slideUp("slow");
				$("#xhot_open").show();
				$("#xhot_close").hide();
				xHelped++;
				}
				break;

		}
		if (!f && !b) return;

		jQuery('.post').each( function(i,el) {
			if(f && parseInt(jQuery(el).offset().top,10) > parseInt(jQuery(document).scrollTop(),10)) {
				if(jQuery(document).scrollTop() + jQuery(window).height() >= jQuery(document).height()){
					pnkeynavPage(1);
				}			
				jQuery(document).scrollTop(jQuery(el).offset().top);
				f = false;
			}
			if(b && parseInt(jQuery(el).offset().top,10) >= parseInt(jQuery(document).scrollTop(),10)) {
				var d=jQuery('.post').get(i-1);
				if (i > 0) {
					jQuery(document).scrollTop(jQuery(d).offset().top);
					b = false;
				} else {
					pnkeynavPage(-1);
				}
			}
		});
		if (f) {
			pnkeynavPage(1);
		}
	});


});

function pnkeynavPage (inc) {
	var path = window.location.pathname;
	if (p=path.match(/\/page\/(\d+)\/?$/)) {
		var p = parseInt(p[1], 10) + inc;
		var s = p > 1 ? '/page/'+p+'/' : '';
		window.location.pathname = path.replace(/\/page\/(\d+)\/?$/, s);
	} else if (inc > 0)
		window.location.pathname = path + 'page/2/';
}

function GoToComments() {
	if (jQuery('.post').length > 2) return;
	else {
		$('html, body').animate({ scrollTop: $(".idc-new").offset().top});
		$('#IDCommentNewThreadText').focus(); return false;
		$('textarea#IDCommentNewThreadText').val('asdf');
	}
}
//Display panel
$(document).ready(function() {
	
	// Expand Panel
	$("#xhot_open").click(function(){
		$("div#xhot_short").slideDown("slow");	
	});	
	
	// Collapse Panel
	$("#xhot_close").click(function(){
		$("div#xhot_short").slideUp("slow");	
	});	
// Switch buttons
	$("#toggle a").click(function () {
		$("#toggle a").toggle();});	
		
});

