$(function(){

if(document.getElementById("menu-restaurant")) {
var queryString = document.location.href.replace(/^[^\?]+\??/,'');
var params = parseQuery(queryString);
openVAR = unescape(params['open']);
if(openVAR == "merkat") {
$("#menu-restaurant li:eq(0) .toggle").slideDown();
}
}

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}
	
	
	//acordeon de menu-restaurant
	$('#menu-restaurant h2 a,#carta-vins h2 a').click(function(){
		if ( $('.toggle',$(this).parent().parent()).is(':hidden') ){
			//antes recogemos la abierta
			$('#menu-restaurant li .toggle,#carta-vins li .toggle').slideUp();
			//ahora desplego
			$('.toggle',$(this).parent().parent()).slideDown();
		}
		else $('.toggle',$(this).parent().parent()).slideUp();
	})
	
		//acordeon de notes premsa
	$('#notas-prensa h3 a').click(function(){
		if ($(this).parent().next().next().is(':hidden')){
			//antes recogemos la abierta			
			$("#notas-prensa .toggle").slideUp();
			//ahora desplego
			$(this).parent().next().next().slideDown();
		}
		else $(this).parent().next().next().slideUp();
	})

	// width = getBrowserWidth()
	// 	widthNew = (width > 960) ? "960px": (width < 800? "765px": "auto");
	
	$(window).resize(function(){
		var alto = getBrowserHeight();
		//calculo el alto del pie
		footerHeight =  alto - $('#wrapper').height();
		(footerHeight < 300) ? $('#footer').height(300) : $('#footer').height(footerHeight);
		
	})
	
	if (navigator.userAgent.indexOf('MSIE 6.0')+1) {		
		width = getBrowserWidth()
		widthNew = (width > 960) ? "960px": (width < 800? "765px": "auto");
		
		$('#wrapper').width(widthNew)
		
		$(window).resize(function(){
			width = getBrowserWidth()
			widthNew = (width > 960) ? "960px": (width < 800? "765px": "auto");
			
			$('#wrapper').width(widthNew)
			//$('#footer').width(widthNew)
		})
	}
	
	//galeria de fotos
	if ($('body').attr('id') != undefined) galeria($('body').attr('id'));
	
	
	var alto = getBrowserHeight();
	//calculo el alto del pie
	footerHeight =  alto - $('#wrapper').height();
	//alert('wrapper='+$('#wrapper').height()+', footer='+footerHeight);
	(footerHeight < 300) ? $('#footer').height(300) : $('#footer').height(footerHeight);
		
})

function getBrowserHeight(){
	if (window.innerHeight) return window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight != 0) return document.documentElement.clientHeight;
	else if (document.body) return document.body.clientHeight;
	
	return 0;
};

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 galeria(carpeta){
	var prev = 0;
	var next = 2;
	var limit = fotos; //fotos es una variable global declarada en el html de la galeria
	var container = $('#galeria-fotos');
	var foto = $('#galeria-fotos > img');
	var path = 'img/' + carpeta;
	
	//por defecto oculto el enlace a la foto anterior
	$('#foto-anterior').css('visibility','hidden');
	
	$('#foto-anterior').click(function(){
		//alert('PREV // next='+next+',prev='+prev)
		if (prev > 0){
			foto.fadeOut(function(){
				var objImagePreloader = new Image();
				objImagePreloader.onload = function() {
					foto.removeAttr('src').attr('src',path + '/' + prev + '.jpg').fadeIn();
					prev--;
					next--;
					if (prev<1) $('#foto-anterior').css('visibility','hidden');
					if (next<=limit)$('#foto-siguiente').css('visibility','visible');
				}
				objImagePreloader.src = path + '/' + prev + '.jpg';
			})
		}
		return false;
	});
	
	$('#foto-siguiente').click(function(){
		//alert('NEXT // next='+next+',prev='+prev)
		if (next <= limit){
			foto.fadeOut(function(){
				var objImagePreloader = new Image();
				objImagePreloader.onload = function() {
					foto.removeAttr('src').attr('src',path + '/' + next + '.jpg').fadeIn();
					next++;
					prev++;
					if (next>limit) $('#foto-siguiente').css('visibility','hidden');					
					if (prev>0) $('#foto-anterior').css('visibility','visible');
				}
				objImagePreloader.src = path + '/' + next + '.jpg';
			})
		}
		return false;
	});
	
	
	
}



