var lastClicked = false;
try
{
	/*
	* @param Element immagine elemento di tipo A da cui prelevare i dati
	* @param boolean notAClick true la chiamata alla funzione non proviene da un click dell'utente
	*/
	function portaInPrimoPiano( immagine, notAClick )
	{
		var container = document.getElementById( 'primopiano-box' );
		
		if ( ! notAClick && lastClicked )
		{
			if ( immagine.id == lastClicked.id )
			{
//				alert(  immagine.getAttribute( 'href' )  );
				document.location.href = immagine.getAttribute( 'href' );
			}
		}
		
		if ( ! container ) return false; 
		if ( ! immagine ) return false;
		/*
		if (  immagine.getAttribute( 'href' ).length > 0 ) 
		{
			immagine.setAttribute( 'cite', immagine.getAttribute( 'href' ) );
			immagine.setAttribute( 'href', '#' );
		}
		*/
		
		primoPianoHref =  immagine.getAttribute( 'href' );
		$('#sfondo-primopiano-box')[0].style.backgroundImage = container.style.backgroundImage;
		$( container ).hide();
		container.style.backgroundImage = 'url(' + immagine.getAttribute('alt') + ')';
		$( container ).fadeIn('slow');
		
		if ( ! notAClick && typeof( primoPianoTimer ) != "undefined" )
		{ //se e' l'utente ha cliccato su una thumbnail fermo il timer che cicla alle immagini
			primoPianoTimer.stop();
		}		
		if ( notAClick )
		{
			lastClicked = false;
			container.setAttribute('alt', primoPianoHref ); 
			primoPianoHref = false;
		} else {
			lastClicked = immagine;
		}
		
		//alert( container.style.backgroundImage );
	}
	function Ticker()
	{
	    this.counter = 0;
	    this.timer = new Timer(this);
	    this.stopped = false;
	}
	Ticker.prototype.stop = function()
	{
		this.stopped = true;
	}
	Ticker.prototype.tick = function(d)
	{
            	    if ( this.stopped ) return false;
            	    
	    portaInPrimoPiano( document.getElementById( teaserIds[ this.counter ] ), true );
	    this.counter++;
	    if ( this.counter >= teaserIds.length )
	    {
	    	this.counter = 0;
	    }
	   this.timer.setTimeout("tick", 7000, d);
	}
	
	//assegno gli onclick
	
	for ( i in teaserIds )
	{
		document.getElementById( teaserIds[ i ] ).onclick = function () 
		{ 
			portaInPrimoPiano(this);
			return false; 
		}
	}
//		for ( var i; i < teaserIds.length; i++ ) alert ( teaserIds[i].id );
	var primoPianoTimer = new Ticker();
	window.onload = function(){
	    primoPianoTimer.tick(1);
	}	
} catch( fakeExc ){
	/*se anche non girano da soli pazienza */  
}