/*!
 * jQuery corner plugin: simple corner rounding
 * Examples and documentation at: http://jquery.malsup.com/corner/
 * version 2.03 (05-DEC-2009)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */

/**
 *  corner() takes a single string argument:  $('#myDiv').corner("effect corners width")
 *
 *  effect:  name of the effect to apply, such as round, bevel, notch, bite, etc (default is round).
 *  corners: one or more of: top, bottom, tr, tl, br, or bl.
 *           by default, all four corners are adorned.
 *  width:   width of the effect; in the case of rounded corners this is the radius.
 *           specify this value using the px suffix such as 10px (and yes, it must be pixels).
 *
 * @author Dave Methvin (http://methvin.com/jquery/jq-corner.html)
 * @author Mike Alsup   (http://jquery.malsup.com/corner/)
 */
;(function($) {

var ua = navigator.userAgent;
var moz = $.browser.mozilla && /gecko/i.test(ua);
var webkit = $.browser.safari && /Safari\/[5-9]/.test(ua);

var expr = $.browser.msie && (function() {
    var div = document.createElement('div');
    try { div.style.setExpression('width','0+0'); div.style.removeExpression('width'); }
    catch(e) { return false; }
    return true;
})();

function sz(el, p) {
    return parseInt($.css(el,p))||0;
};
function hex2(s) {
    var s = parseInt(s).toString(16);
    return ( s.length < 2 ) ? '0'+s : s;
};
function gpc(node) {
    for ( ; node && node.nodeName.toLowerCase() != 'html'; node = node.parentNode ) {
        var v = $.css(node,'backgroundColor');
        if (v == 'rgba(0, 0, 0, 0)')
            continue; // webkit
        if (v.indexOf('rgb') >= 0) {
            var rgb = v.match(/\d+/g);
            return '#'+ hex2(rgb[0]) + hex2(rgb[1]) + hex2(rgb[2]);
        }
        if ( v && v != 'transparent' )
            return v;
    }
    return '#ffffff';
};

function getWidth(fx, i, width) {
    switch(fx) {
    case 'round':  return Math.round(width*(1-Math.cos(Math.asin(i/width))));
    case 'cool':   return Math.round(width*(1+Math.cos(Math.asin(i/width))));
    case 'sharp':  return Math.round(width*(1-Math.cos(Math.acos(i/width))));
    case 'bite':   return Math.round(width*(Math.cos(Math.asin((width-i-1)/width))));
    case 'slide':  return Math.round(width*(Math.atan2(i,width/i)));
    case 'jut':    return Math.round(width*(Math.atan2(width,(width-i-1))));
    case 'curl':   return Math.round(width*(Math.atan(i)));
    case 'tear':   return Math.round(width*(Math.cos(i)));
    case 'wicked': return Math.round(width*(Math.tan(i)));
    case 'long':   return Math.round(width*(Math.sqrt(i)));
    case 'sculpt': return Math.round(width*(Math.log((width-i-1),width)));
    case 'dog':    return (i&1) ? (i+1) : width;
    case 'dog2':   return (i&2) ? (i+1) : width;
    case 'dog3':   return (i&3) ? (i+1) : width;
    case 'fray':   return (i%2)*width;
    case 'notch':  return width;
    case 'bevel':  return i+1;
    }
};

$.fn.Bcorner = function(options) {
    // in 1.3+ we can fix mistakes with the ready state
	if (this.length == 0) {
        if (!$.isReady && this.selector) {
            var s = this.selector, c = this.context;
            $(function() {
                $(s,c).Bcorner(options);
            });
        }
        return this;
	}

    return this.each(function(index){
		var $this = $(this);
		var o = [ options || '', $this.attr($.fn.Bcorner.defaults.metaAttr) || ''].join(' ').toLowerCase();
		//var o = (options || $this.attr($.fn.corner.defaults.metaAttr) || '').toLowerCase();
		var keep = /keep/.test(o);                       // keep borders?
		var cc = ((o.match(/cc:(#[0-9a-f]+)/)||[])[1]);  // corner color
		var sc = ((o.match(/sc:(#[0-9a-f]+)/)||[])[1]);  // strip color
		var width = parseInt((o.match(/(\d+)px/)||[])[1]) || 10; // corner width
		var re = /round|bevel|notch|bite|cool|sharp|slide|jut|curl|tear|fray|wicked|sculpt|long|dog3|dog2|dog/;
		var fx = ((o.match(re)||['round'])[0]);
		var edges = { T:0, B:1 };
		var opts = {
			TL:  /top|tl|left/.test(o),       TR:  /top|tr|right/.test(o),
			BL:  /bottom|bl|left/.test(o),    BR:  /bottom|br|right/.test(o)
		};
		if ( !opts.TL && !opts.TR && !opts.BL && !opts.BR )
			opts = { TL:1, TR:1, BL:1, BR:1 };

		// support native rounding
		if ($.fn.Bcorner.defaults.useNative && fx == 'round' && (moz || webkit) && !cc && !sc) {
			if (opts.TL)
				$this.css(moz ? '-moz-border-radius-topleft' : '-webkit-border-top-left-radius', width + 'px');
			if (opts.TR)
				$this.css(moz ? '-moz-border-radius-topright' : '-webkit-border-top-right-radius', width + 'px');
			if (opts.BL)
				$this.css(moz ? '-moz-border-radius-bottomleft' : '-webkit-border-bottom-left-radius', width + 'px');
			if (opts.BR)
				$this.css(moz ? '-moz-border-radius-bottomright' : '-webkit-border-bottom-right-radius', width + 'px');
			return;
		}

		var strip = document.createElement('div');
		strip.style.overflow = 'hidden';
		strip.style.height = '1px';
		strip.style.backgroundColor = sc || 'transparent';
		strip.style.borderStyle = 'solid';

        var pad = {
            T: parseInt($.css(this,'paddingTop'))||0,     R: parseInt($.css(this,'paddingRight'))||0,
            B: parseInt($.css(this,'paddingBottom'))||0,  L: parseInt($.css(this,'paddingLeft'))||0
        };

        if (typeof this.style.zoom != undefined) this.style.zoom = 1; // force 'hasLayout' in IE
        if (!keep) this.style.border = 'none';
        strip.style.borderColor = cc || gpc(this.parentNode);
        var cssHeight = $.curCSS(this, 'height');

        for (var j in edges) {
            var bot = edges[j];
            // only add stips if needed
            if ((bot && (opts.BL || opts.BR)) || (!bot && (opts.TL || opts.TR))) {
                strip.style.borderStyle = 'none '+(opts[j+'R']?'solid':'none')+' none '+(opts[j+'L']?'solid':'none');
                var d = document.createElement('div');
                $(d).addClass('jquery-corner');
                var ds = d.style;

                bot ? this.appendChild(d) : this.insertBefore(d, this.firstChild);

                if (bot && cssHeight != 'auto') {
                    if ($.css(this,'position') == 'static')
                        this.style.position = 'relative';
                    ds.position = 'absolute';
                    ds.bottom = ds.left = ds.padding = ds.margin = '0';
                    if (expr)
                        ds.setExpression('width', 'this.parentNode.offsetWidth');
                    else
                        ds.width = '100%';
                }
                else if (!bot && $.browser.msie) {
                    if ($.css(this,'position') == 'static')
                        this.style.position = 'relative';
                    ds.position = 'absolute';
                    ds.top = ds.left = ds.right = ds.padding = ds.margin = '0';

                    // fix ie6 problem when blocked element has a border width
                    if (expr) {
                        var bw = sz(this,'borderLeftWidth') + sz(this,'borderRightWidth');
                        ds.setExpression('width', 'this.parentNode.offsetWidth - '+bw+'+ "px"');
                    }
                    else
                        ds.width = '100%';
                }
                else {
                	ds.position = 'relative';
                    ds.margin = !bot ? '-'+pad.T+'px -'+pad.R+'px '+(pad.T-width)+'px -'+pad.L+'px' :
                                        (pad.B-width)+'px -'+pad.R+'px -'+pad.B+'px -'+pad.L+'px';
                }

                for (var i=0; i < width; i++) {
                    var w = Math.max(0,getWidth(fx,i, width));
                    var e = strip.cloneNode(false);
                    e.style.borderWidth = '0 '+(opts[j+'R']?w:0)+'px 0 '+(opts[j+'L']?w:0)+'px';
                    bot ? d.appendChild(e) : d.insertBefore(e, d.firstChild);
                }
            }
        }
    });
};

$.fn.uncorner = function() {
	if (moz || webkit)
		this.css(moz ? '-moz-border-radius' : '-webkit-border-radius', 0);
	$('div.jquery-corner', this).remove();
	return this;
};

// expose options
$.fn.Bcorner.defaults = {
	useNative: true, // true if plugin should attempt to use native browser support for border radius rounding
	metaAttr:  'data-corner' // name of meta attribute to use for options
};

})(jQuery);

/*

	JQuery Curvy Corners by Mike Jolley
	http://blue-anvil.com
	http://code.google.com/p/jquerycurvycorners/
	------------
	Version 2.0.2 Beta 3

	Original by: Terry Riegel, Cameron Cooke and Tim Hutchison
	Website: http://www.curvycorners.net

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

/*
Differences from offical CC:
	- Boxes are not fixed size and will auto size to content
	- Backgrounds aligned to bottom won't work correctly so align to top if you need to tile

Usage:
	Will now autoMagically apply borders via the CSS declarations
	Safari, Mozilla, and Chrome all support rounded borders via

	-webkit-border-radius, and -moz-border-radius

	So to make curvycorners work with any major browser simply add the following
	CSS declarations and it should be good to go...

	.round {
		-webkit-border-radius: 25px;
	    -moz-border-radius: 25px;
	    CCborderRadius: 25px;
	}

You can still use the direct syntax:

	$('.myBox').corner();

Add the styles in the page itself to support Opera!

*/
function styleit()
{
	for(var t = 0; t < document.styleSheets.length; t++)
	{
		var theRules = new Array();
		theRules = document.styleSheets[t].rules;

		for(var i = 0; i < theRules.length; i++)
		{

			var allR = theRules[i].style.CCborderRadius    || 0;
			var tR   = theRules[i].style.CCborderRadiusTR  || allR;
			var tL   = theRules[i].style.CCborderRadiusTL  || allR;
			var bR   = theRules[i].style.CCborderRadiusBR  || allR;
			var bL   = theRules[i].style.CCborderRadiusBL  || allR;

			if (allR || tR || tR || bR || bL)
			{
				var s = theRules[i].selectorText;

				var settings = {
					tl: { radius: makeInt(tL) },
					tr: { radius: makeInt(tR) },
					bl: { radius: makeInt(bL) },
					br: { radius: makeInt(bR) },
					antiAlias: true,
					autoPad: true,
					validTags: ["div"]
				};

				$(s).corner(settings);

			}
		}
	}
}

// static class function to determine if the sheet is worth parsing
function opera_contains_border_radius(sheetnumber) {
  return /border-((top|bottom)-(left|right)-)?radius/.test(document.styleSheets.item(sheetnumber).ownerNode.text);
}

function makeInt(num) {
	var re = new RegExp('([0-9]*)');
	var i = 0;
	if(isNaN(num)) {
		var a = re.exec(num);
		if(!isNaN(parseInt(a[1]))) {
			i = a[1];
		}
	}
	else {
		i = num;
	}
	return i;
}

(function($) {

	$(function(){
		if ($.browser.msie) {
			styleit();
		} else if ($.browser.opera) {

      		for (t = 0; t < document.styleSheets.length; ++t) {

        		if (opera_contains_border_radius(t)) {

					var txt = document.styleSheets.item(t).ownerNode.text;
					txt = txt.replace(/\/\*(\n|\r|.)*?\*\//g, ''); // strip comments
					// this pattern extracts all border-radius-containing rulesets
					// matches will be:
					// [0] = the whole lot
					// [1] = the selector text
					// [2] = all the rule text between braces
					// [3] = top/bottom and left/right parts if present (only if webkit/CSS3)
					// [4] = top|bottom
					// [5] = left|right
					// .. but 3..5 are useless as they're only the first match.
					var pat = new RegExp("^([\\w.#][\\w.#, ]+)[\\n\\s]*\\{([^}]+border-((top|bottom)-(left|right)-)?radius[^}]*)\\}", "mg");
					var matches;
					while ((matches = pat.exec(txt)) !== null) {
						var pat2 = new RegExp("(..)border-((top|bottom)-(left|right)-)?radius:\\s*([\\d.]+)(in|em|px|ex|pt)", "g");
						var submatches;
						while ((submatches = pat2.exec(matches[2])) !== null) {
						    if (submatches[1] !== "z-") {
						    	var tL,tR,bL,bR, tLu,tRu,bLu,bRu;
								if (!submatches[3]) { // no corner specified
									tL = tR = bL = bR = parseInt(submatches[5]);
									//tLu = tRu = bLu = bRu = submatches[6];
								}
								else { // corner specified
									propname = submatches[3].charAt(0) + submatches[4].charAt(0);
									this[propname + 'R'] = parseInt(submatches[5]);
									//this[propname + 'u'] = submatches[6];
								}
								var settings = {
									tl: { radius: tL },
									tr: { radius: tR },
									bl: { radius: bL },
									br: { radius: bR }
								};
								$(matches[1]).corner(settings);
						    }
						}
					}
				}
   			}
   		}
	});

	$.fn.corner = function(options) {

		var settings = {
		  tl: { radius: 8 },
		  tr: { radius: 8 },
		  bl: { radius: 8 },
		  br: { radius: 8 },
		  antiAlias: true,
		  autoPad: true,
		  validTags: ["div"]
		};
		if ( options && typeof(options) != 'string' )
			$.extend(settings, options);

		return this.each(function() {
			if (!$(this).is('.hasCorners')) {
				applyCorners(this, settings);
			}
		});

  		// Apply the corners to the passed object!
		function applyCorners(box,settings)
		{

			// Setup Globals
			var $$ 						= $(box);
			this.topContainer 			= null;
			this.bottomContainer 		= null;
			this.shell            		= null;
			this.masterCorners 			= new Array();
			this.contentDIV 				= null;

			// Get CSS of box and define vars

			// Background + box colour
			this.x_bgi 				= $$.css("backgroundImage");																// Background Image
			this.x_bgi				= (x_bgi != "none" && x_bgi!="initial") ? x_bgi : "";
			this.x_bgr 				= $$.css("backgroundRepeat");																// Background repeat
			this.x_bgposX			= strip_px($$.css("backgroundPositionX")) ? strip_px($$.css("backgroundPositionX")) : 0; 	// Background position
			this.x_bgposY			= strip_px($$.css("backgroundPositionY")) ? strip_px($$.css("backgroundPositionY")) : 0; 	// Background position
			this.x_bgc 				= format_colour($$.css("backgroundColor"));													// Background Colour

			// Dimensions + positioning
			var x_height 			= $$.css("height");		 					// Height

			if(typeof x_height == 'undefined') x_height = 'auto';

			this.x_height       	= parseInt(((x_height != "" && x_height != "auto" && x_height.indexOf("%") == -1)? x_height.substring(0, x_height.indexOf("px")) : box.offsetHeight));

			if($.browser.msie && $.browser.version==6)
			{
				this.x_width	 		= strip_px(box.offsetWidth);
			}
			else
			{
				this.x_width	 		= strip_px($$.css('width'));
			}

			this.xp_height      	= strip_px($$.parent().css("height")) ? strip_px($$.css("height")) : 'auto';				// Parent height

			// Borders
			this.x_bw		     	= strip_px($$.css("borderTopWidth")) ? strip_px($$.css("borderTopWidth")) : 0; 				// Border width

			// Different widths messed up borders
			this.x_bbw		     	= strip_px($$.css("borderBottomWidth")) ? strip_px($$.css("borderBottomWidth")) : 0; 		// Bottom Border width
			this.x_tbw		     	= strip_px($$.css("borderTopWidth")) ? strip_px($$.css("borderTopWidth")) : 0; 				// Top Border width
			this.x_lbw		     	= strip_px($$.css("borderLeftWidth")) ? strip_px($$.css("borderLeftWidth")) : 0; 			// Left Border width
			this.x_rbw		     	= strip_px($$.css("borderRightWidth")) ? strip_px($$.css("borderRightWidth")) : 0; 			// Right Border width

			/*this.x_bbw		     	= strip_px($$.css("borderTopWidth")) ? strip_px($$.css("borderTopWidth")) : 0;
			this.x_tbw		     	= strip_px($$.css("borderTopWidth")) ? strip_px($$.css("borderTopWidth")) : 0;
			this.x_lbw		     	= strip_px($$.css("borderTopWidth")) ? strip_px($$.css("borderTopWidth")) : 0;
			this.x_rbw		     	= strip_px($$.css("borderTopWidth")) ? strip_px($$.css("borderTopWidth")) : 0;*/


			this.x_bc		     	= format_colour($$.css("borderTopColor")); 													// Border colour
			this.x_bbc		     	= format_colour($$.css("borderBottomColor")); 												// Bottom Border colour
			this.x_tbc		     	= format_colour($$.css("borderTopColor")); 													// Top Border colour
			this.x_lbc		     	= format_colour($$.css("borderLeftColor")); 												// Left Border colour
			this.x_rbc		     	= format_colour($$.css("borderRightColor")); 												// Right Border colour
			this.borderString    	= this.x_bw + "px" + " solid " + this.x_bc;
      		this.borderStringB   	= this.x_bbw + "px" + " solid " + this.x_bbc;
      		this.borderStringR    	= this.x_rbw + "px" + " solid " + this.x_bc;
      		this.borderStringL   	= this.x_lbw + "px" + " solid " + this.x_bbc;

			// Padding
			this.x_pad		      	= strip_px($$.css("paddingTop"));															// Padding
			this.x_tpad	 			= strip_px($$.css("paddingTop"));															// Padding top
			this.x_bpad 			= strip_px($$.css("paddingBottom"));														// Padding Bottom
			this.x_lpad			 	= strip_px($$.css("paddingLeft"));															// Padding Left
			this.x_rpad			 	= strip_px($$.css("paddingRight"));															// Padding Right
			this.topPaddingP     	= strip_px($$.parent().css("paddingTop"));													// Parent top padding
			this.bottomPaddingP  	= strip_px($$.parent().css("paddingBottom"));												// Parent bottom padding

			// Margins
			this.x_tmargin	 		= strip_px($$.css("marginTop"));														// Margin top
			this.x_bmargin 			= strip_px($$.css("marginBottom"));														// Margin Bottom

			// Calc Radius
			this.topMaxRadius = Math.max(settings.tl ? settings.tl.radius : 0, settings.tr ? settings.tr.radius : 0);
			this.botMaxRadius = Math.max(settings.bl ? settings.bl.radius : 0, settings.br ? settings.br.radius : 0);

			// Add styles and class
			$$.addClass('hasCorners').css({
				"padding":				"0",
				"border":				"none",
				"backgroundColor":		"transparent",
				"backgroundImage":		"none",
				'overflow':				"visible"
			});

			if(box.style.position != "absolute") $$.css("position","relative");

       		$$.attr("id","ccoriginaldiv");

			// Ok we add an inner div to actually put things into this will allow us to keep the height
			var newMainContainer = document.createElement("div");

			$(newMainContainer).css({"padding" : "0", width: '100%' }).attr('id','ccshell');

       		//this.shell = $$.append(newMainContainer);
       		this.shell = newMainContainer;

      		//this.x_width = strip_px($(this.shell).css('width'));

			/*
			Create top and bottom containers.
			These will be used as a parent for the corners and bars.
			*/
			for(var t = 0; t < 2; t++)
			{
				switch(t)
				{
					// Top
					case 0:

						// Only build top bar if a top corner is to be draw
						if(settings.tl || settings.tr)
						{
							var newMainContainer = document.createElement("div");

							$(newMainContainer).css({
								width: '100%',
								"font-size":		"1px",
								overflow:			"hidden",
								position:			"absolute",
								height:				topMaxRadius + "px",
								top:				0 - topMaxRadius + "px",
								"marginLeft" :			- parseInt( this.x_lbw + this.x_lpad) + "px",
								"marginRight" :			- parseInt( this.x_rbw + this.x_rpad) + "px"
							}).attr('id','cctopcontainer');

							if($.browser.msie && $.browser.version==6) {
								$(newMainContainer).css({
									"paddingLeft" :			Math.abs(parseInt( this.x_lbw + this.x_lpad)) + "px",
									"paddingRight" :		Math.abs(parseInt( this.x_rbw + this.x_rpad)) + "px"
						        });
					        }

							this.topContainer = this.shell.appendChild(newMainContainer);

						}
					break;

					// Bottom
					case 1:

						// Only build bottom bar if a bottom corner is to be draw
						if(settings.bl || settings.br)
						{
							var newMainContainer = document.createElement("div");

							$(newMainContainer).css({
								width: '100%',
								"font-size":		"1px",
								"overflow":			"hidden",
								"position":			"absolute",
								height:				botMaxRadius + "px",
								bottom:				0 - botMaxRadius  + "px",
								"marginLeft" :			- parseInt( this.x_lbw + this.x_lpad) + "px",
								"marginRight" :			- parseInt( this.x_rbw + this.x_rpad) + "px"
							}).attr('id','ccbottomcontainer');

							if($.browser.msie && $.browser.version==6) {
								$(newMainContainer).css({
									"paddingLeft" :			Math.abs(parseInt( this.x_lbw + this.x_lpad)) + "px",
									"paddingRight" :		Math.abs(parseInt( this.x_rbw + this.x_rpad)) + "px"
						        });
					        }

							this.bottomContainer = this.shell.appendChild(newMainContainer);
						}
					break;
				}
			}


			// Create array of available corners
			var corners = ["tr", "tl", "br", "bl"];
			/*
			Loop for each corner
			*/
			for(var i in corners)
			{
				if(i > -1 < 4)
				{
					// Get current corner type from array
					var cc = corners[i];
					// Has the user requested the currentCorner be round?
					// Code to apply correct color to top or bottom
					if(cc == "tr" || cc == "tl")
					{
						var bwidth=this.x_bw;
						var bcolor=this.x_bc;
					} else {
						var bwidth=this.x_bbw;
						var bcolor=this.x_bbc;
					}

					// Yes, we need to create a new corner
					var newCorner = document.createElement("div");

					$(newCorner).css({
						position:"absolute",
						"font-size":"1px",
						overflow:"hidden"
					}).height(settings[cc].radius + "px").width(settings[cc].radius + "px");

					// THE FOLLOWING BLOCK OF CODE CREATES A ROUNDED CORNER
					// ---------------------------------------------------- TOP
					// Get border radius
					var borderRadius = parseInt(settings[cc].radius - bwidth);
					// Cycle the x-axis
					for(var intx = 0, j = settings[cc].radius; intx < j; intx++)
					{
                      // Calculate the value of y1 which identifies the pixels inside the border
                      if((intx +1) >= borderRadius)
                        var y1 = -1;
                      else
                        var y1 = (Math.floor(Math.sqrt(Math.pow(borderRadius, 2) - Math.pow((intx+1), 2))) - 1);
                      // Only calculate y2 and y3 if there is a border defined
                      if(borderRadius != j)
                      {
                          if((intx) >= borderRadius)
                            var y2 = -1;
                          else
                            var y2 = Math.ceil(Math.sqrt(Math.pow(borderRadius,2) - Math.pow(intx, 2)));
                           if((intx+1) >= j)
                            var y3 = -1;
                           else
                            var y3 = (Math.floor(Math.sqrt(Math.pow(j ,2) - Math.pow((intx+1), 2))) - 1);
                      }
                      // Calculate y4
                      if((intx) >= j)
                        var y4 = -1;
                      else
                        var y4 = Math.ceil(Math.sqrt(Math.pow(j ,2) - Math.pow(intx, 2)));
                      // Draw bar on inside of the border with foreground colour

                      if(y1 > -1) drawPixel(intx, 0, this.x_bgc, 100, (y1+1), newCorner, -1, settings[cc].radius, 0, this.x_bgi, this.x_width, this.x_height, this.x_bw, this.x_bgr);
                      // Only draw border/foreground antialiased pixels and border if there is a border defined
                      if(borderRadius != j)
                      {
                          // Cycle the y-axis
                          for(var inty = (y1 + 1); inty < y2; inty++)
                          {
                              // Draw anti-alias pixels
                              if(settings.antiAlias)
                              {
                                  // For each of the pixels that need anti aliasing between the foreground and border colour draw single pixel divs
                                  if(this.x_bgi != "")
                                  {
										var borderFract = (pixelFraction(intx, inty, borderRadius) * 100);
										if(borderFract < 30)
										{
											drawPixel(intx, inty, bcolor, 100, 1, newCorner, 0, settings[cc].radius, 0, this.x_bgi, this.x_width, this.x_height, bwidth, this.x_bgr);
										}
										else
										{
											drawPixel(intx, inty, bcolor, 100, 1, newCorner, -1, settings[cc].radius, 0, this.x_bgi, this.x_width, this.x_height, bwidth, this.x_bgr);
                                  		}
                                  	}
                                  	else
                                  	{
                                      	var pixelcolour = BlendColour(this.x_bgc, bcolor, pixelFraction(intx, inty, borderRadius));
                                     	drawPixel(intx, inty, pixelcolour, 100, 1, newCorner, 0, settings[cc].radius, 0, this.x_bgi, this.x_width, this.x_height, bwidth, this.x_bgr);
                                  	}
                              }
                          }
                          // Draw bar for the border
                          if(settings.antiAlias)
                          {
                              if(y3 >= y2)
                              {
                                 if (y2 == -1) y2 = 0;
                                 drawPixel(intx, y2, bcolor, 100, (y3 - y2 + 1), newCorner, 0, 0, 1, this.x_bgi, this.x_width, this.x_height, bwidth, this.x_bgr);
                              }
                          }
                          else
                          {
                              if(y3 >= y1)
                              {
                                  drawPixel(intx, (y1 + 1), bcolor, 100, (y3 - y1), newCorner, 0, 0, 1, this.x_bgi, this.x_width, this.x_height, bwidth, this.x_bgr);
                              }
                          }
                          // Set the colour for the outside curve
                          var outsideColour = bcolor;
                      }
                      else
                      {
                          // Set the colour for the outside curve
                          var outsideColour = this.x_bgc;
                          var y3 = y1;
                      }
                      // Draw aa pixels?
                      if(settings.antiAlias)
                      {
                          // Cycle the y-axis and draw the anti aliased pixels on the outside of the curve
                          for(var inty = (y3 + 1); inty < y4; inty++)
                          {
                              // For each of the pixels that need anti aliasing between the foreground/border colour & background draw single pixel divs
                              drawPixel(intx, inty, outsideColour, (pixelFraction(intx, inty , j) * 100), 1, newCorner, ((bwidth > 0)? 0 : -1), settings[cc].radius, 0, this.x_bgi, this.x_width, this.x_height, bwidth);
                          }
                      }
                  }
                  // END OF CORNER CREATION
                  // ---------------------------------------------------- END

                  // We now need to store the current corner in the masterConers array
                  masterCorners[settings[cc].radius] = $(newCorner).clone();

                  /*
                  Now we have a new corner we need to reposition all the pixels unless
                  the current corner is the bottom right.
                  */
                  // Loop through all children (pixel bars)
                  for(var t = 0, k = newCorner.childNodes.length; t < k; t++)
                  {
                      // Get current pixel bar
                      var pixelBar = newCorner.childNodes[t];

                      // Get current top and left properties
                      var pixelBarTop    = parseInt(pixelBar.style.top.substring(0, pixelBar.style.top.indexOf("px")));
                      var pixelBarLeft   = parseInt(pixelBar.style.left.substring(0, pixelBar.style.left.indexOf("px")));
                      var pixelBarHeight = parseInt(pixelBar.style.height.substring(0, pixelBar.style.height.indexOf("px")));

                      // Reposition pixels
                      if(cc == "tl" || cc == "bl"){
                          pixelBar.style.left = settings[cc].radius -pixelBarLeft -1 + "px"; // Left
                      }
                      if(cc == "tr" || cc == "tl"){
                          pixelBar.style.top =  settings[cc].radius -pixelBarHeight -pixelBarTop + "px"; // Top
                      }
                      pixelBar.style.backgroundRepeat = this.x_bgr;

                      switch(cc)
                      {
                          case "tr":

 								if($.browser.msie && $.browser.version==6) var offset = this.x_lpad + this.x_rpad + this.x_lbw + this.x_rbw;
 								else var offset = 0;

                                pixelBar.style.backgroundPosition  = parseInt( this.x_bgposX - Math.abs( this.x_rbw - this.x_lbw + (this.x_width - settings[cc].radius + this.x_rbw) + pixelBarLeft) - settings.bl.radius - this.x_bw - settings.br.radius - this.x_bw) + offset + "px " + parseInt( this.x_bgposY - Math.abs(settings[cc].radius -pixelBarHeight -pixelBarTop - this.x_bw)) + "px";

                              break;
                          case "tl":
                              pixelBar.style.backgroundPosition = parseInt( this.x_bgposX - Math.abs((settings[cc].radius -pixelBarLeft -1)  - this.x_lbw)) + "px " + parseInt( this.x_bgposY - Math.abs(settings[cc].radius -pixelBarHeight -pixelBarTop - this.x_bw)) + "px";
                              break;
                          case "bl":

                                  pixelBar.style.backgroundPosition = parseInt( this.x_bgposX - Math.abs((settings[cc].radius -pixelBarLeft -1) - this.x_lbw )) + "px " + parseInt( this.x_bgposY - Math.abs(( this.x_height + (this.x_bw+this.x_tpad+this.x_bpad) - settings[cc].radius + pixelBarTop))) + "px";

                              break;
                          case "br":
								  // Added - settings.bl.radius - this.x_bw - settings.br.radius - this.x_bw to this and tr to offset background image due to neg margins.
								  if($.browser.msie && $.browser.version==6) var offset = this.x_lpad + this.x_rpad + this.x_lbw + this.x_rbw;
 									else var offset = 0;

                                  pixelBar.style.backgroundPosition = parseInt( this.x_bgposX - Math.abs( this.x_rbw - this.x_lbw + (this.x_width - settings[cc].radius + this.x_rbw) + pixelBarLeft) - settings.bl.radius - this.x_bw - settings.br.radius - this.x_bw) + offset + "px " + parseInt( this.x_bgposY - Math.abs(( this.x_height + (this.x_bw+this.x_tpad+this.x_bpad) - settings[cc].radius + pixelBarTop))) + "px";

                              break;
                      }
                  }

                  // Position the container
                  switch(cc)
                  {


                      case "tl":
                        if(newCorner.style.position == "absolute") newCorner.style.top  = "0px";
                        if(newCorner.style.position == "absolute") newCorner.style.left = "0px";
                        if(this.topContainer) temp= this.topContainer.appendChild(newCorner);
						$(temp).attr("id","cctl");


                        break;
                      case "tr":
                        if(newCorner.style.position == "absolute") newCorner.style.top  = "0px";
                        if(newCorner.style.position == "absolute") newCorner.style.right = "0px";
                        if(this.topContainer) temp= this.topContainer.appendChild(newCorner);
						$(temp).attr("id","cctr");

                        break;
                      case "bl":
                        if(newCorner.style.position == "absolute") newCorner.style.bottom  = "0px";
                        if(newCorner.style.position == "absolute") newCorner.style.left = "0px";
						if(this.bottomContainer) temp= this.bottomContainer.appendChild(newCorner);
						$(temp).attr("id","ccbl");

                        break;
                      case "br":
                        if(newCorner.style.position == "absolute") newCorner.style.bottom   = "0px";
                        if(newCorner.style.position == "absolute") newCorner.style.right = "0px";
						if(this.bottomContainer) temp= this.bottomContainer.appendChild(newCorner);
						$(temp).attr("id","ccbr");

                        break;
                  }

              }
          }



          /*
          The last thing to do is draw the rest of the filler DIVs.
          We only need to create a filler DIVs when two corners have
          diffrent radiuses in either the top or bottom container.
          */

          // Find out which corner has the bigger radius and get the difference amount
          var radiusDiff = new Array();
          radiusDiff["t"] = Math.abs(settings.tl.radius - settings.tr.radius);
          radiusDiff["b"] = Math.abs(settings.bl.radius - settings.br.radius);

          for(z in radiusDiff)
          {
              // FIX for prototype lib
              if(z == "t" || z == "b")
              {
                  if(radiusDiff[z])
                  {
                      // Get the type of corner that is the smaller one
                      var smallerCornerType = ((settings[z + "l"].radius < settings[z + "r"].radius)? z +"l" : z +"r");

                      // First we need to create a DIV for the space under the smaller corner
                      var newFiller = document.createElement("DIV");
                      newFiller.style.height = radiusDiff[z] + "px";
                      newFiller.style.width  =  settings[smallerCornerType].radius + "px";
                      newFiller.style.position = "absolute";
                      newFiller.style.fontSize = "1px";
                      newFiller.style.overflow = "hidden";
                      newFiller.style.backgroundColor = this.x_bgc;
                      //newFiller.style.backgroundColor = get_random_color();

                      // Position filler
                      switch(smallerCornerType)
                      {
                          case "tl":
                              newFiller.style.bottom = "0px";
                              newFiller.style.left   = "0px";
                              newFiller.style.borderLeft = this.borderString;
                              temp=this.topContainer.appendChild(newFiller);
temp.id="cctlfiller";

                              break;

                          case "tr":
                              newFiller.style.bottom = "0px";
                              newFiller.style.right  = "0px";
                              newFiller.style.borderRight = this.borderString;
                              temp=this.topContainer.appendChild(newFiller);
temp.id="cctrfiller";

                              break;

                          case "bl":
                              newFiller.style.top    = "0px";
                              newFiller.style.left   = "0px";
                              newFiller.style.borderLeft = this.borderStringB;
                              temp=this.bottomContainer.appendChild(newFiller);
temp.id="ccblfiller";

                              break;

                          case "br":
                              newFiller.style.top    = "0px";
                              newFiller.style.right  = "0px";
                              newFiller.style.borderRight = this.borderStringB;
                              temp=this.bottomContainer.appendChild(newFiller);
temp.id="ccbrfiller";

                              break;
                      }
                  }














                  // Create the bar to fill the gap between each corner horizontally
                  var newFillerBar = document.createElement("div");
                  newFillerBar.style.position = "relative";
                  newFillerBar.style.fontSize = "1px";
                  newFillerBar.style.overflow = "hidden";
                  newFillerBar.style.backgroundColor = this.x_bgc;
                  newFillerBar.style.backgroundImage = this.x_bgi;
                  newFillerBar.style.backgroundRepeat= this.x_bgr;


                  switch(z)
                  {
                      case "t":
                          // Top Bar
                          if(this.topContainer)
                          {
                              // Edit by Asger Hallas: Check if settings.xx.radius is not false
                              if(settings.tl.radius && settings.tr.radius)
                              {
                                  newFillerBar.style.height      = 100 + topMaxRadius - this.x_tbw + "px";
                                  newFillerBar.style.marginLeft  = settings.tl.radius - this.x_lbw + this.x_rbw + "px";
                                  newFillerBar.style.marginRight = settings.tr.radius - this.x_lbw + this.x_rbw + "px";
                                  newFillerBar.style.borderTop   = this.borderString;

                                  if(this.x_bgi != "")
                                    newFillerBar.style.backgroundPosition  = parseInt( this.x_bgposX - (topMaxRadius - this.x_lbw )) + "px " + parseInt( this.x_bgposY ) + "px";
                                    //newFillerBar.style.backgroundPosition  = parseInt( this.x_bgposX - (topMaxRadius - this.x_lbw)) + "px " + parseInt( this.x_bgposY ) + "px";

									if($.browser.msie && $.browser.version==6) {
										$(newFillerBar).css({
											"marginLeft" :		- parseInt( this.x_lbw + this.x_lpad - settings.tl.radius ) + "px",
											"marginRight" :		- parseInt( this.x_rbw + this.x_rpad - settings.tr.radius ) + "px"
										});
										 if(this.x_bgi != "")
                                    		newFillerBar.style.backgroundPosition  = parseInt( this.x_bgposX + this.x_lbw - (topMaxRadius )) + "px " + parseInt( this.x_bgposY ) + "px";
					       			}


                                  temp=this.topContainer.appendChild(newFillerBar);
								  $(temp).attr("id","cctopmiddlefiller");

                                  // Repos the boxes background image
                                  $(this.shell).css("backgroundPosition", parseInt( this.x_bgposX ) + "px " + parseInt( this.x_bgposY - (topMaxRadius - this.x_lbw)) + "px");
                              }
                          }
                          break;
                      case "b":
                          if(this.bottomContainer)
                          {
                              // Edit by Asger Hallas: Check if settings.xx.radius is not false
                              if(settings.bl.radius && settings.br.radius)
                              {
                                  // Bottom Bar
                                  newFillerBar.style.height     = botMaxRadius - this.x_bbw + "px";
                                  newFillerBar.style.marginLeft   = settings.bl.radius - this.x_lbw + this.x_rbw + "px";
                                  newFillerBar.style.marginRight  = settings.br.radius - this.x_lbw + this.x_rbw + "px";
                                  newFillerBar.style.borderBottom = this.borderStringB;
                                  if(this.x_bgi != "")
                                    newFillerBar.style.backgroundPosition  = parseInt( this.x_bgposX - (botMaxRadius - this.x_lbw )) + "px " + parseInt( this.x_bgposY - (this.x_height + this.x_tpad + this.x_bbw + this.x_bpad - botMaxRadius )) + "px";
                                    //newFillerBar.style.backgroundPosition  = parseInt( this.x_bgposX - (botMaxRadius  - this.x_lbw )) + "px " + parseInt( this.x_bgposY - (this.x_height + this.x_tpad + this.x_bw + this.x_bpad - botMaxRadius )) + "px";

                                  if($.browser.msie && $.browser.version==6) {
										$(newFillerBar).css({
											"marginLeft" :		- parseInt( this.x_lbw + this.x_lpad - settings.bl.radius ) + "px",
											"marginRight" :		- parseInt( this.x_rbw + this.x_rpad - settings.br.radius ) + "px"
										});

										if(this.x_bgi != "")
                                    		newFillerBar.style.backgroundPosition  = parseInt( this.x_bgposX - (botMaxRadius - this.x_lbw)) + "px " + parseInt( this.x_bgposY - (this.x_height + this.x_tpad + this.x_bbw + this.x_bpad - botMaxRadius )) + "px";
					       			}

                                  temp=this.bottomContainer.appendChild(newFillerBar);
$(temp).attr("id","ccbottommiddlefiller");

                              }
                          }
                          break;
                  }
              }
          }


          // Create content container
          var contentContainer = document.createElement("div");
          var pd = 0;
          // Set contentContainer's properties
        //  contentContainer.style.position = "absolute";
          contentContainer.className      = "autoPadDiv";
          // Get padding amounts
          var topPadding = Math.abs( this.x_bw  + this.x_pad);
          var botPadding = Math.abs( this.x_bbw + this.x_bpad);

          // Apply top padding
          if(topMaxRadius < this.boxPadding)
            {
             contentContainer.style.paddingTop = Math.abs(parseInt( pd + topPadding)) + "px";
            }
            else
            {
            contentContainer.style.paddingTop = "0";
          }

          // Apply Bottom padding
          if(botMaxRadius < this.x_pad)
            {contentContainer.style.paddingBottom = Math.abs(parseInt(botPadding - botMaxRadius)) + "px";} else
            {contentContainer.style.paddingBottom = "0";}

          // Content container must fill vertically to show the border
          $(contentContainer).css({
			"marginLeft" :			- parseInt( this.x_lbw + this.x_lpad) + "px",
			"marginRight" :			- parseInt( this.x_rbw + this.x_rpad) + "px",
			"marginTop" :			"-" + Math.abs(parseInt( this.x_tbw + (this.x_tpad - topMaxRadius))) + "px",
			"marginBottom" :		"-" + Math.abs(parseInt( this.x_bbw + (this.x_bpad - botMaxRadius))) + "px",
			"border-left" :				this.borderStringL,
			"border-right" :			this.borderStringR,
			"border-top" :				this.borderString,
			"border-bottom" :			this.borderStringB,
			"borderTopWidth" :		"0",
			"borderBottomWidth" :	"0",
			"height" : 				"100%",
			"width" : "100%",
			"paddingLeft" :			Math.abs(parseInt( this.x_lpad)) + "px",
			"paddingRight" :		Math.abs(parseInt( this.x_rpad)) + "px",
			"paddingTop" :			Math.abs(parseInt( this.x_tbw + (this.x_tpad - topMaxRadius))) + "px",
			"paddingBottom" :   	Math.abs(parseInt( this.x_bbw + (this.x_bpad - botMaxRadius))) + "px"
          });

          // Origional container has background image
          $$.css({
          	"paddingLeft" :			Math.abs(parseInt( this.x_lbw + this.x_lpad)) + "px",
			"paddingRight" :		Math.abs(parseInt( this.x_rbw + this.x_rpad)) + "px",
			"paddingTop" :			Math.abs(parseInt( this.x_tbw + (this.x_tpad - topMaxRadius))) + "px",
			"paddingBottom" :      	Math.abs(parseInt( this.x_bbw + (this.x_bpad - botMaxRadius))) + "px",
			"backgroundColor" : 	this.x_bgc,
			"backgroundImage" :		this.x_bgi,
			"backgroundPosition" : 	this.x_bw + 'px -' + Math.abs(parseInt(topMaxRadius - this.x_bw )) + "px",
			'margin-top':			0,
		  	'margin-bottom':		0
          });

          //'margin-top':			parseInt(this.x_tmargin + topMaxRadius) + "px",
		  //'margin-bottom':		parseInt(this.x_bmargin + botMaxRadius) + "px"

          // IE does not like an empty box; without this it won't show the contentContainer
          if ($$.html() == "") $$.html('&nbsp;');

          // Append contentContainer
          $$.wrapInner(contentContainer);
          $$.prepend(this.shell);

          // Wrapper to make margins work correctly
          var wrapper = document.createElement("div");

          $(wrapper).css({
			'margin-top':			parseInt(this.x_tmargin) + "px",
			'margin-bottom':		parseInt(this.x_bmargin) + "px",
			'padding-top':			topMaxRadius + "px",
			'padding-bottom':		botMaxRadius + "px",
			'overflow': 'hidden'
          }).addClass('ccwrapper');

          $$.wrap(wrapper);

          // Because of this method of doing the corners we have magins above and below; the following prevents margin collapsing.
          $$.after('<div class="clear" style="height:0;line-height:0px;">&nbsp;</div>');
      }

		/*
		This function draws the pixels
		*/
		function drawPixel( intx, inty, colour, transAmount, height, newCorner, image, cornerRadius, isBorder, bgImage, x_width, x_height, x_bw, repeat ) {

			//var $$ = $(box);

		    var pixel = document.createElement("div");

		    $(pixel).css({
		    	"height" :			height,
		    	"width" :			"1px",
		    	"position" :		"absolute",
		    	"font-size" :		"1px",
		    	"overflow" :		"hidden",
		    	"top" :				inty + "px",
		    	"left" :			intx + "px",
		    	"background-color" :colour
		    });

		    // Max Top Radius
		    var topMaxRadius = Math.max(settings.tl ? settings.tl.radius : 0, settings.tr ? settings.tr.radius : 0);

		    // Dont apply background image to border pixels
			if(image == -1 && bgImage !="")
			{
				$(pixel).css({
					"background-position":"-" + Math.abs(x_width - (cornerRadius - intx) + x_bw) + "px -" + Math.abs((x_height + topMaxRadius + inty) -x_bw) + "px",
					"background-image":bgImage,
					"background-repeat":repeat
				});
			}
			else
			{
				if (!isBorder) $(pixel).addClass('hasBackgroundColor');
			}
		    if (transAmount != 100)
		    	$(pixel).css({opacity: (transAmount/100) });

		    newCorner.appendChild(pixel);
		};


		// Utilities
		function BlendColour(Col1, Col2, Col1Fraction)
		{

			var red1 = parseInt(Col1.substr(1,2),16);
			var green1 = parseInt(Col1.substr(3,2),16);
			var blue1 = parseInt(Col1.substr(5,2),16);
			var red2 = parseInt(Col2.substr(1,2),16);
			var green2 = parseInt(Col2.substr(3,2),16);
			var blue2 = parseInt(Col2.substr(5,2),16);

			if(Col1Fraction > 1 || Col1Fraction < 0) Col1Fraction = 1;

			var endRed = Math.round((red1 * Col1Fraction) + (red2 * (1 - Col1Fraction)));
			if(endRed > 255) endRed = 255;
			if(endRed < 0) endRed = 0;

			var endGreen = Math.round((green1 * Col1Fraction) + (green2 * (1 - Col1Fraction)));
			if(endGreen > 255) endGreen = 255;
			if(endGreen < 0) endGreen = 0;

			var endBlue = Math.round((blue1 * Col1Fraction) + (blue2 * (1 - Col1Fraction)));
			if(endBlue > 255) endBlue = 255;
			if(endBlue < 0) endBlue = 0;

			return "#" + IntToHex(endRed)+ IntToHex(endGreen)+ IntToHex(endBlue);

		}

		function IntToHex(strNum)
		{
			rem = strNum % 16;
			base = Math.floor(strNum / 16);

			baseS = MakeHex(base);
			remS = MakeHex(rem);

			return baseS + '' + remS;
		}

		function MakeHex(x)
		{
		  if((x >= 0) && (x <= 9))
		  {
		      return x;
		  }
		  else
		  {
		      switch(x)
		      {
		          case 10: return "A";
		          case 11: return "B";
		          case 12: return "C";
		          case 13: return "D";
		          case 14: return "E";
		          case 15: return "F";
		      }
		  }
		}

		/*
		For a pixel cut by the line determines the fraction of the pixel on the 'inside' of the
		line.  Returns a number between 0 and 1
		*/
		function pixelFraction(x, y, r)
		{
			var pixelfraction = 0;

			/*
			determine the co-ordinates of the two points on the perimeter of the pixel that the
			circle crosses
			*/
			var xvalues = new Array(1);
			var yvalues = new Array(1);
			var point = 0;
			var whatsides = "";

			// x + 0 = Left
			var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(x,2)));

			if ((intersect >= y) && (intersect < (y+1)))
			{
				whatsides = "Left";
				xvalues[point] = 0;
				yvalues[point] = intersect - y;
				point =  point + 1;
			}
			// y + 1 = Top
			var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(y+1,2)));

			if ((intersect >= x) && (intersect < (x+1)))
			{
				whatsides = whatsides + "Top";
				xvalues[point] = intersect - x;
				yvalues[point] = 1;
				point = point + 1;
			}
			// x + 1 = Right
			var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(x+1,2)));

			if ((intersect >= y) && (intersect < (y+1)))
			{
				whatsides = whatsides + "Right";
				xvalues[point] = 1;
				yvalues[point] = intersect - y;
				point =  point + 1;
			}
			// y + 0 = Bottom
			var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(y,2)));

			if ((intersect >= x) && (intersect < (x+1)))
			{
				whatsides = whatsides + "Bottom";
				xvalues[point] = intersect - x;
				yvalues[point] = 0;
			}

			/*
			depending on which sides of the perimeter of the pixel the circle crosses calculate the
			fraction of the pixel inside the circle
			*/
			switch (whatsides)
			{
			      case "LeftRight":
			      pixelfraction = Math.min(yvalues[0],yvalues[1]) + ((Math.max(yvalues[0],yvalues[1]) - Math.min(yvalues[0],yvalues[1]))/2);
			      break;

			      case "TopRight":
			      pixelfraction = 1-(((1-xvalues[0])*(1-yvalues[1]))/2);
			      break;

			      case "TopBottom":
			      pixelfraction = Math.min(xvalues[0],xvalues[1]) + ((Math.max(xvalues[0],xvalues[1]) - Math.min(xvalues[0],xvalues[1]))/2);
			      break;

			      case "LeftBottom":
			      pixelfraction = (yvalues[0]*xvalues[1])/2;
			      break;

			      default:
			      pixelfraction = 1;
			}

			return pixelfraction;
		}


		// This function converts CSS rgb(x, x, x) to hexadecimal
		function rgb2Hex(rgbColour)
		{
			try{

				// Get array of RGB values
				var rgbArray = rgb2Array(rgbColour);

				// Get RGB values
				var red   = parseInt(rgbArray[0]);
				var green = parseInt(rgbArray[1]);
				var blue  = parseInt(rgbArray[2]);

				// Build hex colour code
				var hexColour = "#" + IntToHex(red) + IntToHex(green) + IntToHex(blue);
			}
			catch(e){
				alert("There was an error converting the RGB value to Hexadecimal in function rgb2Hex");
			}

			return hexColour;
		}

		// Returns an array of rbg values
		function rgb2Array(rgbColour)
		{
			// Remove rgb()
			var rgbValues = rgbColour.substring(4, rgbColour.indexOf(")"));

			// Split RGB into array
			var rgbArray = rgbValues.split(", ");

			return rgbArray;
		}

		// Formats colours
		function format_colour(colour)
		{
			var returnColour = "#ffffff";

			// Make sure colour is set and not transparent
			if(colour != "" && colour != "transparent")
			{
				// RGB Value?
				if(colour.substr(0, 3) == "rgb" && colour.substr(0, 4) != "rgba")
				{
				  // Get HEX aquiv.
				  returnColour = rgb2Hex(colour);
				}
				else if(colour.length == 4)
				{
				  // 3 chr colour code add remainder
				  returnColour = "#" + colour.substring(1, 2) + colour.substring(1, 2) + colour.substring(2, 3) + colour.substring(2, 3) + colour.substring(3, 4) + colour.substring(3, 4);
				}
				else
				{
				  // Normal valid hex colour
				  returnColour = colour;
				}
			}

			return returnColour;
		}

		// Removes 'px' from string
		function strip_px(value)
		{
			if (typeof(value)!='string') return value;
			return parseInt((( value != "auto" && value.indexOf("%") == -1 && value != "" && value.indexOf("px") !== -1)? Math.round(value.slice(0, value.indexOf("px"))) : 0))
		}

	};
})(jQuery);

/*
 * Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 *
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2007-07-21 18:45:56 -0500 (Sat, 21 Jul 2007) $
 * $Rev: 2447 $
 *
 * Version 2.1.1
 */
(function($){$.fn.bgIframe=$.fn.bgiframe=function(s){if($.browser.msie&&/6.0/.test(navigator.userAgent)){s=$.extend({top:'auto',left:'auto',width:'auto',height:'auto',opacity:true,src:'javascript:false;'},s||{});var prop=function(n){return n&&n.constructor==Number?n+'px':n;},html='<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+'style="display:block;position:absolute;z-index:-1;'+(s.opacity!==false?'filter:Alpha(Opacity=\'0\');':'')+'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+'"/>';return this.each(function(){if($('> iframe.bgiframe',this).length==0)this.insertBefore(document.createElement(html),this.firstChild);});}return this;};})(jQuery);

/* Here we replace each element with class 'wymeditor'
 * (typically textareas) by a WYMeditor instance.
 *
 * We could use the 'html' option, to initialize the editor's content.
 * If this option isn't set, the content is retrieved from
 * the element being replaced.
 */
 
function getBoxHtml(lBlogId,lFocus,lEntryId,lCommentId,lMessage,lClasses) {
	  var z = "<div class='wym_box'>"
              + "<div class='wym_area_top'>"
              + WYMeditor.TOOLS
              + "</div>"
              + "<div class='wym_area_left'></div>"
              + "<div class='wym_area_right'>";
              
              // + WYMeditor.CONTAINERS
              if (lClasses)
                z= z+WYMeditor.CLASSES

              z = z+"</div>"
              + "<div class='wym_area_main'>"
              + WYMeditor.HTML
              + WYMeditor.IFRAME
              + WYMeditor.STATUS
              + "</div>"
              + "<div class='wym_area_bottom'>"
              // + WYMeditor.LOGO
              + "<div class='WymButton'>"
              + "<div id='wSave'><input type='button' name='wSave' value='Save' onclick='WymeComSave("+lBlogId+","+lFocus+","+lEntryId+","+lCommentId+")'></div>"
              + "<div id='wDelete'><input type='button' name='wDelete' value='Delete' onclick='WymeComDelete("+lBlogId+","+lEntryId+","+lCommentId+")'></div>"
              + "<div id='wMessage'>"+lMessage+"</div>"
              + "</div>"
              + "</div>"
              + "</div>";
        return z;      
}              

function ___WymPortal(lBlogId,lFocus,lEntryId,lCommentId,lText,lMessage) {
  
  jQuery.each(WYMeditor.INSTANCES, function() {
      $(this._box).remove();
});

    var elms = document.getElementsByTagName("div");
    for (var i=0; i<elms.length; i++) {
      if (elms[i].id.substr(0,4) == 'bWym') {
        var thisId = elms[i].id.substr(4);
        if (thisId != lEntryId) {
          var o = document.getElementById('bWym'+thisId);
          removeAllChildren(o);
        }
      }
    }
    jQuery('.wymeditor').wymeditor({
        
      //classes panel
      classesItems: [
        {'name': 'date', 'title': 'PARA: Date', 'expr': 'p'},
        {'name': 'hidden-note', 'title': 'PARA: Hidden note',
         'expr': 'p[@class!="important"]'},
        {'name': 'important', 'title': 'PARA: Important',
         'expr': 'p[@class!="hidden-note"]'},
        {'name': 'border', 'title': 'IMG: Border', 'expr': 'img'},
        {'name': 'special', 'title': 'LIST: Special', 'expr': 'ul, ol'}
      ],
      
      //we customize the XHTML structure of WYMeditor by overwriting 
      //the value of boxHtml. In this example, "CONTAINERS" and 
      //"CLASSES" have been moved from "wym_area_right" to "wym_area_top":
      boxHtml:   "<div class='wym_box'>"
              + "<div class='wym_area_top'>"
              + WYMeditor.TOOLS
              + WYMeditor.CONTAINERS
              + WYMeditor.CLASSES
              + "</div>"
              + "<div class='wym_area_left'></div>"
              + "<div class='wym_area_right'>"
              + "</div>"
              + "<div class='wym_area_main'>"
              + WYMeditor.HTML
              + WYMeditor.IFRAME
              + WYMeditor.STATUS
              + "</div>"
              + "<div class='wym_area_bottom'>"
              + "</div>"
              + "</div>",

      //postInit is a function called when WYMeditor instance is ready
      //wym is the WYMeditor instance
      postInit: function(wym) {

        //we make all sections in area_top render as dropdown menus:
        jQuery(wym._box)
            //first we have to select them:
            .find(".wym_area_top .wym_section")
            //then we remove the existing class which make some of them render as a panels:
            .removeClass("wym_panel")
            //then we add the class which will make them render as a dropdown menu:
            .addClass("wym_dropdown")
            //finally we add some css to make the dropdown menus look better:
            .css("width", "120px")
            .css("float", "left")
            .css("margin-right", "5px")
            .find("ul")
            .css("width", "100px");

        //add a ">" character to the title of the new dropdown menus (visual cue)
        jQuery(this._box).find(".wym_tools, .wym_classes ")
            .find(WYMeditor.H2)
            .append("<span>&nbsp;&gt;</span>");
        }
    });
}

function WymPortal(lBlogId,lFocus,lEntryId,lCommentId,lText,lMessage) {
  
  jQuery.each(WYMeditor.INSTANCES, function() {
      $(this._box).remove();
});

    var elms = document.getElementsByTagName("div");
    for (var i=0; i<elms.length; i++) {
      if (elms[i].id.substr(0,4) == 'bWym') {
        var thisId = elms[i].id.substr(4);
        if (thisId != lEntryId) {
          var o = document.getElementById('bWym'+thisId);
          removeAllChildren(o);
        }
      }
    }
    jQuery('.wymeditor').wymeditor({

        stylesheet: '../css/Wym.css',
          html: lText,  
        boxHtml:getBoxHtml(lBlogId,lFocus,lEntryId,lCommentId,lMessage,0),

           classesHtml: "<div class='wym_classes wym_section'>"
                        + "<h2>Tag the title with our own keywords<br />Use one tag per line</h2><ul>"
                        + "<form name='fTag'><textarea id='TagPanel' class='TagPanel'/></form>"
                        + "</ul></div>",
        postInit: function(wym) {
            //construct the button's html
            var html = "</ul>"
                     + "<ul class='ulr'>"
                     + "<li class='wym_tools_Wink'> <a name='NewButton' href='#'"
                     + " style='background-image:"
                     + " url(/pics/Emotion1/Wink.gif)'>"
                     + "Add smiley"
                     + "</a></li>"
                     + "<li class='wym_tools_Smile'>"
                     + "<a name='NewButton' href='#'"
                     + " style='background-image:"
                     + " url(/pics/Emotion1/Smile.gif)'>"
                     + "Add smiley"
                     + "</a></li>"
                     +"<li class='wym_tools_Surprised'>"
                     + "<a name='NewButton' href='#'"
                     + " style='background-image:"
                     + " url(/pics/Emotion1/Surprised.gif)'>"
                     + "Add smiley"
                     + "</a></li>"
                     +"<li class='wym_tools_Sad'>"
                     + "<a name='NewButton' href='#'"
                     + " style='background-image:"
                     + " url(/pics/Emotion1/Sad.gif)'>"
                     + "Add smiley"
                     + "</a></li>"
                     +"<li class='wym_tools_Eek'>"
                     + "<a name='NewButton' href='#'"
                     + " style='background-image:"
                     + " url(/pics/Emotion1/Eek.gif)'>"
                     + "Add smiley"
                     + "</a></li>"
                     +"<li class='wym_tools_Twist'>"
                     + "<a name='NewButton' href='#'"
                     + " style='background-image:"
                     + " url(/pics/Emotion1/Twist.gif)'>"
                     + "Add smiley"
                     + "</a></li>"
                     + "</ul>";
            //add the button to the tools box
            jQuery(wym._box)
            .find(wym._options.toolsSelector + wym._options.toolsListSelector)
            .append(html);
            jQuery(wym._doc.body).focus();


  //handle click event
            jQuery(wym._box)
            .find('li.wym_tools_Surprised a').click(function() {
                //do something
                wym.insert('<img src="/pics/Emotion1/Surprised.gif" />');
                return(false);
            });
            jQuery(wym._box)
            .find('li.wym_tools_Smile a').click(function() {
                //do something
                wym.insert('<img src="/pics/Emotion1/Smile.gif" />');
                return(false);
            });
            jQuery(wym._box)
            .find('li.wym_tools_Sad a').click(function() {
                //do something
                wym.insert('<img src="/pics/Emotion1/Sad.gif" />');
                return(false);
            });
            jQuery(wym._box)
            .find('li.wym_tools_Eek a').click(function() {
                //do something
                wym.insert('<img src="/pics/Emotion1/Eek.gif" />');
                return(false);
            });
            jQuery(wym._box)
            .find('li.wym_tools_Twist a').click(function() {
                //do something
                wym.insert('<img src="/pics/Emotion1/Twist.gif" />');
                return(false);
            });
            jQuery(wym._box)
            .find('li.wym_tools_Wink a').click(function() {
                //do something
                wym.insert('<img src="/pics/Emotion1/Wink.gif" />');
                return(false);
            });
            // jQuery(wym._doc.body).focus();
            
            // wym.hovertools();          //activate hovertools
            // wym.exec();
            // gWymLenght = wym._index;
            // wym.resizable();           //and resizable plugins

            //postInit is executed after WYMeditor initialization
            //'wym' is the current WYMeditor instance

            //we generally activate plugins after WYMeditor initialization

         
           // alert(wym.xhtml);
     
        }

    }

    );
    
}

/*
 * KeyEvents in Reindex...
 */

// Register keypress events on the whole document
 $(document).keydown(function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (e.shiftKey)
      code = code+1000;
    if (e.ctrlKey)
      code = code+2000;
    if (e.altKey)
      code = code+3000;
  // alert(code+'#'+e.ctrlKey+e.shiftKey+e.altKey+'#');
     if (code == 112) {  // F1 == help - not IE..
       doHelp(gKeyContext);
       return false;
     }
     else if (typeof(HandleKeyBoardEvents) == 'function')
       HandleKeyBoardEvents(e,code); // ../js/KeyEvents now ../lib/RexUtil.php
   });
   