$(function() {
	//Va directamente a la sección que se le indique en la url
	var myFile = document.location.toString();
	if (myFile.match('#')) { // the URL contains an anchor
	  // click the navigation item corresponding to the anchor
	  //var myAnchor = '#' + myFile.split('#')[1];
	  var myAnchor = myFile.split('#')[1];
	  var myBtn = $('#menu div.'+ myAnchor).children('div.holder').children('div.btn');
	  clickBtn(myBtn);
	}

	// No-Scroll		
	$('html, body').css('overflow','hidden');
	// Scroll vertical solo para pantallas pequeñas.
	if ($(window).height()<700) {
		$('html, body').css('overflow-y','auto');	
		$('.stitulo').css('padding-right','30px');
	}
	

	// Hack : en IE los nav a 100% a veces se pasan del máximo
	// ++ Evita que se pasen los stitulo y los menus
	var bodyWidth=$('body').width();
	$('#menu div.nav').each(function (index, domEle) {
		var prevWidth=$(domEle).prev('div.btn').width();
		$(this).width(bodyWidth-prevWidth-20+'px');
	});

	// resize de la ventana
	$(window).bind('resize', function() {
		$('html, body').animate({scrollTop:0}, 'slow').css('overflow','hidden');
		if ($(window).height()<630) {
			$('html, body').css('overflow-y','auto');
		}
		var bodyWidth=$('body').width();
		$('#menu div.nav').each(function (index, domEle) {
			var prevWidth=$(domEle).prev('div.btn').width();
			$(this).width(bodyWidth-prevWidth-20+'px');
		});
		$('div.holder div.nav span span.nav_move:visible').hide();
	})
	
	// acordeon del menu
	$('div.btn').css('cursor','pointer').mouseenter(function(){
		// Si cerrado
		if (!$(this).hasClass('btn-isOpen')) {
			var thisNav=$(this).next('div.nav').children('span');
				thisNav.hide();
				thisNav.children('div.stitulo').show();
				thisNav.children('ul').hide();
				thisNav.show();
			$(this).next('#menu div.nav:not(:animated)').animate({width:'show'},{duration:500, complete : function() {
				if ($(this).width()<900) {
					var bodyWidth=$('body').width();
					var prevWidth=$(this).prev('div.btn').width();
					$(this).width(bodyWidth-prevWidth-20+'px');
				}
			}});
		}
	}).mouseleave(function(){
		$('#menu div.nav:visible').each(function (index, domEle) {
			if (!$(domEle).hasClass('nav-isOpen')) {
				$(this).animate({width:'hide'},500);
			}
		});
	});	
	
	//entradas en el menú
	$(".btn").click(function(){ clickBtn($(this)); });
	
	//enlaces a secciones desde el contenido de otras secciones
	$(".btna").click(function(){ 
		var myAnchor = $(this).attr("href").split('#')[1];
		var myBtn = $('#menu div.'+ myAnchor).children('div.holder').children('div.btn');
		clickBtn(myBtn);
	});
	
	//funcion que abre las secciones cuando se clicka en el menu indicado
	function clickBtn(boton)
	{
		//Si se le da a una seccion abierta la cierro
		if(boton.next('div.nav').hasClass("nav-isOpen"))
		{
			boton.next('div.nav').toggleClass("nav-isOpen");					
			boton.parent().next('div.contenido').animate({height:'hide'},{duration:500, complete : function() {
				$('html, body').animate({scrollTop:0}, 500);		
			}});			
			return;
		}
		
		var abiertos = new Array();
		$('#menu div.nav:visible').each(function (index, domEle) {
			if ($(domEle).hasClass('nav-isOpen')) 
			{
				abiertos[abiertos.length] = $(domEle);
			}
		});				

		boton.next('div.nav').toggleClass("nav-isOpen");		
		boton.next('div.nav').show();
		boton.parent().next('div.contenido').animate({height:'show'},{duration:500, complete : function() {
			$.each(abiertos, function(key, value) {
				var abierto = value;
				abierto.toggleClass("nav-isOpen");
				abierto.parent().next('div.contenido').animate({height:'hide'},{duration:500, complete : function() {
					abierto.animate({width:'hide'},{duration:500, complete : function() {
						$('html, body').animate({scrollTop:0}, 500);
						boton.next('div.nav').show();
					}});												  
				}});
			});			
		}});		
	}	
	
	//Historia	
	$('#contenido-historia').serialScroll({
		target:'#leyenda',
		items:'li', 
		prev:'a.prev',
		next:'a.next',
		axis:'xy',
		duration:700,
		force:true, 
		
		onBefore:function( e, elem, $pane, $items, pos ){
			e.preventDefault();
			if( this.blur )
				this.blur();
		},
		onAfter:function( elem ){
			
		}
	});
	
	//Galeria
	$("a[rel=galeria-entrada]").fancybox({
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Imagen ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});	
	$("a[rel=galeria-comedores]").fancybox({
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Imagen ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});	
	$("a[rel=galeria-mesas]").fancybox({
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Imagen ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});	
	$("a[rel=galeria-hogar]").fancybox({
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Imagen ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});	
	
	//Contacto
	$("a[rel=accesos]").fancybox({
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Imagen ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});	
	
	//Carta
	$('.precio-menu').hover(function(){
		$(this).animate({marginRight:'+=10px'});
	},function(){
		$(this).animate({marginRight:'-=10px'});
	});
	
	$('#lacarta').serialScroll({
		target:'#secciones-carta',
		items:'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		prev:'img.ant',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
		next:'img.post',// Selector to the 'next' button (absolute too)		
		axis:'xy',// The default is 'y' scroll on both ways
		navigation:'#navegacion-carta li a',
		duration:400,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)		
		
		onBefore:function( e, elem, $pane, $items, pos ){
			e.preventDefault();
			if( this.blur )
				this.blur();
		},
		onAfter:function( elem ){
		}
	});
});
