///////////////////////////////////
// Spinner
//auteur: niCko
//copyright: ccmedia 2009
///////////////////////////////////
//utilisation avec jquery
//	var container = $("#online");
//	var spinner = new Spinner('online', container.css("color"), container.width(), container.height());
//	startSpinner(spinner);
//////////////////////////////////////

var G_vmlCanvasManager; // so non-IE won't freak out in canvasInit
/////////////////////////////////////////////
//Propriete de la classe Spinner
/////////////////////////////////////////////
Spinner.prototype._canvasID;//nom du canvas contenant le spinner
Spinner.prototype._canvasEl;//Element canvas contenant le spinner
Spinner.prototype._context;//context ou dessiner le spinner 
Spinner.prototype._numTicks;//nb de tick du spinner
Spinner.prototype._width;//lageur du canvas contenant le spinner
Spinner.prototype._height;//hauteur contenant le canvas
Spinner.prototype._cpt;//compteur de progression
Spinner.prototype._intervalID;//id de l'interval utilise pour la maj du dessin
Spinner.prototype._started;//boolean indiquant l'etat de l'interval
Spinner.prototype._color;//style decrivant la couleur du tick
Spinner.prototype._colors;//object decrivant la couleur du tick
Spinner.prototype._tickThickness;//epaisseur du tick
Spinner.prototype._contextAvailable;
///////////////////////////////////
//Constructeur
///////////////////////////////////
function Spinner(canvasID, color, canvasWidth, canvasHeight)
{
	this._contextAvailable = false ;
	this._canvasID = canvasID;
	this._canvasEl = document.getElementById(this._canvasID);
	if (G_vmlCanvasManager != undefined) { // ie IE
		G_vmlCanvasManager.initElement(this._canvasEl);
		//alert('IE Detected!');
	}
	if(this._canvasEl.getContext )
	{ 
		this._contextAvailable = true ;
		this._context = this._canvasEl.getContext('2d');
		this._color = $("#"+canvasID).css('color');
		this._colors = this.getColors();
		this._numTicks = 20;
		this._tickThickness = 3;
		this._width = document.getElementById(this._canvasID).width = $("#"+canvasID).width() ;
		this._height = document.getElementById(this._canvasID).height = $("#"+canvasID).height() ;
		this._size = Math.min(this._width, this._height);
		this._cpt = 0;
		this._intervalID = 0;
		this._started = false;
		//alert('['+canvasID+']Color'+this._color+" r:"+this._colors.r+" g:"+this._colors.g+" b:"+this._colors.b);
		//alert('['+canvasID+']Width='+this._width+" Height="+this._height);
	}
	else
	{
		//alert('error#0045675003!');
	}
}

/*
function Spinner(canvasID, color, canvasWidth, canvasHeight)
{
	this._canvasID = canvasID;
	this._canvasEl = document.getElementById(this._canvasID);
	if (G_vmlCanvasManager != undefined) { // ie IE
		G_vmlCanvasManager.initElement(this._canvasEl);
		//alert('IE Detected!');
	}

	this._context = this._canvasEl.getContext('2d');
	this._color = color;
	this._colors = this.getColors();
	this._numTicks = 20;
	this._tickThickness = 3;
	this._width = document.getElementById(this._canvasID).width = canvasWidth ;
	this._height = document.getElementById(this._canvasID).height = canvasHeight ;
	this._size = Math.min(this._width, this._height);
	this._cpt = 0;
	this._intervalID = 0;
	this._started = false;
	//alert('color'+this._color+" r:"+this._colors.r+" g:"+this._colors.g+" b:"+this._colors.b);
	//alert("Width="+this._width+" Height="+this._height);
}
*/
//////////////////////////////////////////////////////
//Fonctions
//////////////////////////////////////////////////////
Spinner.prototype.getContext = function ()
{
	return this._context;
}

Spinner.prototype.startUpdate = function ()
{
	//perd le pointeur sur this...
	var spinnerobj = this;
	function supdt(){spinnerobj.draw();alert('update!');};
	setInterval(supdt(), 1000);
	//update();
}

Spinner.prototype.getColors = function ()
{
	var strColor = this._color;
	var tab1 = strColor.split(')');
	if(tab1){
		if(tab1.length > 0)
		{
			var tab2 = tab1[0].split('(');
			if(tab2)
			{
				if(tab2[0].match(/rgb/i) && tab2.length > 1)
				{
					var tab3 = tab2[1].split(',');
					if(tab3){
						if(tab3.length == 3){
							//alert(' r'+tab3[0]+" g"+tab3[1]+' b'+tab3[2]);
							return {r:parseInt(tab3[0]),g:parseInt(tab3[1]),b:parseInt(tab3[2])};
						}
					}
				}
			}
		}
	}
	return {r:0,g:0,b:0};
}

Spinner.prototype.updatesize = function ()
{
	//this._width = document.getElementById(this._canvasID).width;// = canvasWidth ;
	//this._height = document.getElementById(this._canvasID).height;// = canvasHeight ;
	//alert("Width="+this._width+" Height="+this._height);
}

Spinner.prototype.draw = function ()
{
	this._context.clearRect(0,0,this._width, this._height); // clear canvas
	var i = 0;
	var radius = this._size / 2;
	var tickWidth = radius/4;
	var angle = 2 * Math.PI/ this._numTicks;
	var decalage = ( this._cpt % this._numTicks ) * angle;
	//alert('draw! '+this._cpt );
	this._context.translate(this._width/2, this._height/2);
	var r = this._colors.r;
	var g = this._colors.g;
	var b = this._colors.b;
	var p = 0;//p=0 transparent - p=1 opaque
	for (i; i <= this._numTicks; i++) {
		//c = Math.floor(255 / this._numTicks * i);
		//this._context.fillStyle = "rgb(" + c + "," + c + "," + c + ")";
		p = 1- ( i+1 ) / this._numTicks;
		this._context.fillStyle = "rgba("+r+", "+g+", "+b+", "+ p +")";
		this._context.fillRect(radius/2+ (1-p) * tickWidth, 0, tickWidth, this._tickThickness);
		//this._context.transform(cos, sin, -sin, cos, 0, 0);
		this._context.rotate(angle);
	}
	//Pour finir sur un tour tout rond
	this._context.rotate(-angle);
	this._context.rotate(-angle);
	(this._cpt)++;
	this._context.translate(-this._width/2, -this._height/2);
}
Spinner.prototype.isStarted = function ()
{
	return this._started ;
}
function startSpinner(spinner)
{
	if( !spinner._contextAvailable ) return ;

	if(spinner._started)
	{
		//
		return;
	}
	else
	{
		spinner.updatesize();
		function update(){spinner.draw();};
		spinner._intervalID = setInterval(update, 100);
		spinner._started = true;
	}
}
function stopSpinner(spinner)
{
	if( 'undefined' == typeof spinner )
	{ 
		alert('Try to stop an undefined spinner...('+typeof spinner+')');
	}
	else
	{
		if( !spinner._contextAvailable ) return ;
		clearInterval(spinner._intervalID);
		spinner._started = false;
	}
}
function isSpinnerStarted(spinner)
{
	if( 'undefined' == typeof spinner )
	{ 
		return false;
	}
	else return spinner.isStarted() ;
}

///////////////////////////////////
// Fin Spinner
///////////////////////////////////
