
var arr_prodotti;
var arr_prodotti_default_lang;
var vectleft=0;
var intervalID;
var isAutoMoving=false;
var intervalAutoMovingID;
var idStartMove;
var timeoutCambioLP;
var timeoutTextShowID;
var timeoutTextHideID;
var secondoBloccoLeft=0;
var numProdottiDefaultLng;
var arrLineeProd;
var timeoutCambiaTag=0;
var ffShifting=false;

function preparaStream() {
	isAutoMoving=false;
	i=0;
	j=0;
	arrLineeProd=new Array('distributori','capsule','bubblegums','kidszone', 'nbo');
	numProdotti=document.getElementById('txtnumProdotti').value;
	numProdottiDefaultLng=parseInt(document.getElementById('txtnumProdottiDefaultLng').value);
	secondoBloccoLeft=(document.getElementById("stream_element1").offsetWidth+2)*numProdottiDefaultLng;
	vectleft=-secondoBloccoLeft;
	timeoutTextShowID=new Array(numProdottiDefaultLng);
	timeoutTextHideID=new Array(numProdottiDefaultLng);
	nextLP=document.getElementById('txtnextLP').value;
	strArrProdotti=stripslashes(document.getElementById('txtstrArrProdotti').value);
	arr_prodotti=strArrProdotti.split("*^*^");
	strArrProdottiDefaultLng=stripslashes(document.getElementById('txtstrArrProdottiDefaultLng').value);
	arr_prodotti_default_lang=strArrProdottiDefaultLng.split("*^*^");
	tmpPage=document.getElementById('txttmpPage').value;
	// document.getElementById('box_stream_vector').style.width= numProdotti*document.getElementById('stream_element1').width;
	while (j<numProdotti*3) { // && i<6 ) {
		j++;
		i=(j%numProdotti)+1;
		document.getElementById('stream_element'+i).style.backgroundImage="url(imgs/stream_bg"+( j%5+1 )+".gif)";
		arr_tupla=arr_prodotti[i-1].split("_^_!_^_");
		arr_tupla_defLng=arr_prodotti_default_lang[i-1].split("_^_!_^_");
		tmpID=arr_tupla[0];
		tmpImg=arr_tupla[3];
		tmplpnome=arr_tupla[4];
		tmpPage=document.getElementById('txttmpPage').value;
		if (arr_tupla[6]!="") {
			tmpPage=arr_tupla[6];
		} else {
			tmpPage=arrDynLinks[tmpPage];	
		}
		document.getElementById('streamPLID'+j).innerHTML=tmplpnome;
		document.getElementById('streamLink'+j).href=tmpPage+'?pid='+tmpID;
		if (tmpImg=="") {
			tmpImg=arr_tupla_defLng[3];
		} else {
			tmpImg=arr_tupla[3]; //Prima immagine
		}
		if (tmpImg=="") {
			tmpImg="noimg_min"+( Math.floor(Math.random()*4)+1 )+".jpg";
		}
		document.getElementById('streamImg'+j).src='usrimages/'+tmpImg;
		timeout=0;
		do {
			timeout++;
		} while (timeout<100 && !(document.getElementById('streamImg'+j).complete));
		/*
		var h=getNaturalHeight(document.getElementById('streamImg'+i));
		var w=getNaturalWidth(document.getElementById('streamImg'+i));
		var hfactor=h/document.getElementById('streamImg'+i).height;
		var wfactor=w/document.getElementById('streamImg'+i).width;
		var scalefactor=0;
		if(hfactor>wfactor) {
			scalefactor=hfactor;
		} else {
			scalefactor=wfactor;	
		}
		document.getElementById('streamImg'+i).style.height=h/scalefactor;
		document.getElementById('streamImg'+i).style.width=w/scalefactor;
		*/
		document.getElementById('streamText'+j).innerHTML=""
		tmpnome="";
		if (arr_tupla[1]=="") {
			tmpnome=arr_tupla_defLng[1];
			document.getElementById('streamText'+j).innerHTML="<strong>"+arr_tupla_defLng[1]+"</strong><br />"; //Nome
		} else {
			tmpnome=arr_tupla[1];
			document.getElementById('streamText'+j).innerHTML="<strong>"+arr_tupla[1]+"</strong><br />"; 
		}
		if (arr_tupla[2]=="" && arr_tupla_defLng[2]!=tmpnome) {
			document.getElementById('streamText'+j).innerHTML=document.getElementById('streamText'+j).innerHTML+arr_tupla_defLng[2].substr(0,100); //Descrizione
		} else {
			if (arr_tupla[2]!=tmpnome) {
			document.getElementById('streamText'+j).innerHTML=document.getElementById('streamText'+j).innerHTML+arr_tupla[2].substr(0,100); 
			}
		}
	}
	document.getElementById('stream_popUpDescription').style.visibility="hidden";
	idStartMove=setTimeout('startMove()',1000);//pausa prima dell'avvio scorrimento = 1000
}



function startMove(){
	autoMove(true);
}


function animaUnSoloSlide(){
	//if(vectLeftStartingAt-vectleft<78) { 
	if(vectleft==vectLeftStartingAt || vectleft%78!=0) { 
		streamSetPosition(-1,6);
	} else {
		clearInterval(intervalAutoMovingID);
		if (isAutoMoving) setTimeout("animaAScatti();",60000);		
	}
}

function stopAndStart(){
	clearInterval(intervalAutoMovingID);
	if (isAutoMoving) setTimeout("animaAScatti();",5000);
}

function animaAScatti(){
	clearInterval(intervalAutoMovingID);
	vectLeftStartingAt=vectleft;
	intervalAutoMovingID = setInterval("animaUnSoloSlide()", 10);
	//setTimeout("stopAndStart();",1380);
}

function autoMove(startMoving) {
	clearTimeout(idStartMove);
	isAutoMoving=(ffShifting==false) && startMoving;
	if (isAutoMoving) {
		clearInterval(intervalAutoMovingID)
		animaAScatti();
		//intervalAutoMovingID = setInterval("streamSetPosition(-1,6)", 20);
		
	} else {
		clearInterval(intervalAutoMovingID);
		clearTimeout(timeoutCambioLP);
	}
}


function hideText(elem) {
	document.getElementById('stream_popUpDescription').innerHTML="";
	document.getElementById('stream_popUpDescription').style.visibility="hidden";	
	//clearInterval(timeoutTextShowID[elem-1]);
	//clearInterval(timeoutTextHideID[elem-1]);
	//timeoutTextHideID[elem-1]=window.setInterval("hideTextDo("+elem+")",50);
}

function hideTextDo(elem) {
var sposta=false;
 	if (toNumero(getStyleProp(document.getElementById('streamText'+elem),'height'))>0) {
		document.getElementById('streamText'+elem).style.height=toNumero(getStyleProp(document.getElementById('streamText'+elem),'height'))-5;
		sposta=true;
	}
	if (toNumero(getStyleProp(document.getElementById('streamText'+elem),'marginTop'))<0) {
		document.getElementById('streamText'+elem).style.marginTop=toNumero(getStyleProp(document.getElementById('streamText'+elem),'marginTop'))+5;		
		sposta=true;
	}
	var opac=getOpacityPercentuale(document.getElementById('streamText'+elem));
 	if (opac>10) {
		setOpacityPercentuale(document.getElementById('streamText'+elem),opac-10);
		sposta=true;
	} 
	if (!sposta) {
		clearInterval(timeoutTextHideID[elem-1]);
	}
}

function showText(elem) {
	var txtInnerHTML="";
	txtInnerHTML=(document.getElementById('streamText'+elem).innerHTML).replace("<BR>", "&nbsp; - &nbsp;");
	txtInnerHTML=txtInnerHTML.replace("<br>", "&nbsp; - &nbsp;");
	if (txtInnerHTML.substr(txtInnerHTML.length-15,15)=="&nbsp; - &nbsp;") {
		txtInnerHTML=txtInnerHTML.substr(0,txtInnerHTML.length-15);
	}
	txtInnerHTML="<div class='stream_popUpDescription_bg_left'></div>"+"<div class='stream_popUpDescription_bg_center'>"+txtInnerHTML
	txtInnerHTML=txtInnerHTML+" </div>"
	txtInnerHTML=txtInnerHTML+"<div class='stream_popUpDescription_bg_right'></div>"
	document.getElementById('stream_popUpDescription').innerHTML=txtInnerHTML;
	document.getElementById('stream_popUpDescription').style.visibility="visible";
	//clearInterval(timeoutTextHideID[elem-1]);
	//timeoutTextShowID[elem-1]=setInterval("showTextDo("+elem+")",50);
}

function showTextDo(elem) {
var sposta=false;
 	if (toNumero(getStyleProp(document.getElementById('streamText'+elem),'height'))<40) {
		document.getElementById('streamText'+elem).style.height=toNumero(getStyleProp(document.getElementById('streamText'+elem),'height'))+5;
		sposta=true;
	} 
	if (toNumero(getStyleProp(document.getElementById('streamText'+elem),'marginTop'))>-40) {
		document.getElementById('streamText'+elem).style.marginTop=toNumero(getStyleProp(document.getElementById('streamText'+elem),'marginTop'))-5;
		sposta=true;
	}
	var opac=getOpacityPercentuale(document.getElementById('streamText'+elem));
 	if (opac<90) {
		setOpacityPercentuale(document.getElementById('streamText'+elem),opac+10);
		sposta=true;
	} 
	if (!sposta) {
		clearInterval(timeoutTextShowID[elem-1]);
	}
}

function streamSetPosition(verso, velocita){
	try {
		numProdottiDefaultLng=Number(document.getElementById('txtnumProdottiDefaultLng').value);
	} catch(e) {
		return(false);
	}
	
	incremento=verso*velocita;
	isAtEnd=true;
	if (vectleft+incremento<-secondoBloccoLeft*2 || vectleft+incremento>0 ){
		vectleft=-secondoBloccoLeft;
	} else {
		vectleft=vectleft+incremento;
	}
	document.getElementById('box_stream_vector').style.marginLeft=vectleft;
	
	for (k=0;k<arrLineeProd.length;k++){
			document.getElementById("box_stream_tag"+arrLineeProd[k]).className="box_stream_tag";
			document.getElementById("stream_tag_left"+arrLineeProd[k]).className="box_stream_tag_left";
			document.getElementById("stream_tag_title"+arrLineeProd[k]).className="box_stream_tag_title";
			document.getElementById("stream_tag_right"+arrLineeProd[k]).className="box_stream_tag_right";			
	}
	tmpElem=parseInt(-vectleft/77+5);
	// tmpElem=parseInt(-vectleft/72+5);
	document.getElementById("box_stream_tag"+document.getElementById('streamPLID'+tmpElem).innerHTML).className="box_stream_tag_selected";
	document.getElementById("stream_tag_left"+document.getElementById('streamPLID'+tmpElem).innerHTML).className="box_stream_tag_selected_left";
	document.getElementById("stream_tag_title"+document.getElementById('streamPLID'+tmpElem).innerHTML).className="box_stream_tag_selected_title";
	document.getElementById("stream_tag_right"+document.getElementById('streamPLID'+tmpElem).innerHTML).className="box_stream_tag_selected_right";
}

function cambiaTag(nextLP){
	/* ajaxGetRand("box_stream.php?idLP="+nextLP,myHandler,"streamcontainer");
	   idTimerCambiaStream=setInterval("controllaNuovoStream()",200); */
	ffShifting=true;
	clearInterval(timeoutCambiaTag);
	timeoutCambiaTag=setInterval("cambiaTagDo('"+nextLP+"')",20);
}

function cambiaTagDo(nextLP){
	var trovato=true;
	for (k=0;k<arrLineeProd.length;k++){
		if(arrLineeProd[k]!=nextLP && document.getElementById("box_stream_tag"+arrLineeProd[k]).className=="box_stream_tag_selected"){
			trovato=false;
		}
	}
	if (!trovato)	{
		streamSetPosition(-1,72);
	} else {
		clearInterval(timeoutCambiaTag);
		ffShifting=false;
		document.getElementById("box_stream_tag"+nextLP).className="box_stream_tag_selected";
		document.getElementById("stream_tag_left"+nextLP).className="box_stream_tag_selected_left";
		document.getElementById("stream_tag_title"+nextLP).className="box_stream_tag_selected_title";
		document.getElementById("stream_tag_right"+nextLP).className="box_stream_tag_selected_right";		
		idStartMove=setTimeout('startMove()',400);//pausa prima dell'avvio scorrimento = 40
	}
}

function streamShift(verso, velocita) {
	intervalID = setInterval("streamSetPosition("+verso+","+velocita+")", 50);
}

function streamStop() {
	clearInterval(intervalID);
	clearInterval(intervalAutoMovingID);
}

function streamTagOver(bolSwitch,tagId) {
	if (bolSwitch==1) {
		document.getElementById("stream_tag_left"+tagId).className="box_stream_tag_over";
		document.getElementById("stream_tag_left"+tagId).className="box_stream_tag_over_left";
		document.getElementById("stream_tag_title"+tagId).className="box_stream_tag_over_title";
		document.getElementById("stream_tag_right"+tagId).className="box_stream_tag_over_right";
	} else {
		document.getElementById("stream_tag_left"+tagId).className="box_stream_tag";
		document.getElementById("stream_tag_left"+tagId).className="box_stream_tag_left";
		document.getElementById("stream_tag_title"+tagId).className="box_stream_tag_title";
		document.getElementById("stream_tag_right"+tagId).className="box_stream_tag_right";
	}
}

function streamCanvasOver(bolSwitch, canvasId) {
	if (bolSwitch==1) {
		document.getElementById("stream_element"+canvasId).style.backgroundImage="url(imgs/stream_bg_over.gif)";
	} else {
		document.getElementById("stream_element"+canvasId).style.backgroundImage="url(imgs/stream_bg"+(canvasId%5+1)+".gif)";
	}	
}