$(document).ready(function(){
	
	var captcha = "";
		
	function controllo(){
 		var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
			if (!espressione.test(document.getElementById('news_email').value)){
     			alert("La mail inserita non e' valida!\n Correggi e riprova");
				 }
			//else if (!(document.getElementById('news_security').value == capt)) {alert ("I codici non corrispondono!");}
			else if (!captCheck('news_security',capt)) {alert ("I codici non corrispondono!");}
			else if (!(document.getElementById('news_privacy').checked == true)) {alert ("Acconsenti!"); }
			else document.forms[0].submit();
		}

			
	function CAPTCHA()
	{
		var car, min, max, dif, lun, inc;
		car  = "abcdefghijklmnopqrstuvwxyz";
		car += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		car += "1234567890";
		min  = 5;
		max  = 5;
		dif  = max - min;
		lun  = Math.round((Math.random() * dif) + min);
		inc  = 0;
		cod  = "";
		while (inc < lun)
		{
			cod += car.charAt(Math.round(Math.random() * car.length));
			inc++;
		}
	return cod;
	}
	
	function captCheck(el,val){
		if ((document.getElementById(el).value == val)) return true;
		else return false;
		}

	$('#news_form').hide();
	//$('#news_form #captcha').hide();
	$('#nl').click(function(){
		capt = CAPTCHA();
		$('#news_form #captcha').text(capt).toggle();
		$('#news_form').slideToggle(500);
		
	});	
	$('#news_b').click(function(){
		controllo();
	});
	
	
	//JQuery fancyBox
	$('.flickr_gallery a').fancybox();
	
	$('.entry a:has(img)').fancybox();
	
	
	$('.evidenza').hover(
		function () {
		$('.boxinfo').animate({top:'0px'},{queue:false,duration:300});
		},
		function () {
		$('.boxinfo').animate({top:'101px'},{queue:false,duration:300});
		}
);
	
		/*$('div.gallery-page .flickr_gallery ul').addClass('gallery_demo'); // adds new class name to maintain degradability
		$('.nav').css('display','none'); // hides the nav initially
		
		$('div.gallery-page .flickr_gallery ul').galleria({
		history : false, // deactivates the history object for bookmarking, back-button etc.
		clickNext : true, // helper for making the image clickable. Let's not have that in this example.
		insert : undefined, // the containing selector for our main image.
							// If not found or undefined (like here), galleria will create a container
							// before the ul with the class .galleria_container (see CSS)
		onImage : function() { $('.nav').css('display','block'); 
								image.css('display','none').fadeIn(); } // shows the nav when the image is showing
		});*/
		
		
		
		$('div.gallery-page .flickr_gallery ul').addClass('gallery_demo'); // adds new class name to maintain degradability
		$('.nav').css('display','none'); // hides the nav initially
		//$('div.galleria_container').next().wrap('<div id="panel-gallery"></div>'); 
	
		$('ul.gallery_demo').galleria({
			history   : false, // deactivates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable. Let's not have that in this example.
			insert    : undefined, // the containing selector for our main image. 
								   // If not found or undefined (like here), galleria will create a container 
								   // before the ul with the class .galleria_container (see CSS)
			onImage   : function() { $('.nav').css('display','block');
			$('.galleria_wrapper img').css('display','none').fadeIn(800);} // shows the nav when the image is showing
		});
		 $.galleria.activate($('.gallery_demo li:first').children(':first').attr('href'));
		 $('.galleria_wrapper').siblings('.caption').text($('.gallery_demo li:first').children(':first').attr('title'));
		
		
		$('ul.gallery_demo').wrap('<div id="panel_gallery"></div>');
		
		/*$('ul.gallery_demo li').click(function(){
			
			var $position_active = $('ul.gallery_demo li.active').position();  //posizione dell'il attivo
			var $h_li = $('ul.gallery_demo li').height(); //altezza di ogni li
			var $h_panel = $('#panel_gallery').height(); //altezza del pannello gallery
			//var $height_gallery = $('ul.gallery_demo').height();
			var $pos_last_li = $('ul.gallery_demo li:last').position().top;  //posizione ultimo elemento li
			var $h_ul = $pos_last_li + $h_li;  //altezza del contenitore ul.gallery
			//alert('altezza UL:' + $height_ul);
			//alert('altezza ul.gallery:' + $height_gallery);
			alert('posizione ultimo li:' + $pos_last_li);
			
			$valore = $position_active - $h_panel;
			$diff = $valore + $h_ul;
			
			if ( $diff > 0 ) $('ul.gallery_demo').animate({top: $valore}, 1000);		
			
		});*/
	
	
});