function pantallacompleta()
{
	$('#flechaOut').trigger('click');
}


function updateBackground() 
{
  screenWidth = $(window).width();
  screenHeight = $(window).height();
  var bg = $("#bg");
 
  // Proporcion horizontal/vertical. En este caso la imagen es cuadrada
  ratio = 1.77//1.68;//1.5;
 
  if (screenWidth/screenHeight > ratio)
  {
    $(bg).height("auto");
    $(bg).width("100%");
  }
  else
  {
    $(bg).width("auto");
    $(bg).height("100%");
  }
 
  // Si a la imagen le sobra anchura, la centramos a mano
  if ($(bg).width() > 0)
  {
    $(bg).css('left', (screenWidth - $(bg).width()) / 2);
  }
}

function updateContenedor()
{
	$('#contenedor').width($(window).width() - $('#menu').width());
}


function checkBrowserWidth()
{
	var theWidth = getBrowserWidth();

	var size=(theWidth*100)/1400

	if (theWidth < 0)//850
	{
		document.getElementsByTagName("body").item(0).style.fontSize= '70%';
		$('img#escudo').hide();
		/*
		$('.contBG').animate({left: '-=5em',}, 0);
		$('.cont').animate({left: '-=5em',}, 0);*/

	}
	else
	{
		document.getElementsByTagName("body").item(0).style.fontSize= size+'%';
		$('img#escudo').show();
		
		/*
		$('.contBG').animate({left: '+=5em',}, 0);
		$('.cont').animate({left: '+=5em',}, 0);*/
	}



/*
if (theWidth > 1400)
		{
			document.getElementsByTagName("body").item(0).style.fontSize='120%';
		}
	else
		if (theWidth > 1200)
				{
					document.getElementsByTagName("body").item(0).style.fontSize='100%';
				}
			else
				if (theWidth > 999)
							{
							document.getElementsByTagName("body").item(0).style.fontSize='100%';
						}
					else { 
						if (theWidth > 850)
							{
							document.getElementsByTagName("body").item(0).style.fontSize='81%';
							}
						else { 
							if (theWidth > 730)
								{
								document.getElementsByTagName("body").item(0).style.fontSize='76%';
								}
							else { 
								document.getElementsByTagName("body").item(0).style.fontSize='71%';
								}
							}
						}*/
	
}

function getBrowserWidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth != 0)
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}
	
	return 0;
}


function adaptaCurva()
{
  var valor = 0;//$(window).height()
  var c=0.16;
  
  //($(window).height() * 0.15)/569
  
  $('ul.menuDespl > li, ul.menu2 > li').each(function(index)
  {	
  	if(index <= 4)
  	{
	  	valor = index*c;
	}
	else
		if(index == 8)
		{
			valor = valor - 3*c*((index%4)+1);
		}
		else
			if(index > 8)
			{
				valor = valor - (index%4)*c;
			}
	

	$(this).css('margin-left',valor.toFixed(2)+'em');
  	
  });
}


function controlOcultar()
{

	 $('img#flechaOut, img#flechaIn').hover(
		function() 
		{
			if($(this).attr('src') == 'img/flechaR.png')
				$(this).attr('src','img/flechaRHo.png');
			else
				$(this).attr('src','img/flechaLHo.png');
		}, 
		function() 
		{
			if($(this).attr('src') == 'img/flechaRHo.png')
				$(this).attr('src','img/flechaR.png');
			else
				$(this).attr('src','img/flechaL.png');
		}); 

  $('img#flechaOut').bind('click',function()
  {
  	$('img#menuBG').fadeOut(500);
  	$('div#menu').fadeOut(500);
  	$('img#flechaOut').fadeOut(500);
  	$('.ui').fadeOut(500);
  	
  	$('img#menuIn').fadeIn(500);
  	$('img#flechaIn').fadeIn(500);
  	
  	
  });
  
  $('img#flechaIn').bind('click',function()
  {
  	$('img#menuIn').fadeOut(500);
  	$('img#flechaIn').fadeOut(500);
  	$('.ui').fadeIn(500);
  	
  	$('div#menu').fadeIn(500);
  	$('img#menuBG').fadeIn(500);
  	$('img#flechaOut').fadeIn(500);
  });
 
}

function controlMenu()
{
	//contorl desplegables del menu
  $('ul.menuDespl li > a').mouseenter(function()
  {
		 
		var panel = $(this).parent().find('.drop')
		
		var margen = $(this).width();
		
		//napa para el menu en ie7
		if (!($.browser.msie && $.browser.version < 8))
		{
			margen = margen + 8;
		}
		
		$(panel).css('left',margen+'px');
		$(panel).show();
		
		
		$(this).parent().mouseleave(function()
		{
			$(panel).hide();
			$(this).unbind('mouseleave');
		});
   });
}

function controlFotos()
{
  	//orden de contenedores
  	
  	
  	var total = $('div.contR img').size();
  	var array = [];
  
  //evento de carga de imagen	
  $('img#bg').load(function ()
  {
		$('div#loadBG').remove();
  });
  	
  	
  	
  	//el primero se oculta
  	$('div.contR img:first').hide();
  	
  	//click en uno implica reordenar sus posiciones 
  	$('div.contR img').live('click',function()
  	{
  		var offset = $('div.contR img').index(this);
  		//ocultamos el contenedor
//  		$('div.contR').fadeOut();
  		
  		//restamos posiciones a todos y creamos el array del nuevo estado
  		$('div.contR img').each(function(pos)
  		{
  			var nPos = pos - offset;
  			
  			if(nPos < 0)
  			{
  				nPos = total + nPos;
  			}
  			
  			array[nPos] = $('div.contR img:eq('+pos+')');
  		})
  		//vaciamos contenedor
  		$('div.contR').empty();
  		
  		//adaptamos el array
  		//array[0].css('display', 'none');

  		//plasmamos el array
  		for(var i = 0; i < total;i++)
  		{
  			$('div.contR').append(array[i]);
  		}
  		
  		//escondemos la primera
  		$('div.contR img').show();
  		$('div.contR img:first').hide();
  		
  		//mostramos el contenedor
//  		$('div.contR').fadeIn();
  		
  		//ajustamos el fondo
  		var foto = $(this).attr('src');
	  	foto = foto.substring(foto.lastIndexOf('/')+1, foto.lastIndexOf('_'));
	
		if($('img#bg').attr('src') != 'img/fotos/'+foto+'.jpg')
		{
		  	$('body').append('<div id="loadBG"></div>');
		  		//$('div#loadBG').html('<img id="load" src="img/load.gif"></img>');
		  		
			  $('img#bg').attr('src','img/fotos/'+foto+'.jpg');		
		}	
  		
  	});
}

//handler para carga suave de imagenes
function loadImg()
	{
	   $(this).fadeIn(200);
	   $(this).removeClass('loading');
	   $(this).unbind('load',loadImg);
	}
	
	
function ajustarThumbs()
{
	$(window).load(function()
	{
		//ajuste de imagenes respecto al texto
		var desp = $('.contStandar').outerHeight()/2 - $('.pagImgs').outerHeight()/2;
		$('.pagImgs').css('bottom',desp+'px');
		$('.pagImgs').fadeIn();
	});	
}
	
	
	
function fadeImgLoad()
{


	$(document).find('img:visible').addClass('loading');
	$('img.loading').hide();
	
	//$('img.loading').load(loadImg);
	$('img.loading').one('load',loadImg).each(function()
	{
		if(this.complete) $(this).trigger("load");
	});
	
	
//	$('img.loading').attr('onload',"alert('jo')");$(this).fadeIn(200)
	
/*	
	
	$("img.loading").one("load",function(){
		$(this).fadeIn(200);
	})
	.each(function(){
	if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
	$(this).trigger("load");
	}); */
	
}



function centrarAlojamientos()
{
	//centrado vertical de contenedores de alojamiento, contacto y multimedia !!nombre mal
	if($('.alojCont').size() > 0)
	{
		var WindowH = $(window).height();
		var ContH = $('.alojCont').height();
		
		if(ContH < WindowH)
		{
			var margin = (WindowH - ContH)/2
			$('.alojCont').css('margin-top',margin+'px');
		}
		else
		{
			$('.alojCont').css('margin-top','1em');
			$('.alojCont').css('margin-bottom','1em');
		}
	}
	
	
	if($('.contactCont').size() > 0)
	{
		var WindowH = $(window).height();
		var ContH = $('.contactCont').height();
		
		if(ContH < WindowH)
		{
			var margin = (WindowH - ContH)/2
			$('.contactCont').css('margin-top',margin+'px');
		}
		else
		{
			$('.contactCont').css('margin-top','1em');
			$('.contactCont').css('margin-bottom','1em');
		}
	}
	
	
	if($('#multimediaCont').size() > 0)
	{
		var WindowH = $(window).height();
		var ContH = $('#multimediaCont').height();
		
		if(ContH < WindowH)
		{
			var margin = (WindowH - ContH)/2
			$('#multimediaCont').css('margin-top',margin+'px');
		}
		else
		{
			$('#multimediaCont').css('margin-top','1em');
			$('#multimediaCont').css('margin-bottom','1em');
		}
	}
}



function controlContacto()
{
	$('#gmaps').hide();
	
	$('.contactCont input').toggle(
		function()
		{
			//$('.contactCont p').hide();
			$('#mapa').hide();
			
			//calculo sus proporciones según el ancho de la ventana
			
			var width = $(window).width() * 0.3;
			var height = $(window).width() * 0.199;
			
			var html= '<iframe width="'+width+'" height="'+height+'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.es/maps?f=q&amp;source=s_q&amp;hl=es&amp;geocode=&amp;q=caleta+de+fuste,antigua&amp;aq=&amp;sll=40.396764,-3.713379&amp;sspn=8.948955,19.731445&amp;ie=UTF8&amp;hq=&amp;hnear=Caleta+de+Fuste,+35630+Antigua,+Las+Palmas,+Islas+Canarias&amp;t=h&amp;ll=28.395025,-13.856936&amp;spn=0.018876,0.025749&amp;z=14&amp;output=embed"></iframe><br /><small><a target="_blank" href="http://maps.google.es/maps?f=q&amp;source=embed&amp;hl=es&amp;geocode=&amp;q=caleta+de+fuste,antigua&amp;aq=&amp;sll=40.396764,-3.713379&amp;sspn=8.948955,19.731445&amp;ie=UTF8&amp;hq=&amp;hnear=Caleta+de+Fuste,+35630+Antigua,+Las+Palmas,+Islas+Canarias&amp;t=h&amp;ll=28.395025,-13.856936&amp;spn=0.018876,0.025749&amp;z=14" style="color:white;text-align:left;text-decoration:none;font-family: Georgia,Palatino,Times New Roman;font-style: italic;font-weight: normal;">Ver mapa más grande</a></small>'
			
			$('#gmaps').html(html);
			$('#gmaps').show();
		},
		function()
		{
			$('#mapa').show();
			//$('.contactCont p').show();
			$('#gmaps').hide();
		});
	
}

	
$(document).ready(function() 
{
  // Actualizamos el fondo al cargar la pagina
  
  checkBrowserWidth();
  updateBackground();
  updateContenedor();
  centrarAlojamientos()
//  Cufon.replace('h1');

	controlContacto();
  
  $(window).bind("resize", function() 
  {
    // Y tambien cada vez que se redimensione el navegador
    checkBrowserWidth();
   // adaptaCurva();
    updateBackground();
    updateContenedor();
    centrarAlojamientos()
    ajustarThumbs()
//    Cufon.replace('h1');
  });
  
  
  
  
  
  $('img#menuIn').hide();
  $('img#flechaIn').hide();
 
 controlOcultar();
  
  adaptaCurva();

  /* mov contenedor */
	$(".recogerBoton").hover(
	  function () 
	  {
	    if($('.recogerBoton').attr('src') =='img/recogerBotonOut')
	    {
	    	$('.recogerBoton').attr('src','img/recogerBotonOutHo');
	    }
	    else
	    	if($('.recogerBoton').attr('src') =='img/recogerBotonIn')
		    {
		    	$('.recogerBoton').attr('src','img/recogerBotonInHo');
		    }
	  }, 
	  function () 
	  {
	    if($('.recogerBoton').attr('src') =='img/recogerBotonOutHo')
	    {
	    	$('.recogerBoton').attr('src','img/recogerBotonOut');
	    }
	    else
	    	if($('.recogerBoton').attr('src') =='img/recogerBotonInHo')
		    {
		    	$('.recogerBoton').attr('src','img/recogerBotonIn');
		    }
	  }
	);


  $('.recogerBoton').toggle(
  		function()
  		{
  			$('.recogerBoton').attr('src','img/recogerBotonIn');
  		
		  	$('.ui').animate(
		  		{left: '+=57em'},
		  		 200,
		  		 function() {
			    // Animation complete.
			  		});
			  /*
			$('.contBG').animate(
		  		{left: '+=57em'},
				 200, function() {
			    // Animation complete.
			  });*/
	  },
	  function()
	  {
	  		$('.recogerBoton').attr('src','img/recogerBotonOut');
	  		
	  		$('.ui').animate(
		  		{left: '-=57em'},
			    200, function() {
			    // Animation complete.
			  });
			  /*
			$('.contBG').animate(
		  		{left: '-=57em'}, 
				200, function() {
			    // Animation complete.
			  });*/
	  }
  );
  
  
  /*   */
  controlFotos();
  controlMenu();
  
	//carga suave de imagenes la primera vez (no interfiere con las siguientes)
	fadeImgLoad();
	//if ($.browser.msie)
	
	
	

	//ajuste de imagenes respecto al texto
	$('.pagImgs').hide();
	ajustarThumbs();
	
	
	
	//window.location.href = dir[0] +'?lang='+this.value;
	
	
	
	
	/* control multimedia*/
	$('.multi2, .multi3').hide();
	
	
	$('.multiButton1').bind('click',function()
	{
		$('.multi2, .multi3').hide();
		$('.multi1').show();
	});
	
	$('.multiButton2').bind('click',function()
	{
		$('.multi1, .multi3').hide();
		//$('.multi2').show()
		//$('.multi2').slideDown();
		//$('.multi2').fadeIn(500);
		$('.multi2').show();
	});
	
	$('.multiButton3').bind('click',function()
	{
		$('.multi1, .multi2').hide();
		//$('.multi3').fadeIn(500);
		//$('.multi3').slideDown();
		$('.multi3').show();
	});
	
	
});




//google analitycs
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-21027284-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
  
