var IE = /*@cc_on!@*/false;

function links(id) {
	var link = document.getElementById(id).getElementsByTagName("a");
	for (var i = 0; i < link.length; i++)
		link[i].setAttribute("target","_blank");
}

function close() {
	slowFadeOut("#alert",500);
	slowFadeOut("#alpha",500);
	setTimeout('document.getElementsByTagName("body").item(0).removeChild(document.getElementById("alert"));', 500);
	setTimeout('document.getElementsByTagName("body").item(0).removeChild(document.getElementById("alpha"));', 500);
}

function send() {

	if (document.getElementById("alert") != undefined) {
		document.getElementsByTagName("body").item(0).removeChild(document.getElementById("alert"));
		document.getElementsByTagName("body").item(0).removeChild(document.getElementById("alpha"));
	}

	var contact_nome = document.getElementById("contact_nome");
	var contact_email = document.getElementById("contact_email");
	var emailEXP = new RegExp("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$");
	var contact_telefono = document.getElementById("contact_telefono");
	var contact_msg = document.getElementById("contact_msg");
	var result = true;
	
	if(
	contact_nome.value == "" ||
	contact_email.value == "" ||
	!contact_email.value.match(emailEXP) ||
	contact_telefono.value == "" ||
	contact_msg.value == ""
	) {
	
		var body  = document.getElementsByTagName("body").item(0);
		var alpha = document.createElement("div");
		var alert = document.createElement("div");
		alpha.setAttribute('id','alpha');
		alert.setAttribute('id','alert');

		body.appendChild(alpha);
		body.appendChild(alert);
		document.getElementById("alert").onclick = close;
		document.getElementById("alpha").onclick = close;

		alert.innerHTML = "<b style='font-size: 18px'>Messaggio non inviato</b><br/><br/>";
		
		if(contact_nome.value == "")
			alert.innerHTML += "Inserire il nome<br/>";
			
		if(contact_email.value == "" || !contact_email.value.match(emailEXP))
			alert.innerHTML += "Inserire una email valida<br/>";
			
		if(contact_telefono.value == "")
			alert.innerHTML += "Inserire un numero di telefono<br/>";
			
		if(contact_msg.value == "")
			alert.innerHTML += "Inserire il testo del messaggio<br/>";
			
		alert.innerHTML += "<br/><span></span>";

		slowShow("#alert",1000);
		slowShow("#alpha",1000);
		result = false;
		
	}

	return result;

}

Cufon.replace('h1,h2,h3,h4,h5,h6');

function slowShow(x,y) { $(x).hide(); $(x).hide(function(){$(x).fadeIn(y); return false;}); }
function slowHide(x,y) { $(x).hide(y); return false; }
function slowFadeOut(x,y) { $(x).fadeOut(y); return false; }

$(function(){
    $('a[href*=#]').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
        && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);
                return false;
            }
        }
    });
});

$(document).ready(function() {

	if (document.getElementById("map") != undefined) {
		$(function()
		{
			$("#map").gMap({ markers: [{
			latitude: 43.878268,
			longitude: 10.779574
			}],
			zoom: 13 });
		});
	}

	if (document.getElementById("footer") != undefined)
		links("footer");

	if (document.getElementById("content-contact") != undefined)
		links("content-contact");

	if (document.getElementById("slide") != undefined)
		document.getElementById("slide").setAttribute('style','cursor: pointer');

	$('div#photos div#slide').cycle({
		fx:     'scrollLeft',
		timeout: 7000,
		next: 'div#photos div#slide',
		pager:  'div#photos div.controls div.inner',
		pagerAnchorBuilder: function(idx, slide) { 
			myClass = (idx==0)?' class="activeSlide"':'';
			return '<a href="#" '+myClass+'>&nbsp;</a>'; 
		}
	});
	
	$('#screen').cycle({
		fx: 'fade',
		timeout: 5000,
		next: '#screen'
	});
	
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	if (document.getElementById("send") != undefined)
		document.getElementById("send").onclick = send;
		
	if (document.getElementById("alert") != undefined) {
		slowShow("#alert",1000);
		slowShow("#alpha",1000);
		document.getElementById("alert").onclick = close;
		document.getElementById("alpha").onclick = close;
	}
	
	$("#gallery a").stop().animate({"opacity": "1"}, "slow");
	$("#gallery a").hover(
		function() {
			$(this).stop().animate({"opacity": "0.4"}, "slow");
		},
		function() {
			$(this).stop().animate({"opacity": "1"}, "slow");
		}
	);
	
	if(!IE){
		$('head').append('<link rel="stylesheet" type="text/css" href="' + base + 'css/css3.css" />');
	}

});
