function getURLParameter(name) {
	return unescape(
		(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
	);
}

//loads calendar
function loadCalendar(month,year) {
	
	$(".calendar").load('calendar.php?m='+month+'&y='+year, function(response, status, xhr) {
																	 
	$('a.event_day').parent('td').addClass('event_day');
												 
	  if (status == "error") {
		var msg = "Sorry but there was an error: ";
		//alert(msg + xhr.status + " " + xhr.statusText);
	  }
	  
	});
	
}

function qtip_error(e,context,custom_error) {
		
		if (custom_error) {
			var error = custom_error;
		} else {
			var error = $(e).next('a').attr('title');
		}
		
		//espera o elemento e o contexto deste mesmo
	    $(e,context).qtip({
				content: {
					text: custom_error
				},
				position: {
					my: "top right",
					at: "bottom right",
					viewport: e
				},
				style: {
					classes: "ui-tooltip-dark"
				},
				show: {
				 ready: true
				}
			})

}

$(function(){
		   
	//menu accordion
	$('.nav').accordion({ collapsible: true, autoHeight: false, alwaysOpen:true, active: 0, navigation:true, header:'h3' });
		   
	//backgrounds
	var path = 'images/bg/';
	
	if ( getURLParameter('p') ) {
	 
		var param = getURLParameter('p');
		
		switch(param) {
			case "inicio":
			var bg = 'home';
			break;
			case "cervejaria":
			var bg = 'cervejaria.jpg'
			break;
			case "restaurante":
			var bg = 'restaurantevip.jpg';
			break;
			case "eventos":
			var bg = 'eventos.jpg';
			break;
			case "emfoco":
			var bg = 'emfoco.jpg';
			break;
			case "catering":
			var bg = 'catering.jpg';
			break;
			case "contactos":
			var bg = 'contactos.jpg';
			break;
			case "noticias":
			var bg = 'noticias.jpg';
			break;
			case "fotos":
			var bg = 'fotos.jpg';
			
			break;
			case "videos":
			var bg = 'videos.jpg';
			
			break;
			default:
			var bg = 'home.jpg';
		}
		
	} else {
		var bg = 'home.jpg';
	}
	
	//bg images
	$.backstretch(path+bg, {speed: 'normal'});   
		
	//loads calendar
	loadCalendar();
	
	//facybox
	$('ul.photos li a, .news-thumb a').fancybox();
	
	
//------------------------------------newsletter---------------------------------------------------------------------------------------

	function checkEmail (strng) {
	
		var error="";
		
		if (strng) {
	
		var emailFilter = /^.+@.+\..{2,3}$/;
		
			if (!(emailFilter.test(strng))) { 
			   error = "invalid";
			}
			
			else {
		//test email for illegal characters
			   var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
				 if (strng.match(illegalChars)) {
				  error = "illegal";
			   }
			}
	
		} else {
		
			error = "empty";
		}
			
		return error;
	} 

	$("form[name=newsletter]").submit(function() {
	
		var form = this;
		var url= 'ajax_newsletter.php';
		var input = $('input[name=email]', this);
		var output = $('.output', this);
			
		switch (checkEmail(input.val())) {
		
			case 'empty':	
				qtip_error(input, form, 'Introduza um endereço de email');
			break;
			case 'invalid':
				qtip_error(input, form, 'Introduza um email válido');
			break;
			case 'illegal':
				qtip_error(input, form, 'O email que introduziu tem caracteres não permitidos');
			break;
			
			default:
	   
			$.ajax({
			  url: url,
			  type: 'GET',
			  data: $(form).serialize(),
			  cache:  false,
			  dataType: 'json',
			  beforeSend: function() {
				  $('.spinner', form).css('display','inline-block');
			  },
			  complete: function(data) {		  
				  $('.spinner', form).hide();
			  },
			  success: function(data){
				
					//se houver erros retornados
					if ( data.status == 'error') {
					
							switch (data.error) {
								case 'inexistant':
									qtip_error(input, form, 'O e-mail indicado não está subscrito à newsletter');
								break;
								case 'already_subscribed':
									qtip_error(input, form, 'O e-mail indicado já está subscrito à newsletter');
								break;
							}
					} else if ( data.status == 'ok') {
					
						qtip_error(input, form, 'Foi-lhe enviado um e-mail contendo indicações e uma ligação onde deverá clicar para confirmar a sua subscrição. Obrigado!');
					
					};
			  }
	
	
			});  
	
			break;
	   }
	
	return false;
	
	});
	
});
