$(document).ready(function() {
						   
	Recaptcha.create("6Lfp5cESAAAAAHSGrS0iOt49AEWPZF6TS66-aFFN",
		"reCaptchaDiv",
		{
		  theme: "red",
		  callback: Recaptcha.focus_response_field
		}
	);
							   
						   
	// ===================
    //        jMenu
	// ===================
	$("#jMenu").jMenu({
      ulWidth : '200',
      effects : {
        effectSpeedOpen : 500,
        effectSpeedClose : 200,
        effectTypeOpen : 'slide',
        effectTypeClose : 'slide',
        effectOpen : 'linear',
        effectClose : 'linear'
      }
    });
	/*$('#jmenu').jmenu({animation:'slide',duration:1000});*/
	
	
	
	// ==========================
    //  jMenu identify base link
	// ==========================	
	$('.fNiv').removeClass('selected');
	var base = $('div.rootBaseHolder');
	var num = base.attr("id");
	$('#menu'+num).addClass('selected').delay(1000);
	
	
    
    // ===================
    //        LANGS
	// ===================
	$("div.lang a.selected").click(function(){
		$("ul.otherLangs").slideDown("slow").delay(3000).slideUp("slow");
	});

	
	
	
	// ===================
	//       SLIDER
	// ===================
	$("div.sliderUnit").hide();
	$("div.sliderUnit:first").show();
	$("div.selectorSlider a.selector:first").addClass("selector on");
	
	var currentSlide = 1,
		sliderTimer,
		sliderInterval = 6000,
		sliderButtons = $("div.selectorSlider a");
	var newsShow = function (aitem) {
		if (!aitem) return;
		ref = $(aitem);
		var num = ref.attr("id").replace("sel","");
		$('.sliderUnit:visible').hide();
		$('#slider' +num).fadeIn("1000");
		$('.selector').removeClass("on");
		$('#sel' +num).addClass("selector on png-fix");
	}
	
	
	//If has more than one slide
	if (sliderButtons.length > 1) {
		newsShow(sliderButtons);
		sliderButtons.click(function(){
			clearInterval(sliderTimer);
			newsShow(this);	
			return false;			
		});
		
		sliderTimer = setInterval(function() {
			var nrs = sliderButtons.length;
			newsShow( sliderButtons[currentSlide] );
			currentSlide++;
			if (currentSlide >= sliderButtons.length) currentSlide = 0;
		}, sliderInterval);
	//With just one slide no buttons and no animation
	} else {
		sliderButtons.hide();
	}
	
	
	
	// ===================
	// IMAGES OVERLAY
	// ===================
	$("div.imgbox a").fancybox({
		'hideOnContentClick': true,
		'overlayShow': true,
		'overlayOpacity': 0.8
	});
	
	// SubFolders MouseOver
	$("div#subfolders div").hover(function(){
		$(this).css('background-color','#B9D473');	
	},function(){
		$(this).css('background-color','#A7C950');			
	});
	
	
	
	
	
	// =====================
	// SPEAKERS LIST EFFECTS
	// =====================
	$("div.speakerWrapper").hover(function(){
		$(this).addClass('wrapperHover');	
	},function(){
		$(this).removeClass('wrapperHover');			
	});
	
	
	
	// ======================
	// PROGRAMME LIST EFFECTS
	// ======================
	$("li.sessionLine").hover(function(){
		$(this).addClass('sessionLineHover');	
	},function(){
		$(this).removeClass('sessionLineHover');			
	});
	
	
	
	// ===========================
	// SPEAKERS TABLE MANIPULATION
	// ===========================
	$('.speakerWrapper table').each(function(){
		for (var i = 0; i < 5; i++) {
			var cols = $(this).find('.c'+i);
				count = 0;
			cols.each(function() {
				count += $(this).text().replace(/\s/g, '') ? 1 : 0;
			});
			if (count < 2) {
				cols.remove();
			}
		}
	});
	
	
	
	// ===================
	// HOTELS LIST EFFECTS
	// ===================
	$("div.hotelWrapper").hover(function(){
		$(this).addClass('wrapperHover');	
	},function(){
		$(this).removeClass('wrapperHover');			
	});
	
	
	
	// =====================
	// SPONSORS LIST EFFECTS
	// =====================
	$("div.sponsorWrapper").hover(function(){
		$(this).addClass('wrapperHover');	
	},function(){
		$(this).removeClass('wrapperHover');			
	});
		
	
	
	
	// ===================
	// = Form validation =
	// ===================
	function formValidation(f) {
		$.validator.messages.required = "Obrigatório";
		$.validator.messages.email = "E-mail inválido.";
		$.validator.messages.digits = "Apenas digitos.";

		var rules = {
			'informacoes' : {
				required	: true,
				minlength	: 1
			}
		};
	
		var messages = {
			'informacoes'	: 'Seleccione pelo menos uma opção'
		};
	
		var errorPlacement =  function(error, element) {
			var count = 0;
		    var place = element.parent().prev().find('label');
			/*while (place.length == 0) {
							element = element.parent();
							place = element.prevAll('label');
							count++;
							if (count > 2) return;
						}*/
			//place.append(error);
		};
	
		f.validate({
			rules			: rules,
			messages		: messages,
			errorPlacement 	: errorPlacement
		});
	}
	
	function formSetup(f) {
		f.find('input[class*=required]').each(function() {
			var element = $(this);
			var label = $(this).prevAll('label');
			var count = 0;
			while (label.length == 0) {
				element = element.parent();
				place = element.prevAll('label');
				count++;
				if (count > 2) return;
			}
			label.append("*");
		});
	}
	
	function checkFormSent() {
	    var hash;
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		var count = 0;
	    for(var i = 0; i < hashes.length; i++)
	    {
			
	        hash = hashes[i].split('=');
	        if (hash[0] == "esnt" &&  hash[1] == 1) {
				count++;
			}
			
			if (hash[0] == "nl" &&  (hash[1] == 1 || hash[1] == 2)) {
				count += parseInt(hash[1]);
			}
			
	    }
	    return count;
	}
	
	function formSent(f, nl) {
		var title = "Formulário Enviado";
		var msg = '<span style="font-size:13px;color:#55555">O seu formulário foi submetido com sucesso. Obrigado pelo seu contacto.</span>';
		var msgnlOk = '<span style="font-size:13px;color:#55555">O seu contacto foi adicionado. Obrigado pela sua atenção.</span>';
		var msgnlError = '<span style="font-size:13px;color:#55555">O seu e-mail é inválido por favor tente novamente com um endereço correcto.</span>';
		if (nl == 2) {
			msg = msgnlOk;
		} else  if (nl== 3) {
			msg = msgnlError;
		}
		$('div.leftcolumn > h2 > em').empty()
									 .text(title);
		f.empty()
		 .append(msg);
	}
	
	var f = $("#formulario");
	if (f.length) {
		var v = checkFormSent()
		if (v) {
			formSent(f, v);
			return;
		}
		formValidation(f);
		formSetup(f);
	}
	
	
	
	
});



