/*
  JACG: Just Another Corners&Gradients.
  Requires jQuery (tested on 1.4.1, but no new features used).
  Optionally uses base64 ( http://plugins.jquery.com/project/base64 ).

  Copyright 2010 MyFreeWeb
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
  http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
function svgradient(start, end, stround, horiz) {
  if (typeof(stround) != 'undefined') {
    stround.replace('px', '');
    var r = 'rx="' + stround + '" ry="' + stround + '"';
  }
  else { var r = ''; }
  if (horiz == true) {
    var dir = 'x1="0%" y1="0%" x2="100%" y2="0%"';
  }
  else {
    var dir = 'x1="0%" y1="0%" x2="0%" y2="100%"';
  }
  var svg = '<?xml version="1.0" ?><svg xmlns="http://www.w3.org/2000/svg"><defs><linearGradient id="gr" ' + dir + '><stop offset="0%" style="stop-color:' +  start + '"/><stop offset="100%" style="stop-color:' + end + '"/></linearGradient></defs><rect x="0" y="0" ' + r + ' width="100%" height="100%" style="fill:url(#gr)"/></svg>';
  if (jQuery.base64Encode) {
    return 'data:image/svg+xml;base64,' + jQuery.base64Encode(svg);
  }
  else {
    return 'data:image/svg+xml,' + escape(svg);
  }
}
function webgr(obj, start, end, height, padding) { // It doesn't support horizontal gradients for now...
  he = parseInt(height+''.replace('px', '')) + parseInt(padding+''.replace('px', ''))*2; // This is strange, really. But I have to.
  return obj.css({'background':
      'url("http://webgradients.appspot.com/make?width=1&height=' + he + '&start=' + start.replace('#', '') +'&end=' + end.replace('#', '') + '") repeat-x'});
}
(function($){  
  $.fn.extend({
    jacg: function(options) {
	var defaults = {
	radius: '15px',
	start: '#dddddd',
	end: '#cccccc',
	gradient: true,
        horizontal: false,
	topleft: true,
	topright: true,
	bottomleft: true,
	bottomright: true,
	opera_prefer: 'image' // image or corners
	}
	var o = $.extend(defaults, options);
	return this.each(function() {
	    if (o.topleft == false) { tl = '0' } else if (o.topleft == true) { tl = o.radius }
	    if (o.topright == false) { tr = '0' } else if (o.topright == true) { tr = o.radius }
	    if (o.bottomleft == false) { bl = '0' } else if (o.bottomleft == true) { bl = o.radius }
	    if (o.bottomright == false) { br = '0' } else if (o.bottomright == true) { br = o.radius }
	    radius = [tl, tr, br, bl].join(' ');
	    //console.log(radius);
	    obj = $(this).css({'border-radius': radius}); // I can haz chains!

	    if ($.browser.webkit == true) { // WebKit: Safari, Chrome, Midori, etc.
		obj.css('-webkit-border-radius', radius);
		if (o.gradient == true) {
		    if (o.horizontal == true) {
			var dir = 'left top, right top';
		    }
		    else {
			var dir = 'left top, left bottom';
		    }
		    obj.css('background', '-webkit-gradient(linear, ' + dir + ', from(' + o.start + '), to(' + o.end + '))');
		}
	    }
	    else if ($.browser.mozilla == true) { // Gecko: Firefox, Flock, Conkeror, etc.
		obj.css('-moz-border-radius', radius);
		if (o.gradient == true) {
		    if (parseInt($.browser.version.replace('.', '').replace('.', '')) < 192) { // Gecko 1.9.2 is in Fx 3.6
			// Firefox <= 3.5 has no support for CSS gradients
			// So we use Web Gradients (that's my app too!)
			webgr(obj, o.start, o.end, obj.css('height'), obj.css('padding-bottom'));
		    }
		    else {
			if (o.horizontal == true) {
			    var dir = 'left';
			}
			else {
			    var dir = 'top';
			}
			obj.css('background', '-moz-linear-gradient(' + dir + ', ' + o.start + ', ' + o.end + ')');
		    }
		}
	    }
	    else if ($.browser.opera == true) { // Presto: Opera
		if (opera.version() >= 10.50) {
		    if (o.gradient == true) {
			obj.css({'background': 'url("' + svgradient(o.start, o.end, undefined, o.horizontal) + '")'});
		    }
		    // The corners are specified at top of the fuction
		}
		else {
		    if (o.gradient == true) {
			obj.css({'background': 'url("' + svgradient(o.start, o.end, o.radius, o.horizontal) + '")'});
		    }
		    else if (o.opera_prefer == 'corners') {
			bgcolor = obj.css('background-color');
			obj.css({'background': 'url("' + svgradient(bgcolor, bgcolor, o.radius) + '")'});	
		    }
		}
	    }
	    else { // Any other browser
		if (o.gradient == true) {
		    webgr(obj, o.start, o.end, obj.height(), obj.css('padding-bottom'));
		}
	    }
	  });
    }
  });
})(jQuery);

