
 
/***********
CARGAR IMAGEN
*/
function cargar_imagen(s, i) {
	
	var miniaturaseleccionada = $("#mini-img-"+i);
	miniaturaseleccionada.addClass("visionado"); 
	
	$("#imagen").fadeOut('slow', function () {
		 $(this).load("inc/asinc/imagen_trabajo.php?id_s="+s+"&id_i="+i, function () {
			 $(this).fadeIn("slow");
		 });
	});
};


function cargar_trabajo(s, i, t) {
	
	var miniaturaseleccionada = $("#mini-serv-"+s);
	miniaturaseleccionada.addClass("visionado");

	$("#trabajo").fadeOut('slow', function () {
		 $(this).load("inc/asinc/trabajo.php?id_s="+s+"&id_i="+i, function () {
			 $(this).fadeIn("slow");
		 });
	});
};



/*******************
MINIATURAS
*******************/
function colocarMiniaturas() {	
	
	var tempTop = $.cookie("posicion");
	
	var contenedor = $("#miniaturas");
	var altoContenedor = contenedor.height();
	
	var elemento = $("#contenido_miniaturas");
	var posicionElemento = elemento.position();
	
	
	if (posicionElemento.top != tempTop && tempTop!= null) {
		var num = posicionElemento.top;
		var num2 = tempTop;
		var mov = num - num2;
		elemento.css("top", "-="+mov);
	}
	var t = elemento.position();
	var tt = t.top;
	//alert ("Pos. elem.: " + num + " / tempTop: " + tempTop +  " / mov: " + mov + " / nueva Posicion: "+ tt);
	comprobarSiPintarScroll();
}



/*******************
SCROLL
*******************/
function comprobarSiPintarScroll() {
	
	var contenedor = $("#miniaturas");
	var posicionContenedor = contenedor.position();
	var altoContenedor = contenedor.height();
	
	var elemento = $("#contenido_miniaturas");
	var posicionElemento = elemento.position();
	var altoElemento = elemento.height();
	
	
	var limiteTop = posicionContenedor.top - (altoElemento - altoContenedor);
	
	var up = $("#up");
	var down = $("#down");
	
	var visibleUp;
	var visibleDown;
	var altoUp = up.height();
	var altoDown = down.height();
	var posUp = up.position();
	var posYup = posUp.top;
	var posDown = down.position();
	var posYdown = posDown.top;
	
	//alert("Cookie Posicion del Elemento: " + $.cookie("posicion"));
	if (altoElemento>altoContenedor) {
				
		if(posicionElemento.top != posicionContenedor.top) {
			
			up.css("left", 0);
			up.fadeIn("slow");
			visibleUp = true;
		
		} else {
			up.fadeOut("slow");
		}
		
		
		if (limiteTop < posicionElemento.top ) {
			
			//posicion Y de down dependiendo de si esta visible up
			if(visibleUp == true) {
				var posY = altoContenedor - (altoDown + altoUp);
			} else {
				var posY = altoContenedor - altoDown; 
			};
			
			down.css("top",posY);
			down.css("left", 0);
			down.fadeIn("slow");
			visibleDown = true;
		
		} else {
			
			down.fadeOut("slow");
		}
	} else {
		up.hide();
		down.hide();
	}
}



function scroll(accion) {

	var contenedor = $("#miniaturas");
	var posicionContenedor = contenedor.position();
	var altoContenedor = contenedor.height();
	
	var elemento = $("#contenido_miniaturas");	
	var posicionElemento = elemento.position();
	var altoElemento = elemento.height();
	
	var limiteTop = posicionContenedor.top - (altoElemento - altoContenedor);
	
	var movimiento = altoContenedor; 
	var elementoOcultoTop = posicionContenedor.top - posicionElemento.top;
	var elementoOcultoDown = (posicionElemento.top + altoElemento) - (posicionContenedor.top + altoContenedor);
	
		
	if(accion=='subir'){
		
		if (elementoOcultoTop < altoContenedor ) movimiento = elementoOcultoTop;
		
		elemento.animate({top: "+="+movimiento}, "easing", function () {
			// sino creamos una nueva variable para la posicion top del elemento, no se actualiza
			var pos = elemento.position();
			var posTop = pos.top;
			$.cookie("posicion", posTop);
			//alert("guardando cookie: " + $.cookie("posicion"));
			comprobarSiPintarScroll();
		});
	} else {

			
		if (elementoOcultoDown < altoContenedor ) movimiento = elementoOcultoDown;
		
		elemento.animate({top: "-="+movimiento}, "easing", function () {
			// sino creamos una nueva variable para la posicion top del elemento, no se actualiza
			var pos = elemento.position();
			var posTop = pos.top;
			$.cookie("posicion", posTop);
			//alert("guardando cookie: " + $.cookie("posicion"));
			comprobarSiPintarScroll();
		});
			
	}
}

