// var IE = /*@cc_on!@*/false;

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 msieversion() {
	var ua = window.navigator.userAgent;
	var msie = ua.indexOf ( "MSIE " );
	return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )));
}

var open = false;

function closeBorder() {
	if(open) return false;
	open = true;
	$('#border').delay(200).animate({height: "0"},{duration: 500, easing:'easeOutQuart'});
	$('#opener').attr('class','off');
	slowFadeOut('#nastro',500);
	setTimeout('open = false;', 1000);
}

function openBorder() {
	if(open) return false;
	open = true;
	$('#border').delay(200).animate({height: "460"},{duration: 500, easing:'easeOutQuart'});
	$('#opener').attr('class','on');
	slowShow('#nastro',1000);
	setTimeout('open = false;', 1000);
}

function checkdate(x) {
	var year = x.slice(0,4);
	var month = x.slice(5,7);
	var day = x.slice(8,10);
	month = month - 1;
	var result = true;
	var date = new Date(year,month,day);
	if (year != date.getFullYear())
		result = false;
	if (month != date.getMonth())
		result = false;
	if (day != date.getDate())
		result = false;
	return result;
}

function c(elm){
if (!elm.base) elm.base = elm.value
if (elm.value == elm.base) elm.value = "";
else if (elm.value == "") elm.value = elm.base;
}

function blank(id) {
	var link = document.getElementById(id).getElementsByTagName("a");
	for (var i = 0; i < link.length; i++)
		link[i].setAttribute("target","_blank");
}

function send() {

	var data = document.getElementsByName('prenotazione_anno')[0].value + '-' + document.getElementsByName('prenotazione_mese')[0].value + '-' + document.getElementsByName('prenotazione_giorno')[0].value;
	var prenotazione_nome = document.getElementsByName('prenotazione_nome')[0];
	var prenotazione_email = document.getElementsByName('prenotazione_email')[0];
	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 prenotazione_telefono = document.getElementsByName('prenotazione_telefono')[0];
	var prenotazione_messaggio = document.getElementsByName('prenotazione_messaggio')[0];
	var checkbox = document.getElementsByName('prenotazione_checkbox')[0];	
	var result = true;

	if(
	!checkdate(data) ||
	prenotazione_nome.value == "" ||
	prenotazione_email.value == "" ||
	!prenotazione_email.value.match(emailEXP) ||
	prenotazione_telefono.value == "" ||
	prenotazione_messaggio.value == "Descrivi il tuo problema.." ||
	prenotazione_messaggio.value == "" ||
	checkbox.checked == false
	) {
	
		var body = document.getElementsByTagName("body")[0];
		var div3 = document.createElement("div");
		var div2 = document.createElement("div");
		var div1 = document.createElement("div");
		div2.setAttribute("id","pageBackground");
		div1.setAttribute("id","pageAlert");
		
		div3.innerHTML = "<b style='font-size: 18px'>" + document.getElementById("pageAlertText").getElementsByTagName("span")[0].firstChild.nodeValue + "</b><br/><br/>";
		if(!checkdate(data)) div3.innerHTML += document.getElementById("pageAlertText").getElementsByTagName("span")[1].firstChild.nodeValue + "<br/>";
		if(prenotazione_nome.value == "") div3.innerHTML += document.getElementById("pageAlertText").getElementsByTagName("span")[2].firstChild.nodeValue + "<br/>";
		if(prenotazione_telefono.value == "") div3.innerHTML += document.getElementById("pageAlertText").getElementsByTagName("span")[3].firstChild.nodeValue + "<br/>";
		if(prenotazione_email.value == "" || !prenotazione_email.value.match(emailEXP)) div3.innerHTML += document.getElementById("pageAlertText").getElementsByTagName("span")[4].firstChild.nodeValue + "<br/>";
		if(prenotazione_messaggio.value == "Descrivi il tuo problema.." || prenotazione_messaggio.value == "") div3.innerHTML += document.getElementById("pageAlertText").getElementsByTagName("span")[5].firstChild.nodeValue + "<br/>";
		if(checkbox.checked == false) div3.innerHTML += document.getElementById("pageAlertText").getElementsByTagName("span")[6].firstChild.nodeValue + "<br/>";	
		div3.innerHTML += "<br/><span></span>";
		
		div1.innerHTML = '<a href="#" id="close" onclick="return noPage()"></a>';
		div2.setAttribute("onclick","return noPage()");
		div1.appendChild(div3);		
		body.appendChild(div2);
		body.appendChild(div1);
		slowShow("#pageBackground",500);
		slowShow("#pageAlert",1000);
		return false;

	}

	return result;
}

function page(x) {

	var href = x;
	var url = href.split("/");
	var body = document.getElementsByTagName("body")[0];
	var div2 = document.createElement("div");
	var div1 = document.createElement("div");
	div2.setAttribute("id","pageBackground");
	div1.setAttribute("id","pageContent");
	//div1.innerHTML = '<a href="' + x +'" id="close" onclick="return noPage()"></a><iframe id="iframe" src="' + './_' + url[url.length - 2] +'/' + url[url.length - 1] +'" frameborder="0" style="overflow-x: hidden; overflow-y: auto; " width="800" height="440"></iframe>';
	div1.innerHTML = '<a href="' + x +'" id="close" onclick="return noPage()"></a><iframe id="iframe" src="' + './_' + url[url.length - 2] +'/' + url[url.length - 1] +'" frameborder="0" style="overflow-y: scroll; overflow-x: hidden;" width="800" height="440"></iframe>';
	div2.setAttribute("onclick","return noPage()");
	body.appendChild(div2);
	body.appendChild(div1);
	slowShow("#pageBackground",500);
	slowShow("#pageContent",1000);
	return false;
}

function noPage() {

	slowFadeOut("#pageBackground",250);
	setTimeout('document.getElementsByTagName("body").item(0).removeChild(document.getElementById("pageBackground"));', 250);
	
	if (document.getElementById("pageContent") != undefined) {
		slowFadeOut("#pageContent",500);
		setTimeout('document.getElementsByTagName("body").item(0).removeChild(document.getElementById("pageContent"));', 500);
	}
	if (document.getElementById("pageAlert") != undefined) {
		slowFadeOut("#pageAlert",500);
		setTimeout('document.getElementsByTagName("body").item(0).removeChild(document.getElementById("pageAlert"));', 500);
	}
	
	return false;
}

function award(x,y) {

	if (document.getElementById(x) != undefined) {
		blank(x);
		$('#' + x).css('display','block');
		$('#' + x).hover(function() {
			$(this).animate({
				right: '0' 
			}, 200);
		}, function() {
			$(this).animate({
				right: y
			}, 400);
		});
	};

}

$(document).ready(function() {

	award('cssdesignawards','-45px');

	if (document.getElementById('opener') != undefined) {		
		if ( msieversion() <= 7 ) {
			$('#border, #opener').hide();
		} else {			
			$('#opener').toggle(openBorder,closeBorder);
			$('#border').css('height','0');
			$('#opener').attr('class','off');
			$('#nastro').hide();
			$('#opener').show();
		}
	}

	blank('sitemap_right');

	$(".blank").attr('target','_blank');	
	$("textarea").css('resize','none');

	$('.null').click(new Function('return false;'));
	$('.null').attr('style','cursor: default');
	
	/* pageContent */
	$('.list a').click(new Function('return page(this.href);'));
	$('.list h3').each( function(index) {
		var ul = $(this).siblings('ul')[index];
		$(ul).hide();
		$(this).click(function(){$(ul).toggle(400); return false;});
	});
	$('.list h3').attr('style','margin: 0 0 20px');

	$('#photo').cycle({
		fx: 'fade',
		timeout: 5000,
		next: '#photo'
	});
	
	$(".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 (document.getElementById("map") != undefined) {
		$(function()
		{
			$("#map").gMap({ markers: [{
			latitude: 43.887643,
			longitude: 10.764761
			}],
			zoom: 8 });
		});
	}
	
	/* alert */
	if (document.getElementsByName('prenotazione_messaggio')[0] != undefined) {
		document.getElementsByName('prenotazione_messaggio')[0].onblur = function () { c(this) };
		document.getElementsByName('prenotazione_messaggio')[0].onfocus = function () { c(this) };
	}
	if (document.getElementsByName('send')[0] != undefined)
		document.getElementsByName('send')[0].onclick = send;

	$("a[rel=p]").fancybox({
	'opacity'			: true,
	'overlayShow'		: true,
	'transitionIn'		: 'elastic',
	'transitionOut'		: 'elastic',
	'titlePosition'		: 'outside',
	'overlayColor'		: '#000',
	'overlayOpacity'	: 0.75,
	'titleFormat'		: function() { return ''; }
	});	
	
});

Cufon.replace('#navigation a.font,h1,h2,h3,h4,h5,h6, .ico_studio-privato, .ico_intramoenia, #photo p');

//if(!IE){
var head  = document.getElementsByTagName("head").item(0);
var style = document.createElement("style");
style.setAttribute('type','text/css');
style.setAttribute('media','all');
style.appendChild(document.createTextNode("#pageContent, #pageAlert { -moz-box-shadow: 2px 2px 20px rgba(0, 0, 0, .5); -webkit-box-shadow: 2px 2px 20px rgba(0, 0, 0, .5); -goog-ms-box-shadow: 2px 2px 20px rgba(0, 0, 0, .5); box-shadow: 2px 2px 20px rgba(0, 0, 0, .5); } .form input, .form textarea, .gallery, #content input[type='text'], #content input[type='password'] { -moz-border-radius: 3px; -webkit-border-radius: 3px; } .border, .gallery, .form input[type='submit'] { -moz-box-shadow: 2px 2px 10px rgba(0, 0, 0, .3); -webkit-box-shadow: 2px 2px 10px rgba(0, 0, 0, .3); -goog-ms-box-shadow: 2px 2px 10px rgba(0, 0, 0, .3); box-shadow: 2px 2px 10px rgba(0, 0, 0, .3); } .form input[type='submit']:hover { -moz-box-shadow: none; -webkit-box-shadow: none; -goog-ms-box-shadow: none; box-shadow: none; } "));
head.appendChild(style);
//}
