(function(){tinymce.create("tinymce.plugins.Layer",{init:function(e,d){var f=this;f.editor=e;e.addCommand("mceInsertLayer",f._insertLayer,f);e.addCommand("mceMoveForward",function(){f._move(1)});e.addCommand("mceMoveBackward",function(){f._move(-1)});e.addCommand("mceMakeAbsolute",function(){f._toggleAbsolute()});e.addButton("moveforward",{title:"layer.forward_desc",cmd:"mceMoveForward"});e.addButton("movebackward",{title:"layer.backward_desc",cmd:"mceMoveBackward"});e.addButton("absolute",{title:"layer.absolute_desc",cmd:"mceMakeAbsolute"});e.addButton("insertlayer",{title:"layer.insertlayer_desc",cmd:"mceInsertLayer"});e.onInit.add(function(){if(tinymce.isIE){e.getDoc().execCommand("2D-Position",false,true)}});e.onNodeChange.add(f._nodeChange,f);e.onVisualAid.add(f._visualAid,f)},getInfo:function(){return{longname:"Layer",author:"Moxiecode Systems AB",authorurl:"http://tinymce.moxiecode.com",infourl:"http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/layer",version:tinymce.majorVersion+"."+tinymce.minorVersion}},_nodeChange:function(f,g,h){var j,i;j=this._getParentLayer(h);i=f.dom.getParent(h,"DIV,P,IMG");if(!i){g.setDisabled("absolute",1);g.setDisabled("moveforward",1);g.setDisabled("movebackward",1)}else{g.setDisabled("absolute",0);g.setDisabled("moveforward",!j);g.setDisabled("movebackward",!j);g.setActive("absolute",j&&j.style.position.toLowerCase()=="absolute")}},_visualAid:function(f,h,e){var g=f.dom;tinymce.each(g.select("div,p",h),function(a){if(/^(absolute|relative|static)$/i.test(a.style.position)){if(e){g.addClass(a,"mceItemVisualAid")}else{g.removeClass(a,"mceItemVisualAid")}}})},_move:function(l){var d=this.editor,n,m=[],o=this._getParentLayer(d.selection.getNode()),p=-1,k=-1,i;i=[];tinymce.walk(d.getBody(),function(a){if(a.nodeType==1&&/^(absolute|relative|static)$/i.test(a.style.position)){i.push(a)}},"childNodes");for(n=0;n<i.length;n++){m[n]=i[n].style.zIndex?parseInt(i[n].style.zIndex):0;if(p<0&&i[n]==o){p=n}}if(l<0){for(n=0;n<m.length;n++){if(m[n]<m[p]){k=n;break}}if(k>-1){i[p].style.zIndex=m[k];i[k].style.zIndex=m[p]}else{if(m[p]>0){i[p].style.zIndex=m[p]-1}}}else{for(n=0;n<m.length;n++){if(m[n]>m[p]){k=n;break}}if(k>-1){i[p].style.zIndex=m[k];i[k].style.zIndex=m[p]}else{i[p].style.zIndex=m[p]+1}}d.execCommand("mceRepaint")},_getParentLayer:function(b){return this.editor.dom.getParent(b,function(a){return a.nodeType==1&&/^(absolute|relative|static)$/i.test(a.style.position)})},_insertLayer:function(){var d=this.editor,c=d.dom.getPos(d.dom.getParent(d.selection.getNode(),"*"));d.dom.add(d.getBody(),"div",{style:{position:"absolute",left:c.x,top:(c.y>20?c.y:20),width:100,height:100},"class":"mceItemVisualAid"},d.selection.getContent()||d.getLang("layer.content"))},_toggleAbsolute:function(){var d=this.editor,c=this._getParentLayer(d.selection.getNode());if(!c){c=d.dom.getParent(d.selection.getNode(),"DIV,P,IMG")}if(c){if(c.style.position.toLowerCase()=="absolute"){d.dom.setStyles(c,{position:"",left:"",top:"",width:"",height:""});d.dom.removeClass(c,"mceItemVisualAid")}else{if(c.style.left==""){c.style.left=20+"px"}if(c.style.top==""){c.style.top=20+"px"}if(c.style.width==""){c.style.width=c.width?(c.width+"px"):"100px"}if(c.style.height==""){c.style.height=c.height?(c.height+"px"):"100px"}c.style.position="absolute";d.addVisual(d.getBody())}d.execCommand("mceRepaint");d.nodeChanged()}}});tinymce.PluginManager.add("layer",tinymce.plugins.Layer)})();