////////////////////////////////////////////////////////////////////////////////////////////////

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function YY_checkform() { //v4.71
//copyright (c)1998,2002 Yaromat.com
  var a=YY_checkform.arguments,oo=true,v='',s='',err=false,r,o,at,o1,t,i,j,ma,rx,cd,cm,cy,dte,at;
  for (i=1; i<a.length;i=i+4){
    if (a[i+1].charAt(0)=='#'){r=true; a[i+1]=a[i+1].substring(1);}else{r=false}
    o=MM_findObj(a[i].replace(/\[\d+\]/ig,""));
    o1=MM_findObj(a[i+1].replace(/\[\d+\]/ig,""));
    v=o.value;t=a[i+2];
    if (o.type=='text'||o.type=='password'||o.type=='hidden'){
      if (r&&v.length==0){err=true}
      if (v.length>0)
      if (t==1){ //fromto
        ma=a[i+1].split('_');if(isNaN(v)||v<ma[0]/1||v > ma[1]/1){err=true}
      } else if (t==2){
        rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");if(!rx.test(v))err=true;
      } else if (t==3){ // date
        ma=a[i+1].split("#");at=v.match(ma[0]);
        if(at){
          cd=(at[ma[1]])?at[ma[1]]:1;cm=at[ma[2]]-1;cy=at[ma[3]];
          dte=new Date(cy,cm,cd);
          if(dte.getFullYear()!=cy||dte.getDate()!=cd||dte.getMonth()!=cm){err=true};
        }else{err=true}
      } else if (t==4){ // time
        ma=a[i+1].split("#");at=v.match(ma[0]);if(!at){err=true}
      } else if (t==5){ // check this 2
            if(o1.length)o1=o1[a[i+1].replace(/(.*\[)|(\].*)/ig,"")];
            if(!o1.checked){err=true}
      } else if (t==6){ // the same
            if(v!=MM_findObj(a[i+1]).value){err=true}
      }
    } else
    if (!o.type&&o.length>0&&o[0].type=='radio'){
          at = a[i].match(/(.*)\[(\d+)\].*/i);
          o2=(o.length>1)?o[at[2]]:o;
      if (t==1&&o2&&o2.checked&&o1&&o1.value.length/1==0){err=true}
      if (t==2){
        oo=false;
        for(j=0;j<o.length;j++){oo=oo||o[j].checked}
        if(!oo){s+='* '+a[i+3]+'\n'}
      }
    } else if (o.type=='checkbox'){
      if((t==1&&o.checked==false)||(t==2&&o.checked&&o1&&o1.value.length/1==0)){err=true}
    } else if (o.type=='select-one'||o.type=='select-multiple'){
      if(t==1&&o.selectedIndex/1==0){err=true}
    }else if (o.type=='textarea'){
      if(v.length<a[i+1]){err=true}
    }
    if (err){s+='- '+a[i+3]+'\n'; err=false}
  }
	var lingua ='ita';
	
	if (window.document.forms['newsletter']) {
		lingua = window.document.forms['newsletter'].lingua.value;
		
	}
	
	switch (lingua) {
			case 'ita':
				var myalert = 'ATTENZIONE!';				
			break;
			
			case 'eng':
				var myalert = 'ATTENTION!';				
			break;
			
			case 'deu':
				var myalert = 'ACHTUNG!';				
			break;
			
			case 'fra':
				var myalert = 'ATTENTION!';				
			break;
			
			case 'spa':
				var myalert = 'PRECAUCION!';				
			break;
	}
	
	if (s!=''){alert(myalert+':\t\t\t\t\n\n'+s)}
  document.MM_returnValue = (s=='');
}


// FUNZIONI PER LA MAPPA V3
var infowindow,maps;var setDirection={options:{dirContainer:document.getElementById('dirContainer'),dirService:"",dirRenderer:"",myMarker:"",mapId:"",zoom:"",mapType:"",showCursor:""},set:function(mapId,optiones,zoom,mapType,showCursor){setDirection.options.mapId=mapId;setDirection.options.zoom=zoom;setDirection.options.mapType=mapType;setDirection.options.showCursor=showCursor;setDirection.options.myMarker=new Array();for(names in optiones)setDirection.options.myMarker[names]=optiones[names];setDirection.options.myMarker['infowindow']='<div id="ballon"><h5>'+setDirection.options.myMarker['labelTitle']+'</h5>'+setDirection.options.myMarker['infowindow']+'<br /><br />'+setDirection.options.myMarker['labelCalculate']+'<br /><a href="javascript:;" onclick="setDirection.init(\'<h5>'+setDirection.options.myMarker['labelTitle']+'</h5><br />'+setDirection.options.myMarker['labelCalculate']+'\', \''+setDirection.options.myMarker['labelFrom']+'\', \''+setDirection.options.myMarker['labelTo']+'\', \'FROM\', \''+setDirection.options.myMarker['lat']+', '+setDirection.options.myMarker['lon']+'\');" title="'+setDirection.options.myMarker['labelCalculate']+'"><strong>'+setDirection.options.myMarker['labelFrom']+'</strong></a> - <a href="javascript:;" onclick="setDirection.init(\'<h5>'+setDirection.options.myMarker['labelTitle']+'</h5><br />'+setDirection.options.myMarker['labelCalculate']+'\', \''+setDirection.options.myMarker['labelFrom']+'\', \''+setDirection.options.myMarker['labelTo']+'\', \'TO\', \''+setDirection.options.myMarker['lat']+', '+setDirection.options.myMarker['lon']+'\');" title="'+setDirection.options.myMarker['labelCalculate']+'"><strong>'+setDirection.options.myMarker['labelTo']+'</strong></a></div>';setDirection.openMap();},openMap:function(){var latlng=new google.maps.LatLng(setDirection.options.myMarker['lat'],setDirection.options.myMarker['lon']);if(setDirection.options.mapType==undefined)setDirection.options.mapType="ROADMAP";if(setDirection.options.showCursor==undefined)setDirection.options.showCursor=false;var myOptions={disableDefaultUI:setDirection.options.showCursor,mapTypeControl:false,mapTypeControlOptions:{style:google.maps.MapTypeControlStyle.DROPDOWN_MENU},zoom:setDirection.options.zoom,center:latlng,mapTypeId:google.maps.MapTypeId[setDirection.options.mapType]};maps=new google.maps.Map(document.getElementById(setDirection.options.mapId),myOptions);var myLatLng=new google.maps.LatLng(setDirection.options.myMarker['lat'],setDirection.options.myMarker['lon']);var marker=new google.maps.Marker({position:myLatLng,map:maps,icon:setDirection.options.myMarker['icon'],zIndex:10000});if(setDirection.options.myMarker['infowindow']!=""){infowindow=new google.maps.InfoWindow({content:setDirection.options.myMarker['infowindow'],maxWidth:350,position:myLatLng,zIndex:100});infowindow.open(maps,infowindow);google.maps.event.addListener(marker,"click",function(){infowindow.open(maps,infowindow);});}},init:function(myText,from,to,direction,latLan){if(document.getElementById('ballon')){setDirection.options.dirService=new google.maps.DirectionsService();setDirection.options.dirRenderer=new google.maps.DirectionsRenderer();var myFormDirection,textDirection;if(direction=="FROM"){myFormDirection='<form name="direction" action="javascript:;" method="get"><input id="from-input" type="hidden" value="'+latLan+'" />';myFormDirection+='<input id="to-input" type="text" value="" class="inputE" />';myFormDirection+='<input class="inputB" onclick="setDirection.getDirections(document.getElementById(\'from-input\').value, document.getElementById(\'to-input\').value);" type="button" value=">" /></form>';textDirection=myText+'<br /><strong>'+from+'</strong> - <a href="javascript:;" onclick="setDirection.init(\''+myText+'\', \''+from+'\', \''+to+'\', \'TO\', \''+latLan+'\');"><strong>'+to+'</strong></a>'+myFormDirection;}else{myFormDirection='<form name="direction" action="javascript:;" method="get"><input id="from-input" type="text" value="" class="inputE" />';myFormDirection+='<input id="to-input" type="hidden" value="'+latLan+'" />';myFormDirection+='<input class="inputB" onclick="setDirection.getDirections(document.getElementById(\'from-input\').value, document.getElementById(\'to-input\').value);" type="button" value=">" /></form>';textDirection=myText+'<br /><a href="javascript:;" onclick="setDirection.init(\''+myText+'\', \''+from+'\', \''+to+'\', \'FROM\', \''+latLan+'\');"><strong>'+from+'</strong></a> - <strong>'+to+'</strong>'+myFormDirection;}document.getElementById('ballon').innerHTML=textDirection;}},getDirections:function(fromStr,toStr){var dirRequest={origin:fromStr,destination:toStr,travelMode:google.maps.DirectionsTravelMode.DRIVING,unitSystem:google.maps.DirectionsUnitSystem.METRIC,provideRouteAlternatives:true};setDirection.options.dirService.route(dirRequest,setDirection.showDirections);},showDirections:function(dirResult,dirStatus){if(dirStatus!=google.maps.DirectionsStatus.OK){if(typeof(dirStatusErrora)!=undefined){alert(dirStatusError+"\n(Google reports: "+dirStatus+")");}else{alert("The address entered was not found\n(Google reports: "+dirStatus+")");}return;}setDirection.openMap();$('html,body').animate({scrollTop:$('#'+setDirection.options.mapId).offset().top-20},1000);setDirection.options.dirRenderer.setMap(null);setDirection.options.dirRenderer.setMap(maps);$('#dirContainer').html('');setDirection.options.dirRenderer.setPanel(document.getElementById('dirContainer'));setDirection.options.dirRenderer.setDirections(dirResult);}}
// FINE MAPPA


// Funzione per creare testo in javascript
var jbase64 = {
 _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 encode : function (input) {
  var output = "";
  var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  var i = 0;
  input = jbase64._utf8_encode(input);
  while (i < input.length) {
   chr1 = input.charCodeAt(i++);
   chr2 = input.charCodeAt(i++);
   chr3 = input.charCodeAt(i++);
   enc1 = chr1 >> 2;
   enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
   enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
   enc4 = chr3 & 63;
   if (isNaN(chr2)) {
    enc3 = enc4 = 64;
   } else if (isNaN(chr3)) {
    enc4 = 64;
   }
   output = output +
   this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
   this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
  }
  return output;
 },
 decode : function (input) {
  var output = "";
  var chr1, chr2, chr3;
  var enc1, enc2, enc3, enc4;
  var i = 0;
  input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  while (i < input.length) {
   enc1 = this._keyStr.indexOf(input.charAt(i++));
   enc2 = this._keyStr.indexOf(input.charAt(i++));
   enc3 = this._keyStr.indexOf(input.charAt(i++));
   enc4 = this._keyStr.indexOf(input.charAt(i++));
   chr1 = (enc1 << 2) | (enc2 >> 4);
   chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
   chr3 = ((enc3 & 3) << 6) | enc4;
   output = output + String.fromCharCode(chr1);
   if (enc3 != 64) {
    output = output + String.fromCharCode(chr2);
   }
   if (enc4 != 64) {
    output = output + String.fromCharCode(chr3);
   }
  }
  output = jbase64._utf8_decode(output);
  return output;
 },
 _utf8_encode : function (string) {
  string = string.replace(/\r\n/g,"\n");
  var utftext = "";
  for (var n = 0; n < string.length; n++) {
   var c = string.charCodeAt(n);
   if (c < 128) utftext += String.fromCharCode(c);
   else if((c > 127) && (c < 2048)) {
    utftext += String.fromCharCode((c >> 6) | 192);
    utftext += String.fromCharCode((c & 63) | 128);
   } else {
    utftext += String.fromCharCode((c >> 12) | 224);
    utftext += String.fromCharCode(((c >> 6) & 63) | 128);
    utftext += String.fromCharCode((c & 63) | 128);
   }
  }
  return utftext;
 },
 _utf8_decode : function (utftext) {
  var string = "";
  var i = 0;
  var c = c1 = c2 = 0;
  while ( i < utftext.length ) {
   c = utftext.charCodeAt(i);
   if (c < 128) {
    string += String.fromCharCode(c);
    i++;
   } else if((c > 191) && (c < 224)) {
    c2 = utftext.charCodeAt(i+1);
    string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
    i += 2;
   } else {
    c2 = utftext.charCodeAt(i+1);
    c3 = utftext.charCodeAt(i+2);
    string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
    i += 3;
   }
  }
  return string;
 }
}

// Funzione per l'inserimento della shadowbox
function shadowRel(){
	$('a.shadowBox').each(function() {
		$(this).attr('rel', 'shadowbox['+ $(this).attr('class').replace(/ /g, '_') +']');
	});
}

// Funzione per slideShow immagini nel visual
function slideShow() {

	var totPics =  $('#visualImgHome img').length;
  	var active = $('#visualImgHome img:first');
	var picTit = active.attr('alt');
	var picDesc = active.attr('title');
	
	$('.testoVisual').fadeOut(0);
	$('.testoVisual p.title').html(picTit);
	$('.testoVisual p.testo').html(picDesc);
	
	active
		.remove()
		.insertAfter('#visualImgHome img:last')
		.animate({opacity: 1.0}, 1000, function() {
			$(this).siblings().removeAttr('style');
			$('.testoVisual').fadeIn(500);
		});
	
}

/******************************************************* BOX NEWS HOMEPAGE */


function newsInitHome() {
	
	var list = $('#newsHome ul > li');

	if(list.length > 3) {
		setInterval ( "newsScrollHome()", 7000 );
	}
}

function newsScrollHome() {

	var myHeight = $('#newsHome ul > li:first').outerHeight();
	
	//console.log(myHeight);
	
	$('#newsHome ul > li:first').animate(			
		{opacity:0,
		marginTop: '-'+myHeight+'px'
		},
		700,
		'',
		function(){
			$(this).remove().insertAfter('#newsHome ul > li:last');
			$('#newsHome ul > li').removeAttr('style');
		});	

}

/******************************************************* BOX NEWS */


function newsInit() {
	
	var list = $('#news > li');

	if(list.length > 2) {
		setInterval ( "newsScroll()", 7000 );
	}
}

function newsScroll() {

	var myHeight = $('#news > li:first').outerHeight();
	
	$('#news > li:first').animate(			
		{opacity:0,
		marginTop: '-'+myHeight+'px'
		},
		700,
		'',
		function(){
			$(this).remove().insertAfter('#news > li:last');
			$('#news > li').removeAttr('style');
		});	

}

/*******************************************************   LINKS IN JAVASCRIPT */

function goToUrl(enUrl){
	
	var decUrl = jbase64.decode(enUrl);
	location.href=''+decUrl+'';	
}

///////////
$(document).ready(function(){
			
	// Init della shadowbox ////////////////
		shadowRel();
		Shadowbox.init();
		newsInitHome();
		newsInit();
		
		// Init slideShow 
		slideShow();
		$("#visualImg").css("background-image", "none");
		setInterval( "slideShow()", 5000 );
		
		// Accordion Menù Laterale  ///////////////
		$("#section").accordion({header:'li a.sections', active:false, navigation:true, autoHeight:false});
		//var active = $('#section > li').index('.active');
		var active = $('li.active a').attr('id');
		$("#section").accordion('activate',$('#'+active));
			
		// Funzione GalleryOne ////////////////
		var lista = $('.images');
		var lista_li = $('.images img').length-4;
		var move = 0;
		
		$('.gallery span.dx').click(function(){ 
			if ((move < lista_li) && (!lista.is(':animated'))) {
				lista.stop(true).animate({left:'+=-152px'});
				move++;
			}
		});
		
		$('.gallery span.sx').click(function(){
			if ((move > 0) && (!lista.is(':animated'))) {
				lista.stop(true).animate({left:'+=152px'});
				move--;
			}
			
		});	
		
		// Menu in Homepage
		var menu = $("#sectionsHome");
		$("ul", menu).hide();
		$("ul#topAtt li ul").show();
		// animazione menu
		$('.select', menu).hover(
			function() {
				$(this).children(".listSecHome").addClass('active').next().filter(':not(:animated)').slideDown();		// apro il sottomenu corrispondente e...
			},
			function() {
				$(this).children(".listSecHome").removeClass('active').next().slideUp();		// in uscita dall'hover chiudo il sottomenu corrispondente
			}
		);

		
});
