function automatico(){
	if($("#c0").css('display')=='none'){
		intervalo=setTimeout("automatico()",imagenes[actual].ti*1000);
		mueveExpo(direccion);
	}
}
function activaAuto(){
	
	if (!encendido){
		encendido=1;
		automatico();
	}
}
function mueveExpo(dir){
	encendido=1;
	if(t==0){
		var pos = $('.expo-interior').css('left');
			der = -((actual-1)*anc);
			izq = -( (actual+1)*anc);
		if(dir==0){
			actual--;
			$('.expo-interior').animate({ left:(der) }, 800, function(){});
		}else{
			actual++;
			$('.expo-interior').animate({ left:izq }, 800, function(){});
		}
	}else{
		for(i=0;i<imagenes.length;i++){
			if(i==actual){ $('#b'+actual).animate({opacity:1},500,'linear', function(){$("#b"+actual).css({'z-index':'10'});});
			}else{ $('#b'+i).animate({opacity:0},500,'linear', function(){}); }
		}
		if(dir==0){
			if(actual==0){ actual=imagenes.length-1; }else{ actual--; }
		}else{
			if(actual==(imagenes.length-1)){ actual=0; }else{ actual++; }
		}
	}
}

function activaEventos(){
	/*$(".expo").mouseenter(function(){

		$("#c0").show();
		$("#c1").show();
		clearTimeout(intervalo);
		encendido=0;
	}).mouseleave(function(){
  
		$("#c0").hide();
		$("#c1").hide();
		clearTimeout(intervalo);
		encendido=0;
		$('.expo').animate({opacity:0.99},100,'linear', function(){});
		$('.expo').animate({opacity:1},900,'linear', function(){activaAuto();});
	});*/
	$(".bloque").mouseover(function(){

		$("#c0").show();
		$("#c1").show();
		clearTimeout(intervalo);
		encendido=0;
	});
	$(".bloque").mouseout(function(){
  
		$("#c0").hide();
		$("#c1").hide();
		clearTimeout(intervalo);
		encendido=0;
		$('.expo').animate({opacity:0.99},100,'linear', function(){});
		$('.expo').animate({opacity:1},900,'linear', function(){activaAuto();});
	});

}

function cargaDatos(){
	var elemento = function(ur,im,ti){
		this.ur = new String(ur);
		this.im = new String(im);
		this.ti = new Number(ti);
	}
	function azar(){
		return (Math.round(Math.random())-0.5);
	}
	$(document).ready(function(){
		$('.expo').animate({opacity:0},10,'linear');
		$.ajax({  
			type: "GET",
			url: "/consum-doc/home/carrusel/intro.xml",
			dataType: "xml",
			success: function(xml) {
				imagenes = new Array();
				var inicio = 0;
				t = $(xml).find('diaporama').attr('transicion');
				var a = $(xml).find('diaporama').attr('aleatorio');
				anc = $(xml).find('diaporama').attr('ancho');
				alt = $(xml).find('diaporama').attr('alto');
				aut = $(xml).find('diaporama').attr('auto');
				$('.expo').css({'width':anc, 'height':alt});
				$(xml).find('imagen').each(function(){
					
					var url = $(this).attr('url');
					var imagen = $(this).attr('imagen');
					var tiempo = $(this).attr('tiempo');
				
					imagenes[inicio] = new elemento(url,imagen,tiempo);
					inicio++;

				});
				if(a==1){
					imagenes.sort(azar);
				}
				actual = 0;
				direccion = 1;
				canc = 0;
				encendido=0;
				for (i=0;i<imagenes.length;i++){ 
//					$('<div></div>').html('<a href="'+imagenes[i].ur+'" target="_blank"><img src="'+imagenes[i].im+'" alt="" /></a></div></div>').appendTo('.expo-interior');
					$('<a href="'+imagenes[i].ur+'" target="_self"><img src="'+imagenes[i].im+'" alt="" /><span></span></a>').appendTo('#diapo');
				}
				//activaEventos();
				$('#diapo').coinslider({width: 480, height: 414, navigation: true, delay: 4000, spw: 1, sph: 1, hoverPause: true});
				if(t==0){
					//	si la transici�n es slide, alineamos las cajas
					for(j=0;j<imagenes.length;j++){
						$('#b'+j).css({'left':anc*j});
					}
				}else{
					for(j=0;j<imagenes.length;j++){
						$('#b'+j).css({'left':0, 'z-index':-j, 'opacity':1});
					}
				}
				if(aut==1){
					$('.expo').animate({opacity:0.9},1000,'linear', function(){});
					$('.expo').animate({opacity:1},imagenes[actual].ti*1000,'linear', function(){activaAuto();});
				}else{
					$('.expo').animate({opacity:1},5000,'linear');	
				}
			}
		});
	});
}
