/********************************************************************
    Brief:  
        keypress image navigation script
    Dependencies:
        jquery-1.xxx.js     (jQuery library)
    Author:
        xaxa
*********************************************************************/


var $j = jQuery.noConflict();

$j(document).ready(function(){
	//ensure imgs load is done (webkit loads imgs in a concurrent thread)
	$j(window).load(function(){
		//article pages only
		if ($j("#contentwrapper2").length > 0) {
			var imgstack = new Array();
			imgstack = fillStack();
			var idx = -1;
			var end = imgstack.length - 1;
			var active = 1;
			
			$j(document).keypress(function(e){
				switch (e.which) {
					case 106:
					    if(active)
							idx = ScrollFw(imgstack,idx,end);
						break;
					case 107:
						if(active)
							idx = ScrollRv(imgstack,idx,end);
						break;
				}
			});
			
			$j("input,textarea").keypress(function(){
				active = 0;
			});
		}
	});
});

function fillStack(){
	var theimgs = new Array();
	var theimgList = $j("#contentwrapper2 .topPost img");
	
	for(var i=0;i<theimgList.length;i++){
		var obj = new Object();
		obj.name = $j(theimgList[i]).attr("src");
		obj.xcord = $j(theimgList[i]).offset().left;
		obj.ycord = $j(theimgList[i]).offset().top - 3;
		theimgs.push(obj);
	}
	return theimgs;
}

function ScrollFw(imgstack,idx,end){
	if (idx < end) {
		idx++;
		window.scrollTo(imgstack[idx].xcord, imgstack[idx].ycord);
	}
	return idx;
}

function ScrollRv(imgstack,idx,end){
	if(idx > 0){
		idx--;
		window.scrollTo(imgstack[idx].xcord,imgstack[idx].ycord);
	}
	return idx;
}


