$(document).ready(function(){ 

  // INIT LIGHTBOX
	$('#wrapper_allegati_correlati .lightbox, #wrapper_correlati_spettacoli .lightbox').lightBox({
			imageLoading: '/immagini/lightbox/loading.gif',
			imageBtnClose: '/immagini/lightbox/closelabel.gif',
			imageBtnPrev: '/immagini/lightbox/prevlabel.gif',
			imageBtnNext: '/immagini/lightbox/nextlabel.gif',
			txtImage: 'Immagine',
			txtOf: 'di'
   });
	
	// FADE TASTI CHIUSI MENU SX
	$('ul.menu_nav_sx li > a.closed').fadeTo(100, 0.4);
	
	// OVER TASTI MENU SX
	$('#colonna_sx ul.menu_nav_sx > li > a').hover(
		function() {
			$(this).fadeTo(100, 1)
		}, function () {
			if($(this).attr('class') == 'closed') {
				$(this).fadeTo(100, 0.4)
			}
		}
	);
	
	// CLICK TASTI MENU SX
	$('ul.menu_nav_sx > li > a').click(function() {
			if(!$(this).next().is(':visible')) {
				// CHIUDO GLI APERTI
				$('ul.menu_nav_sx > li > a.open').fadeTo(100, 0.4);
  			$('ul.menu_nav_sx > li > a.open').addClass('closed');
  			$('ul.menu_nav_sx > li > a.open').removeClass('open');
				$('#menu_nav_sx > li > ul:visible').slideUp(500,'easeOutCubic');
				// APRO IL SELEZIONATO
				$(this).fadeTo(100, 1);
  			$(this).addClass('open');
  			$(this).removeClass('closed');
				$(this).next().slideDown(500,'easeOutCubic');
				return false;
			}
	})
	
	// FUNZIONE CUSTOM CHE APRE VOCE MENU SX PER SEZIONE SELEZIONATA	
	$.fn.ApriMenu = function() { 
		return this.each (function () {
			$(this).fadeTo(100, 1);
			$(this).removeClass('closed');
			$(this).addClass('open');
			$(this).next().slideDown(500,'easeOutCubic');
		});
	}
	
	// INIT SERVIZI HP
	$('.box_servizi_hp .box_servizi .titoli > a').click(function() {
		$('.box_servizi_hp .box_servizi .servizi').slideToggle(500,'easeOutCubic');
	})

	// CYCLIC FADE BOX HEADER	
	$.fn.tdrFade = function(durata) { 
		return this.each (function () {
			$(this).cyclicFade({
				repeat: 0,
				params: [{fadeout:durata, stayout:0, opout:0, fadein:durata, stayin:500, opin:1
				}]
			});
		});
	}
	
	$('#header .box_quadrati .box_3').tdrFade(5500);
	$('#header .box_quadrati .box_4').tdrFade(4000);
	$('#header .box_quadrati .box_5').tdrFade(6500);
	$('#header .box_quadrati .box_6').tdrFade(4500);
	$('#header .box_quadrati .box_7').tdrFade(7000);

});

// LOGIN
var v = new Array();
v["username"] = 'utente';
v["password"] = 'password';
v["name"] = 'Inserisci qui il tuo Nome*';
v["surname"] = 'Inserisci qui il tuo Cognome*';
v["email"] = 'Inserisci qui la tua Email*';

function InputDel(campo) {
	if (campo.value == v[campo.name]) {
		campo.value= '';
	}	
}

function InputFill(campo) {
	if (campo.value=='') {
		campo.value= v[campo.name];
	}	
}

var v_ing = new Array();
v_ing["username"] = 'utente';
v_ing["password"] = 'password';
v_ing["name"] = 'Type your first name here*';
v_ing["surname"] = 'Type your last name here*';
v_ing["email"] = 'Type your e-mail address here*';

function InputDel_ing(campo) {
	if (campo.value == v_ing[campo.name]) {
		campo.value= '';
	}	
}

function InputFill_ing(campo) {
	if (campo.value=='') {
		campo.value= v_ing[campo.name];
	}	
}

function PopUp(page,w,h,t,s,m) 
	{
	popwidth = w;
	popheight = h;
  if (!t) {t = 'no'};
	if (!s) {s = 'no'};
	if (!m) {m = 'no'};
	OpenWin = this.open(page, "prn", "toolbar=" + t + ", menubar=" + m +", location=no, scrollbars=" + s + ", resizable=no, width="+popwidth+", height="+popheight+", left=" + Math.floor((screen.width - popwidth) / 2) + ", top=" + Math.floor((screen.height - popheight) / 2));
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
  if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function controllo_ricerca(theForm){
	if (theForm.SearchedTxt.value == "" || theForm.testo_ricerca.value == " ") {
    	alert("E' necessario inserire il testo da ricercare.\nGrazie.");
    	theForm.SearchedTxt.focus();
    	return (false);
	}
}

function ControlloLogin(theForm){
	if (theForm.username.value == "") {
    	alert("Per favore inserire l'utente.\nGrazie.");
    	theForm.username.focus();
    	return (false);
	}
	else if (theForm.password.value == "") {
    	alert("Per favore inserire la password.\nGrazie.");
    	theForm.password.focus();
    	return (false);
	}	
}

function ControlloNewsletter(theForm){
	if (theForm.name.value == "" || theForm.name.value == "Inserisci qui il tuo Nome*") {
    	alert("Per favore inserisci il tuo nome.\nGrazie.");
    	theForm.name.focus();
    	return (false);
	}
	else if(theForm.surname.value == "" || theForm.surname.value == "Inserisci qui il tuo Cognome*") {
    	alert("Per favore inserisci il tuo cognome.\nGrazie.");
    	theForm.surname.focus();
    	return (false);
	}	
	else if (theForm.email.value == "" || theForm.email.value == "Inserisci qui la tua Email*") {
    	alert("Per favore inserisci la tua email.\nGrazie.");
    	theForm.email.focus();
    	return (false);
	}	
	else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theForm.email.value))) {
		alert("Indirizzo E-Mail non valido, si prega di verificarlo");
		theForm.email.focus();
		return (false);
	}
	else if (theForm.privacy.checked == false) {
		alert("Per procedere è necessario acconsentire al trattamento dei dati");
		theForm.privacy.focus();
		return (false);
	}
}

function ControlloNewsletterIng(theForm){
	if (theForm.name.value == "" || theForm.name.value == "Type your first name here*") {
    	alert("Please insert your first name");
    	theForm.name.focus();
    	return (false);
	}
	else if(theForm.surname.value == "" || theForm.surname.value == "Type your last name here*") {
    	alert("Please insert your last name");
    	theForm.surname.focus();
    	return (false);
	}	
	else if (theForm.email.value == "" || theForm.email.value == "Type your e-mail address here*") {
    	alert("Please insert your e-mail address");
    	theForm.email.focus();
    	return (false);
	}	
	else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theForm.email.value))) {
		alert("E-Mail address not valid");
		theForm.email.focus();
		return (false);
	}
	else if (theForm.privacy.checked == false) {
		alert("Please consent to handling of your personal data");
		theForm.privacy.focus();
		return (false);
	}
}

function URLEncode(s)
{
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = s;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	
	return encoded;
};

function URLDecode(s)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = s;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   
   return plaintext;
};

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/	

var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

function Chiocciola() {
document.write('@');
}

function goToDay(day, incr, key) {
  var oday = new String;
  oday += day;
  var year = oday.replace(/(.{4}).{2}.{2}/, "$1");
  var month = oday.replace(/.{4}(.{2}).{2}/, "$1");
  var day = oday.replace(/.{4}.{2}(.{2})/, "$1");
  var nday = new Date(year, month - 1, day);
  nday.setDate(nday.getDate() + incr);
  nday = nday.getFullYear() * 10000 + (nday.getMonth() + 1) * 100 + nday.getDate();
  document.location.href= "/adon.pl?act=Calendar&Mode=Src&day="+nday+"&key="+key;
}

