// Copyright 2006 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// patched with http://code.google.com/p/explorercanvas/issues/detail?id=79 compressed with AjaxMin
document.createElement("canvas").getContext||function(){function ut(){return this.context_||(this.context_=new a(this))}function rt(n,t){var r=d.call(arguments,2);return function(){return n.apply(t,r.concat(d.call(arguments)))}}function it(n){var t=n.srcElement;switch(n.propertyName){case"width":t.style.width=t.attributes.width.nodeValue+"px",t.getContext().clearRect();break;case"height":t.style.height=t.attributes.height.nodeValue+"px",t.getContext().clearRect()}}function nt(n){var t=n.srcElement;t.firstChild&&(t.firstChild.style.width=t.clientWidth+"px",t.firstChild.style.height=t.clientHeight+"px")}function l(){return[[1,0,0],[0,1,0],[0,0,1]]}function e(n,t){for(var e=l(),i,f,r,u=0;u<3;u++)for(i=0;i<3;i++){for(f=0,r=0;r<3;r++)f+=n[u][r]*t[r][i];e[u][i]=f}return e}function p(n,t){t.fillStyle=n.fillStyle,t.lineCap=n.lineCap,t.lineJoin=n.lineJoin,t.lineWidth=n.lineWidth,t.miterLimit=n.miterLimit,t.shadowBlur=n.shadowBlur,t.shadowColor=n.shadowColor,t.shadowOffsetX=n.shadowOffsetX,t.shadowOffsetY=n.shadowOffsetY,t.strokeStyle=n.strokeStyle,t.globalAlpha=n.globalAlpha,t.arcScaleX_=n.arcScaleX_,t.arcScaleY_=n.arcScaleY_,t.lineScale_=n.lineScale_}function g(n){var i,f=1,t;if(n=String(n),n.substring(0,3)=="rgb"){var u=n.indexOf("(",3),e=n.indexOf(")",u+1),r=n.substring(u+1,e).split(",");for(i="#",t=0;t<3;t++)i+=c[Number(r[t])];r.length==4&&n.substr(3,1)=="a"&&(f=r[3])}else i=n;return{color:i,alpha:f}}function ft(n){switch(n){case"butt":return"flat";case"round":return"round";case"square":default:return"square"}}function a(n){this.m_=l(),this.mStack_=[],this.aStack_=[],this.currentPath_=[],this.strokeStyle="#000",this.fillStyle="#000",this.lineWidth=1,this.lineJoin="miter",this.lineCap="butt",this.miterLimit=i*1,this.globalAlpha=1,this.canvas=n;var t=n.ownerDocument.createElement("div");t.style.width=n.clientWidth+"px",t.style.height=n.clientHeight+"px",t.style.overflow="hidden",t.style.position="absolute",n.appendChild(t),this.element_=t,this.arcScaleX_=1,this.arcScaleY_=1,this.lineScale_=1}function b(n,t,i,r){n.currentPath_.push({type:"bezierCurveTo",cp1x:t.x,cp1y:t.y,cp2x:i.x,cp2y:i.y,x:r.x,y:r.y}),n.currentX_=r.x,n.currentY_=r.y}function tt(n){for(var t,i=0;i<3;i++)for(t=0;t<2;t++)if(!isFinite(n[i][t])||isNaN(n[i][t]))return!1;return!0}function s(n,t,i){if(tt(t)&&(n.m_=t,i)){var r=t[0][0]*t[1][1]-t[0][1]*t[1][0];n.lineScale_=ot(et(r))}}function h(n){this.type_=n,this.x0_=0,this.y0_=0,this.r0_=0,this.x1_=0,this.y1_=0,this.r1_=0,this.colors_=[]}function w(){}var r=Math,t=r.round,y=r.sin,v=r.cos,et=r.abs,ot=r.sqrt,i=10,u=i/2,d=Array.prototype.slice,k={init:function(n){if(/MSIE/.test(navigator.userAgent)&&!window.opera){var t=n||document;t.createElement("canvas"),t.readyState!=="complete"?t.attachEvent("onreadystatechange",rt(this.init_,this,t)):this.init_(t)}},init_:function(n){var i,r,t;for(n.namespaces.g_vml_||n.namespaces.add("g_vml_","urn:schemas-microsoft-com:vml","#default#VML"),n.namespaces.g_o_||n.namespaces.add("g_o_","urn:schemas-microsoft-com:office:office","#default#VML"),n.styleSheets.ex_canvas_||(i=n.createStyleSheet(),i.owningElement.id="ex_canvas_",i.cssText="canvas{display:inline-block;overflow:hidden;text-align:left;width:300px;height:150px}g_vml_\\:*{behavior:url(#default#VML)}g_o_\\:*{behavior:url(#default#VML)}"),r=n.getElementsByTagName("canvas"),t=0;t<r.length;t++)this.initElement(r[t])},initElement:function(n){if(!n.getContext){n.getContext=ut,n.innerHTML="",n.attachEvent("onpropertychange",it),n.attachEvent("onresize",nt);var t=n.attributes;t.width&&t.width.specified?n.style.width=t.width.nodeValue+"px":n.width=n.clientWidth,t.height&&t.height.specified?n.style.height=t.height.nodeValue+"px":n.height=n.clientHeight}return n}},c,f,o,n;for(k.init(),c=[],f=0;f<16;f++)for(o=0;o<16;o++)c[f*16+o]=f.toString(16)+o.toString(16);n=a.prototype,n.clearRect=function(){this.element_.innerHTML=""},n.beginPath=function(){this.currentPath_=[]},n.moveTo=function(n,t){var i=this.getCoords_(n,t);this.currentPath_.push({type:"moveTo",x:i.x,y:i.y}),this.currentX_=i.x,this.currentY_=i.y},n.lineTo=function(n,t){var i=this.getCoords_(n,t);this.currentPath_.push({type:"lineTo",x:i.x,y:i.y}),this.currentX_=i.x,this.currentY_=i.y},n.bezierCurveTo=function(n,t,i,r,u,f){var s=this.getCoords_(u,f),o=this.getCoords_(n,t),e=this.getCoords_(i,r);b(this,o,e,s)},n.quadraticCurveTo=function(n,t,i,r){var e=this.getCoords_(n,t),f=this.getCoords_(i,r),u={x:this.currentX_+2/3*(e.x-this.currentX_),y:this.currentY_+2/3*(e.y-this.currentY_)},o={x:u.x+(f.x-this.currentX_)/3,y:u.y+(f.y-this.currentY_)/3};b(this,u,o,f)},n.arc=function(n,t,r,f,e,o){r*=i;var p=o?"at":"wa",s=n+v(f)*r-u,b=t+y(f)*r-u,l=n+v(e)*r-u,w=t+y(e)*r-u;s!=l||o||(s+=.125);var h=this.getCoords_(n,t),c=this.getCoords_(s,b),a=this.getCoords_(l,w);this.currentPath_.push({type:p,x:h.x,y:h.y,radius:r,xStart:c.x,yStart:c.y,xEnd:a.x,yEnd:a.y})},n.rect=function(n,t,i,r){this.moveTo(n,t),this.lineTo(n+i,t),this.lineTo(n+i,t+r),this.lineTo(n,t+r),this.closePath()},n.strokeRect=function(n,t,i,r){var u=this.currentPath_;this.beginPath(),this.moveTo(n,t),this.lineTo(n+i,t),this.lineTo(n+i,t+r),this.lineTo(n,t+r),this.closePath(),this.stroke(),this.currentPath_=u},n.fillRect=function(n,t,i,r){var u=this.currentPath_;this.beginPath(),this.moveTo(n,t),this.lineTo(n+i,t),this.lineTo(n+i,t+r),this.lineTo(n,t+r),this.closePath(),this.fill(),this.currentPath_=u},n.createLinearGradient=function(n,t,i,r){var u=new h("gradient");return u.x0_=n,u.y0_=t,u.x1_=i,u.y1_=r,u},n.createRadialGradient=function(n,t,i,r,u,f){var e=new h("gradientradial");return e.x0_=n,e.y0_=t,e.r0_=i,e.x1_=r,e.y1_=u,e.r1_=f,e},n.drawImage=function(n){var e,f,l,c,a,w,b,p,rt=n.runtimeStyle.width,ut=n.runtimeStyle.height,o,s,k;if(n.runtimeStyle.width="auto",n.runtimeStyle.height="auto",o=n.width,s=n.height,n.runtimeStyle.width=rt,n.runtimeStyle.height=ut,arguments.length==3)e=arguments[1],f=arguments[2],a=w=0,b=l=o,p=c=s;else if(arguments.length==5)e=arguments[1],f=arguments[2],l=arguments[3],c=arguments[4],a=w=0,b=o,p=s;else if(arguments.length==9)a=arguments[1],w=arguments[2],b=arguments[3],p=arguments[4],e=arguments[5],f=arguments[6],l=arguments[7],c=arguments[8];else throw Error("Invalid number of arguments");var y=this.getCoords_(e,f),ft=b/2,et=p/2,v=[],it=10,d=10;if(v.push(" <g_vml_:group",' coordsize="',i*it,",",i*d,'"',' coordorigin="0,0"',' style="width:',it,"px;height:",d,"px;position:absolute;"),this.m_[0][0]!=1||this.m_[0][1]){k=[],k.push("M11=",this.m_[0][0],",","M12=",this.m_[1][0],",","M21=",this.m_[0][1],",","M22=",this.m_[1][1],",","Dx=",t(y.x/i),",","Dy=",t(y.y/i),"");var h=y,nt=this.getCoords_(e+l,f),g=this.getCoords_(e,f+c),tt=this.getCoords_(e+l,f+c);h.x=r.max(h.x,nt.x,g.x,tt.x),h.y=r.max(h.y,nt.y,g.y,tt.y),v.push("padding:0 ",t(h.x/i),"px ",t(h.y/i),"px 0;filter:progid:DXImageTransform.Microsoft.Matrix(",k.join(""),", sizingmethod='clip');")}else v.push("top:",t(y.y/i),"px;left:",t(y.x/i),"px;");v.push(' ">','<g_vml_:image src="',n.src,'"',' style="width:',i*l,"px;"," height:",i*c,'px;"',' cropleft="',a/o,'"',' croptop="',w/s,'"',' cropright="',(o-a-b)/o,'"',' cropbottom="',(s-w-p)/s,'"'," />","</g_vml_:group>"),this.element_.insertAdjacentHTML("BeforeEnd",v.join(""))},n.stroke=function(n){var f=[],ti=!1,et=g(n?this.fillStyle:this.strokeStyle),ot=et.color,tt=et.alpha*this.globalAlpha,ht=10,st=10,u,lt,a,d,s,c,y;f.push("<g_vml_:shape",' filled="',!!n,'"',' style="position:absolute;width:',ht,"px;height:",st,'px;"',' coordorigin="0 0" coordsize="',i*ht," ",i*st,'"',' stroked="',!n,'"',' path="');var ni=!1,o={x:null,y:null},h={x:null,y:null};for(c=0;c<this.currentPath_.length;c++){u=this.currentPath_[c];switch(u.type){case"moveTo":lt=u,f.push(" m ",t(u.x),",",t(u.y));break;case"lineTo":f.push(" l ",t(u.x),",",t(u.y));break;case"close":f.push(" x "),u=null;break;case"bezierCurveTo":f.push(" c ",t(u.cp1x),",",t(u.cp1y),",",t(u.cp2x),",",t(u.cp2y),",",t(u.x),",",t(u.y));break;case"at":case"wa":f.push(" ",u.type," ",t(u.x-this.arcScaleX_*u.radius),",",t(u.y-this.arcScaleY_*u.radius)," ",t(u.x+this.arcScaleX_*u.radius),",",t(u.y+this.arcScaleY_*u.radius)," ",t(u.xStart),",",t(u.yStart)," ",t(u.xEnd),",",t(u.yEnd))}u&&((o.x==null||u.x<o.x)&&(o.x=u.x),(h.x==null||u.x>h.x)&&(h.x=u.x),(o.y==null||u.y<o.y)&&(o.y=u.y),(h.y==null||u.y>h.y)&&(h.y=u.y))}if(f.push(' ">'),n)if(typeof this.fillStyle=="object"){var e=this.fillStyle,l=0,nt={x:0,y:0},w=0,rt=1;if(e.type_=="gradient"){var dt=e.x0_/this.arcScaleX_,gt=e.y0_/this.arcScaleY_,kt=e.x1_/this.arcScaleX_,bt=e.y1_/this.arcScaleY_,v=this.getCoords_(dt,gt),it=this.getCoords_(kt,bt),wt=it.x-v.x,pt=it.y-v.y;l=Math.atan2(wt,pt)*180/Math.PI,l<0&&(l+=360),l<1e-6&&(l=0)}else{var v=this.getCoords_(e.x0_,e.y0_),b=h.x-o.x,k=h.y-o.y;nt={x:(v.x-o.x)/b,y:(v.y-o.y)/k},b/=this.arcScaleX_*i,k/=this.arcScaleY_*i,d=r.max(b,k),w=2*e.r0_/d,rt=2*e.r1_/d-w}s=e.colors_,s.sort(function(n,t){return n.offset-t.offset});var p=s.length,ct=s[0].color,at=s[p-1].color,yt=s[0].alpha*this.globalAlpha,vt=s[p-1].alpha*this.globalAlpha,ut=[];for(c=0;c<p;c++)y=s[c],ut.push(y.offset*rt+w+" "+y.color);f.push('<g_vml_:fill type="',e.type_,'"',' method="none" focus="100%"',' color="',ct,'"',' color2="',at,'"',' colors="',ut.join(","),'"',' opacity="',vt,'"',' g_o_:opacity2="',yt,'"',' angle="',l,'"',' focusposition="',nt.x,",",nt.y,'" />')}else f.push('<g_vml_:fill color="',ot,'" opacity="',tt,'" />');else a=this.lineScale_*this.lineWidth,a<1&&(tt*=a),f.push("<g_vml_:stroke",' opacity="',tt,'"',' joinstyle="',this.lineJoin,'"',' miterlimit="',this.miterLimit,'"',' endcap="',ft(this.lineCap),'"',' weight="',a,'px"',' color="',ot,'" />');f.push("</g_vml_:shape>"),this.element_.insertAdjacentHTML("beforeEnd",f.join(""))},n.fill=function(){this.stroke(!0)},n.closePath=function(){this.currentPath_.push({type:"close"})},n.getCoords_=function(n,t){var r=this.m_;return{x:i*(n*r[0][0]+t*r[1][0]+r[2][0])-u,y:i*(n*r[0][1]+t*r[1][1]+r[2][1])-u}},n.save=function(){var n={};p(this,n),this.aStack_.push(n),this.mStack_.push(this.m_),this.m_=e(l(),this.m_)},n.restore=function(){p(this.aStack_.pop(),this),this.m_=this.mStack_.pop()},n.translate=function(n,t){var i=[[1,0,0],[0,1,0],[n,t,1]];s(this,e(i,this.m_),!1)},n.rotate=function(n){var i=v(n),t=y(n),r=[[i,t,0],[-t,i,0],[0,0,1]];s(this,e(r,this.m_),!1)},n.scale=function(n,t){this.arcScaleX_*=n,this.arcScaleY_*=t;var i=[[n,0,0],[0,t,0],[0,0,1]];s(this,e(i,this.m_),!0)},n.transform=function(n,t,i,r,u,f){var o=[[n,t,0],[i,r,0],[u,f,1]];s(this,e(o,this.m_),!0)},n.setTransform=function(n,t,i,r,u,f){var e=[[n,t,0],[i,r,0],[u,f,1]];s(this,e,!0)},n.clip=function(){},n.arcTo=function(){},n.createPattern=function(){return new w},h.prototype.addColorStop=function(n,t){t=g(t),this.colors_.push({offset:n,color:t.color,alpha:t.alpha})},G_vmlCanvasManager=k,CanvasRenderingContext2D=a,CanvasGradient=h,CanvasPattern=w}();