/**
* 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(){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(){var a=document.createElement("style");document.documentElement.firstChild.insertBefore(a,document.documentElement.firstChild.firstChild);if(a.styleSheet){try{var c=a.styleSheet;c.addRule(this.ns+"\\:*","{behavior:url(#default#VML)}");this.styleSheet=c}catch(b){}}else{this.styleSheet=a}},addRule:function(b,a,c){if(typeof a=="undefined"||a===null){a=0}if(a.constructor.toString().search("Array")==-1){a=a.toString().replace(/[^0-9 ]/g,"").split(" ")}for(var e=0;e<4;e++){a[e]=(!a[e]&&a[e]!==0)?a[Math.max((e-2),0)]:a[e]}if(this.styleSheet){if(this.styleSheet.addRule){var d=b.split(",");for(var e=0;e<d.length;e++){this.styleSheet.addRule(d[e],"behavior:expression(DD_roundies.roundify.call(this, ["+a.join(",")+"]))")}}else{if(c){var f=a.join("px ")+"px";this.styleSheet.appendChild(document.createTextNode(b+" {border-radius:"+f+"; -moz-border-radius:"+f+";}"));this.styleSheet.appendChild(document.createTextNode(b+" {-webkit-border-top-left-radius:"+a[0]+"px "+a[0]+"px; -webkit-border-top-right-radius:"+a[1]+"px "+a[1]+"px; -webkit-border-bottom-right-radius:"+a[2]+"px "+a[2]+"px; -webkit-border-bottom-left-radius:"+a[3]+"px "+a[3]+"px;}"))}}}else{if(this.IE8){this.selectorsToProcess.push({selector:b,radii:a})}}},readPropertyChanges:function(a){switch(event.propertyName){case"style.border":case"style.borderWidth":case"style.padding":this.applyVML(a);break;case"style.borderColor":this.vmlStrokeColor(a);break;case"style.backgroundColor":case"style.backgroundPosition":case"style.backgroundRepeat":this.applyVML(a);break;case"style.display":a.vmlBox.style.display=(a.style.display=="none")?"none":"block";break;case"style.filter":this.vmlOpacity(a);break;case"style.zIndex":a.vmlBox.style.zIndex=a.style.zIndex;break}},applyVML:function(a){a.runtimeStyle.cssText="";this.vmlFill(a);this.vmlStrokeColor(a);this.vmlStrokeWeight(a);this.vmlOffsets(a);this.vmlPath(a);this.nixBorder(a);this.vmlOpacity(a)},vmlOpacity:function(c){if(c.currentStyle.filter.search("lpha")!=-1){var b=c.currentStyle.filter;b=parseInt(b.substring(b.lastIndexOf("=")+1,b.lastIndexOf(")")),10)/100;for(var a in c.vml){c.vml[a].filler.opacity=b}}},vmlFill:function(d){if(!d.currentStyle){return}else{var c=d.currentStyle}d.runtimeStyle.backgroundColor="";d.runtimeStyle.backgroundImage="";var a=(c.backgroundColor=="transparent");var e=true;if(c.backgroundImage!="none"||d.isImg){if(!d.isImg){d.vmlBg=c.backgroundImage;d.vmlBg=d.vmlBg.substr(5,d.vmlBg.lastIndexOf('")')-5)}else{d.vmlBg=d.src}var f=this;if(!f.imgSize[d.vmlBg]){var b=document.createElement("img");b.attachEvent("onload",function(){this.width=this.offsetWidth;this.height=this.offsetHeight;f.vmlOffsets(d)});b.className=f.ns+"_sizeFinder";b.runtimeStyle.cssText="behavior:none; position:absolute; top:-10000px; left:-10000px; border:none;";b.src=d.vmlBg;b.removeAttribute("width");b.removeAttribute("height");document.body.insertBefore(b,document.body.firstChild);f.imgSize[d.vmlBg]=b}d.vml.image.filler.src=d.vmlBg;e=false}d.vml.image.filled=!e;d.vml.image.fillcolor="none";d.vml.color.filled=!a;d.vml.color.fillcolor=c.backgroundColor;d.runtimeStyle.backgroundImage="none";d.runtimeStyle.backgroundColor="transparent"},vmlStrokeColor:function(a){a.vml.stroke.fillcolor=a.currentStyle.borderColor},vmlStrokeWeight:function(c){var d=["Top","Right","Bottom","Left"];c.bW={};for(var a=0;a<4;a++){c.bW[d[a]]=parseInt(c.currentStyle["border"+d[a]+"Width"],10)||0}},vmlOffsets:function(c){var g=["Left","Top","Width","Height"];for(var f=0;f<4;f++){c.dim[g[f]]=c["offset"+g[f]]}var b=function(i,d){var h=i.style;h.left=(d?0:c.dim.Left)+"px";h.top=(d?0:c.dim.Top)+"px";h.width=c.dim.Width+"px";h.height=c.dim.Height+"px"};for(var a in c.vml){var e=(a=="image")?1:2;c.vml[a].coordsize=(c.dim.Width*e)+", "+(c.dim.Height*e);b(c.vml[a],true)}b(c.vmlBox,false);if(DD_roundies.IE8){c.vml.stroke.style.margin="-1px";if(typeof c.bW=="undefined"){this.vmlStrokeWeight(c)}c.vml.color.style.margin=(c.bW.Top-1)+"px "+(c.bW.Left-1)+"px"}},vmlPath:function(e){var f=function(s,t,o,j,u,p,l){var k=s?["m","qy","l","qx","l","qy","l","qx","l"]:["qx","l","qy","l","qx","l","qy","l","m"];u*=l;p*=l;t*=l;o*=l;var n=j.slice();for(var m=0;m<4;m++){n[m]*=l;n[m]=Math.min(t/2,o/2,n[m])}var q=[k[0]+Math.floor(0+u)+","+Math.floor(n[0]+p),k[1]+Math.floor(n[0]+u)+","+Math.floor(0+p),k[2]+Math.ceil(t-n[1]+u)+","+Math.floor(0+p),k[3]+Math.ceil(t+u)+","+Math.floor(n[1]+p),k[4]+Math.ceil(t+u)+","+Math.ceil(o-n[2]+p),k[5]+Math.ceil(t-n[2]+u)+","+Math.ceil(o+p),k[6]+Math.floor(n[3]+u)+","+Math.ceil(o+p),k[7]+Math.floor(0+u)+","+Math.ceil(o-n[3]+p),k[8]+Math.floor(0+u)+","+Math.floor(n[0]+p)];if(!s){q.reverse()}var v=q.join("");return v};if(typeof e.bW=="undefined"){this.vmlStrokeWeight(e)}var h=e.bW;var a=e.DD_radii.slice();var d=f(true,e.dim.Width,e.dim.Height,a,0,0,2);a[0]-=Math.max(h.Left,h.Top);a[1]-=Math.max(h.Top,h.Right);a[2]-=Math.max(h.Right,h.Bottom);a[3]-=Math.max(h.Bottom,h.Left);for(var c=0;c<4;c++){a[c]=Math.max(a[c],0)}var b=f(false,e.dim.Width-h.Left-h.Right,e.dim.Height-h.Top-h.Bottom,a,h.Left,h.Top,2);var g=f(true,e.dim.Width-h.Left-h.Right+1,e.dim.Height-h.Top-h.Bottom+1,a,h.Left,h.Top,1);e.vml.color.path=b;e.vml.image.path=g;e.vml.stroke.path=d+b;this.clipImage(e)},nixBorder:function(c){var b=c.currentStyle;var d=["Top","Left","Right","Bottom"];for(var a=0;a<4;a++){c.runtimeStyle["padding"+d[a]]=(parseInt(b["padding"+d[a]],10)||0)+(parseInt(b["border"+d[a]+"Width"],10)||0)+"px"}c.runtimeStyle.border="none"},clipImage:function(a){var g=DD_roundies;if(!a.vmlBg||!g.imgSize[a.vmlBg]){return}var e=a.currentStyle;var d={X:0,Y:0};var l=function(m,b){var c=true;switch(b){case"left":case"top":d[m]=0;break;case"center":d[m]=0.5;break;case"right":case"bottom":d[m]=1;break;default:if(b.search("%")!=-1){d[m]=parseInt(b,10)*0.01}else{c=false}}var n=(m=="X");d[m]=Math.ceil(c?((a.dim[n?"Width":"Height"]-(a.bW[n?"Left":"Top"]+a.bW[n?"Right":"Bottom"]))*d[m])-(g.imgSize[a.vmlBg][n?"width":"height"]*d[m]):parseInt(b,10));d[m]+=1};for(var h in d){l(h,e["backgroundPosition"+h])}a.vml.image.filler.position=(d.X/(a.dim.Width-a.bW.Left-a.bW.Right+1))+","+(d.Y/(a.dim.Height-a.bW.Top-a.bW.Bottom+1));var k=e.backgroundRepeat;var f={T:1,R:a.dim.Width+1,B:a.dim.Height+1,L:1};var j={X:{b1:"L",b2:"R",d:"Width"},Y:{b1:"T",b2:"B",d:"Height"}};if(k!="repeat"){f={T:(d.Y),R:(d.X+g.imgSize[a.vmlBg].width),B:(d.Y+g.imgSize[a.vmlBg].height),L:(d.X)};if(k.search("repeat-")!=-1){var i=k.split("repeat-")[1].toUpperCase();f[j[i].b1]=1;f[j[i].b2]=a.dim[j[i].d]+1}if(f.B>a.dim.Height){f.B=a.dim.Height+1}}a.vml.image.style.clip="rect("+f.T+"px "+f.R+"px "+f.B+"px "+f.L+"px)"},pseudoClass:function(b){var a=this;setTimeout(function(){a.applyVML(b)},1)},reposition:function(a){this.vmlOffsets(a);this.vmlPath(a)},roundify:function(g){var k=document;this.style.behavior="none";if(!this.currentStyle){return}else{var e=this.currentStyle}var j={BODY:false,TABLE:false,TR:false,TD:false,SELECT:false,OPTION:false,TEXTAREA:false};if(j[this.nodeName]===false){return}var n=this;var f=DD_roundies;this.DD_radii=g;this.dim={};var c={resize:"reposition",move:"reposition"};if(this.nodeName=="A"){var b={mouseleave:"pseudoClass",mouseenter:"pseudoClass",focus:"pseudoClass",blur:"pseudoClass"};for(var i in b){c[i]=b[i]}}for(var d in c){this.attachEvent("on"+d,function(){f[c[d]](n)})}this.attachEvent("onpropertychange",function(){f.readPropertyChanges(n)});var m=function(h){var a=h.style;a.zoom=1;if(h.currentStyle.position=="static"){a.position="relative"}};m(this.offsetParent);m(this);this.vmlBox=k.createElement("ignore");this.vmlBox.style.display="none";this.vmlBox.runtimeStyle.cssText="behavior:none; position:absolute; margin:0; padding:0; border:0; background:none;";this.vmlBox.style.zIndex=e.zIndex;this.vml={color:true,image:true,stroke:true};for(var l in this.vml){this.vml[l]=k.createElement(f.ns+":shape");this.vml[l].filler=k.createElement(f.ns+":fill");this.vml[l].appendChild(this.vml[l].filler);this.vml[l].stroked=false;this.vml[l].style.position="absolute";this.vml[l].style.zIndex=e.zIndex;this.vml[l].coordorigin="1,1";this.vmlBox.appendChild(this.vml[l])}this.vml.image.fillcolor="none";this.vml.image.filler.type="tile";this.parentNode.insertBefore(this.vmlBox,this);this.vmlBox.style.display="block";this.isImg=false;if(this.nodeName=="IMG"){this.isImg=true;this.style.visibility="hidden"}setTimeout(function(){f.applyVML(n)},1)}};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 c=DD_roundies.selectorsToProcess;var f=c.length;var e=function(i,j,h){setTimeout(function(){DD_roundies.roundify.call(i,j)},h*100)};for(var b=0;b<f;b++){var a=document.querySelectorAll(c[b].selector);var g=a.length;for(var d=0;d<g;d++){if(a[d].nodeName!="INPUT"){e(a[d],c[b].radii,d)}}}}})};
