var navtop = 1;
var busy = false;

function shownews(obj){
	var $this = $(obj).parent().parent();
	$this.parent().find('.newstext').slideUp('fast');
	$this.parent().find('.newsicon').show();
	$this.find('.newstext').slideDown('fast');
	$this.find('.newsicon').hide();
}

function navTopRight(tvshow){
	if(busy || navtop >= maxtop){ return; }
	busy = true;
	$.post(root + "ajax/ajaxTopmovies.php", { "action":"navto", "p":navtop+1, "tvshow":tvshow },
	function(data){
		if(data.state == "ok"){	
			navtop++;
			if(navtop >= data.data["maxpage"]){ 
				navtop=data.data["maxpage"]; 
				$("#topnavright").removeClass("navright");
			}
			if(navtop == 2){ $("#topnavleft").addClass("navleft"); }
			maxtop = data.data["maxpage"];
			populatetop(data.data["items"],tvshow);
		}else{
			alert("Fehler:\n" + data.msg);
		}
		busy = false;
	}, "json");
}

function navTopLeft(tvshow){
	if(busy || navtop <= 1){ return; }
	busy = true;
	$.post(root + "ajax/ajaxTopmovies.php", { "action":"navto", "p":navtop-1, "tvshow":tvshow },
	function(data){
		if(data.state == "ok"){	
			navtop--;
			if(navtop <= 1){ 
				navtop=1;
				$("#topnavleft").removeClass('navleft');
			}
			if(navtop < maxtop){ $("#topnavright").addClass("navright"); }
			maxtop = data.data["maxpage"];
			populatetop(data.data["items"],tvshow);
		}else{
			alert("Fehler:\n" + data.msg);
		}
		busy = false;
	}, "json");
}

function populatetop(items,tvshow){
	var taga = $(document.createElement("a"));
	var tagimg = $(document.createElement("img"));
	taga.addClass("img");
	tagimg.attr("width","93").attr("height","116").attr("alt","Cover");
	$("#topitems").html("");
	$(items).each(function(){
		var newitem=taga.clone();
		newitem.attr("href",root + "search?q=" + this.search);
		newitem.append(tagimg.clone().attr("src",root + "images/" + this.image).attr("title",this.name));
		$("#topitems").append(newitem);
	});
}