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