// JavaScript Document
$(document).ready(function () {
	
var	thumb;
var time;
var posicion=0;
var overDa;
var overIz;
var speed=8;
var tope=218;
	// deshabilito la flecha izquierda
	disableIz();
	
	$("#thumbnails img").mousedown( function(a) { 
		thumb=this;
		time=400;
		$("#fotoPrincipal").fadeOut(time,callb);

	});
	callb = function(){
		cambiarFoto(thumb, showFoto);
	}

	showFoto = function(){
		$("#fotoPrincipal").fadeIn(time);		
	}
	
// Test
	$("#flechaDa").live("mousedown", function(e) { 
		overDa=true;	
		$("#flechaDa").trigger('mueve');
	});
	$("#flechaIz").live("mousedown", function(e) { 
		overIz=true;	
		$("#flechaIz").trigger('mueve');
	});
	
	$("#flechaIz").live("mueve", function(e) { 
			if(posicion<0)
			{
				posicion=posicion+1+parseInt(speed-speed*((posicion+tope)/tope));
				$("#thumbCont").css("margin-left",posicion+"px");
				delay=parseInt(speed*((posicion+tope)/tope));
				if(overIz) setTimeout(function(){$("#flechaIz").trigger('mueve');},delay);
				checkDisable();
			}
	});

	$("#flechaDa").live("mueve", function(e) { 
			if(posicion>-tope)
			{
				posicion=posicion-1-parseInt(speed*((posicion+tope)/tope));
				$("#thumbCont").css("margin-left",posicion+"px");
				delay=parseInt(speed-speed*((posicion+tope)/tope));
				if(overDa) setTimeout(function(){$("#flechaDa").trigger('mueve');},delay);
				checkDisable();
			}
	});

	$("#flechaDa").live("mouseup", function(e) { 
		overDa=false;
	});
	$("#flechaIz").live("mouseup", function(e) { 
		overIz=false;
	});
	function checkDisable(){
			if(posicion==0)
			{
				if(!$("#flechaIz").hasClass("disabled")){
					disableIz();
				}
			} else {
				
				if($("#flechaIz").hasClass("disabled")){
					enableIz();
				}
				
			}
			if(posicion!=-tope){
				if($("#flechaDa").hasClass("disabled")){
						enableDa();
				}
			} else {
					if(!$("#flechaDa").hasClass("disabled")){
						disableDa();
					}
			}
		
	}
	
	function disableIz(){
		$("#flechaIz").addClass("disabled");
	}
	function disableDa(){
		$("#flechaDa").addClass("disabled");
	}
	function enableIz(){
		$("#flechaIz").removeClass("disabled");
	}
	function enableDa(){
		$("#flechaDa").removeClass("disabled");
	}
	
	
	cambiarFoto = function (f, callback){
		tb=$(f).attr("src");
		lnk=$(f).attr("longdesc");
		pThumb=tb.split(".");
		var pStr="";
		for(i=1;i<=(pThumb.length-2);i++){
			pStr+=pThumb[i]+'.';
		}
		
		$("#fotoPrincipal").html('<div class="bdr"><a href="'+lnk+'"><img src="/fotos/restaurante.'+pStr+'jpg" /></a></div>');
		// Callback
		if($.isFunction(callback)) {
			
			callback.call(this);
		}		
	}
	// Tooltip
	$(".tooltip").qtip({
   	content: 'Este valor se calcula en base a los usuarios que le recomendar&iacute;an este restaurante a un amigo.',
	position: {
		target: 'mouse',
		adjust: { scroll: true, resize: true, mouse: true },
		corner: {
			tooltip: 'bottomLeft',
			target: 'topRight'
		}
    },
	style: { 
	  width: 180,
	 'font-family': 'arial',
	 'font-size':'10pt',
	 tip: true, // Give it a speech bubble tip with automatic corner detection
	 border: {
         width: 0,
         radius: 2,
         color: '#e82428'
      },
   	 color: 'black',
	 background: '#FFFFFF',
     textAlign: 'center',
     name: 'red' // Inherit from preset style
   	}
	});	



}); // fin docready
