//parametros
minSize = 10;
maxSizeW = new Array(222,111,110,112,150,67,51,98);
maxSizeH = new Array(169,83,83,87,114,51,39,74);

popX 	 = new Array(-8,-8,101,0,0,0,70,110);
popY 	 = new Array(-8,62,62,0,50,62,20,55);

chrs	 = new Array(100,15,15,15,30,5,0,15);

velSize = 20;
timeSize= 1;
intervalTrade = 5000;
lastArray=new Array();
detOpen=false;
onMouseDet=false;

function autoTradeOn(){
	autoTradeOff();
	window.tradeInter = window.setInterval(fecha,intervalTrade);
}

function autoTradeOff(){
	if(window.tradeInter)
		clearInterval(window.tradeInter);
}

function startDest(){

	abre();
}


function fecha(){
	
	if(detOpen) return;
	
	autoTradeOff()
	window.perc=100;
	window.resizeInter = window.setInterval(resizeOutDest,timeSize);

}

function replaceAll(string, token, newtoken) {
	while (string.indexOf(token) != -1) {
 		string = string.replace(token, newtoken);
	}
	return string;
}

function abre(){
	autoTradeOff();
	HTML = document.getElementById("mod_destHome").innerHTML;
	

	setArtista();
	artIdArray = new Array();
	
	for(ii in artistas){

		do {
			x = Math.floor(Math.random()*artistas.length);
		}while(artistas[x] == "");
		
		descHTML = artistas[x][2].substring(0,chrs[ii])
		descHTML = descHTML.substring(0,descHTML.lastIndexOf(" "));
		descHTML +="... <strong>>></strong>";
		
		HTML = replaceAll(HTML,"#bg"+ii+"#",artistas[x][0]);
		HTML = replaceAll(HTML,"#artista"+ii+"#",artistas[x][1]);		
		HTML = replaceAll(HTML,"#desc"+ii+"#",descHTML);
		HTML = replaceAll(HTML,"#link"+ii+"#",artistas[x][3]);

		if(artistas[x][4]){
			HTML = replaceAll(HTML,"#display"+x+"#","");
		}

		artIdArray[ii] = artistas[x][4];
		artistas[x] = "";
		
	}

	for(i=0;i<=7;i++){
		HTML = replaceAll(HTML,"#display"+i+"#","display:none;");
	}
	
	HTML = HTML.replace("<!--","");
	HTML = HTML.replace("-->","");
	
	document.getElementById("destHome").innerHTML = HTML;
	onRoll();
	window.perc=1;
	window.resizeInter = window.setInterval(resizeInDest,timeSize);
	
	
	//document.getElementById("destHome_light").onmouseover = function(){
//		if(onMouseDet){
			//closeDetalhe();
		//}
		//onMouseDet = false;
	//};	
	document.getElementById("destHome_pop").onmouseover = function(){
			onMouseDet=true;
	};	
	document.getElementById("destHome_pop").onmouseout = function(){
			onMouseDet=true;
	};
	document.getElementById("destHome_pop").style.display="none";
}

function resizeInDest(){
	
	EaseVelSize = (velSize/100*(100-window.perc));
	window.perc+=EaseVelSize;
	resizeDest();
	marginZero();
}

function resizeOutDest(){
	
	EaseVelSize = (velSize/100*(window.perc));
	window.perc-=EaseVelSize;
	resizeDest();
	marginSize();
}

function resizeDest(){
	arrDest = getElementsById("dest")
	for(i in arrDest){
		maxSizeDestW = maxSizeW[i];
		maxSizeDestH = maxSizeH[i];
		
		/*gambs*/
		if(i==5 && Is())
			maxSizeDestH-=1;
		if(i==2 && !Is())
			maxSizeDestW+=1;
		/*----*/
		
		h = (maxSizeDestH/100*window.perc);
		w = (maxSizeDestW/100*window.perc);
		
		arrDest[i].style.width=w+"px";
		arrDest[i].style.height=h+"px";
	}	
	if(window.perc <= 1 || window.perc >= 99 )
		clearInterval(window.resizeInter);	
	if(window.perc >= 99)
		autoTradeOn();
	if(window.perc <= 1 )
		abre();
}

function marginZero(){
	for(i in arrDest){
		arrDest[i].style.marginLeft="0px";
		arrDest[i].style.marginTop="0px";
	}
}

function marginSize(){
	for(i in arrDest){
		maxSizeDestW = maxSizeW[i];
		w = (maxSizeDestW/100*(100-window.perc));
		h = (maxSizeDestH/100*(100-window.perc));
		arrDest[i].style.marginLeft=w+"px";
		arrDest[i].style.marginTop=h+"px";
	}
}

function onRoll(){
	arrDest = getElementsById("dest");
	for(i in arrDest){
		arrDest[i].i=i;
		arrDest[i].onmouseover = function(){
			artid = artIdArray[this.i];
			autoTradeOff();			
			timeToOpenDet(this.i,artid);

		};
		arrDest[i].onmouseout = function(){
			autoTradeOn();				
			timeToOpenDetOff();
		};		
	}
}

function timeToOpenDet(iDest){
	window.toDet = iDest;
	timeToOpenDetOff();
	window.interDet = setInterval(openDetalhe,500);
}

function timeToOpenDetOff(){
	if(window.interDet)
		clearInterval(window.interDet);
}

function openDetalhe(){

	iDet = window.toDet; 

	if(iDet > 2){
		side="r";		
	}else{
		side="l"; 
	}
	
	if(iDet==0)
		side="p"; 
	LEFT = popX[iDet];
	
	if(!Is())
		LEFT-=484;
	
	//LEFT=-8;
	//popY[iDet] = -8;
	//if() LEFT-=497; 
	//document.getElementById("destHome_pop").style.marginLeft	=LEFT+"px";
	//document.getElementById("destHome_pop").style.marginTop		=popY[iDet]+"px";
	
	//document.getElementById("destHome_light").style.marginLeft		=(LEFT-50)+"px";
	//document.getElementById("destHome_light").style.marginTop		=(popY[iDet]-50)+"px";

	//alert(document.getElementById("destHome_pop").style.marginTop);
	
	artid = artIdArray[iDet];

	ajaxpage("view_pop_detalhe.asp?mtd=pop_dest_art&side="+side+"&artid="+artid, "destHome_pop");
	
	//document.getElementById("destHome_light").style.display=
	document.getElementById("destHome_pop").style.display="block";
	autoTradeOff();
	detOpen = true;
	
}

function closeDetalhe(){
	autoTradeOn();
	//Pausar player
	if(document.getElementById("homePlayer")) {
		userPause();
	}
	document.getElementById("destHome_pop").style.display="";
	//document.getElementById("destHome_light").style.display="none";
	document.getElementById("destHome_pop").innerHTML = "";
	detOpen = false;
}

