// JavaScript Document
var speedAni = 500;
var cClima = "";
var over = 0;
var cSliding = 0;

//Para controlar peticiones al servidor
var climaCacheArray = new Array();

$(function() {
	if($.browser.safari){//Fix para safari, chrome	
		$("#climas1").css({"width":"141px","height":"70px"})
	}	

	$('#climas1').cycle({
    		fx:     'scrollHorz',
			prev:   '#prev1',
    		next:   '#next1',
			speed:   speedAni,
			before:  chgStatus,
			after:  fixAfter,
    		timeout: 0
	});	
	
	$("#climas1 div").mouseenter(function(){
		over = 1;
		cClima = $(this).attr("id");
		if(climaCacheArray[cClima]){ //Si ya esta en cache
			$("#htmlClima").html(climaCacheArray[cClima]);
			showNextClima();			
		}else{ //Si no esta guardado, hacer ajax
			$.ajax({
				type: 'GET',
				url: $(this).attr("class"),
				beforeSend: function (resp) {
					$("#htmlClima").html("<div style='text-align:center;width:100&;height=100%;margin-top:50px' ><img border='0' src='../img/climaLoader.gif'</div>")
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					$("#htmlClima").html("<div class='cmsgBox' >Informaci&oacute;n no disponible</div>")
					showNextClima();
				},
				success: function (resp) {
					if(over == 1){
						$("#htmlClima").html(resp);
						showNextClima();
						climaCacheArray[cClima] = resp;
					}
				}
			});			
		}		
		
	})
	.mouseleave(function(){
		over = 0;
		$(this).css({
			//'background':'none',
			//'border':'1px solid #fff'
		});
		
		$("#climAjax").stop().css( { 
				//display: 'none'
		});		
	});
	
	$(".climaBox").css({'display':'inline'});
});

function showNextClima(html){
	
	if(over == 1 && cSliding == 0){
		var el = "#"+cClima;
		//$(el).css({'background':'url(http://www.depuntoapunto.com/img/cuadro1.png) no-repeat'});
		
		//get the position of the placeholder element	
		var pos = $(el).offset();    
		var eWidth = $(el).outerWidth();
		var eHeight = $(el).outerHeight();
		var mWidth = $("#climAjax").outerWidth();
		
		var left = (pos.left + 1) + "px";
		var top = ((pos.top + eHeight)-3) + "px"; 
		$("#climAjax").css( { 
				display: 'inline',
				position: 'absolute',
				zIndex: 5000,
				left: left,
				top: top
		});
		$("#htmlClima").show();
	}
}


/*
	Evita mostrar pox. fechas mientras
	el slide esta en movimiento */
function chgStatus(){
	cSliding = (cSliding == 0) ? 1 : 0;
}

function fixAfter(){
	if($.browser.msie){
		$("#climas1").css({"width":"144px"})
	}	
	chgStatus();
}
