/**
 * jCorners - Cross-Browser Corners with JQuery
 *   http://jcorners.culturezoo.com/
 *
 * Copyright (c) 2008 Levi Nunnink, Culturezoo, LLC (http://culturezoo.com)
 * Licensed under the GPL
 *
 * Built on top of the jQuery library
 *   http://jquery.com
 *
 * Inspired by the "Corners Experiment" by Jonathan Snook
 *   http://snook.ca/archives/html_and_css/rounded_corners_experiment_ie/
 */

var defaults = {
	radius: 5,
	debugie: false
};
jQuery.fn.jcorners = function(o) {
	return this.each(function() {
		new $jc(this, o);
	});
};
jQuery.jcorners = function(e, o) {
	this.options = $.extend({}, defaults, o || {});
	if(this.browser.msie || this.options.debugie){
		this("body").prepend("<xml:namespace ns='urn:schemas-microsoft-com:vml' prefix='v' />");
		if($(e).css("background-color") != undefined){
			var bg = $(e).css("background-color");
		}else{
			var bg = "white";
		}

/* This creates a structure like so:
<div> //wrapOut (block level)
	<v> //wrapIn (inline-block)
		<original tag>

	The padding originally on <original> is moved to $wrapIn
*/

		var guid = this.guid();
		var guid_out = this.guid();

		var padding_l = this.intval(this(e).css("padding-left"));
                var padding_r = this.intval(this(e).css("padding-right"));

		var $e = this(e);

		var arc = (this.options.radius / this(e).height())*2;

		
		this(e).wrap("<v:roundrect arcsize='"+arc+"' class='corner-wrapper' id='wrapper-"+guid+"' fillcolor='"+bg+"' strokecolor='"+bg+"'></v:roundrect>");

		var $wrapIn = this("#wrapper-"+guid+"");

		$wrapIn.wrap("<div class='corner-wrapper' id='wrapper-"+guid_out+"'></div>");

		var $wrapOut = this("#wrapper-"+guid_out+"");

		$wrapIn.css("behavior","url(#default#VML)");
		$wrapIn.css("background-color","transparent");


                this(e).css("padding-left", "0");
                this(e).css("padding-right", "0");

		$wrapIn.css("padding-left",+padding_l+"px");
                $wrapIn.css("padding-right",+padding_r+"px");

		$wrapIn.css("height","100%");
		$wrapIn.css("border-color:",bg);
		$wrapIn.css("display","inline-block");

		$wrapOut.css("display", "block");
                $wrapOut.css("margin-left", $e.css("margin-left"));
                $wrapOut.css("margin-right", $e.css("margin-right"));

                $wrapIn.css("width",this(e).width()+"px");
                $wrapOut.css("width",this(e).width()+"px");

	}else if(this.browser.mozilla){
		this(e).css("-moz-border-radius",this.options.radius+"px");
	}else if(this.browser.safari){
		this(e).css("-webkit-border-radius",this.options.radius+"px");
	}else{
		this(e).css("border-radius",this.options.radius+"px");
	}
	return this;
}
jQuery.extend({
	intval: function(v) {
        v = parseInt(v);
        return isNaN(v) ? 0 : v;
    },
    guid: function(){
		var result, i, j;
		result = '';
		for(j=0; j<32; j++)
		{
			if( j == 8 || j == 12|| j == 16|| j == 20)
			result = result + '-';
			i = Math.floor(Math.random()*16).toString(16).toUpperCase();
			result = result + i;
		}
		return result
	} 
});


