this.arcs	= null;
this.t		= 0;

// --------------------------------
// ArcCircles
// --------------------------------
this.Arcs = function()
{
	this.m_items	= new Array();
	this.M_add		= function(item)
	{
		this.m_items.push(item);
	}
	this.M_init		= function()
	{
		for (var i=0 ; i<this.m_items.length ; i++)
			this.m_items[i].M_init();	
	}
	
	this.M_update	= function(dt)
	{
		for (var i=0 ; i<this.m_items.length ; i++)
			this.m_items[i].M_update(dt);
	}
	this.M_draw		= function()
	{
		for (var i=0 ; i<this.m_items.length ; i++)
			this.m_items[i].M_draw();
	}
}



// --------------------------------
// Arc
// --------------------------------
this.Arc = function(r,w,astart,aend,rspeed,color)
{
	this.m_glList = 0;
	this.m_r = r;
	this.m_w = w;
	this.m_astartDeg= astart;
	this.m_astart	= astart*Math.PI/180;
	this.m_aend		= aend*Math.PI/180;
	this.m_nbStep	= 30;
	this.m_astep	= (this.m_astart-this.m_aend)/(this.m_nbStep-1);
	this.m_rotSpeed	= rspeed == undefined ? Math.random()*10 : rspeed;
	this.m_rot		= 0;
	this.m_rotx		= 0;
	this.m_rotSpeedx= 0;
	this.m_color	= color == undefined ? {r:1,g:1,b:1} : color;
	
	this.M_norm = function(v)
	{
		n = Math.sqrt(v.x*v.x+v.y*v.y);
		return {x:v.x/n,y:v.y/n};
	}

	this.M_init = function()
	{
// ->BUG
//		this.m_glList = gl.StartList();
//		gl.EndList();
	}

	this.M_update = function(dt)
	{
		this.m_rot += this.m_rotSpeed * dt;
		if (this.m_rot >=360)
			this.m_rot -= 360;

		this.m_rotx +=this.m_rotSpeedx * dt
		if (this.m_rotx >=360)
			this.m_rotx -= 360;

		if (t>2)
		{
			this.m_rotSpeedx += 0.3*(this.m_rotSpeed-this.m_rotSpeedx)*dt;
		}


	}

	this.M_draw = function()
	{
//		gl.CallList(this.m_glList);

		gl.Color(this.m_color.r,this.m_color.g,this.m_color.b);
		gl.Rotate(-this.m_rot,0,0,1);
		gl.Rotate(this.m_rotx,1,1,0);
		
		a = this.m_astart;
		x1=y1=x2=y2=x3=y3=x4=y4=0;
		cosa=sina=0;
		for (var i=0 ; i<this.m_nbStep ; i++)
		{
			cosa = Math.cos(a);
			sina = Math.sin(a);

			cosa2 = Math.cos(a+this.m_astep);
			sina2 = Math.sin(a+this.m_astep);
		
			x1 = cosa * this.m_r;
			y1 = sina * this.m_r;

			x2 = cosa2 * this.m_r;
			y2 = sina2 * this.m_r;

			x3 = cosa2 * (this.m_r+this.m_w);
			y3 = sina2 * (this.m_r+this.m_w);

			x4 = cosa * (this.m_r+this.m_w);
			y4 = sina * (this.m_r+this.m_w);

			gl.Quad(x1,y1,x2,y2,x3,y3,x4,y4);

			a+=this.m_astep;
		} 


		// Cap
		xc = 0.5*(x2+x3);
		yc = 0.5*(y2+y3);

		vx = this.M_norm({x:x3-x2,y:y3-y2});
		vy = {x:vx.y,y:-vx.x};

		xarrow = xc + vy.x*0.05;
		yarrow = yc + vy.y*0.05;

		gl.Triangle(x2,y2,x3,y3,xarrow,yarrow);

	}
}


// --------------------------------
// Plugin methods
// --------------------------------
function init()
{
	pad = 0.02;
	w	= 0.075;
	r	= 0.2;
	c1	= {r:130,g:254,b:254};
	c2	= {r:53,g:104,b:135};
	nb	= 8;
	rmax=10;
	rmin=10;
	astart = -90;

	this.arcs = new Arcs();
	for (var i=0 ; i<nb ; i++)
	{
		f = i/(nb-1);
		this.arcs.M_add(new Arc(r,w,astart,astart+(90-315)/(nb-1)*i + 315,(rmin-rmax)/(nb-1)*i+rmax, {r:((c2.r-c1.r)*f+c1.r)/255,g:((c2.g-c1.g)*f+c1.g)/255,b:((c2.b-c1.b)*f+c1.b)/255}));
		r += (w+pad);
		
	}
}

function prepare()
{
}

function update(dt)
{
	// Timer update
	this.t += dt;
	// Arcs
	this.arcs.M_update(dt);
}

function render()
{
	this.arcs.M_draw();
}






