function isEmail(email) {
	return email.match(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
}

$(document).ready(function() {

	$('#dialog').jqm();

	$('#toolbar li span').click(function(){
		location.href = $(this).parent().find('a').attr('href');
	});

	$('#toolbar li a').hover(function() {
		$(this).stop().animate({ backgroundPosition:[$(this).css('backgroundPosition').split(' ')[0],'0px'].join(' ')}, 550);
	}, function() {
		$(this).stop().animate({ backgroundPosition:[$(this).css('backgroundPosition').split(' ')[0],'-25px'].join(' ')}, 550);
	});	

	$('#toolbar li ul').hide();

	$('#toolbar li').hover(function(){
		$(this).find('ul').fadeIn('slow');
	}, function () {
		$(this).find('ul').fadeOut('slow');
	});

/* 	$('#toolbar li a').hover(function() { */
/* 		$(this).stop().animate({ backgroundPosition:[$(this).css('backgroundPosition').split(' ')[0],'0px'].join(' ')}, 550); */
/* 		$(this).parent().find('span').fadeIn('slow'); */
/* 	}, function() { */
/* 		$(this).stop().animate({ backgroundPosition:[$(this).css('backgroundPosition').split(' ')[0],'-25px'].join(' ')}, 550); */
/* 		$(this).parent().find('span').fadeOut('fast'); */
/* 	});	 */

	$('#logo').hover(function() {
		$(this).stop().animate({ top: '0px' }, 150);
	}, function() {
		$(this).stop().animate({ top: '-23px' }, 150);
	});	

	$('#pecettapreventivo').hover(function() {
		$(this).stop().animate({ bottom: '-=5' }, 150);
	}, function() {
		$(this).stop().animate({ bottom: '+=5' }, 150);
	});	

	$('#richiedipreventivo').hover(function() {
		$(this).stop().animate({ top: '-=5' }, 150);
	}, function() {
		$(this).stop().animate({ top: '+=5' }, 150);
	});	

	if($("#id_newsletter_email") ){
		$("#id_newsletter_email").focus(function(){this.value = ''});
		$("#id_newsletter_email").blur(function(){
			if(this.value == '') this.value = 'La tua email';
		});
	}

	$('#iscriviml').click(function(){
		var email = $('#id_newsletter_email').val();

		if (isEmail(email)) {
			$.post("/newsletter/api/iscrivi/", { 'email':email  }, function(ret){
				if (!ret.error) { 
					$('#dialog .text').html('<img src="/static/images/ok.png" width="400" height="200" alt="Grazie di esserti iscritto!"/>');
				}
				else {
					$('#dialog .text').html('<img src="/static/images/ops.png" width="400" height="200" alt="C\'e\' stato un errore nell\'iscrizione. Riprova piu\' tardi." />');
				}
			});
		} else {
			$('#dialog .text').html('<img src="/static/images/ops.png" width="400" height="200" alt="C\'e\' stato un errore nell\'iscrizione. Riprova piu\' tardi." />');
		}

		$('#dialog').jqmShow();
	});

	if ($('.chi').length) {
		$('.chi li').hover(function() {
			$(this).find('img').fadeIn();
		},function() {
			$(this).find('img').fadeOut();
		});
	}

	if ($('.service').length) {
		$('.service').hover(function() {
			$(this).css('backgroundColor','#940c0c');
			$(this).find('a').css('color','#fff');
			$(this).find('p').css('color','#fff');
		}, function() {
			$(this).css('backgroundColor','transparent');
			$(this).find('a').css('color','#cb3737');
			$(this).find('p').css('color','#646464');
		});	

		$('.nolink').hover(function() {
			$(this).css('backgroundColor','#d8d8d8');
			$(this).find('a').css('color','#fff');
			$(this).find('p').css('color','#646464');
		}, function() {
			$(this).css('backgroundColor','transparent');
			$(this).find('a').css('color','#cb3737');
			$(this).find('p').css('color','#646464');
		});	

		$('.service').click(function(){
			if ($(this).find('a').length) {
				location.href = $(this).find('a')[0].href;
			}
		});
	}

	if ($('#slider').length) {
		$('#slider').nivoSlider({
			effect: 'fade',
			directionNavHide: false,
			pauseTime: 3000
		});
	}

	if ($('.portfolio').length) {
		$('.portfolio .item').hover(function() {
			$(this).css('borderColor','#afafaf');
			$(this).find('a').fadeIn(150);
		}, function() {
			$(this).css('borderColor','#f1f1f1');
			$(this).find('a').fadeOut(150);
		});	
	}

	if ($('.works').length) {
		$('.works .work').hover(function() {
			$(this).css('borderColor','#afafaf');
			$(this).find('a').fadeIn(150);
		}, function() {
			$(this).css('borderColor','#f1f1f1');
			$(this).find('a').fadeOut(150);
		});	
	}

	if ($('#map').length) {
		$('#map').gmap3({ 
			action:'init',
			options:{center: [ 45.475, 9.22 ],zoom:14}
		},
		{ 
			action: 'addMarkers',
			markers:[
		{lat:45.476015, lng:9.223255, data:'Sede Operativa'},
			{lat:45.47083807122496, lng:9.20952558517456, data:'Sede Legale'}
		]
		});
	}
});

