var array= new Array();
var total = 0;
$(document).ready(function(){
$('#galeria_content').html('<div style="margin-top:150px;"><img src="imagenes/ajax-loader.gif" /></div>');
$.ajax({
   type: "GET",
   url: "xml_notas.php",  
   success: function(xml){
	   $('#galeria_content').html('')
	var i=0;
    $(xml).find('itemD').each(function(){
		if (i==0) {
			$(document.createElement('div')).attr({'rel':'t-5000','class':'visible','style':'display:block'}).appendTo('#galeria_content').html('<img src="'+decodeURIComponent($(this).attr('foto'))+'"');			
		}
		else {
			$(document.createElement('div')).attr({'rel':'t-5000','style':'display:none'}).appendTo('#galeria_content').html('<a href="'+$(this).attr('link')+'"><img src="'+decodeURIComponent($(this).attr('foto'))+'"/></a>');
		}
		array[i] = new Image();
		array[i].src = decodeURIComponent($(this).attr('foto'));
		total++;
		i++;
	});
	empezar();
   }
});
});

var tiempo = 5000;
var control;
var contador = 0;
function cambiar(mover){	
	if (mover==1) {
		if (contador==total-1) {
		contador=0;
		div = $('#galeria_content .visible').removeClass('visible').fadeOut('slow');
		$('#galeria_content div:first').fadeIn('slow').addClass('visible');
		control = setTimeout(function(){cambiar(1)},tiempo);
		contador++;	
		}
		else {
		div = $('#galeria_content .visible').removeClass('visible').fadeOut('slow');
		div.next().fadeIn('slow').addClass('visible');
		control = setTimeout(function(){cambiar(1)},tiempo);
		contador++;
		}
	}
	else {
		if (contador==1) {
		contador=total;
		$('#galeria_content .visible').removeClass('visible').fadeOut('slow');
		$('#galeria_content div:last').fadeIn('slow').addClass('visible');
		control = setTimeout(function(){cambiar(1)},tiempo);
		contador--;
		} else {
		div = $('#galeria_content .visible').removeClass('visible').fadeOut('slow');
		div.prev().fadeIn('slow').addClass('visible');
		control = setTimeout(function(){cambiar(1)},tiempo);
		contador--;
		}
	}
	
	
}


function empezar(){
	if (array[2].complete){
		setTimeout('cambiar(1)',tiempo);
	}
	else
	{
	setTimeout('empezar()',100)	 
	 }
}

$(document).ready(function(){
	$('#fle_izq img:first').click(function(){
		clearTimeout(control);
		cambiar(-1);
	});
	$('#fle_der img:first').click(function(){
		clearTimeout(control);
		cambiar(1);
	});
});


