/**
 * Javascript de Galeria de Fotos con paginador y una foto en grande.
 * 
 * @author: Natxo Vera
 * @version: 0.1
 * 
 */
var next = 0;
var prev = 0;
var maxfotos = 12;
function changeMainImage(name)
{
	$("#mainimage").hide();
	$("#mainimage").attr("src","../admin/fotos/"+name);
	$("#openimage").attr("href","../admin/fotos/"+name);
	$("#openimage").attr("rel","shadowbox"); //para el efecto del shadowbox
	$("#mainimage").show("slow");
}
function getImages(s)
{
	ruta = "paginas/ask4photos.php?limit="+maxfotos+"&start="+s;
	params = "";
	$("#listafotos").hide("slow");
	petAjaxGaleria(ruta,params,"reloadimagelist");
	
}

function reloadimagelist(json)
{
	json = eval("("+json+")");
	
	next = json.start+maxfotos;
	//por si te pasas
	if (next>json.total)
		next = next-maxfotos;
		
	if((json.start-maxfotos)<0)
		prev = 0;
	else
		prev = json.start-maxfotos;
	
	for(x=0;x<maxfotos;x++)
	{
		//$("#li_img_"+x).hide("slow");
		$("#li_img_"+x).remove();
	}	
	for(x=0;x<json.fotos.length;x++)
	{
		
		//$("#img_"+x).attr("src","fotos/thumbs/thm_"+json.fotos[x].name);
		$("#fotothumbs").append(
			jQuery("<li/>")
				.attr("id","li_img_"+x)
				.attr("style","display:inline;")
				.append(
					jQuery("<img/>")
					.attr("src",json.fotos[x].path)
					.attr("id","img_"+x)
					.attr("name",json.fotos[x].name)
					.attr("width","100px")
					.addClass("minimagen")
					.click(function(){ changeMainImage(''+this.name+'');})
				)
		)
	}
	$("#listafotos").show("slow");
	/*Paginacion*/
	pagenum = json.start/maxfotos;
	paginas = json.total/maxfotos;
	nums2show = 10; //numeros de paginacion que se van a mostrar
	
	$("#page_num_p").remove();
	$("#page_num_n").remove();
	$("#page_num_p1").remove();
	$("#page_num_n1").remove();
	for(x=0;x<nums2show;x++)
	{
		$("#page_num_"+x).remove();
	}
	
	if(pagenum>0)
	{
		$("#numeros").append(
					jQuery("<li/>")
						.attr("id","page_num_p")
						.attr("title",(pagenum-1)*maxfotos)
						.html("<")
						.click(function(){ getImages(''+this.title+'');})
		)
	}
	
	num2print = 4;
	//X numeros por detras de donde estoy, siempre y cuando sean mayores o igual a 0
	id = 0;
	//Poner una casilla con .. para que se note que hay mas...
	if ((pagenum-num2print)>=1)
	{
		$("#numeros").append(
				jQuery("<li/>")
					.attr("id","page_num_p1")
					.html("..")
				)
			id++;
	}
	
	for(x=num2print;x>0;x--)
	{
		if(((pagenum-x)>=0))
		{
			if((pagenum-x)==pagenum)
				clase = "selected";
			else
				clase = "";
				
			$("#numeros").append(
				jQuery("<li/>")
					.attr("id","page_num_"+id)
					.attr("title",(pagenum-x)*maxfotos)
					.click(function(){ getImages(''+this.title+'');})
					.addClass(clase)
					.html((pagenum-x).toString())
				)
			id++;
		}	
	}
	
	
	//X Numeros hacia delante, o los que sean hasta llegar al maximo
	for(x=0;x<num2print;x++)
	{
		if(((x+pagenum)<paginas))
		{
			if((pagenum+x)==pagenum)
				clase = "selected";
			else
				clase = "";
			
			/* un .html(0) lo interpreta como false, asi que se le ponen las comillas para que sea cadena*/			
			$("#numeros").append(
				jQuery("<li/>")
					.attr("id","page_num_"+id)
					.attr("title",(x+pagenum)*maxfotos)
					.addClass(clase)
					.html((pagenum+x).toString())
					.click(function(){ getImages(''+this.title+'');})
				)
			id++;
		}	
	}
	if ((pagenum+num2print)<parseInt(paginas))
	{
		$("#numeros").append(
				jQuery("<li/>")
					.attr("id","page_num_n1")
					.html("..")
				)
			id++;
	}
	if(pagenum<parseInt(paginas))
	{	
		$("#numeros").append(
					jQuery("<li/>")
						.attr("id","page_num_n")
						.attr("title",(pagenum+1)*maxfotos)
						.click(function(){ getImages(''+this.title+'');})
						.html(">")
					)
	}			
}
/*
 * Ver de cambiar para adaptar a la petAjax general
 */
function petAjaxGaleria(ruta, param,fnc)
{
	
var ajaxGET = false; 
	if (window.XMLHttpRequest) 
		  { 
			ajaxGET = new XMLHttpRequest(); 
		  } else if (window.ActiveXObject) 
		  		{ 
					ajaxGET = new ActiveXObject("Microsoft.XMLHTTP"); 
				}
		if(ajaxGET) { 
			 
			 if(param!=null && param!="null")
			 	ruta=ruta+"&"+param;

			 ajaxGET.onreadystatechange = function()  {
				if (ajaxGET.readyState == 4) 
				{ 
					txt = ajaxGET.responseText; 
					if (fnc!=null)
					{
						eval(fnc+"('"+txt+"')")
					}				
				}
			 }
			
			ajaxGET.open("POST", ruta, true); 			
			ajaxGET.send(param);
						 
		 } 
}