// ************ variabili base
var inLoading = 0;
var paginaBak = 1;
var fx21;
var precedente = 1;

// ************ cerca nel sito
function cercanelsitofunct(){
	if ($('searchstr').value.length < 4) {
		alert("Immettere almeno 3 caratteri");
	}
	else {
		carica('cerca.asp?searchstr='+$('searchstr').value,'corpoPagina',400, '','', 'loaderCorpoCentrale');
	}
	return false;
}

// ************ tasti multimediali
function controller (what) {
if (what == 1 && precedente!=1) {$('butlive').src='gfx/button_live_selected.gif';$('butlive').setProperty("rel",1);$('butlive').className='';updateButtons(1);insertLiveStreaming();}
//if (what == 2 && precedente!=2) {$('butondemand').src='gfx/button_on_demand_selected.gif';$('butondemand').setProperty("rel",1);$('butondemand').className='';updateButtons(2);openOnDemandList();}
//if (what == 3 && precedente!=3) {$('butradio').src='gfx/button_radio_palio_selected.gif';$('butradio').setProperty("rel",1);$('butradio').className='';updateButtons(3);openRadioPalio();}
}

// ************ aggiornamento tasti multimediali
function updateButtons (what){
	
	switch (precedente){
		
		case 1:
		$('butlive').src='gfx/button_live.gif';
		$('butlive').setProperty("rel",0);
		$('butlive').className='manina';
		break;
		
		case 2:
		$('butondemand').src='gfx/button_on_demand.gif';
		$('butondemand').setProperty("rel",0);
		$('butondemand').className='manina';
		break;
		
		case 3:
		$('butradio').src='gfx/button_radio_palio.gif';
		$('butradio').setProperty("rel",0);
		$('butradio').className='manina';
		break;
		
	}
	
	precedente = what;
	
}

// ************ invio richiesta di iscrizione alla newsletter
function emailSend() {
	if ($('email').value != "") {
			    strScelta = new String($('email').value);
			    PositP = strScelta.indexOf("@");
		
			    if ((PositP == -1) || (PositP == 0)) {
			      alert("Indirizzo Email non valido.");
			     $('email').focus();
			      return false;
			    }
		
			    PositC = strScelta.lastIndexOf(".");

			    if ((PositC == -1) || (PositC == 0) || (PositC == strScelta.length - 1) || (PositC < PositP) || (Math.abs(PositC - PositP) == 1)) {
			      	alert(emailError1);
			    	$('email').focus();
			      	return false;
			    }
		
		  	}else{
				alert(emailError2);$('email').focus();return false;
			}

			carica("newsletter_registrazione.asp?email="+$('email').value,'feedbackInvio',400, '','', '')
}

// ************ questione per uscita pagina
window.onbeforeunload = question;



// ************ check inserimento solo numeri
function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
		 
		//alert(charCode)
         if (charCode > 31 && (charCode < 48 || charCode > 57)) {

			return false;
		 }

		if (charCode == 13) {
		
			$('searchstr').focus();
		}
		return (charCode != 13);
      }

// ************ scansione links:  previene loadUrl e stoppa la propagazione dell'evento
function scanLink(){

	$$('.openAjax').each(function(el){
	
		if (el.getProperty('rel') != "1") {
			el.setProperty('rel', '1');
			el.addEvent('click', function(ce){
				new Event(ce).stop();
				// debug $('searchstr').value=( el.getProperty( 'href' )+"/"+el.getProperty( 'target' ) );
				
				if (el.getProperty('target') == "corpoPagina") {
					loader = "loaderCorpoCentrale";
				}
				else {
					loader = "";
				}
				
				if (el.getProperty('href') != "" && el.getProperty('target') != null) 
					carica(el.getProperty('href'), el.getProperty('target'), 400, '', '', loader)
			});
		}
	});
	
	if (ie6) {
		$$('.mo4IE6').each(function(el){
		
			el.addEvents({
				'mouseover': function(event){
					this.addClass('forIE6on');
				}
			});
			
			el.addEvents({
				'mouseout': function(event){
					this.removeClass('forIE6on');
				}
			});
			
			
		})
	}
	
}

// ************ mostra bolla messaggio
function mostraBubble(bubbleMex){

	$('contenuto').innerHTML = bubbleMex;
	
	
	var running = false;
	$('bubble').setStyle('opacity', 0);
	
	fx21.stop();
	fx21.start({
		'top': [0, 500],
		'opacity': 1
	});
	
	
	
}

function myFunction(){
	var img = $('bubble')

	fx21.start({
		'top': [500, 0],
		'opacity': 0
	});
}



// ************ caricamento pagina

function carica(what,where,durata, trans0,trans1, loader){
	
	// what: la URL, gia con ?
	// where: id destinazione
	// durata: ms durata animazione opacity
	// trans0: ms durata animazione margin-left
	// trans1: pos partenza in px di margin-left
	// loader: id del div con gif animata di loading
	
		
		var tail='&r='+Math.floor(Math.random()*99999);
		var myAjax = new Ajax(what+tail+" ", {method: 'get', update: where, onSuccess: function(text){
			// contenuto funzione al completamento
			if (!ie7) {
				var fx2 = $(where).effects({
					duration: durata,
					transition: Fx.Transitions.Quart.easeOut
				});
				fx2.set({
					'opacity': 0
				});
			}

			 var fx = new Fx.Style($(where), 'margin-left', {
						duration: trans0,
						transition: Fx.Transitions.Circ.easeOut,
						wait: true
			   });
			
			if (!ie7) {
				fx2.stop();
			}
			
			fx.stop();
			
			if (trans1 != '') {
				fx.start(trans1, 0).chain(function(){
					inLoading = 0;
				});
			}else{
				inLoading = 0;
			}
			
			if (!ie7) {
				fx2.start({
					'opacity': 1
				});
			}
			
			}, onComplete: function(text){
				
			if (loader!="")	$(loader).style.display='none';
			if ($(where).id == "corpoPagina" || $(where).id.substring(0,12)=="sviluppoClip"){
			

			$('velinaCorpoCentrale').style.height = (ie6) ? document.body.offsetHeight - 132 + "px" : document.body.offsetHeight - 350 + "px"


			$('velinaCorpoCentrale').style.display='none';
			}
			

			}, evalScripts: true });
			
			//if (!inLoading) {
			myAjax.cancel();
				inLoading = 1;
				
		
			if (loader!="")$(loader).style.display='inline';
		
			if (typeof $(where) != "undefined") {
				if ($(where).id == "feedbackInvio") 
					$(where).innerHTML = "<div style=\"width:16px;margin:auto;\"><img src=\"gfx/ajax-loader.gif\" alt=\"\" /></div>";
				if ($(where).id == "corpoPagina" || $(where).id.substring(0, 12) == "sviluppoClip") {
				$('velinaCorpoCentrale').style.height = (ie6) ? document.body.offsetHeight - 132 + "px" : document.body.offsetHeight - 350 + "px"

					$('velinaCorpoCentrale').style.display = 'inline';
					
				}
			}
				myAjax.request();
				
			//}
}

// ************ youtube video
function youtubeVideo(URL){
	precedente = 0;
	$('butlive').src='gfx/button_live.gif';
	$('butlive').setProperty("rel",0);
	$('butlive').className='manina';
	var attributes = {allowFullScreen: "true"};
	swfobject.embedSWF(URL, "videoBox", "300", "252", "9.0", false,flashvars,attributes);

}
			
// ************ youtube video
function localeVideo(URL){
	precedente = 0;
	$('butlive').src='gfx/button_live.gif';
	$('butlive').setProperty("rel",0);
	$('butlive').className='manina';
	var flashvars = {mediaURL : "http://www.paliodelrecioto.com/documenti/video_gallery/" + URL + "&autoPlay=true&loop=true&buffer=10"};
	var attributes = {allowFullScreen: "true"};
	swfobject.embedSWF("gfx/nonverblaster.swf", "videoBox", "300", "252", "9.0", false,flashvars,attributes);

}

// ************ tasto live

function insertLiveStreaming(){
	swfobject.embedSWF(livestreamURL, "videoBox", "300", "260", "9.0", false,flashvars,attributes);
	//swfobject.embedSWF(livestreamURL, "videoBox", "300", "252", "9.0", false,flashvars,attributes);

}

// ************ tasto ondemand
function openOnDemandList(){
	carica("videogallery.asp?show=1","corpoPagina",400, '','', "loaderCorpoCentrale")
}

// ************ tasto radiopalio
function openRadioPalio(){
		window.open('radio.asp','finestra','width=300, height=200, toolbar=no, location=no, status=yes ,menubar=no, scrollbars=no, resizable=no');
}
		
// ************ variabili per paginatura news secondarie in hp e photogallery

		var totIncrement		= 0;
		var increment			= 226;
		var maxRightIncrement	= increment*(-NofArticles);
			  
		var totIncrementV		= 0;
		var incrementV			= 110;
		var maxRightIncrementV= incrementV*(-NofArticlesV);
			  
		var totIncrementArchive = 0;
		
// ************ aggiornamento controlli box storico-news
function updateControlsStorico(URL) {
			//alert(URL)
			 var fx = new Fx.Style('slider-list', 'margin-left', {
						duration: 1000,
						transition: Fx.Transitions.Back.easeInOut,
						wait: true
			   });
			   
			  //-------------------------------------
			  // EVENTS for the button "previousArchive"
			  $('previousArchive').addEvents({
		          'click' : function(event){ 
				  if(storicoPagina>1 && !inLoading){
				  		storicoPagina--;
							$('pagina').value=storicoPagina;
							carica(URL+'&pagina='+storicoPagina,'articleStoricoContents',1,400,448,'articleStoricoLoader')
						}
					}			  	  
		      }); 
			  
			  $('previousArchive').addEvents({ 
		          'mouseover' : function(event){ 
				    if(storicoPagina>1 && !inLoading){
				  		this.src='gfx/button_left_hover.gif'
				  	}
					}			  	  
		      }); 
			  
			  $('previousArchive').addEvents({ 
		          'mouseout' : function(event){ 
				  this.src='gfx/button_left.gif'
					}			  	  
		      }); 
			  
			  //-------------------------------------
			  // EVENTS for the button "previousArchive"
			  $('nextArchive').addEvents({ 
		          'click' : function(event){ 
				  if(storicoPagina<storicoPaginaMax && !inLoading){
				  		storicoPagina++;
							$('pagina').value=storicoPagina;
							carica(URL+'&pagina='+storicoPagina,'articleStoricoContents',1,400,-448,'articleStoricoLoader')
						}
					}			  	  
		      }); 
			  
			  $('nextArchive').addEvents({ 
		          'mouseover' : function(event){ 
				    if(storicoPagina<storicoPaginaMax && !inLoading){
				  		this.src='gfx/button_right_hover.gif'
				  	}
					}			  	  
		      }); 
			  
			  $('nextArchive').addEvents({ 
		          'mouseout' : function(event){ 
				  this.src='gfx/button_right.gif'
					}			  	  
		      }); 
			  
			 
			 
			 $('pagina').addEvents({
				'focus' : function(event){ 
					paginaBak=(this.value)
				}
			});
			 
			 
			  $('pagina').addEvents({
				'change' : function(event){ 

				if (this.value > 0 && this.value <= storicoPaginaMax) {
					storicoPagina = this.value;
					$('pagina').value = storicoPagina;
					carica(URL+'&pagina=' + storicoPagina, 'articleStoricoContents', 1, 400, 448,'articleStoricoLoader')
				}else{
					this.value=paginaBak;
				}
				}
			}); 
			   
}

// ************ aggiornamento controlli box news secondarie e photogallery
function updateControls() {
			 
			   var fx = new Fx.Style('slider-list', 'margin-left', {
						duration: 1000,
						transition: Fx.Transitions.Back.easeInOut,
						wait: true
			   });
			   
			    var fxV = new Fx.Style('slider-listV', 'margin-top', {
						duration: 1000,
						transition: Fx.Transitions.Back.easeInOut,
						wait: true
			   });
			  
			  //-------------------------------------
			  // EVENTS for the button "previous"
			  $('previous').addEvents({ 
		          'click' : function(event){ 
				  if(totIncrement<0){
							totIncrement = totIncrement+increment;
							fx.stop()
							fx.start(totIncrement);
						}
					}			  	  
		      }); 
			  
			  $('previous').addEvents({ 
		          'mouseover' : function(event){ 
				    if(totIncrement<0){
				  		this.src='gfx/button_left_hover.gif'
				  	}
					}			  	  
		      }); 
			  
			  $('previous').addEvents({ 
		          'mouseout' : function(event){ 
				  this.src='gfx/button_left.gif'
					}			  	  
		      }); 
			  
			  
			 
		       //-------------------------------------
			  // EVENTS for the button "next"
		  	  $('next').addEvents({ 
		          'click' : function(event){ 
					 if(totIncrement>maxRightIncrement){
						 totIncrement = totIncrement-increment;
				    	fx.stop()
						fx.start(totIncrement);
					}
		          }		  		  
		      })
			  
			  $('next').addEvents({ 
		          'mouseover' : function(event){ 
				   if(totIncrement>maxRightIncrement){
				  	this.src='gfx/button_right_hover.gif'
					}
					}			  	  
		      }); 
			  
			  $('next').addEvents({ 
		          'mouseout' : function(event){ 
				  this.src='gfx/button_right.gif'
					}			  	  
		      }); 
			  
			  
				updateControlsStorico('storico_articoli.asp?righe=8');
			  
			  
}
		
// ************ operazioni al caricamento della pagina
window.addEvent('domready', function(){ 
		
		MM_preloadImages('gfx/button_up_hover.gif','gfx/button_down_hover.gif','gfx/button_left_hover.gif','gfx/button_right_hover.gif','gfx/sf_menu_button_selected.gif');
		
		$('newsLetterBox').innerHTML = newsLetterBoxText;
		
					
		$('pageMax').innerHTML = storicoPaginaMax;
		var x = window.setInterval("carica('banners.asp?tipo=1','bannersGroup1',1000,1000,'','')", bannerGruppo1secs);
		var x = window.setInterval("carica('banners.asp?tipo=2','bannersGroup2',4000,1000,'','')", bannerGruppo2secs);
		
		 new YOOaccordion($$('#yoo-accordion-1 .toggler'), $$('#yoo-accordion-1 .content'), { open: 'ddd', allowMultipleOpen: false });
		 
		scanLink();

			  var fx = new Fx.Style('slider-list', 'margin-left', {
						duration: 1000,
						transition: Fx.Transitions.Back.easeInOut,
						wait: true
			   });
			   
			    var fxV = new Fx.Style('slider-listV', 'margin-top', {
						duration: 1000,
						transition: Fx.Transitions.Back.easeInOut,
						wait: true
			   });
			   
				updateControls();

			  //-------------------------------------
			  // EVENTS for the button "previousV"
			  $('previousV').addEvents({ 
		          'click' : function(event){ 
				  if(totIncrementV<0){
							totIncrementV = totIncrementV+incrementV;
							fxV.stop()
							fxV.start(totIncrementV);
						}
					}			  	  
		      }); 
			  
			  $('previousV').addEvents({ 
		          'mouseover' : function(event){ 
				    if(totIncrementV<0){
				  		this.src='gfx/button_up_hover.gif'
				  	}
					}			  	  
		      }); 
			  
			  $('previousV').addEvents({ 
		          'mouseout' : function(event){ 
				  this.src='gfx/button_up.gif'
					}			  	  
		      }); 
			  
			  
			 
		      //-------------------------------------
			  // EVENTS for the button "nextV"
		  	  $('nextV').addEvents({ 
		          'click' : function(event){ 
					 if(totIncrementV>maxRightIncrementV){
						 totIncrementV = totIncrementV-incrementV;
				    	fxV.stop()
						fxV.start(totIncrementV);
					}
		          }		  		  
		      })
			  
			  $('nextV').addEvents({ 
		          'mouseover' : function(event){ 
				   if(totIncrementV>maxRightIncrementV){
				  	this.src='gfx/button_down_hover.gif'
					}
					}			  	  
		      }); 
			  
			  $('nextV').addEvents({ 
		          'mouseout' : function(event){ 
				  this.src='gfx/button_down.gif'
					}			  	  
		      }); 
		
		
			fx21 = $('bubble').effects({
			duration: 1000,
			transition: Fx.Transitions.Quart.easeOut,
			onComplete: function(){
			myFunction.delay(3000); //Waits 5 seconds then executes myFunction.
			}
			});
			
			if (confermaNewsLetter == 1) {
				mostraBubble('<br/>Complimenti<br/>Iscrizione alla newsletter completata.');
			}
	

});

function moverout (el,img){
	
	if (!ie) {
		if (el.getProperty('rel') != '1') 
			el.src = img;
	} else {
		
		if (el.rel != '1')
		el.src = img;
	}
	
}

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 MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}