/**
 * jQuery Templates
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Written by: Stan Lemon <stanlemon@mac.com>
 * Based off of the Ext.Template library, available at:
 * http://www.extjs.com
 * This library provides basic templating functionality, allowing for macro-based
 * templates within jQuery.
 */
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(6($){$.9=6(a,b){4 Q $.9.x(a,b)};$.9.x=6(a,b){8(b&&b[\'7\'])b.7=2.7[b.7];2.t=$.G({y:R,7:2.7.H},b||{});2.l=a;8(2.t.y){2.y()}2.A=S};$.9.7=$.9.x.E.7={I:/\\$\\{([\\w-]+)(?:\\:([\\w\\.]*)(?:\\((.*?)?\\))?)?\\}/g,T:/\\{([\\w-]+)(?:\\:([\\w\\.]*)(?:\\((.*?)?\\))?)?\\}/g,U:/\\{\\{([\\w-]+)(?:\\:([\\w\\.]*)(?:\\((.*?)?\\))?)?\\}\\}/g};$.9.7.H=$.9.7.I;$.9.B=$.9.x.E.B={u:6(a,b,c){4 V(a).u(b,c)}};$.G($.9.x.E,{p:6(e){8(2.t.y){4 2.F(e)}q{3 f=2;3 g=2.B;3 h=6(m,a,b,c){8(b){8(b.u(0,5)=="2."){4 f.J(b.u(5),e[a],e)}q{8(c){3 d=/^\\s*[\'"](.*)["\']\\s*$/;c=c.W(\',\');X(3 i=0,K=c.Y;i<K;i++){c[i]=c[i].k(d,"$1")}c=[e[a]].Z(c)}q{c=[e[a]]}4 g[b].p(g,c)}}q{4 e[a]!==L?e[a]:""}};4 2.l.k(2.t.7,h)}},y:6(){3 d=$.M.N?"+":",";3 e=2.B;3 f=6(m,a,b,c){8(b){c=c?\',\'+c:"";8(b.u(0,5)!="2."){b="10."+b+\'(\'}q{b=\'2.J("\'+b.u(5)+\'", \';c=", z"}}q{c=\'\';b="(z[\'"+a+"\'] == L ? \'\' : "}4"\'"+d+b+"z[\'"+a+"\']"+c+")"+d+"\'"};3 g;8($.M.N){g="2.F = 6(z){ 4 \'"+2.l.k(/\\\\/g,\'\\\\\\\\\').k(/(\\r\\n|\\n)/g,\'\\\\n\').k(/\'/g,"\\\\\'").k(2.t.7,f)+"\';};"}q{g=["2.F = 6(z){ 4 [\'"];g.O(2.l.k(/\\\\/g,\'\\\\\\\\\').k(/(\\r\\n|\\n)/g,\'\\\\n\').k(/\'/g,"\\\\\'").k(2.t.7,f));g.O("\'].P(\'\');};");g=g.P(\'\')}11(g);4 2}});3 j={C:$.v.C,D:$.v.D,l:$.v.l};$.v.C=6(a,b,c,d){8(a[0].A){a[0]=a[0].p(a[1]);12 a[1]}3 r=j.C.p(2,13);4 r};$.v.l=6(a,o){8(a&&a.A)3 a=a.p(o);3 r=j.l.p(2,[a]);4 r};$.v.D=6(a,o){8(a&&a.A)3 a=a.p(o);3 r=j.D.p(2,[a]);4 r}})(14);',62,67,'||this|var|return||function|regx|if|template|||||||||||replace|html||||apply|else|||options|substr|fn||instance|compile|values|isTemplate|helpers|domManip|text|prototype|compiled|extend|standard|jsp|call|len|undefined|browser|mozilla|push|join|new|false|true|ext|jtemplates|String|split|for|length|concat|fm|eval|delete|arguments|jQuery'.split('|'),0,{}))
/**
 * Interface Elements for jQuery
 * utility function
 *
 * http://interface.eyecon.ro
 *
 * Copyright (c) 2006 Stefan Petre
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 *
 */
 eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('6.E={1J:p(e){8 x=0;8 y=0;8 a=e.C;8 b=j;7(6(e).k(\'I\')==\'M\'){8 c=a.1b;8 d=a.V;b=N;a.1b=\'2d\';a.I=\'1K\';a.V=\'1L\'}8 f=e;1M(f){x+=f.2e+(f.1N&&!6.10.1O?q(f.1N.2N)||0:0);y+=f.2f+(f.1N&&!6.10.1O?q(f.1N.2O)||0:0);f=f.2P}f=e;1M(f&&f.2Q&&f.2Q.2R()!=\'J\'){x-=f.1c||0;y-=f.1d||0;f=f.1e}7(b==N){a.I=\'M\';a.V=d;a.1b=c}s{x:x,y:y}},3u:p(a){8 x=0,y=0;1M(a){x+=a.2e||0;y+=a.2f||0;a=a.2P}s{x:x,y:y}},1f:p(e){8 w=6.k(e,\'1P\');8 h=6.k(e,\'1Q\');8 a=0;8 b=0;8 c=e.C;7(6(e).k(\'I\')!=\'M\'){a=e.2g;b=e.2h}F{8 d=c.1b;8 f=c.V;c.1b=\'2d\';c.I=\'1K\';c.V=\'1L\';a=e.2g;b=e.2h;c.I=\'M\';c.V=f;c.1b=d}s{w:w,h:h,11:a,R:b}},3v:p(a){s{11:a.2g||0,R:a.2h||0}},2S:p(e){8 h,w,1g;7(e){w=e.1w;h=e.1x}F{1g=z.W;w=1h.2i||1R.2i||(1g&&1g.1w)||z.J.1w;h=1h.2j||1R.2j||(1g&&1g.1x)||z.J.1x}s{w:w,h:h}},2T:p(e){8 t=0,l=0,w=0,h=0,1i=0,1j=0;7(e&&e.3w.2R()!=\'J\'){t=e.1d;l=e.1c;w=e.2k;h=e.2l;1i=0;1j=0}F{7(z.W){t=z.W.1d;l=z.W.1c;w=z.W.2k;h=z.W.2l}F 7(z.J){t=z.J.1d;l=z.J.1c;w=z.J.2k;h=z.J.2l}1i=1R.2i||z.W.1w||z.J.1w||0;1j=1R.2j||z.W.1x||z.J.1x||0}s{t:t,l:l,w:w,h:h,1i:1i,1j:1j}},3x:p(e,a){8 c=6(e);8 t=c.k(\'2m\')||\'\';8 r=c.k(\'2n\')||\'\';8 b=c.k(\'2o\')||\'\';8 l=c.k(\'2p\')||\'\';7(a)s{t:q(t)||0,r:q(r)||0,b:q(b)||0,l:q(l)};F s{t:t,r:r,b:b,l:l}},3y:p(e,a){8 c=6(e);8 t=c.k(\'3z\')||\'\';8 r=c.k(\'3A\')||\'\';8 b=c.k(\'3B\')||\'\';8 l=c.k(\'3C\')||\'\';7(a)s{t:q(t)||0,r:q(r)||0,b:q(b)||0,l:q(l)};F s{t:t,r:r,b:b,l:l}},1S:p(e,a){8 c=6(e);8 t=c.k(\'2O\')||\'\';8 r=c.k(\'3D\')||\'\';8 b=c.k(\'3E\')||\'\';8 l=c.k(\'2N\')||\'\';7(a)s{t:q(t)||0,r:q(r)||0,b:q(b)||0,l:q(l)||0};F s{t:t,r:r,b:b,l:l}},2q:p(a){8 x=a.3F||(a.3G+(z.W.1c||z.J.1c))||0;8 y=a.3H||(a.3I+(z.W.1d||z.J.1d))||0;s{x:x,y:y}},2r:p(a,b){b(a);a=a.2U;1M(a){6.E.2r(a,b);a=a.3J}},3K:p(c){6.E.2r(c,p(a){2s(8 b 2V a){7(2W a[b]===\'p\'){a[b]=X}}})},3L:p(a,b){8 c=6.E.2T();8 d=6.E.1f(a);7(!b||b==\'1y\')6(a).k({O:c.t+((S.1T(c.h,c.1j)-c.t-d.R)/2)+\'P\'});7(!b||b==\'1z\')6(a).k({Q:c.l+((S.1T(c.w,c.1i)-c.l-d.11)/2)+\'P\'})},3M:p(a,b){8 c=6(\'3N[@1U*="1V"]\',a||z),1V;c.1A(p(){1V=9.1U;9.1U=b;9.C.2t="3O:3P.3Q.3R(1U=\'"+1V+"\')"})}};[].2X||(2u.3S.2X=p(v,n){n=(n==X)?0:n;8 m=9.2v;2s(8 i=n;i<m;i++)7(9[i]==v)s i;s-1});6.g={A:X,T:X,2Y:p(){s 9.1A(p(){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){a.5.G={x:q(a.5.D[0])||0,y:q(a.5.D[1])||0,w:q(a.5.D[2])||0,h:q(a.5.D[3])||0}}a.5.G.15=a.5.G.x-a.5.u.x;a.5.G.16=a.5.G.y-a.5.u.y},2a:p(a){7(a.5.1D||a.5.D==\'27\'){6(\'J\',z).24(6.g.A.17(0))}6.g.A.38().42().k(\'Y\',1);7(1h.29){6.g.A.k(\'2t\',\'3d(Y=3e)\')}},1X:p(e){6(z).2w(\'33\',6.g.2A).2w(\'34\',6.g.1X);7(6.g.T==X){s}8 a=6.g.T;6.g.T=X;7(a.5.1Z==j){s j}7(a.5.1q==N){6(a).k(\'V\',a.5.1C)}8 b=a.C;7(a.19){6.g.A.k(\'3g\',\'3h\')}7(a.5.1n){6.g.A.43(a.5.1n)}7(a.5.2J==j){7(a.5.13>0){7(!a.5.L||a.5.L==\'1z\'){8 x=3i 6.13(a,{3j:a.5.13},\'Q\');x.3k(a.5.B.x,a.5.1G)}7(!a.5.L||a.5.L==\'1y\'){8 y=3i 6.13(a,{3j:a.5.13},\'O\');y.3k(a.5.B.y,a.5.1H)}}F{7(!a.5.L||a.5.L==\'1z\')a.C.Q=a.5.1G+\'P\';7(!a.5.L||a.5.L==\'1y\')a.C.O=a.5.1H+\'P\'}6.g.2a(a);7(a.5.1p==j){6(a).k(\'I\',a.5.20)}}F 7(a.5.13>0){a.5.28=N;8 c=j;7(6.Z&&6.2b&&a.5.1q){c=6.E.1J(6.2b.A.17(0))}6.g.A.44({Q:c?c.x:a.5.u.x,O:c?c.y:a.5.u.y},a.5.13,p(){a.5.28=j;7(a.5.1p==j){a.C.I=a.5.20}6.g.2a(a)})}F{6.g.2a(a);7(a.5.1p==j){6(a).k(\'I\',a.5.20)}}7(6.Z&&6.Z.2I>0){6.Z.45(a)}7(6.2b&&a.5.1q){6.2b.46(a)}7(a.5.1a&&(a.5.1G!=a.5.B.x||a.5.1H!=a.5.B.y)){a.5.1a.1F(a,a.5.47||[0,0,a.5.1G,a.5.1H])}7(a.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1h.29){c.2y="3s"}F{d.4e=\'M\';d.32=\'M\';d.31=\'M\'}}7(!o){o={}}s 9.1A(p(){7(9.1W||!6.E)s;7(1h.29){9.4f=p(){s j};9.4g=p(){s j}}8 a=9;8 b=o.3t?6(9).4h(o.3t):6(9);7(6.10.1B){b.1A(p(){9.2y="3s"})}F{b.k(\'-4i-1s-2K\',\'M\');b.k(\'1s-2K\',\'M\');b.k(\'-4j-1s-2K\',\'M\')}9.5={2Z:b,2J:o.2J?N:j,1p:o.1p?N:j,1q:o.1q?o.1q:j,19:o.19?o.19:j,1D:o.1D?o.1D:j,1m:o.1m?q(o.1m)||0:j,Y:o.Y?4k(o.Y):j,13:q(o.13)||X,2L:o.2L?o.2L:j,12:{},K:{},1o:o.1o&&o.1o.U==1v?o.1o:j,1r:o.1r&&o.1r.U==1v?o.1r:j,1a:o.1a&&o.1a.U==1v?o.1a:j,L:/1y|1z/.4l(o.L)?o.L:j,2c:o.2c?q(o.2c)||0:0,H:o.H?o.H:j,2F:o.2F?N:j,1n:o.1n||j};7(o.12&&o.12.U==1v)9.5.12.1s=o.12;7(o.1u&&o.1u.U==1v)9.5.1u=o.1u;7(o.D&&((o.D.U==3f&&(o.D==\'27\'||o.D==\'z\'))||(o.D.U==2u&&o.D.2v==4))){9.5.D=o.D}7(o.2M){9.5.2M=o.2M}7(o.18){7(2W o.18==\'4m\'){9.5.1l=q(o.18)||1;9.5.1I=q(o.18)||1}F 7(o.18.2v==2){9.5.1l=q(o.18[0])||1;9.5.1I=q(o.18[1])||1}}7(o.1t&&o.1t.U==1v){9.5.1t=o.1t}9.1W=N;b.1A(p(){9.1Y=a});b.2z(\'30\',6.g.2x)})}};6.4n.23({4o:6.g.2Y,4p:6.g.3p});',62,274,'|||||dragCfg|jQuery|if|var|this|||||||iDrag|||false|css|||||function|parseInt||return||oC|||||document|helper|oR|style|containment|iUtil|else|cont|cursorAt|display|body|pointer|axis|none|true|top|px|left|hb|Math|dragged|constructor|position|documentElement|null|opacity|iDrop|browser|wb|onDragModifier|fx|currentPointer|dx|dy|get|grid|si|onChange|visibility|scrollLeft|scrollTop|parentNode|getSize|de|window|iw|ih|0px|gx|zIndex|frameClass|onStart|ghosting|so|onStop|user|onSlide|onDrag|Function|clientWidth|clientHeight|vertically|horizontally|each|msie|oP|insideParent|parentBorders|apply|nRx|nRy|gy|getPosition|block|absolute|while|currentStyle|opera|width|height|self|getBorder|max|src|png|isDraggable|dragstop|dragElem|init|oD|diffX|diffY|extend|append|nx|ny|parent|prot|ActiveXObject|hidehelper|iSort|snapDistance|hidden|offsetLeft|offsetTop|offsetWidth|offsetHeight|innerWidth|innerHeight|scrollWidth|scrollHeight|marginTop|marginRight|marginBottom|marginLeft|getPointer|traverseDOM|for|filter|Array|length|unbind|draginit|unselectable|bind|dragmove|parentPos|sliderSize|sliderPos|iSlider|autoSize|right|bottom|count|revert|select|hpc|fractions|borderLeftWidth|borderTopWidth|offsetParent|tagName|toLowerCase|getClient|getScroll|firstChild|in|typeof|indexOf|destroy|dhe|mousedown|KhtmlUserSelect|userSelect|mousemove|mouseup|dragstart|initialPosition|relative|empty|auto|getContainment|fitToContainer|snapToGrid|alpha|100|String|cursor|move|new|duration|custom|abs|min|distance|pow|build|div|dragHelper|on|handle|getPositionLite|getSizeLite|nodeName|getMargins|getPadding|paddingTop|paddingRight|paddingBottom|paddingLeft|borderRightWidth|borderBottomWidth|pageX|clientX|pageY|clientY|nextSibling|purgeEvents|centerEl|fixPNG|img|progid|DXImageTransform|Microsoft|AlphaImageLoader|prototype|off|MozUserSelect|fromHandler|dragmoveBy|selectKeyHelper|cloneNode|modifyContainer|addClass|highlight|hide|removeClass|animate|checkdrop|check|lastSi|sqrt|Object|checkhover|id|listStyle|overflow|mozUserSelect|onselectstart|ondragstart|find|moz|khtml|parseFloat|test|number|fn|DraggableDestroy|Draggable'.split('|'),0,{}))

/**
* DD_roundies, this adds rounded-corner CSS in standard browsers and VML sublayers in IE that accomplish a similar appearance when comparing said browsers.
* Author: Drew Diller
* Email: drew.diller@gmail.com
* URL: http://www.dillerdesign.com/experiment/DD_roundies/
* Version: 0.0.2a
* Licensed under the MIT License: http://dillerdesign.com/experiment/DD_roundies/#license
*
* Usage:
* DD_roundies.addRule('#doc .container', '10px 5px'); // selector and multiple radii
* DD_roundies.addRule('.box', 5, true); // selector, radius, and optional addition of border-radius code for standard browsers.
* 
* Just want the PNG fixing effect for IE6, and don't want to also use the DD_belatedPNG library?  Don't give any additional arguments after the CSS selector.
* DD_roundies.addRule('.your .example img');
**/

var DD_roundies = {

	ns: 'DD_roundies',
	
	IE6: false,
	IE7: false,
	IE8: false,
	IEversion: function() {
		if (document.documentMode != 8 && document.namespaces && !document.namespaces[this.ns]) {
			this.IE6 = true;
			this.IE7 = true;
		}
		else if (document.documentMode == 8) {
			this.IE8 = true;
		}
	},
	querySelector: document.querySelectorAll,
	selectorsToProcess: [],
	imgSize: {},
	
	createVmlNameSpace: function() { /* enable VML */
		if (this.IE6 || this.IE7) {
			document.namespaces.add(this.ns, 'urn:schemas-microsoft-com:vml');
		}
		if (this.IE8) {
			document.writeln('<?import namespace="' + this.ns + '" implementation="#default#VML" ?>');
		}
	},
	
	createVmlStyleSheet: function() { /* style VML, enable behaviors */
		/*
			Just in case lots of other developers have added
			lots of other stylesheets using document.createStyleSheet
			and hit the 31-limit mark, let's not use that method!
			further reading: http://msdn.microsoft.com/en-us/library/ms531194(VS.85).aspx
		*/
		var style = document.createElement('style');
		document.documentElement.firstChild.insertBefore(style, document.documentElement.firstChild.firstChild);
		if (style.styleSheet) { /* IE */
			try {
				var styleSheet = style.styleSheet;
				styleSheet.addRule(this.ns + '\\:*', '{behavior:url(#default#VML)}');
				this.styleSheet = styleSheet;
			} catch(err) {}
		}
		else {
			this.styleSheet = style;
		}
	},
	
	/**
	* Method to use from afar - refer to it whenever.
	* Example for IE only: DD_roundies.addRule('div.boxy_box', '10px 5px');
	* Example for IE, Firefox, and WebKit: DD_roundies.addRule('div.boxy_box', '10px 5px', true);
	* @param {String} selector - REQUIRED - a CSS selector, such as '#doc .container'
	* @param {Integer} radius - REQUIRED - the desired radius for the box corners
	* @param {Boolean} standards - OPTIONAL - true if you also wish to output -moz-border-radius/-webkit-border-radius/border-radius declarations
	**/
	addRule: function(selector, rad, standards) {
		if (typeof rad == 'undefined' || rad === null) {
			rad = 0;
		}
		if (rad.constructor.toString().search('Array') == -1) {
			rad = rad.toString().replace(/[^0-9 ]/g, '').split(' ');
		}
		for (var i=0; i<4; i++) {
			rad[i] = (!rad[i] && rad[i] !== 0) ? rad[Math.max((i-2), 0)] : rad[i];
		}
		if (this.styleSheet) {
			if (this.styleSheet.addRule) { /* IE */
				var selectors = selector.split(','); /* multiple selectors supported, no need for multiple calls to this anymore */
				for (var i=0; i<selectors.length; i++) {
					this.styleSheet.addRule(selectors[i], 'behavior:expression(DD_roundies.roundify.call(this, [' + rad.join(',') + ']))'); /* seems to execute the function without adding it to the stylesheet - interesting... */
				}
			}
			else if (standards) {
				var moz_implementation = rad.join('px ') + 'px';
				this.styleSheet.appendChild(document.createTextNode(selector + ' {border-radius:' + moz_implementation + '; -moz-border-radius:' + moz_implementation + ';}'));
				this.styleSheet.appendChild(document.createTextNode(selector + ' {-webkit-border-top-left-radius:' + rad[0] + 'px ' + rad[0] + 'px; -webkit-border-top-right-radius:' + rad[1] + 'px ' + rad[1] + 'px; -webkit-border-bottom-right-radius:' + rad[2] + 'px ' + rad[2] + 'px; -webkit-border-bottom-left-radius:' + rad[3] + 'px ' + rad[3] + 'px;}'));
			}
		}
		else if (this.IE8) {
			this.selectorsToProcess.push({'selector':selector, 'radii':rad});
		}
	},
	
	readPropertyChanges: function(el) {
		switch (event.propertyName) {
			case 'style.border':
			case 'style.borderWidth':
			case 'style.padding':
				this.applyVML(el);
				break;
			case 'style.borderColor':
				this.vmlStrokeColor(el);
				break;
			case 'style.backgroundColor':
			case 'style.backgroundPosition':
			case 'style.backgroundRepeat':
				this.applyVML(el);
				break;
			case 'style.display':
				el.vmlBox.style.display = (el.style.display == 'none') ? 'none' : 'block';
				break;
			case 'style.filter':
				this.vmlOpacity(el);
				break;
			case 'style.zIndex':
				el.vmlBox.style.zIndex = el.style.zIndex;
				break;
		}
	},
	
	applyVML: function(el) {
		el.runtimeStyle.cssText = '';
		this.vmlFill(el);
		this.vmlStrokeColor(el);
		this.vmlStrokeWeight(el);
		this.vmlOffsets(el);
		this.vmlPath(el);
		this.nixBorder(el);
		this.vmlOpacity(el);
	},
	
	vmlOpacity: function(el) {
		if (el.currentStyle.filter.search('lpha') != -1) {
			var trans = el.currentStyle.filter;
			trans = parseInt(trans.substring(trans.lastIndexOf('=')+1, trans.lastIndexOf(')')), 10)/100;
			for (var v in el.vml) {
				el.vml[v].filler.opacity = trans;
			}
		}
	},
	
	vmlFill: function(el) {
		if (!el.currentStyle) {
			return;
		} else {
			var elStyle = el.currentStyle;
		}
		el.runtimeStyle.backgroundColor = '';
		el.runtimeStyle.backgroundImage = '';
		var noColor = (elStyle.backgroundColor == 'transparent');
		var noImg = true;
		if (elStyle.backgroundImage != 'none' || el.isImg) {
			if (!el.isImg) {
				el.vmlBg = elStyle.backgroundImage;
				el.vmlBg = el.vmlBg.substr(5, el.vmlBg.lastIndexOf('")')-5);
			}
			else {
				el.vmlBg = el.src;
			}
			var lib = this;
			if (!lib.imgSize[el.vmlBg]) { /* determine size of loaded image */
				var img = document.createElement('img');
				img.attachEvent('onload', function() {
					this.width = this.offsetWidth; /* weird cache-busting requirement! */
					this.height = this.offsetHeight;
					lib.vmlOffsets(el);
				});
				img.className = lib.ns + '_sizeFinder';
				img.runtimeStyle.cssText = 'behavior:none; position:absolute; top:-10000px; left:-10000px; border:none;'; /* make sure to set behavior to none to prevent accidental matching of the helper elements! */
				img.src = el.vmlBg;
				img.removeAttribute('width');
				img.removeAttribute('height');
				document.body.insertBefore(img, document.body.firstChild);
				lib.imgSize[el.vmlBg] = img;
			}
			el.vml.image.filler.src = el.vmlBg;
			noImg = false;
		}
		el.vml.image.filled = !noImg;
		el.vml.image.fillcolor = 'none';
		el.vml.color.filled = !noColor;
		el.vml.color.fillcolor = elStyle.backgroundColor;
		el.runtimeStyle.backgroundImage = 'none';
		el.runtimeStyle.backgroundColor = 'transparent';
	},
	
	vmlStrokeColor: function(el) {
		el.vml.stroke.fillcolor = el.currentStyle.borderColor;
	},
	
	vmlStrokeWeight: function(el) {
		var borders = ['Top', 'Right', 'Bottom', 'Left'];
		el.bW = {};
		for (var b=0; b<4; b++) {
			el.bW[borders[b]] = parseInt(el.currentStyle['border' + borders[b] + 'Width'], 10) || 0;
		}
	},
	
	vmlOffsets: function(el) {
		var dims = ['Left', 'Top', 'Width', 'Height'];
		for (var d=0; d<4; d++) {
			el.dim[dims[d]] = el['offset'+dims[d]];
		}
		var assign = function(obj, topLeft) {
			obj.style.left = (topLeft ? 0 : el.dim.Left) + 'px';
			obj.style.top = (topLeft ? 0 : el.dim.Top) + 'px';
			obj.style.width = el.dim.Width + 'px';
			obj.style.height = el.dim.Height + 'px';
		};
		for (var v in el.vml) {
			var mult = (v == 'image') ? 1 : 2;
			el.vml[v].coordsize = (el.dim.Width*mult) + ', ' + (el.dim.Height*mult);
			assign(el.vml[v], true);
		}
		assign(el.vmlBox, false);
		
		if (DD_roundies.IE8) {
			el.vml.stroke.style.margin = '-1px';
			if (typeof el.bW == 'undefined') {
				this.vmlStrokeWeight(el);
			}
			el.vml.color.style.margin = (el.bW.Top-1) + 'px ' + (el.bW.Left-1) + 'px';
		}
	},
	
	vmlPath: function(el) {
		var coords = function(direction, w, h, r, aL, aT, mult) {
			var cmd = direction ? ['m', 'qy', 'l', 'qx', 'l', 'qy', 'l', 'qx', 'l'] : ['qx', 'l', 'qy', 'l', 'qx', 'l', 'qy', 'l', 'm']; /* whoa */
			aL *= mult;
			aT *= mult;
			w *= mult;
			h *= mult;
			var R = r.slice(); /* do not affect original array */
			for (var i=0; i<4; i++) {
				R[i] *= mult;
				R[i] = Math.min(w/2, h/2, R[i]); /* make sure you do not get funky shapes - pick the smallest: half of the width, half of the height, or current value */
			}
			var coords = [
				cmd[0] + Math.floor(0+aL) +','+ Math.floor(R[0]+aT),
				cmd[1] + Math.floor(R[0]+aL) +','+ Math.floor(0+aT),
				cmd[2] + Math.ceil(w-R[1]+aL) +','+ Math.floor(0+aT),
				cmd[3] + Math.ceil(w+aL) +','+ Math.floor(R[1]+aT),
				cmd[4] + Math.ceil(w+aL) +','+ Math.ceil(h-R[2]+aT),
				cmd[5] + Math.ceil(w-R[2]+aL) +','+ Math.ceil(h+aT),
				cmd[6] + Math.floor(R[3]+aL) +','+ Math.ceil(h+aT),
				cmd[7] + Math.floor(0+aL) +','+ Math.ceil(h-R[3]+aT),
				cmd[8] + Math.floor(0+aL) +','+ Math.floor(R[0]+aT)
			];
			if (!direction) {
				coords.reverse();
			}
			var path = coords.join('');
			return path;
		};
	
		if (typeof el.bW == 'undefined') {
			this.vmlStrokeWeight(el);
		}
		var bW = el.bW;
		var rad = el.DD_radii.slice();
		
		/* determine outer curves */
		var outer = coords(true, el.dim.Width, el.dim.Height, rad, 0, 0, 2);
		
		/* determine inner curves */
		rad[0] -= Math.max(bW.Left, bW.Top);
		rad[1] -= Math.max(bW.Top, bW.Right);
		rad[2] -= Math.max(bW.Right, bW.Bottom);
		rad[3] -= Math.max(bW.Bottom, bW.Left);
		for (var i=0; i<4; i++) {
			rad[i] = Math.max(rad[i], 0);
		}
		var inner = coords(false, el.dim.Width-bW.Left-bW.Right, el.dim.Height-bW.Top-bW.Bottom, rad, bW.Left, bW.Top, 2);
		var image = coords(true, el.dim.Width-bW.Left-bW.Right+1, el.dim.Height-bW.Top-bW.Bottom+1, rad, bW.Left, bW.Top, 1);
		
		/* apply huge path string */
		el.vml.color.path = inner;
		el.vml.image.path = image;
		el.vml.stroke.path = outer + inner;
		
		this.clipImage(el);
	},
	
	nixBorder: function(el) {
		var s = el.currentStyle;
		var sides = ['Top', 'Left', 'Right', 'Bottom'];
		for (var i=0; i<4; i++) {
			el.runtimeStyle['padding' + sides[i]] = (parseInt(s['padding' + sides[i]], 10) || 0) + (parseInt(s['border' + sides[i] + 'Width'], 10) || 0) + 'px';
		}
		el.runtimeStyle.border = 'none';
	},
	
	clipImage: function(el) {
		var lib = DD_roundies;
		if (!el.vmlBg || !lib.imgSize[el.vmlBg]) {
			return;
		}
		var thisStyle = el.currentStyle;
		var bg = {'X':0, 'Y':0};
		var figurePercentage = function(axis, position) {
			var fraction = true;
			switch(position) {
				case 'left':
				case 'top':
					bg[axis] = 0;
					break;
				case 'center':
					bg[axis] = 0.5;
					break;
				case 'right':
				case 'bottom':
					bg[axis] = 1;
					break;
				default:
					if (position.search('%') != -1) {
						bg[axis] = parseInt(position, 10) * 0.01;
					}
					else {
						fraction = false;
					}
			}
			var horz = (axis == 'X');
			bg[axis] = Math.ceil(fraction ? (( el.dim[horz ? 'Width' : 'Height'] - (el.bW[horz ? 'Left' : 'Top'] + el.bW[horz ? 'Right' : 'Bottom']) ) * bg[axis]) - (lib.imgSize[el.vmlBg][horz ? 'width' : 'height'] * bg[axis]) : parseInt(position, 10));
			bg[axis] += 1;
		};
		for (var b in bg) {
			figurePercentage(b, thisStyle['backgroundPosition'+b]);
		}
		el.vml.image.filler.position = (bg.X/(el.dim.Width-el.bW.Left-el.bW.Right+1)) + ',' + (bg.Y/(el.dim.Height-el.bW.Top-el.bW.Bottom+1));
		var bgR = thisStyle.backgroundRepeat;
		var c = {'T':1, 'R':el.dim.Width+1, 'B':el.dim.Height+1, 'L':1}; /* these are defaults for repeat of any kind */
		var altC = { 'X': {'b1': 'L', 'b2': 'R', 'd': 'Width'}, 'Y': {'b1': 'T', 'b2': 'B', 'd': 'Height'} };
		if (bgR != 'repeat') {
			c = {'T':(bg.Y), 'R':(bg.X+lib.imgSize[el.vmlBg].width), 'B':(bg.Y+lib.imgSize[el.vmlBg].height), 'L':(bg.X)}; /* these are defaults for no-repeat - clips down to the image location */
			if (bgR.search('repeat-') != -1) { /* now let's revert to dC for repeat-x or repeat-y */
				var v = bgR.split('repeat-')[1].toUpperCase();
				c[altC[v].b1] = 1;
				c[altC[v].b2] = el.dim[altC[v].d]+1;
			}
			if (c.B > el.dim.Height) {
				c.B = el.dim.Height+1;
			}
		}
		el.vml.image.style.clip = 'rect('+c.T+'px '+c.R+'px '+c.B+'px '+c.L+'px)';
	},
	
	pseudoClass: function(el) {
		var self = this;
		setTimeout(function() { /* would not work as intended without setTimeout */
			self.applyVML(el);
		}, 1);
	},
	
	reposition: function(el) {
		this.vmlOffsets(el);
		this.vmlPath(el);
	},
	
	roundify: function(rad) {
		this.style.behavior = 'none';
		if (!this.currentStyle) {
			return;
		}
		else {
			var thisStyle = this.currentStyle;
		}
		var allowed = {BODY: false, TABLE: false, TR: false, TD: false, SELECT: false, OPTION: false, TEXTAREA: false};
		if (allowed[this.nodeName] === false) { /* elements not supported yet */
			return;
		}
		var self = this; /* who knows when you might need a setTimeout */
		var lib = DD_roundies;
		this.DD_radii = rad;
		this.dim = {};

		/* attach handlers */
		var handlers = {resize: 'reposition', move: 'reposition'};
		if (this.nodeName == 'A') {
			var moreForAs = {mouseleave: 'pseudoClass', mouseenter: 'pseudoClass', focus: 'pseudoClass', blur: 'pseudoClass'};
			for (var a in moreForAs) {
				handlers[a] = moreForAs[a];
			}
		}
		for (var h in handlers) {
			this.attachEvent('on' + h, function() {
				lib[handlers[h]](self);
			});
		}
		this.attachEvent('onpropertychange', function() {
			lib.readPropertyChanges(self);
		});
		
		/* ensure that this elent and its parent is given hasLayout (needed for accurate positioning) */
		var giveLayout = function(el) {
			el.style.zoom = 1;
			if (el.currentStyle.position == 'static') {
				el.style.position = 'relative';
			}
		};
		giveLayout(this.offsetParent);
		giveLayout(this);
		
		/* create vml elements */
		this.vmlBox = document.createElement('ignore'); /* IE8 really wants to be encased in a wrapper element for the VML to work, and I don't want to disturb getElementsByTagName('div') - open to suggestion on how to do this differently */
		this.vmlBox.runtimeStyle.cssText = 'behavior:none; position:absolute; margin:0; padding:0; border:0; background:none;'; /* super important - if something accidentally matches this (you yourseld did this once, Drew), you'll get infinitely-created elements and a frozen browser! */
		this.vmlBox.style.zIndex = thisStyle.zIndex;
		this.vml = {'color':true, 'image':true, 'stroke':true};
		for (var v in this.vml) {
			this.vml[v] = document.createElement(lib.ns + ':shape');
			this.vml[v].filler = document.createElement(lib.ns + ':fill');
			this.vml[v].appendChild(this.vml[v].filler);
			this.vml[v].stroked = false;
			this.vml[v].style.position = 'absolute';
			this.vml[v].style.zIndex = thisStyle.zIndex;
			this.vml[v].coordorigin = '1,1';
			this.vmlBox.appendChild(this.vml[v]);
		}
		this.vml.image.fillcolor = 'none';
		this.vml.image.filler.type = 'tile';
		this.parentNode.insertBefore(this.vmlBox, this);
		
		this.isImg = false;
		if (this.nodeName == 'IMG') {
			this.isImg = true;
			this.style.visibility = 'hidden';
		}
		
		setTimeout(function() {
			lib.applyVML(self);
		}, 1);
	}
	
};

try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}
DD_roundies.IEversion();
DD_roundies.createVmlNameSpace();
DD_roundies.createVmlStyleSheet();

if (DD_roundies.IE8 && document.attachEvent && DD_roundies.querySelector) {
	document.attachEvent('onreadystatechange', function() {
		if (document.readyState == 'complete') {
			var selectors = DD_roundies.selectorsToProcess;
			var length = selectors.length;
			var delayedCall = function(node, radii, index) {
				setTimeout(function() {
					DD_roundies.roundify.call(node, radii);
				}, index*100);
			};
			for (var i=0; i<length; i++) {
				var results = document.querySelectorAll(selectors[i].selector);
				var rLength = results.length;
				for (var r=0; r<rLength; r++) {
					if (results[r].nodeName != 'INPUT') { /* IE8 doesn't like to do this to inputs yet */
						delayedCall(results[r], selectors[i].radii, r);
					}
				}
			}
		}
	});
	
	$(document).bind('roundify', function(e, a){
		var selectors = DD_roundies.selectorsToProcess;
		var length = selectors.length;
		var delayedCall = function(node, radii, index) {
			setTimeout(function() {
				DD_roundies.roundify.call(node, radii);
			}, index*100);
		};
		for (var i=0; i<length; i++) {
			var results = document.querySelectorAll(selectors[i].selector);
			var rLength = results.length;
			for (var r=0; r<rLength; r++) {
				if (results[r].nodeName != 'INPUT') { /* IE8 doesn't like to do this to inputs yet */
					delayedCall(results[r], selectors[i].radii, r);
				}
			}
		}
	});
}
