var page=0;
var pages=0;
var currentItemUrl;
var currentTitle;
var currentListUrl;

$(document).ready(function() {
	var url;
	var title;
	
	if($(document).getUrlParam("id")!=null){
		var id = $(document).getUrlParam("id");
		url = "data/getMedia.php?id="+id;
		title = "Noticia";
	} else {
		url = "data/getMedia.php?type=3&l=3&o=0&ord=date&desc=0&long=";
		title = "Últimas noticias";
	}

	updateNews(url,title);
	
	$('.gotoNextPage').click(function(){
		if(page!=pages){			
			url = "data/getMedia.php?type=3&l=3&o="+(page)+"&ord=date&desc=0&long=";
			title = "Noticias";
			updateNews(url,title);
		}
	});
	
	$('.gotoPrevPage').click(function(){
		if(page>1){
			url = "data/getMedia.php?type=3&l=3&o="+(page-2)+"&ord=date&desc=0&long=";
			title = "Noticias";
			updateNews(url,title);
		}
	});
	
	// paginas
	$('.gotoPage').click(function(){
		var gotoPage = parseInt($(this).html());
		if(!isNaN(gotoPage)){
			url = "data/getMedia.php?type=3&l=3&o="+(gotoPage-1)+"&ord=date&desc=0&long=";
			title = "Noticias";
			updateNews(url,title);
		}
	});
});

function updateNews(itemurl,title){
	
	if(itemurl != currentItemUrl){
		$("#noticiasElegidos").html("<div class='ajaxloader'><img alt='loading' src='img/ajax-loader.gif'/></div>");
		$.ajax({
	        type: "POST",
			url: itemurl,
			dataType: "xml",
			error: function(data){
				alert(data.responseText);
				$("#noticiasElegidos").html("error occured, please refresh");
			},
			success: function(xml) {
				$(xml).find('data').each(function(){
					pages = parseInt($(this).attr('pages'));
					$("#pager_pages").val(pages);
					page = parseInt($(this).attr('page'));
					$("#pager_page").val(page-1);
				});
				updatePgr();
				currentItemUrl=itemurl;
				$("#noticiasElegidos").html("");
				$(xml).find('media').each(function(){
					$("#titolo").html("<h1>"+title+"</h1>");
		 			var name = $(this).find('name').text();
		 			var desc = $(this).find('description').find('long').text();
		 			var url = $(this).find('url').text();
		 			var date = $(this).attr('date').substring(0,10);
		 			var insertHtml = "";
		 			insertHtml = insertHtml+
		 							"<div class='noticia'>"+
		 							"	<img class='noticiaImage' alt='' src='"+url+"'/>"+
		 							"	<h3>"+name+"</h3>"+
		 							"	<div class='noticiaDate'>"+date+"</div>"+
		 							"	<div class='noticiaDescription'>"+desc+"</div>"+
		 							"	<div class='subir'>"+
		 							"		<img alt='subir' src='img/noticias_subir.jpg'></img>"+
		 							"	</div>"+
		 							"</div>";
		 			$("#noticiasElegidos").append(insertHtml);
		  		});
	 			$(".subir").click(function(){
	 				$('html, body').animate({scrollTop:0}, 'slow');
	 			});
	 			
	 			// over description
				$('.noticia').hover(function(){
					$(".noticiaDescription",this).css("overflow","auto");
					$(".noticiaDescription",this).css("height","auto");
				},function(){
					$(".noticiaDescription",this).css("overflow","hidden");
					$(".noticiaDescription",this).css("height","58px");
				});
			}
		});
	}
}
