var cont = 1;
var tiempo_carga = 12000;
var tiempo_layer = 11000;
var time, time2;
var cantLayer = 0; // Cantidad de veces que se muestra el layer
var bannerAleatorio = 1; //1 es SI aleatorio, 0 es NO aleatorio

$(document).ready(function(){
	
	if ($.cookie("esconderbanner")<cantLayer)
		{
			var actual = $.cookie("esconderbanner");
			$('#layerFull').load('banner_central/layer_full.asp', function(){
				if ($('#layerFull').html()==""){ 
					$('#layerFull').remove();
					
				}
				else
					{
						time2 = setInterval("_cerrarLayer()",tiempo_layer);
						$('#layerFull').append("<a href='#' class='cerrar'>&nbsp; </a>");
						//$('#layerFull').append("<div id='enlace' class='capas1'>&nbsp;</div>");
					}
			});
			
			/*$("#enlace").live("click", function(evento){
				window.document.location.href='/campanas/pi_seguros_verano/index.asp';				
	
			});*/
			$("#layerFull .cerrar").live("click", function(evento){
				evento.preventDefault();
				_cerrarLayer();
			});
			$.cookie('esconderbanner',++actual);
		}
	else
		{
			_cerrarLayer();	
		}
	
	$('#calugaTamanos').load('banner_central/calugas.asp',function (){
		var cambio = $("#calugaTamanos div").length;
		$("#calugaTamanos div").addClass("calugaTamano"+cambio);
		
		if (cambio > 1)
			{
				$(".calugaTamano"+cambio).each(function(i){
					if (i==0)
						{
							$(this).css("margin-left","0");	
						}	
				});				
				//$("#calugaTamanos div").last().css("margin-right","0");
			}
	});
	//$('#home_prods').load('banner_central/menu_lateral.asp');
	_crearPaginador();
		
	$(".tiempo_nro").live("click",function(evento){
		evento.preventDefault();
		clearInterval(time);
		time = setInterval("_banner()",tiempo_carga);
		cont = $(this).text() - 1;
		_banner();
	});
	
	$(".detener").live("click",function(evento){
		evento.preventDefault();
		$(this).removeClass("detener").addClass("iniciar");
		$("#accion").attr("src","/banner_central/estilos/bitmaps/play.gif");
		clearInterval(time);
	});

	$(".iniciar").live("click",function(evento){
		evento.preventDefault();
		time = setInterval("_banner()",tiempo_carga);
		$(this).addClass("detener").removeClass("iniciar");
		$("#accion").attr("src","/banner_central/estilos/bitmaps/pause.gif");
	});	
})

function _banner(){
	var total = _cantidadBanner();
	if (cont >= total) cont=1;
	else ++cont;
	
	var actual = $(".seleccionado").text();
	$(".seleccionado").removeClass("seleccionado");
	$("#" + cont).addClass("seleccionado");

	$('#banner' + actual).fadeOut("fast", function(){
		$('#banner' + cont).css("opacity","0").show().animate({"opacity":"1"}, "fast");
	});
}

function _crearPaginador()
{
	$('div.banner_contenedor').load('banner_central/banner.asp',function(){
		
		if (bannerAleatorio==1){
			var contenido="";
			caluga_cantidad = $('div.banner_contenedor div').length;
	
	 	 $('div.banner_contenedor .fijo').each(function(i){
				contenido += "<div>";
				contenido += $(this).html();
				contenido += "</div>";
				$(this).remove();
				caluga_cantidad--;
			 });
			 
			 $('div.banner_contenedor div:not(.fijo)').each(function(i){
				var numero = aleatorio("0",(caluga_cantidad - (i +1)));
				
				 $('div.banner_contenedor div').each(function(o){
					if (o == numero)								
						{
						
							contenido += "<div>";
							contenido += $(this).html();
							contenido += "</div>";
							$(this).remove();
							
						}
					});																 
				 });
			$("div.banner_contenedor").html(contenido);
		}
		else{  //inicio codigo para tomar desde siempre el paginador al hacer click.
			$("div .banner_contenedor div").each(function(i){
				if(i==0)
				    $("#banner1").css("display","block");
				else{
					$(this).attr("id", "banner" + (i)).css("display","none");
					}
					i=i+1;
			  });
			}//fin codigo paginador click
		
		$("div .banner_contenedor div").each(function(i){
			$(this).attr("id", "banner" + (i + 1));
			$(".banner_tiempo").append('<li id="'+ (i + 1) +'"><a href="#" class="tiempo_nro">'+ (i + 1) +'</a></li>');
		});
		
		var total = _cantidadBanner();
		if (total > 1)
			{
				$(".banner_tiempo").append('<li id="pausa"><a href="#" class="detener"><img id="accion" src="/banner_central/estilos/bitmaps/pause.gif" border="0" /></a></li>');
				$("#1").addClass("seleccionado");
				
				time = setInterval("_banner()",tiempo_carga);
			}
		else
			$(".banner_tiempo").remove();
    });
}

function _cantidadBanner(){
	return $(".tiempo_nro").length;
}

function _cerrarLayer(){
	$('#layerFull').fadeOut();
	clearInterval(time2);
}

function aleatorio(a,b) {
		return Math.round(Math.random()*(b-a)+a);
}
