(function(I){var L=0;I.widget("ui.htmlInput",{_init:function(){var O=this;this.toolbar=new J("<div class='ui-widget-header'></div>",function(U,T){switch(T.attr("name")){case"link":var S=N(O.selectedElement,["a"]);if(S&&!I.browser.msie){O.setSelected(S)}O.options.editLink(U,O,S,"insertLink");break;case"image":O.options.editImage(U,O,N(O.selectedElement,["img"]),"insertImage");break;default:var R=T.attr("value")?T.attr("value").split("."):"";O._applyCommand(T.attr("name"),R[0]);if(R.length==2){I(O.getSelected()).addClass(R[1])}break}});this.toolbar.addButtons(this.options);if(I.browser.safari){this.toolbar.element.css("height","21px")}this.container=I("<div class='ui-htmlInput ui-widget' style='position:relative;overflow:hidden;'></div>").addClass(this.element.attr("class")).attr("id",this.element.attr("id").concat("_container")).append(this.toolbar.element);this.element.after(this.container);var Q=this.toolbar.element.outerHeight();if(!this.options.showToolbar){this.toolbar.element.css("display","none");Q=0}var P=this.element.outerHeight()-Q-1;L++;this._frameId="htmlInput_".concat(L);this._frame=I("<iframe frameborder='no' border='0' marginWidth='0' marginHeight='0' leftMargin='0' topMargin='0' rightMargin='0' bottomMargin='0' width='100%' height='".concat(P,"px' allowTransparency='true' scroll='yes'>")).attr("id",this._frameId).attr("src",I.browser.msie?"javascript:false;":"javascript:;");if(!this.options.debug){this.element.css("display","none")}this.container.append(I("<div class='ui-widget-content'/>").append(this._frame));this._setContent(this.element.val());this.container.height(this.element.outerHeight()).width(this.options.widthAuto?"auto":this.element.width());this._frame.css({width:"100%",height:P-1+"px"});P=this._frame.height();this._bindEvents();if(I.browser.mozilla||I.browser.safari){this._frame.parents().bind("DOMNodeInserted.htmlInput",function(R){if(I(R.target).find("#".concat(O._frameId)).length==1){setTimeout(function(){O._setContent(this.element.val());O._bindEvents()},0);R.stopPropagation()}})}if(window.getSelection){this.bookmark=function(){O._bookmark=O._window.getSelection()};this.restoreBookmark=function(){if(O._bookmark){var R=O._window.getSelection();R.removeAllRanges();R.addRange(O._bookmark);O._bookmark=null}};this.getSelected=function(){var T=O._window.getSelection();if(T.rangeCount>0){var R=T.getRangeAt(0);var S=R.collapsed||R.startContainer.childNodes.length==0?T.focusNode:R.startContainer.childNodes[R.startOffset];return S.tagName==undefined?S.parentNode:S}};this.getSelectedHTML=function(){var T=O._window.getSelection();if(T.rangeCount>0){var R=T.getRangeAt(0);var S=document.createElement("div");S.appendChild(R.cloneContents());return S.innerHTML}};this.setSelected=function(T){var S=O._window.getSelection();S.removeAllRanges();if(T){var R=O._document.createRange();R.selectNodeContents(T);S.addRange(R)}};this.insertHtml=function(R){O._document.execCommand("insertHTML",null,R);O._update()}}else{this.bookmark=function(){try{O._bookmark=O._document.selection.createRange()}catch(R){}};this.getSelected=function(){O._window.focus();var R=O._document.selection.createRange();return R.item?R.item(0):R.parentElement()};this.getSelectedHTML=function(){O._window.focus();var R=O._document.selection.createRange();return R.htmlText?R.htmlText:R.item?R.item(0).outerHTML:""};this.setSelected=function(S){try{O._window.focus();var R=O._document.selection.createRange();R=O._document.body.createTextRange();R.moveToElementText(S);R.select()}catch(T){}};this.insertHtml=function(S){var R=this._document.selection.createRange();if(R.item){R.item(0).outerHTML=S}else{R.pasteHTML(S)}O._update()};this.restoreBookmark=function(){if(O._bookmark){O._bookmark.select();O._bookmark=null}}}},value:function(O){if(arguments.length){O=this._htmlClean(O);if(O!=this.element.val()){this.element.val(O);this._document.body.innerHTML=O;this._triggerChange()}return this}return this._htmlClean(this._document.body.innerHTML)},clean:function(){html=this._htmlClean(this.value());if(html!=this.element.val()){this.element.val(html);this._document.body.innerHTML=html;this._triggerChange()}},insertLink:function(U,S,P){if(U=="http:/".concat("/")||U==""){U==null}this._window.focus();var R=N(this.selectedElement,["a"]);if(U==null){if(R!=null){this._document.execCommand("unLink",false,null)}return }else{if(R==null){var Q=this.selectedHTML,T="",O=Q.length-/\s*$/.exec(Q).length;if(O>-1){T=Q.substring(O);Q=Q.substring(0,O)}this.insertHtml("<a href='".concat(U,"'>",Q,"</a>",T));R=N(this.getSelected(),["a"])}}R=I(R);R.attr("href",U);S!=null&&S.length>0?R.attr("target",S):R.removeAttr("target");P!=null&&P.length>0?R.attr("type",P):R.removeAttr("type");this._update()},insertImage:function(P){this._window.focus();var O=N(this.selectedElement,["img"]);if(O==null&&P!=null){this._document.execCommand("insertimage",false,P);this.selectedElement=this.getSelected();O=N(this.selectedElement,["img"])}else{if(O!=null&&P==null){return }}O=I(O);O.attr("src",P);O.attr("alt",O.attr("alt")||"");this._update()},focus:function(){this._window.focus()},destroy:function(){I.widget.prototype.destroy.apply(this,arguments);I(this._document).add(this._document.body).add(this._frame).unbind(".htmlInput")},_update:function(){if(this.getSelected().tagName=="BODY"||(I.browser.safari&&this.getSelected().tagName=="DIV")){this._document.execCommand("formatblock",false,"<p>")}this.selectedElement=this.getSelected();this.selectedHTML=this.getSelectedHTML();if(this.options.debug){this._showStatus("_update")}for(var P in this.toolbar.tools){var S=this.toolbar.tools[P];var Q=false;var O=true;switch(P){case G.bulletList.command:case G.numberList.command:var R=H(this.selectedElement,["ul","ol"]);Q=(P==G.bulletList.command&&R=="ul")||(P==G.numberList.command&&R=="ol");break;case G.bold.command:Q=N(this.selectedElement,["strong","b",["span",{style:/weight:\s*bold/i}]]);break;case G.italic.command:Q=N(this.selectedElement,["em","i",["span",{style:/style:\s*italic/i}]]);break;case G.superscript.command:Q=N(this.selectedElement,["sup",["span",{style:/-align:\s*super/i}]]);break;case G.subscript.command:Q=N(this.selectedElement,["sub",["span",{style:/-align:\s*sub/i}]]);break;case G.block.command:S.command.val(H(this.selectedElement,S.data("tags")));continue;case G.link.command:Q=N(this.selectedElement,["a"]);O=Q||(this.selectedHTML&&this.selectedHTML.length>0)||N(this.selectedElement,["img"]);break;case G.image.command:Q=N(this.selectedElement,["img"]);break;case G.leftAlign.command:Q=I(N(this.selectedElement,this.options.canAlign)).hasClass("left");break;case G.middleAlign.command:Q=I(N(this.selectedElement,this.options.canAlign)).hasClass("middle");break;case G.rightAlign.command:Q=I(N(this.selectedElement,this.options.canAlign)).hasClass("right");break}Q?S.addClass("ui-state-active"):S.removeClass("ui-state-active");O?S.removeClass("ui-state-disabled"):S.addClass("ui-state-disabled")}},_updateElement:function(){var O=this._htmlClean(this._document.body.innerHTML);if(O!=this.element.val()){this.element.val(O);this._triggerChange()}},_showStatus:function(O){window.status=O.concat(": selected html: '",this.selectedHTML,"' element: ",(this.selectedElement?this.selectedElement.tagName==undefined?this.selectedElement.toString():this.selectedElement.tagName:""))},_htmlClean:function(P,O){return this.options.clean&&I.htmlClean?I.htmlClean(P,{allowedClasses:this.options.allowedClasses,replace:O,format:this.options.format,formatIndent:this.options.formatIndent}):P},_triggerChange:function(O){this.element.trigger("change",O,{value:this.value()});if(this.options.debug){this._showStatus("changed")}},_bindEvents:function(){var O=this;I(this._document).unbind(".htmlInput").bind("keydown.htmlInput keypress.htmlInput keyup.htmlInput mouseup.htmlInput focus.htmlInput blur.htmlInput",function(Q){O._bubbleEvent(Q)});if(this.element[0].form){I(this.element[0].form).unbind(".htmlInput").bind("submit.htmlInput",function(){O.clean()})}if(I.browser.msie){this._frame.unbind(".htmlInput").bind("focus.htmlInput blur.htmlInput",function(Q){O._bubbleEvent(Q)});this._document.body.onpaste=function(){setTimeout(function(){O.clean()},0)};this._document.onbeforedeactivate=function(){O.bookmark()};this._document.onactivate=function(){O.restoreBookmark()}}if(I.browser.safari){I.each("keydown keypress keyup mouseup focus blur".split(" "),function(Q){O._window.removeEventListener(this);O._window.addEventListener(this,function(R){O._bubbleEvent(R)},false)})}if(I.browser.mozilla){try{I(this._document.body).unbind(".htmlInput").bind("input.htmlInput",function(){O.clean()})}catch(P){}}},_bubbleEvent:function(O){if(O){switch(O.type){case"blur":this._updateElement();break;case"mouseup":case"keyup":this._update();break}this.element.triggerHandler(O.type,O)}},_setContent:function(P){this._window=this._frame.attr("contentWindow")?this._frame.attr("contentWindow"):window.frames[this._frameId].window;this._document=this._window.document;P="<html><head>".concat("<link href='",this.options.styleUrl,"' rel='stylesheet' type='text/css' />",this.options.baseUrl?"<base href='".concat(this.options.baseUrl,"' />"):"","<style type='text/css'>body{overflow:auto;}</style></head><body class='editor'>",this._htmlClean(P,[[["strong","big",/span.*?weight:\s*bold/i],"b"],[["em",/span.*?style:\s*italic/i],"i"],[[/span.*?-align:\s*super/i],"sup"],[[/span.*?-align:\s*sub/i],"sub"]]),"</body></html>");try{this._document.designMode="on";this._document.open();this._document.write(P);this._document.close()}catch(Q){}try{this._document.execCommand("useCSS",false,true);this._document.execCommand("styleWithCSS",false,false)}catch(O){}},_applyCommand:function(P,O){this.focus();switch(P){default:this._document.execCommand(P,false,null);break;case G.block.command:this._document.execCommand(P,false,"<"+O+">");break;case G.leftAlign.command:I(N(this.selectedElement,this.options.canAlign)).removeClass(G.middleAlign.command).removeClass(G.rightAlign.command).toggleClass(P);break;case G.middleAlign.command:I(N(this.selectedElement,this.options.canAlign)).removeClass(G.leftAlign.command).removeClass(G.rightAlign.command).toggleClass(P);break;case G.rightAlign.command:I(N(this.selectedElement,this.options.canAlign)).removeClass(G.leftAlign.command).removeClass(G.middleAlign.command).toggleClass(P);break;case G.bulletList.command:case G.numberList.command:this._document.execCommand(P,false,null);break}}});function H(P,O){while(P!=null){var Q=D(P,O);if(Q>-1){return O[Q]}P=P.parentNode}return""}function N(P,O){while(P!=null&&(D(P,O)==-1)){P=P.parentNode}return P}function D(T,Q){if(T.tagName){var O=T.tagName.toLowerCase();for(var S=0;S<Q.length;S++){if(Q[S].constructor==Array){if(Q[S][0]==O){var U=true;for(var P in Q[S][1]){if(!Q[S][1][P].test(T.getAttribute(P))){U=false;continue}}if(U){return S}}}else{var R=Q[S].split(".");if(O==R[0]&&(R.length==1||T.getAttribute("class")==R[1])){return S}}}}return -1}function C(O){O=N(O,["a"]);return O==null?"":O.href}var B;function F(T,S,P,V,R,Q){if(!B){B=I("<div id='Popup' style='position:relative;zoom:1;overflow:auto'><label style='float:left;width:35px;padding:3px 5px 0 0;text-align:right'>".concat(S.options.textUrl,"</label></div>"));B.input=I("<input style='float:left;height:100%' />");B.ok=I("<span class='ui-tool ui-state-default'><a style='width:55px'>".concat(S.options.textOk,"</a></span>"));B.cancel=I("<span class='ui-tool ui-state-default'><a style='width:55px'></a></span>");B.append(B.input).append(B.ok).append(B.cancel).bind("click.htmlInput",function(W){W.stopPropagation()})}var O=S.toolbar.element.width(),U=S.toolbar.element.outerHeight();S.toolbar.element.append(B);B.input.width(O>500?335:O-175);B.width(O-2);B.cancel.contents("a").text(S.options.textCancel);if(P){if(R){B.cancel.contents("a").text(S.options.textRemove)}B.input.val(P[V])}else{B.input.val("")}S.toolbar.element.contents("span").animate({marginTop:-U},{duration:500,complete:function(){B.input.focus()}});B.buttons=B.ok.add(B.cancel);B.buttons.bind("mouseover.htmlInput",function(){I(this).addClass("ui-state-hover")}).bind("mouseout.htmlInput",function(){I(this).removeClass("ui-state-hover")}).bind("click.htmlInput",function(W){S[Q](I(this).contents("a").text()=="ok"?B.input.val():null);S.toolbar.element.contents("span").animate({marginTop:0},{duration:250,complete:function(){I(document).add(S.document).add(B.buttons).add(B.input).add(B).unbind(".htmlInput");B.remove()}});W.preventDefault()});I(document).add(S.document).bind("click.htmlInput",function(){B.cancel.click()});B.input.keydown(function(W){switch(W.which){case I.ui.keyCode.ENTER:B.ok.click();break;case I.ui.keyCode.ESCAPE:B.cancel.click();break}})}function E(R,Q,O,P){F(R,Q,O,"href",true,P)}function K(R,Q,P,O){F(R,Q,P,"src",false,O)}function J(P,O){this.element=I(P);this.tools={};this.element.bind("click.htmlInput",function(Q){Q.stopPropagation()}).addClass("ui-toolbar");this.add=function(R,S,Q){var T=I("<span class='ui-tool ".concat(R,"'></span>"));this.element.append(T);if(S){T.append(S);S.attr("name",R);T.command=S}if(Q){T.data("tags",Q)}this.tools[R]=T;return T};this.addButton=function(Q){var R=I("<a title='".concat(Q.tooltip,"' tabindex='1000'>",Q.content,"</a>"));var S=this.add(Q.command,R).addClass("ui-state-default");R.bind("click.htmlInput",function(T){S.hasClass("ui-state-disabled")||O(T,R)});S.mouseover(function(){I(this).addClass("ui-state-hover")});S.mouseout(function(){I(this).removeClass("ui-state-hover")})};this.addList=function(Q){var S=I("<select title='".concat(Q.tooltip,"' tabindex='1000' style='height:100%'></select>"));var U=this.add(Q.command,S,[]).addClass("ui-state-default");S.bind("change.htmlInput",function(V){U.hasClass("ui-state-disabled")||O(V,S)});for(var T in Q.content){var R="<option value='".concat(T,"'>",Q.content[T],"</option>");S.append(R);U.data("tags").push(T)}};this.addSeparator=function(Q){this.add(Q.command,null)};this.addButtons=function(S){for(var R in S.tools){var Q=S.tools[R];if(Q.add){this["add"+Q.type](Q)}}}}function M(O,R,P,Q){this.type=O;this.command=R;this.content=P;this.add=true;this.tooltip=Q}var A={Button:"Button",List:"List",Separator:"Separator"};var G={bold:new M(A.Button,"bold","<strong>B</strong>","selection bold"),italic:new M(A.Button,"italic","<em>I</em>","selection itallic"),superscript:new M(A.Button,"superscript","X<sup>2</sup>","selection superscript"),subscript:new M(A.Button,"subscript","X<sub>2</sub>","selection subscript"),subSeparator:new M(A.Separator),removeFormat:new M(A.Button,"removeformat","~","remove selection formatting"),removeFormatSeparator:new M(A.Separator),block:new M(A.List,"formatBlock",{p:"Normal",h1:"Heading 1",h2:"Heading 2",h3:"Heading 3",h4:"Heading 4",h5:"Heading 5",h6:"Heading 6",pre:"Preformatted"},"Format selection as..."),blockSeparator:new M(A.Separator),leftAlign:new M(A.Button,"left","&lArr;","left align"),middleAlign:new M(A.Button,"middle","&hArr;","middle align"),rightAlign:new M(A.Button,"right","&rArr;","right align"),rightAlignSeparator:new M(A.Separator),bulletList:new M(A.Button,"insertUnorderedList","&bull;&equiv;","selection as a bulleted list"),numberList:new M(A.Button,"insertOrderedList","1&equiv;","selection as a numbered list"),increaseIndent:new M(A.Button,"indent","&gt;&equiv;","increase indent"),decreaseIndent:new M(A.Button,"outdent","&lt;&equiv;","decrease indent"),decreaseIndentSeparator:new M(A.Separator),link:new M(A.Button,"link","<strong>&infin;</strong>","add/edit link"),image:new M(A.Button,"image","&#10065;","add/edit image")};I.extend(I.ui.htmlInput,{getter:"value",version:"1.1.1",eventPrefix:"htmlInput",defaults:{debug:false,clean:true,format:true,formatIndent:0,styleUrl:"/content/editor.css",editLink:E,editImage:K,showToolbar:true,tools:G,widthAuto:false,baseUrl:false,allowedClasses:["left","middle","right"],canAlign:["img","p","h1","h2","h3","h4","h5","h6","th","td","li","dt"],textCancel:"cancel",textOk:"ok",textRemove:"remove",textUrl:"url"}})})(jQuery);