var counter = 0;
var noticias = null;
function rotarNoticia(){
	$('#noticias').find('div.noticia:eq('+counter+')').animate({left:-350,opacity:'hide'},'slow',function(){
		$(this).css('left','5px');																									  
	});
	if(counter < noticias-1 ){
		counter = counter + 1;
		$('#noticias').find('div.noticia:eq('+counter+')').fadeIn('slow');
	}else{
		counter = 0;
		$('#noticias').find('div.noticia:eq('+counter+')').fadeIn('slow');
	}
}
$('#noticias').ready(function(){
	$('#noticias').load('http://www.cccucuta.org.co/c.c.c/web/hoy/noticias.xml.php',function(data){
		
		$(this).html(data);
		$(this).find('div.noticia:eq('+counter+')').fadeIn('slow',function(){
			intervaloNoticia = setInterval("rotarNoticia()",5000);															   
		});
		$(this).find('div.noticia').each(function(){
			noticias = noticias + 1;
			$('#btn-noticias').append('<span id="'+noticias+'">'+ noticias +'</span>');
		});
		$('#btn-noticias > span').each(function(){
			$(this).click(function(){
				
				if(counter != $(this).attr('id') - 1){
					$('#noticias').find('div.noticia:eq('+counter+')').animate({left:-350,opacity:'hide'},'slow',function(){
						$(this).css('left','5px');
					});
					
					counter = $(this).attr('id') - 1;
					clearInterval(intervaloNoticia);
					
					$('#noticias').find('div.noticia:eq('+counter+')').fadeIn('slow',function(){
						intervaloNoticia = setInterval("rotarNoticia()",5000);													   
					});
				}

			});
		});
		
		//adelante ó atras;
		$('#btn-principales').find('.btn-principal').each(function(i){
			if(i==0){
				$(this).click(function(){
					
					$('#noticias').find('div.noticia:eq('+counter+')').animate({left:-350,opacity:'hide'},'slow',function(){
						$(this).css('left','5px');
					});
					
					if(counter <= 0){
						counter = noticias-1;
					}else{
						counter--;	
					};
					
					clearInterval(intervaloNoticia);
					$('#noticias').find('div.noticia:eq('+counter+')').fadeIn('slow',function(){
						intervaloNoticia = setInterval("rotarNoticia()",5000);													   
					});
					
				});
			}else{
				$(this).click(function(){
					
					$('#noticias').find('div.noticia:eq('+counter+')').animate({left:-350,opacity:'hide'},'slow',function(){
						$(this).css('left','5px');
					});
					
					if(counter >= noticias-1){
						counter = 0;
					}else{
						counter++;	
					};
					
					clearInterval(intervaloNoticia);
					$('#noticias').find('div.noticia:eq('+counter+')').fadeIn('slow',function(){
						intervaloNoticia = setInterval("rotarNoticia()",5000);													   
					});
					
				});	
			}
		});
		/*$(data).find("entrada").each(function(){
			var $entrada = $(this);
			var html = '<div class="noti">';
			//alert($entrada.attr('archivo'));
			html += '<img src="../images/noticias/'+$entrada.attr('archivo')+'" class="img-left" alt="jirafa"/>';
			html += '<h3>'+$entrada.find('titulo').text()+'</h3>';
			html += '<p>'+$entrada.find('contenido').text()+'</p>';
			html += '</div>';
			$('#noticias').append($(html));
		});*/
	});
});