var anchoContainer = 0;
var thumbs_visible = true;
var fotosCategoria = new Array();  
var actualImgNum = 0;
$(document).ready(function(){	//Se ejecuta cuando se carga la pag.
 	$('a.mainMenu').click(function (e){
		checkURL(this.hash);
	});
	$('#fp_thumbtoggle').click(function(e){
		if(thumbs_visible){
			hideThumbs();
		}else{
			showThumbs(500);	
		}
		
	});
	//controlamos si se mueven en el historial del navegador.
	setInterval("checkURL()",250);
	checkURL();
});
$(document).keydown(function(event) {
    switch (event.keyCode) {
        case 37: loadPrev(); break;
        case 39: loadNext(); break;
    }
});
function resetCategoria(){
	actualImgNum = 0;
	fotosCategoria = new Array();
}
function addContent2Categoria(content){
	fotosCategoria.push(content);
}
function loadNext(){
	if(actualImgNum<fotosCategoria.length-1){
		actualImgNum++;
	}else{
		actualImgNum = 0;
	}
	loadImg(fotosCategoria[actualImgNum]);
}
function loadPrev(){
	if(actualImgNum>0){
		actualImgNum--;
	}else{
		actualImgNum = fotosCategoria.length-1;
	}
	loadImg(fotosCategoria[actualImgNum]);
}
function loadByNum(num){
	
	if(num>fotosCategoria.length-1){
		num = fotosCategoria.length-1;
	}
	actualImgNum = num;
	loadImg(fotosCategoria[actualImgNum]);
}
function loadImg(img_src){
	var img = new Image();
	img.onload = function() {
	  //
	  if(this.height > $("#imageBox").height()) {
	  		$(this).css("height",$("#imageBox").height());
			$(this).css("width",'auto');
	        //this.height = $("#imageBox").height();
	        //this.width = 'auto';
	    }else if(this.width > $("#imageBox").width()){
	    	
	    	$(this).css("width",$("#imageBox").width());
			$(this).css("height",'auto');

	    	//this.width = $("#imageBox").width();
	        //this.height = 'auto';
	    }
	    
	}
	
	img.src = img_src;
	$("#imageBox").html(img);
	if(thumbs_visible){
		hideThumbs();	
	} 
}
function hideThumbs(){
	thumbs_visible = false;
	$('#mainContainer').stop().animate({'bottom':'-220px'},500);
	hideThumbsBtn();
}

function showThumbs(speed){
	thumbs_visible = true;
	$('#mainContainer').stop().animate({'bottom':'0px'},speed);
	showThumbsBtn();
}

function hideThumbsBtn(){
	$('#fp_thumbtoggle').stop().animate({'bottom':'0px'},500);
}

function showThumbsBtn(){
	$('#fp_thumbtoggle').stop().animate({'bottom':'220px'},500);
}


var lasturl="";
var homeurl="";

function checkURL(hash)
{
	if(!hash) hash=window.location.hash;	//si no viene el hash tomamos el de la url actual.
	//console.log(hash);
	if(hash != lasturl)	// si cambio el hash
	{
		//console.log("entra al if hash != lasturl");
		lasturl=hash;	//actualizar hash actual
		loadCategoria(hash);	// cargar nueva pagina.
	}
}

function str_replace(busca, repla, orig)
{
	str 	= new String(orig);

	rExp	= "/"+busca+"/g";
	rExp	= eval(rExp);
	newS	= String(repla);

	str = new String(str.replace(rExp, newS));

	return str;
}

function loadCategoria(url){ //cargar web por Ajax.
	//console.log("entra al loadCategoria "+url);
	url=url.replace('#/','');
	var split_arr = url.split("@");
	var section =  split_arr[0];
	//section= section.replace('-',' ');
	idcategoria = split_arr[1];
	//$('#categoriaLoading').css('visibility','visible');
	$("#fp_thumbtoggle").html(section);
	
	$.ajax({
		type: "GET",
		url: "cargar_categoria.php",
		data: 'idcategoria='+idcategoria,	//informacion a cargar
		dataType: "html",	//expect html to be returned
		success: function(msg){
			if(parseInt(msg)!=0){
				$("#container").html(msg);	//cargar el resultado en el div destino
				//$("#categoriaLoading").css('visibility','hidden');
				anchoContainer = 10;
				$("#container").children("a").children("img").load(
					function (e){
					 anchoContainer +=  $(this).width()+5;
					$("#container").css('width',anchoContainer);
					}
				)

				showThumbs(500);
				//console.log(fotosCategoria.length);
				
			}
		},
		statusCode: {
   			 404: function() {
     		 alert('page not found');
   		 	}
 		}

	});

}
