(function( $ ){
  $.fn.lvhCircle = function(method) {
  
    var interval, c1, c2;
    var stop = 0;
    var end = 0.95;
  
    var settings = {
      'colors' : ['#00FF00', '#FF0000', '#0000FF', '#00FF00'],
      'background' : '#FFFFFF',
      'animate' : true,
      
      'step':0.001,
      'rate':100
      
    };
    
    
    var methods = {
      
      init:function(options){
        
        if ( options ) { 
          $.extend( settings, options );
        }
        c1 = settings.colors[0];
        c2 = settings.colors[1];
       
        
        if(settings.animate){
          setTimeout(rescope(this, methods.update), settings.rate);
        }else{
          methods.update();
        }
        return this;
        
      },
      
      update:function(){
         
        /*
        if(stop >= end || stop < 0){
          if(stop < 0) stop = 0;
          if(stop > end) stop = end;
          settings.step = -settings.step;
        }*/
          if(stop >= end){
            stop = 0;
            
            
            for(var i = 0; i < settings.colors.length-1; i+=2){
              var cc = settings.colors[i+1];
              settings.colors[i+1] = settings.colors[i];
              settings.colors[i] = cc;
            }
         
         
          }
          var i = 0;
        
                    
          this.each(function(){
            
            if(i > 0)var b = i+1;
            else var b = i;
          
            var stops = { 0:[0, settings.colors[b]], 1:[stop, settings.colors[b]], 2:[end, settings.colors[b+1]],
                    3:[0.99,settings.background],4:[1,settings.background]
                    };
            $this =$(this);
            
            
                var ctx = this.getContext("2d");
             
            var halfWidth =  $this.width()/2;
            var halfHeight =  $this.height()/2;

            var grad = ctx.createRadialGradient(
                    halfWidth, halfHeight, 0,
                    halfWidth, halfHeight, halfWidth);
        
            for (var id in stops) {
              var position = stops[id][0];
              var color = stops[id][1];

              grad.addColorStop(position, color);
            }

            // draw gradient
            ctx.fillStyle = grad;
            ctx.fillRect(0, 0, $this.width(), $this.height());
            i++;
          });
       
        stop+=settings.step;
        if(settings.animate)setTimeout(rescope(this, methods.update), settings.rate);
        return this;
        
      }
      
    };
    
    
    
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.lvhCircle' );
    }    
  
  };
    
})( jQuery );
