
$(document).ready(function(){ 
	$(".ui-tabs-nav a[href=#mapa]").click(function(){
		$('#mapaAmpli').innerfade({speed: 'normal', timeout: 2000, type: 'sequence', containerheight: '270px'}); 
	});
	$('.fade').innerfade({speed: 'normal',timeout: 1000,type: 'sequence',containerheight: '1.5em'}); 
	
	// velocidad
	var fadeVel=500;
	var changeVel=2000;
	
	// sets on year/month change
	played=0;
	act=$("#fadeImg").children().attr("id");
	fst=act;
	lst=$("#fadeImg>div:last").attr("id");
	$("#"+act).css("display", "block").css("z-index", "1000");

	$("#buttright").click(function(){goFade(1,0);});
	$("#buttleft").click(function(){goFade(0,0)});

	$("#buttpp").click(function(){
		if(played){
			$(this).attr("src", "img/play.gif");
			played=0;
			$("#buttpp").stop();
		}else{
			$(this).attr("src", "img/pause.gif");
			played=1;
			goFade(1,1);
		}
	});

	function goFade(d,r){
		(d==0)?nxt=$("#"+act).prev("div").attr("id"):nxt=$("#"+act).next("div").attr("id");
		if(!nxt){(d==0)?nxt=lst:nxt=fst;}
			$("#"+act).fadeOut(fadeVel).css("z-index", "1");
			$("#"+nxt).fadeIn(fadeVel).css("z-index", "1000");
			act=nxt;
		if(r){$("#buttpp").animate({opacity:'1'},changeVel,function(){goFade(1,1);});}
	}
	
	var mons=new Array();
	mons[0]="";
	mons[1]="enero";
	mons[2]="febrero";
	mons[3]="marzo";
	mons[4]="abril";
	mons[5]="mayo";
	mons[6]="junio";
	mons[7]="julio";
	mons[8]="agosto";
	mons[9]="septiembre";
	mons[10]="octubre";
	mons[11]="noviembre";
	mons[12]="diciembre";

	//to set on load if there's collection
	if($("#allImg").html()!=""){
		avMons=new Array();
		currMon=0;
		currYear="2008";
		setMon(currYear);
		$("#fadeImg").html($("#c-"+currYear+"-"+avMons[0]).html());
		setIni();
	}

	$(".calendarYear a").click(function(){
		z=$("#allImg div[id^=c-"+$(this).text()+"]").attr("id");
		if($(this).text()!=currYear && z){
			$(".calendarYear a").removeClass("select");
			$(this).addClass("select");
			currYear=$(this).text();
			$("#fadeImg").html($("#"+z).html());
			setMon(currYear);
			setIni();
			currMon=0;
		}
		return false;
	});
	
	$("#mleft, #mright").click(function(){
		cm=currMon;
		($(this).attr("id")=="mleft")?cm--:cm++;
		if(!isNaN(parseInt(avMons[cm]))){
			$(".calendarMonth span").text(mons[avMons[cm]]);
			currMon=cm;
			$("#fadeImg").html($("#c-"+currYear+"-"+avMons[cm]).html());
			setIni();
		}
		return false;
	});
	
	function setMon(yid){
		avMons.length=0;
		$("#allImg div[id^=c-"+yid+"]").each(function(i){
			qa=$(this).attr("id").split("-");
			avMons[i]=qa[2];
		});
		avMons.sort(function(a,b){return a - b});
		$(".calendarMonth span").text(mons[avMons[0]]);
	}
	
	function setIni(){
		played=0;
		act=$("#fadeImg").children().attr("id");
		fst=act;
		lst=$("#fadeImg>div:last").attr("id");
		$("#"+act).css("display", "block").css("z-index", "1000");
	}
});






