// JavaScript Document
var nPaisajes=53;
var proximoPaisaje=1;
var destino = "#fondoH1";
var secundario = "#fondoH2";
var espSlideVert = 133; //Espaciado en slides verticales

$(document).ready(function(){
	$('.redondeado').corner();
	$(".item").hover(
		function(){$(this).stop().animate({paddingLeft : 10},200);},
		function(){$(this).stop().animate({paddingLeft : 0},200);}
	);
	$("#fondoH1").fadeTo(0,0);
	$("#fondoH2").fadeTo(0,0);
	//if($("#opacidad").length > 0) $("#opacidad").hide();
	if($("#cmSlide").length > 0) $("#cmSlide").hide();
	
	cambiarFondo();	
});

function cambiarFondo(){
	proximoPaisaje = Math.round(Math.random()*nPaisajes) + 1;
	var actual = new Image();
	var siguiente = new Image();	
	actual.onload = function(){
		$(destino).html(actual);
		$(destino).fadeTo(2000,.5);
		$(secundario).fadeTo(2000,0);
		if(destino == "#fondoH1"){ 
			destino = "#fondoH2"; 
			secundario = "#fondoH1";
		}else{
			destino = "#fondoH1"; 
			secundario = "#fondoH2";
		}
		setTimeout(cambiarFondo,8000);
	};
	actual.src = "imagenes/fondos/paisaje"+proximoPaisaje+".jpg";
}

function cmFav(){
	if ((navigator.appName=="Microsoft Internet Explorer") && (parseInt(navigator.appVersion)>=4)) {
		var url="http://www.cristalmedia.com.ar/";
		var titulo="CristalMedia";
		window.external.AddFavorite(url,titulo);
	}else {
		if(navigator.appName == "Netscape")
		window.sidebar.addPanel("CristalMedia", "http://www.cristalmedia.com.ar","");
	}
}

function pagAntH(div){
	$(div).stop().animate({
		scrollLeft : $(div).scrollLeft() - $(div + "> *:first > *:first").width()},"easeOutQuad",function(){checkLimH(div);}
	);	
}

function pagSigH(div){
	$(div).stop().animate({
		scrollLeft : $(div).scrollLeft() + $(div + "> *:first > *:first").width()},"easeOutQuad",function(){checkLimH(div);}
	);	
}

function pagAntV(div){
	$(div).stop().animate({
		scrollTop : $(div).scrollTop() - espSlideVert},"easeOutQuad",function(){checkLimV(div);}
	);	
}

function pagSigV(div){
	$(div).stop().animate({
		scrollTop : $(div).scrollTop() + espSlideVert},"easeOutQuad",function(){checkLimV(div);}
	);	
}

function checkLimV(div){
	if($(div).scrollTop()==0){ 
		$(div).parent().find(".msAr").fadeTo(500,.3);
	}else{ 
		$(div).parent().find(".msAr").fadeTo(500,1);
	}
	if($(div).scrollTop()>=$(div + "> *:first").height() - espSlideVert){ 
		$(div).parent().find(".msAb").fadeTo(500,.3);
	}else{ 
		$(div).parent().find(".msAb").fadeTo(500,1);
	}		
}

function checkLimH(div){
	if($(div).scrollLeft()==0){ 
		$(div).parent().find(".msAi").fadeTo(500,.3);
	}else{ 
		$(div).parent().find(".msAi").fadeTo(500,1);
	}
	if($(div).scrollLeft()>= $(div + "> *:first").width() - $(div + "> *:first >*:first").width()){ 
		$(div).parent().find(".msAd").fadeTo(500,.3);
	}else{ 
		$(div).parent().find(".msAd").fadeTo(500,1);
	}
}

$(".SlideMini").click(function(){
	var titulo = $(this).attr("titulo") || "Imagen sin nombre";
	var url = $(this).attr("imagen");
	var descripcion = $(this).attr("descripcion") || "CristalMedia";
							   
	$("#opacidad").show().fadeTo(500,0.7);
	$("#cmSlide").show().fadeTo(500,1);
	
	$("#cmSlideContImg").html("<img src='imagenes/cargando.gif' />");
	
	var posicionamiento = setInterval(function(){
		$("#cmSlide").css({marginLeft : $("#cmSlide").width() / -2 , marginTop : $("#cmSlide").height() / -2});		
	},50);
	
	imagen = new Image();
	imagen.src = url;
	imagen.onload = function(){
		if($("#cmSlideContImg").width() != imagen.width && $("#cmSlideContImg").height() != imagen.height){
			$("#cmSlideContImg").animate({
				width : imagen.width,
				height : imagen.height
			},500,function(){
				$("#cmSlideContImg").fadeTo(0,0).fadeTo(500,1);
				$("#cmSlideTitulo").html(titulo);
				$("#cmSlideContImg").html(imagen);
				$("#cmSlideDesc").width($("#cmSlideContImg").width());
				$("#cmSlideDesc").html(descripcion);
				$("#cmSlide").css({marginLeft : $("#cmSlide").width() / -2 , marginTop : $("#cmSlide").height() / -2});
				clearInterval(posicionamiento);
			});	
		}else{
			$("#cmSlideContImg").fadeTo(0,0).fadeTo(500,1);
			$("#cmSlideTitulo").html(titulo);
			$("#cmSlideContImg").html(imagen);
			$("#cmSlideDesc").width($("#cmSlideContImg").width());
			$("#cmSlideDesc").html(descripcion);
			$("#cmSlide").css({marginLeft : $("#cmSlide").width() / -2 , marginTop : $("#cmSlide").height() / -2});
			clearInterval(posicionamiento);
		}
	}
});

function cerrarSlide(){
	$("#cmSlideContImg").html(null);
	$("#cmSlide").fadeTo(500,0,function(){$("#cmSlide").css({display : "none"})});
	$("#opacidad").fadeTo(500,0,function(){$("#opacidad").css({display : "none"})});
}
