
/********************************************************************/
//** 
    core.loadUnit("animate");
    core.loadUnit("listbox");
    core.loadUnit("color");


/********************************************************************/
//** 


var reactionSpeed=.1;


var currStep= 0;
var maxStep = 0;
var lastCoords = '';

/********************************************************************/
//** 
//* PARAMETROS DE CONFIGURACION
//* Esto parametros se pueden sobreescribir desde la ventana que llama al plugin
//* para configurarlo de forma especifica. Los valores aqui mostrados son los valores
//* por defecto
//* Ademas deben existir los DIV siguientes

//* <DIV ID=WHLbackborder  style="visibility:hidden" > contiene el fondo que se mostrara
//* detras del objeto elegido. Por defecto es un borde amarillo

//* <DIV ID=WHLcontentXXX  style="position:absolute; left:-200;"> contienen cada uno de los
//* elementos del la rueda

//* 

/********************************************************************/


WHL_X0 = 400;				//centro del circulo
WHL_Y0 = 280;

WHL_Vradio= 0.8*220; 			// radio vertical
WHL_Hradio= 220; 			// radio horizontal

var degStartPoint = -90;  		// indica el punto de origen de la rueda  en grados. + direccion horaria - antihoraria

WHL_onArriving = '';			// contiene una/s funciones que se llaman al llegar el item a su posicion final
WHLalpha	=25;			// transparencia de los objetos de fondo

WHLcontentW = 100;			// ancho que tiene cada elemento
WHLcontentH = 100;			// alto que tiene cada elemento

y=new Array();
x=new Array();
Y=new Array();
X=new Array();

WHLelement = new Array();


var  OrigSt;
Nitems = 0;
Nsteps = 200;
Kdirection = 1;
nchunks = 0;
nchtoitem = 0;

wheelTimer = null;
currItem = 1;
ACTIVATEWHEEL = true;

window.onload = WHL_init;



/********************************************************************/
//** 
//* <descripcion>
//* @param <name> -> <valor>
//* ...
//* @param <name> -> <valor>
//* @return <retorno>
/********************************************************************/

function WHL_init()
{

	// obtiene el numero de items
	i=1;
	while ( document.getElementById('WHLcontent'+(1*i))!=null)
	{  i++; }
	

	Nitems = i-1;


	for (i=1; i <=Nitems; i++)
	{
		y[i]=0;
		x[i]=0;
		Y[i]=0;
		X[i]=0;
 

 		WHLelement[i] = core.createElm(null,300,0);

		thecontent = document.getElementById('WHLcontent'+(1*i)).innerHTML;

		WHLelement[i].id = i;
		WHLelement[i].setText(thecontent);
		WHLelement[i].setZ(3);
		WHLelement[i].setW(100);
		WHLelement[i].setH(100);
		WHLelement[i].setAlpha(WHLalpha);
		WHLelement[i].show();
		WHLelement[i].enabled = true;

	}

		borderW = core.createElm(null,0,0);
		
		thebackborder = document.getElementById('WHLbackborder');
		if ( thebackborder!=null )
			borderW.setText(thebackborder.innerHTML);
		else
			borderW.setText('<table width=100% height=100% border=2><tr><td>&nbsp;</td></tr></table>');
		
		borderW.hide();
	






	OrigSt		= degStartPoint*Nsteps/360;  //convierte los grados del punto de origen en steps
	maxStep 	= OrigSt;
	Kdirection 	= maxStep / Math.abs(maxStep);



	ACTIVATEWHEEL = true;

	WHL_moveWheel();

}




/********************************************************************/
//** 
//* <descripcion>
//* @param <name> -> <valor>
//* ...
//* @param <name> -> <valor>
//* @return <retorno>
/********************************************************************/


function WHL_gencircle()
{ 


	y[1]=Math.round(Y[1]+=((WHL_Y0)-Y[1])*reactionSpeed);
	x[1]=Math.round(X[1]+=((WHL_X0)-X[1])*reactionSpeed);

	for (var i=2; i <Nitems+1; i++){
		y[i]=Math.round(Y[i]+=(y[i-1]-Y[i])*reactionSpeed);
		x[i]=Math.round(X[i]+=(x[i-1]-X[i])*reactionSpeed);

	}




	kx=Math.PI*2/Nitems; 		// separacion entre cada parte
	ky=Math.PI*2/Nitems;

	radperstep = Math.PI*2/Nsteps;  	//rad de cada step

//window.status = 'cur=' + currStep + ' / max=' + maxStep + ' / dir=' + Kdirection + ' / ' + new Date();
//window.status = 'nchunks=' + ((OrigSt - currStep) * Nitems/Nsteps) +  ' '	
	WHLM_setData()


	if (parseInt(currStep)!= parseInt(maxStep))  // quitar esta linea para que gire continuamente
	{
		borderW.hide();
		currStep += Kdirection;
		
		currStep = currStep%Nsteps;
		
	}
	else
	{
		WHLelement[currItem].setAlpha(100);
		
		borderW.setX(WHLelement[currItem].getX()-15)
		borderW.setY(WHLelement[currItem].getY()-15)
		
		borderW.setSize(WHLelement[currItem].getW()+30,WHLelement[currItem].getH()+30);
		borderW.show();
		
	}
		


// coordenadas del circulo
	fcoords = '';
	for (i=1; i <=Nitems; i++){ 

		vy = y[i]+WHL_Vradio*Math.sin(currStep*radperstep + (i-1)*kx);
		vx = x[i]+WHL_Hradio*Math.cos(currStep*radperstep + (i-1)*ky );

	
		WHLelement[i].moveTo(vx,vy);

		Kdis=1;	 // k=0...4
		angledistance = Math.abs( Math.ceil(-degStartPoint + 180*Math.atan2((vy-y[i]),(vx-x[i]))/Math.PI ) );
		angledistance = angledistance>180 ? 360-angledistance : angledistance;
		Zindex = angledistance>=90 ? 1 : 5;
		Xmagnify = ((angledistance / 180) ) ;
		magnify = (1/150)*Math.exp(5-Kdis*Xmagnify)
//angdis = Math.abs(Math.ceil( -degStartPoint + 180*Math.atan2((vy-WHL_Y0),(vx-WHL_X0))/Math.PI ));
//angdis = angdis>180 ? 360-angdis : angdis;
//WHLelement[i].setText( ' <a href=# onclick=WHL_itemToOrigin(' + i + ')>' + angdis  + '<br>' +  magnify + '</a>')
		
		WHLelement[i].setW( WHLcontentW*magnify); //cambiamos el tamaño: mas pequeño cuanto mas se aleja
		WHLelement[i].setH( WHLcontentH*magnify);
		WHLelement[i].setZ( Zindex );				// si está mas lejos debe verse detrás de los mas cercanos
		
	fcoords += vx + vy;
	}
		


	if (lastCoords == fcoords )
	{
		eval(WHL_onArriving);
		ACTIVATEWHEEL = false;
	}
	else
		lastCoords = fcoords;



			



}


/********************************************************************/
//** 
//* <descripcion>
//* @param <name> -> <valor>
//* ...
//* @param <name> -> <valor>
//* @return <retorno>
/********************************************************************/

function WHL_moveWheel(){ 

	

	WHL_gencircle();


	if (ACTIVATEWHEEL)
		wheelTimer = setTimeout('WHL_moveWheel()',1);
	//else
	//	borderW.hide();
	
}

function WHL_stopWheel(){ 
	
	clearTimeout(wheelTimer);
}



function WHL_deactivateWheel()
{
	ACTIVATEWHEEL = false;
	borderW.hide();
	
}


function WHL_activateWheel()
{
	ACTIVATEWHEEL = true;

	WHL_moveWheel();
	
	
}



/********************************************************************/
//** 
//* <descripcion>
//* @param <name> -> <valor>
//* ...
//* @param <name> -> <valor>
//* @return <retorno>
/********************************************************************/

function WHL_slideOneChunk (kd) {
	
	
	Kdirection = kd;
	
	maxStep += Kdirection*Nsteps/Nitems ;
	
	maxStep = maxStep%Nsteps;


	WHL_activateWheel();	

}





/********************************************************************/
//** 
//* <descripcion>
//* @param <name> -> <valor>
//* ...
//* @param <name> -> <valor>
//* @return <retorno>
/********************************************************************/

function WHL_itemToOrigin (itemid) {

		
	if ( itemid==currItem || !WHLelement[itemid].enabled)
		return;
	
	WHLelement[currItem].setAlpha(WHLalpha);


	currItem = itemid;

	nchunks = Math.round(  (OrigSt - currStep) * Nitems/Nsteps  );   // distancia en chunks del item 0 al origen
		
	nchtoitem = nchunks - 1*itemid +1;	// total de chunks del origen al item

	if (nchtoitem==0)
		return;
	
	

	nchtoitem = nchtoitem%Nitems;
	var sng = nchtoitem/Math.abs(nchtoitem);

	d1 = nchtoitem;
	d2 = -sng*(Nitems-Math.abs(d1))

	nchtoitem = Math.abs(d1)>Math.abs(d2) ? Math.round(d2) :  Math.round(d1);
	sng = nchtoitem/Math.abs(nchtoitem);
	
	for (var i=0; i<Math.abs(nchtoitem); i++)
		WHL_slideOneChunk(sng);
		
	
}



/********************************************************************/
//** 
//* <descripcion>
//* @param <name> -> <valor>
//* ...
//* @param <name> -> <valor>
//* @return <retorno>
/********************************************************************/


function WHLM_setData() {}
