
var polar = Class.create({
	initialize: function(canvas, color) {

		this.opacity = 0.5;
		this.frameRate = 12;
		this.fgMod = 1;
		this.topMod = 1.4;
		this.btmMod = 0.8;
		
		this.color = this.hexToRgb(color);
		this.applyColor(color);
		
		//this.debug = $('debug').setStyle({color:'#FFFFFF'});
		
		this.canvas = $(canvas);
		this.canvasSetup();
		
		this.arcs = [];
		
		for(var i=0;i<(Math.random()*5+5);i++){
			this.arcs[i] = {
				wid:(Math.random()*100),
				len:(Math.random()*6),		// 0 to 6 radians
				rot:(Math.random()*6.28),	
				spd:(Math.random()/100),
				dist:(Math.random()*(this.dim.height-200)+60)
			};
		}
		
		Event.observe(document.onresize ? document : window, "resize", this.canvasSetup.bind(this));  

	},

	applyColor: function(color){
	
		var rgb = this.hexToRgb(color);
		this.color = rgb;
		this.fg = "rgba("+(this.checkColor(rgb[0]*this.fgMod))+", "+(this.checkColor(rgb[1]*this.fgMod))+", "+(this.checkColor(rgb[2]*this.fgMod))+", "+this.opacity+")";
		this.bgColorTop = "rgba("+(this.checkColor(rgb[0]*this.topMod))+", "+(this.checkColor(rgb[1]*this.topMod))+", "+(this.checkColor(rgb[2]*this.topMod))+", 1)";
		this.bgColorBtm = "rgba("+(this.checkColor(rgb[0]*this.btmMod))+", "+(this.checkColor(rgb[1]*this.btmMod))+", "+(this.checkColor(rgb[2]*this.btmMod))+", 1)";
		
	},
	
	checkColor:function(color){
		if(color > 255) return Math.round(255);
		else return Math.round(color);
	},
	
	canvasSetup: function(){
		this.dim = {width:document.viewport.getDimensions().width, height:document.viewport.getDimensions().height};
		this.canvas.writeAttribute({height:this.dim.height,width:this.dim.width});
		this.ctx = this.canvas.getContext('2d');
		this.ctx.save();
		this.ctx.translate(this.dim.width / 2, (this.dim.height / 2));
		this.ctx.rotate(-(90).toRadians());
		this.clearCanvas();
		this.ctx.save();
	},

	clearCanvas: function() {
		var grad = this.ctx.createLinearGradient(this.dim.height, 0, -(this.dim.height/2), 0);
		grad.addColorStop(0,this.bgColorTop);
		grad.addColorStop(0.4,this.bgColorTop);
		grad.addColorStop(1,this.bgColorBtm);
		this.ctx.fillStyle = grad;
		//this.ctx.strokeStyle = this.fg;
		this.ctx.fillRect(-(this.dim.height/2), -(this.dim.width/2), this.dim.height, this.dim.width);
		//if(this.arcs) this.debug.innerHTML = this.arcs[0].spd;
		//this.ctx.stroke();
	},

	setColor: function(color) {
		this.ctx.strokeStyle = color;
		this.ctx.fillStyle = color;
		
	},
		
	drawSolidArc: function(color, radius, width, radians, length) {
		this.setColor(color);
		var innerRadius = radius-width;
		this.ctx.beginPath();
		
		var sradians = radians-(length/2);
		var eradians = radians+(length/2);
		
		var coords = this.getArcCoords(radius, width, sradians);
		this.ctx.arc(0, 0, radius, eradians, sradians, false);
		this.ctx.lineTo(coords.x, coords.y);
		this.ctx.arc(0, 0, innerRadius, sradians, eradians, true);
		this.ctx.closePath();
		this.ctx.fill();
		//this.ctx[i].stroke();
		//this.debug.innerHTML = color;
		
	},

	getArcCoords: function(radius, width, radians) {
		var innerRadius = radius-width;
		return	{
			x: innerRadius * Math.cos(radians),
			y: innerRadius * Math.sin(radians)
		};
	},

	draw:function(){
		this.clearCanvas();
		this.arcs.each(function(arc){
			arc.rot = arc.rot + arc.spd;
			this.drawSolidArc(this.fg, arc.dist, arc.wid, arc.rot, arc.len);
		}.bind(this));
	},
	start:function(){
		if(!this.animator){
			this.animator = new PeriodicalExecuter(function(pe) {	 
				this.draw();
			}.bind(this), (1/this.frameRate));
		}
	},
	stop:function(){
		if(this.animator){
			this.animator.stop();
			delete this.animator;
		}
	},
	
	hexToRgb: function(hex){
		hex = hex.replace('#', '');
		var r = parseInt(hex.substr(0,2),16);
		var g = parseInt(hex.substr(2,2),16);
		var b = parseInt(hex.substr(4,2),16);
		return([r, g, b]);
	}
});

Object.extend(Number.prototype, {
	toRadians: function() {
	 		return (Math.PI / 180) * this;
	},
	wrap: function(min,max) { 
		if(this < min) return this + max;
		if(this > max) return this - max;
		return this;
	},
	toFixed:function(x) {
		var temp=this;
		temp=Math.round(temp*Math.pow(10,x))/Math.pow(10,x);
		return temp;
	}


});




