var divs = new Array();
var small = new Array();


var tendina_prefix = "tendina_";
var div_prefix = "div_";

var layer_immagine_big = 'img-strillo-p';
var big_prefix = "big_strillo_";
var small_prefix = "small_strillo_";
var content_prefix = "content_";
var href_prefix = "href_";
// var border_prefix = "bordo_";


var slide_show_time = 5000;
var fade_velocity = 80;
var delta_opacity = 10;

var min_opacity = 20;
var max_opacity = 60;
var text_changed = 0;

var animation_running = true;
var stop_animation = 0;
var opacity_ingresso = max_opacity;
var opacity_uscita = min_opacity;
var index = 0;
var old_index;
var my_interval;
var tm2;
var img;
var active;


var ctesto_layer;
var contents = new Array();
var links    = new Array();
var hrefs    = new Array();
var borders  = new Array();

var urlPerBacoIE5eSafari;

// Default - azioni specifiche da definire negli strilli
var rossoalicePopupAction = new Array();


var browserEnabled = true;

if( navigator.userAgent.indexOf("Opera")>-1 || navigator.userAgent.indexOf("Safari")>-1 ||
	( navigator.userAgent.indexOf("Mac")>-1 && navigator.appVersion.indexOf('MSIE')>-1 )){
	browserEnabled = false;
}

var firstRun = 1;

function start() {

	divs[0] = '1';
	divs[1] = '2';
	divs[2] = '3';

	if (findLayer(content_prefix + divs[0])){


		contents[0] = findLayer(content_prefix + divs[0]).innerHTML;
		contents[1] = findLayer(content_prefix + divs[1]).innerHTML;
		contents[2] = findLayer(content_prefix + divs[2]).innerHTML;


		hrefs[0] = document.getElementById(href_prefix + divs[0]).href;
		hrefs[1] = document.getElementById(href_prefix + divs[1]).href;
		hrefs[2] = document.getElementById(href_prefix + divs[2]).href;

//		borders[0] = document.images[border_prefix + divs[0]];
//		borders[1] = document.images[border_prefix + divs[1]];
//		borders[2] = document.images[border_prefix + divs[2]];

		ctesto_layer  = findLayer('testo-strillo-p');

		findLayer(layer_immagine_big).style.visibility="visible";

//		document.images[border_prefix + '1'].style.visibility="visible";
		setHrefMask( hrefs[0] );

		old_index = 1;

		for (i=0;i<divs.length;i++){

			document.images[big_prefix + divs[i]].style.visibility = "visible";

			if(browserEnabled){
				setOpacity(document.images[tendina_prefix + divs[i]], max_opacity);
				document.images[tendina_prefix + divs[i]].style.visibility = "visible";
			}else{

				document.images[tendina_prefix + divs[i]].src = tendina_nofading;
				document.images[tendina_prefix + divs[i]].style.visibility = "visible";
			}

		}

		if(browserEnabled){
			setOpacity(document.images[tendina_prefix + divs[0]], min_opacity);
		}else{
			document.images[tendina_prefix + divs[0]].style.visibility="hidden";
		}


		document.images[small_prefix + divs[0]].style.zIndex=1;
		document.images[small_prefix + divs[1]].style.zIndex=1;
		document.images[small_prefix + divs[2]].style.zIndex=1;


		go();
	}else{
		/* strillo di emergenza - nessuna rotazione */
	}


}

function go(mouseOverDetected) {
	if (index >= divs.length) {
		index = 0;
		if (old_index != -1) { old_index = divs.length-1; }
	}


	for (i=0;i<divs.length;i++){
		document.images[big_prefix + divs[i]].style.zIndex = 1;
	}

	animation_running = true;

	if (!firstRun &&!mouseOverDetected) {
		setOpacity(document.images[big_prefix + divs[index]], min_opacity);
	}

	document.images[big_prefix + divs[old_index]].style.zIndex = divs.length;
	document.images[big_prefix + divs[index]].style.zIndex = divs.length+1;

	opacity_ingresso = max_opacity;

	ingresso = document.images[tendina_prefix+divs[index]];

	uscita = document.images[tendina_prefix+divs[((old_index) >= 0) ? old_index : divs.length - 1]];

	swap(mouseOverDetected);

}

function mouseOverThmb(imgName) {
	if(index!=parseInt(imgName) && !stop_animation && !animation_running){
		clearTimeout(my_interval);
		clearTimeout(tm2);
		index = parseInt(imgName)-1;
		go(true);
	}

}

function mouseOutThmb(imgName) {
}

function swap(mouseOverDetected){
	 if(browserEnabled){
		swapFade(mouseOverDetected);
	 }else{
		swapNoFade();
	 }
}


function setOpacity(image, opacity) {

	if (browserEnabled){
		opacity=(opacity == 100)?99.999:opacity;
		image.style.filter="alpha(opacity:"+opacity+")";
		image.style.KHTMLOpacity=opacity/100;
		image.style.MozOpacity=opacity/100;
		image.style.opacity=opacity/100;
	}
}

function swapTexts(){
	testo = ctesto_layer.innerHTML;
	if (testo!=contents[index]) {
		ctesto_layer.innerHTML=contents[index];
		//borders[old_index].style.visibility = "hidden";
		//borders[index].style.visibility = "visible";
		setHrefMask( hrefs[index] );
		text_changed = 1;
	}

}

function swapNoFade() {

	swapTexts();
	active = ingresso;
	document.images[tendina_prefix + divs[old_index]].style.visibility="visible";
	document.images[tendina_prefix + divs[index]].style.visibility="hidden";

	firstRun = 0;
	animation_running = 0;
	schedule();

}


function schedule(){
	if (!stop_animation) {
		old_index = index;
		index++;
		tm2 = window.setTimeout("go()", slide_show_time);
	}
}



function swapFade(mouseOverDetected) {

	if (opacity_ingresso >= min_opacity) {
    	active = ingresso;

		swapTexts();

		if(!mouseOverDetected){
			opacity_ingresso -= delta_opacity;
			if (opacity_uscita < max_opacity){opacity_uscita += delta_opacity;}
			if (!firstRun) {
				setOpacity(ingresso, opacity_ingresso);
				setOpacity(uscita, opacity_uscita);
				setOpacity(document.images[big_prefix + divs[index]], opacity_uscita/max_opacity*100 );
			}
			my_interval = window.setTimeout("swapFade()", fade_velocity);
		}else{
			animation_running = 0;
			setOpacity(ingresso, min_opacity);
			setOpacity(uscita, max_opacity);
			setOpacity(document.images[big_prefix + divs[index]], 100);
			schedule();
		}
	} else {
	      animation_running = 0;
	      text_changed = 0;
	      opacity_uscita = opacity_ingresso;
	      firstRun = 0;
	      schedule();
    }
}




function goToUrl(){
	
	document.location.href=urlPerBacoIE5eSafari;
}

function popup(arg){
	eval(rossoalicePopupAction[arg]);
}

setHrefMask = function(href){
	if(document.getElementById && document.getElementById('href-strillo-p')){
		if (rossoalicePopupAction && rossoalicePopupAction[index] && rossoalicePopupAction[index].length>1){
			document.getElementById('href-strillo-p').href='javascript:popup(' + index + ');';
		}else{
			document.getElementById('href-strillo-p').href=href;
		}
	}
}
