
var pic_data={
    file:"pics_ajax.php", // Where we get the suggest.
    xTimeOut:null, // will use us later.
    txtInput:null, // Text input object.
    rule:/^[0-9]{1,}$/ // Rule for the input.
};

function hOnLoad(){
    pic_data.txtInput = document.getElementById("pics_usr_offset"); // init input
	pic_data.cur_pic = document.getElementById("pics_cur_pic");
	pic_data.order = document.getElementById("pics_order");
	picture = document.getElementById("pics_picture");
	picture_h2 = document.getElementById("pics_h2");
	picture_href = document.getElementById("pics_href");
	picture_href2 = document.getElementById("pics_href2");
	//the first result:
	navigation('jump');
}

function validate_checkboxes(){
	//we will use query_string to send the checkboxes values, because the regular way doesn't accept the [] in cat[]:
	var query_string = ''; 
	var checkboxes = document.getElementsByName('cat');
	limit = checkboxes.length;
	for (w=0; w<limit; w++) {
		if (checkboxes[w].checked) {
			query_string += "cat[]=" + checkboxes[w].value+"&";
		}
	}
	//get rid of the last &:
	query_string = query_string.substr(0, query_string.length-1);
	if (query_string.length == 0) {
		alert('יש לבחור קטגוריה אחת לפחות!');
		return false;
	} else {
		return query_string;
	}
}

function navigation(action) {
//used to jump to the wanted picture:
	//show the "loading" pic:
	picture.style.width="16px";
	picture.style.height="16px";
	picture.style.marginTop = '95px';
	picture.src='ajax-loader.gif';
	var offset = 1;
	query_string = validate_checkboxes();
	if (query_string!=false) {
	//we have at least 1 cat:
		if (action == 'next') {
			pic_data.cur_pic.value = parseInt(pic_data.cur_pic.value, 10) + 1;
		} else if (action == 'prev') {
			pic_data.cur_pic.value = parseInt(pic_data.cur_pic.value, 10) - 1;
		} else if (action == 'jump') {
			pic_data.cur_pic.value = pic_data.txtInput.value;
		} else {
			offset = 1;
		}
		offset = pic_data.cur_pic.value;
		pic_data.txtInput.value = offset;
		show_pic(offset,query_string);
	}
}
function show_pic(offset,query_string,e){
    clearTimeout(pic_data.xTimeOut); // Important
    if(pic_data.rule.test(offset)){
        pic_data.xTimeOut = setTimeout(function(){ // important
            // Do an Ajax Request after X ms
			$.ajax({
			    type: "GET",
			    url: pic_data.file,
			    data: "q="+offset+"&a="+pic_data.order.value+"&"+query_string,
			    success: function(req)
			    {
					ajaxSuccess(req);
				}
			 });
			/*
			var parameters = Object.toQueryString({q: offset, a: pic_data.order.value});
			var url = pic_data.file+"?"+parameters+"&"+query_string;
			new Ajax(url, {method: 'get',onComplete:ajaxSuccess}).request();
			*/
		}, pic_data.xTime);
    } else {
		alert('יש להכניס לשדה זה מספרים בלבד.');
	}
}


function ajaxSuccess(oReq){
        // Add new results
		if (oReq != '') {
            data = oReq.split("|");
			picture_href.innerHTML = data[1];
			picture.src = data[2];
			picture.style.marginTop = '0';
			picture.style.width="320px";
			picture.style.height="200px";
			picture_href.href = 'game.php?game='+data[3];
			picture_href2.href = 'game.php?game='+data[3];
			pic_data.txtInput.value = data[0];
    }
}

function handleKeyPress(e){
//submit the request if the user presses ENTER in the textarea
	var key=e.keyCode || e.which;
	if (key==13){
		navigation("jump");
	}
}

$(document).ready(function () {
    hOnLoad();
});
//window.addEvent('domready', hOnLoad);
