/*  Spinners 1.2.1.1
 *  (c) 2010-2011 Nick Stakenburg - http://www.nickstakenburg.com
 *
 *  Spinners is freely distributable under the terms of an MIT-style license.
 *
 *  Works with your framework of choice using BridgeJS:
 *  http://www.github.com/staaky/bridgejs
 *
 *  Requires ExplorerCanvas to work in Internet Explorer:
 *  http://code.google.com/p/explorercanvas
 */

var Spinners = {
  Version: '1.2.1.1'
};

(function(d){function b(g,f){g=d.$(g);!g||(this.element=g.source,Spinners.remove(g),Spinners.removeDetached(),this.options=d.Object.extend({radii:[5,10],color:"#000",dashWidth:1.8,dashes:12,opacity:1,padding:3,speed:0.7,build:true},f),this._position=0,this._state="stopped",this.options.build&&this.build(),Spinners.add(this))}function c(f){return f*Math.PI/180}d.Object.extend(Spinners,{spinners:[],Required:{Bridge:"1.0_alpha2"},support:{canvas:!!document.createElement("canvas").getContext},insertScript:function(g){try{document.write("<script type='text/javascript' src='"+g+"'><\/script>")}catch(f){Bridge.$$("head").source[0].appendChild(new Bridge.Element("script",{src:g,type:"text/javascript"}))}},require:function(g,f){if(typeof window[g]=="undefined"||this.convertVersionString(window[g].Version)<this.convertVersionString(this.Required[g])){throw"Spinners requires "+(f||g)+" >= "+this.Required[g]}},convertVersionString:function(g){var f=g.replace(/_.*|\./g,"");f=parseInt(f+Bridge.String.times("0",4-f.length));return g.indexOf("_")>-1?f-1:f},start:function(){this.require("Bridge");!this.support.canvas&&!window.G_vmlCanvasManager&&alert("Spinners requires ExplorerCanvas (excanvas.js)")},get:function(g){if(g=d.$(g).source){var f=null;d._each(this.spinners,function(h){h.element==g&&(f=h)});return f}},add:function(f){this.spinners.push(f)},remove:function(f){(f=this.get(f))&&(f.remove(),this.spinners=d.Array.without(this.spinners,f))},removeDetached:function(){return function(){d.each(this.spinners,function(g){if(g.element){var f;for(f=g.element;f&&f.parentNode;){f=f.parentNode}f=f;(!f||!f.body)&&this.remove(g.element)}},this)}}()});var a={drawRoundedRectangle:function(n,g){var l=d.Object.extend({top:0,left:0,width:0,height:0,radius:0},g||{}),i=l.left,m=l.top,k=l.width,j=l.height;l=l.radius;n.beginPath();n.moveTo(i+l,m);n.arc(i+k-l,m+l,l,c(-90),c(0),false);n.arc(i+k-l,m+j-l,l,c(0),c(90),false);n.arc(i+l,m+j-l,l,c(90),c(180),false);n.arc(i+l,m+l,l,c(-180),c(-90),false);n.closePath();n.fill()}},e=function(){function h(i){var j=[];i.indexOf("#")==0&&(i=i.substring(1));i=i.toLowerCase();if(i.replace(f,"")!=""){return null}i.length==3?(j[0]=i.charAt(0)+i.charAt(0),j[1]=i.charAt(1)+i.charAt(1),j[2]=i.charAt(2)+i.charAt(2)):(j[0]=i.substring(0,2),j[1]=i.substring(2,4),j[2]=i.substring(4));for(i=0;i<j.length;i++){j[i]=parseInt(j[i],16)}j.red=j[0];j.green=j[1];j.blue=j[2];return j}var f=RegExp("[0123456789abcdef]","g"),g=function(){function i(l,k,j){l=l.toString(j||10);return d.String.times("0",k-l.length)+l}return function(l,k,j){return"#"+i(l,2,16)+i(k,2,16)+i(j,2,16)}}();return{hex2rgb:h,hex2fill:function(i,l){d.Object.isUndefined(l)&&(l=1);var k=l,j=h(i);j[3]=k;j.opacity=k;return"rgba("+j.join()+")"},rgb2hex:g}}();d.Object.extend(b.prototype,function(){function f(){var g=this.options.speed*1000/this.options.dashes;this.nextPosition();this._playTimer=window.setTimeout(d.Function.bind(f,this),g)}return{remove:function(){!this.canvas||(this.stop(),this.canvas.remove(),this.canvas=null,this.ctx=null)},build:function(){this.remove();var g=this.getLayout().workspace.radius,h=g*2;d.$(this.element).insert(this.canvas=(new d.Element("canvas",{height:h,width:h})).setStyle({zoom:1}));window.G_vmlCanvasManager&&G_vmlCanvasManager.initElement(this.canvas.source);this.ctx=this.canvas.source.getContext("2d");this.ctx.globalAlpha=this.options.opacity;this.ctx.translate(g,g);this.drawPosition(0);return this},getLayout:function(){if(this._layout){return this._layout}var r=this.options,p=r.dashes,s=r.radii,q=r.dashWidth,n=Math.min(s[0],s[1]);s=Math.max(s[0],s[1]);var m=Math.ceil(Math.max(Math.max(q,s),Math.sqrt(s*s+q/2*(q/2))));m+=r.padding;r=m;for(var g=1/p,i=[],k=0;k<p;k++){i.push((k+1)*g)}return this._layout=p={workspace:{radius:r,opacities:i},dash:{position:{top:m-s,left:m-q/2},dimensions:{width:q,height:s-n}}}},_nextPosition:f,nextPosition:function(){this._position==this.options.dashes-1&&(this._position=-1);this._position++;this.drawPosition(this._position)},drawPosition:function(g){var j=this.getLayout().workspace,h=j.radius*2,k=-1*j.radius,i=this.options.dashes;this.ctx.clearRect(k,k,h,h);h=c(360/i);j=j.opacities;if(g=g*-1){k=j.slice(0,g);g=j.slice(g,j.length).concat(k)}else{g=j}for(j=0;j<i;j++){this.drawDash(g[j],this.options.color);this.ctx.rotate(h)}},drawDash:function(g,j){this.ctx.fillStyle=e.hex2fill(j,g);var h=this.getLayout(),k=h.workspace.radius,i=h.dash.position;h=h.dash.dimensions;a.drawRoundedRectangle(this.ctx,{top:i.top-k,left:i.left-k,width:h.width,height:h.height,radius:Math.min(h.height,h.width)/2})},play:function(){if(this._state!="playing"){this._state="playing";var g=this.options.speed*1000/this.options.dashes;this._playTimer=window.setTimeout(d.Function.bind(f,this),g);return this}},pause:function(){if(this._state!="paused"){this._pause();this._state="paused";return this}},_pause:function(){!this._playTimer||(window.clearTimeout(this._playTimer),this._playTimer=null)},stop:function(){if(this._state!="stopped"){this._pause();this._position=0;this.drawPosition(0);this._state="stopped";return this}},toggle:function(){this[this._state=="playing"?"pause":"play"]();return this}}}());window.Spinner=b;Spinners.start()})(Bridge);
