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