// Gradientz 0.2
// Replace your gradient images
// USE
// $(document).ready(function() {
//  $('#box1').gradientz({
//    start: "#fcc",
//    end: "yellow",
//    angle: 45
//  })
// For more information: see www.parkerfox.co.uk/labs/gradientz

;(function($){

  if($.browser.msie && document.namespaces["v"] == null) {
    document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
    var ss = document.createStyleSheet().owningElement;
    ss.styleSheet.cssText = "v\\:*{behavior:url(#default#VML);}";
  }
  
  function vmlGradient(angle, colorStart, colorEnd, width, height, distance) {
    var html ='<v:rect style="position:absolute; top:0px; left: 0px; z-index: -9000; width:' + width + ";height:" + height + ';" stroked="false"  fillcolor="' + colorEnd + '" >';
    html += '<v:fill method="sigma"  color2="' + colorStart + '" type="gradient" angle="' + angle + '">' ;
    html += '</v:rect>';
    return html;
  }
  
  function canvasGradient(angle, colorStart, colorEnd, width, height, distance) {
    var canvas=$("<canvas width="+width+"px height="+ height +"px style='position:absolute; z-index: -9000; left:0px; top:0px;'></canvas>");
    var ctx=canvas[0].getContext('2d');
    
    var x = Math.sin(angle) * distance;
    var y = Math.cos(angle) * distance;
    
    var lingrad = ctx.createLinearGradient(0,0,x,y);
    
    lingrad.addColorStop(0, colorStart);
    lingrad.addColorStop(1, colorEnd);
    
    ctx.fillStyle = lingrad;
    ctx.fillRect(0,0,width,height);
    return canvas;
  }
  
  $.fn.gradientz = function(options){
    
      var settings = {
        angle : 0,
        start: "white",
        end: "black"
        };
      $.extend(settings, options || {});
    
      return this.each(function() {

        var $$ = $(this);
        
        if(this.style.position != "absolute") {
          this.style.position = "relative";
          this.style.zoom = 1; // give layout in IE
        }
          
        var width = $$.innerWidth();
        var height = $$.innerHeight();
        
        var distance = (settings.angle == 0) ? height : width;

        if($.browser.msie) {//need to use innerHTML rather than jQuery
          var h = vmlGradient(settings.angle, settings.start, settings.end, width, height, distance) ;    
          this.innerHTML += h;
        }
        else  //canvasGradient returns a DOM element
          $$.append(canvasGradient(settings.angle * Math.PI / 180, settings.start, settings.end, width, height, distance));
      })  
    }
  })(jQuery);

  
