/*

 CLEditor WYSIWYG HTML Editor v1.3.0

 http://premiumsoftware.net/cleditor

 requires jQuery v1.4.2 or later



 Copyright 2010, Chris Landowski, Premium Software, LLC

 Dual licensed under the MIT or GPL Version 2 licenses.

*/

(function(C){C.cleditor={defaultOptions:{width:"100%",height:450,controls:"bold italic underline strikethrough subscript superscript | font size style | color highlight removeformat | bullets numbering | outdent indent | alignleft center alignright justify | undo redo | rule image link unlink | paste pastetext source",colors:"FFF FCC FC9 FF9 FFC 9F9 9FF CFF CCF FCF CCC F66 F96 FF6 FF3 6F9 3FF 6FF 99F F9F BBB F00 F90 FC6 FF0 3F3 6CC 3CF 66C C6C 999 C00 F60 FC3 FC0 3C0 0CC 36F 63F C3C 666 900 C60 C93 990 090 399 33F 60C 939 333 600 930 963 660 060 366 009 339 636 000 300 630 633 330 030 033 006 309 303",fonts:"Arial,Arial Black,Comic Sans MS,Courier New,Narrow,Garamond,Georgia,Impact,Sans Serif,Serif,Tahoma,Trebuchet MS,Verdana",sizes:"1,2,3,4,5,6,7",styles:[["Paragraph","<p>"],["Header 1","<h1>"],["Header 2","<h2>"],["Header 3","<h3>"],["Header 4","<h4>"],["Header 5","<h5>"],["Header 6","<h6>"]],useCSS:false,docType:'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',docCSSFile:"",bodyStyle:"margin:4px; font:10pt Arial,Verdana; cursor:text"},buttons:{init:"bold,,|italic,,|underline,,|strikethrough,,|subscript,,|superscript,,|font,,fontname,|size,Font Size,fontsize,|style,,formatblock,|color,Font Color,forecolor,|highlight,Text Highlight Color,hilitecolor,color|removeformat,Remove Formatting,|bullets,,insertunorderedlist|numbering,,insertorderedlist|outdent,,|indent,,|alignleft,Align Text Left,justifyleft|center,,justifycenter|alignright,Align Text Right,justifyright|justify,,justifyfull|undo,,|redo,,|rule,Insert Horizontal Rule,inserthorizontalrule|image,Insert Image,insertimage,url|link,Insert Hyperlink,createlink,url|unlink,Remove Hyperlink,|cut,,|copy,,|paste,,|pastetext,Paste as Text,inserthtml,|print,,|source,Show Source"},imagesPath:function(){return t()}};C.fn.cleditor=function(ag){var af=C([]);this.each(function(ah,ai){if(ai.tagName=="TEXTAREA"){var aj=C.data(ai,d);if(!aj){aj=new cleditor(ai,ag)}af=af.add(aj)}});return af};var n="backgroundColor",Z="button",Y="buttonName",A="change",d="cleditor",Q="click",z="disabled",D="<div>",w="transparent",M="unselectable",b="cleditorMain",u="cleditorToolbar",aa="cleditorGroup",L="cleditorButton",ab="cleditorDisabled",J="cleditorDivider",G="cleditorPopup",ad="cleditorList",U="cleditorColor",c="cleditorPrompt",W="cleditorMsg",H=C.browser.msie,q=/msie\s6/i.test(navigator.userAgent),l=/iphone|ipad|ipod/i.test(navigator.userAgent),g={},B,v=C.cleditor.buttons;C.each(v.init.split("|"),function(af,ai){var ag=ai.split(","),ah=ag[0];v[ah]={stripIndex:af,name:ah,title:ag[1]===""?ah.charAt(0).toUpperCase()+ah.substr(1):ag[1],command:ag[2]===""?ah:ag[2],popupName:ag[3]===""?ah:ag[3]}});delete v.init;cleditor=function(ak,ah){var aj=this;aj.options=ah=C.extend({},C.cleditor.defaultOptions,ah);var af=aj.$area=C(ak).hide().data(d,aj).blur(function(){N(aj,true)});var al=aj.$main=C(D).addClass(b).width(ah.width).height(ah.height);var ag=aj.$toolbar=C(D).addClass(u).appendTo(al);var ai=C(D).addClass(aa).appendTo(ag);C.each(ah.controls.split(" "),function(an,am){if(am===""){return true}if(am=="|"){var ap=C(D).addClass(J).appendTo(ai);ai=C(D).addClass(aa).appendTo(ag)}else{var aq=v[am];var ao=C(D).data(Y,aq.name).addClass(L).attr("title",aq.title).bind(Q,C.proxy(e,aj)).appendTo(ai).hover(X,f);var ar={};if(aq.css){ar=aq.css}else{if(aq.image){ar.backgroundImage=E(aq.image)}}if(aq.stripIndex){ar.backgroundPosition=aq.stripIndex*-24}ao.css(ar);if(H){ao.attr(M,"on")}if(aq.popupName){p(aq.popupName,ah,aq.popupClass,aq.popupContent,aq.popupHover)}}});al.insertBefore(af).append(af);if(!B){C(document).click(function(an){var am=C(an.target);if(!am.add(am.parents()).is("."+c)){ae()}});B=true}if(/auto|%/.test(""+ah.width+ah.height)){C(window).resize(function(){ac(aj)})}ac(aj)};var x=cleditor.prototype,F=[["clear",I],["disable",i],["execCommand",a],["focus",R],["hidePopups",ae],["sourceMode",h,true],["refresh",ac],["select",S],["selectedHTML",m,true],["selectedText",O,true],["showMessage",y],["updateFrame",N],["updateTextArea",K]];C.each(F,function(af,ag){x[ag[0]]=function(){var ak=this,aj=[ak];for(var ai=0;ai<arguments.length;ai++){aj.push(arguments[ai])}var ah=ag[1].apply(ak,aj);if(ag[2]){return ah}return ak}});x.change=function(af){var ag=C(this);return af?ag.bind(A,af):ag.trigger(A)};function e(ak){var aj=this,am=ak.target,an=C.data(am,Y),ai=v[an],al=ai.popupName,ag=g[al];if(aj.disabled||C(am).attr(z)==z){return}var ah={editor:aj,button:am,buttonName:an,popup:ag,popupName:al,command:ai.command,useCSS:aj.options.useCSS};if(ai.buttonClick&&ai.buttonClick(ak,ah)===false){return false}if(an=="source"){if(h(aj)){delete aj.range;aj.$area.hide();aj.$frame.show();am.title=ai.title}else{aj.$frame.hide();aj.$area.show();am.title="Show Rich Text"}setTimeout(function(){o(aj)},100)}else{if(!h(aj)){if(al){var af=C(ag);if(al=="url"){if(an=="link"&&O(aj)===""){y(aj,"A selection is required when inserting a link.",am);return false}af.children(":button").unbind(Q).bind(Q,function(){var ap=af.find(":text"),ao=C.trim(ap.val());if(ao!==""){a(aj,ah.command,ao,null,ah.button)}ap.val("http://");ae();R(aj)})}else{if(al=="pastetext"){af.children(":button").unbind(Q).bind(Q,function(){var ap=af.find("textarea"),ao=ap.val().replace(/\n/g,"<br />");if(ao!==""){a(aj,ah.command,ao,null,ah.button)}ap.val("");ae();R(aj)})}}if(am!==C.data(ag,Z)){k(aj,ag,am);return false}return}else{if(an=="print"){aj.$frame[0].contentWindow.print()}else{if(!a(aj,ah.command,ah.value,ah.useCSS,am)){return false}}}}}R(aj)}function X(ag){var af=C(ag.target).closest("div");af.css(n,af.data(Y)?"#FFF":"#FFC")}function f(af){C(af.target).closest("div").css(n,"transparent")}function T(al){var aj=this,af=al.data.popup,am=al.target;if(af===g.msg||C(af).hasClass(c)){return}var ao=C.data(af,Z),ap=C.data(ao,Y),ai=v[ap],ag=ai.command,an,ak=aj.options.useCSS;if(ap=="font"){an=am.style.fontFamily.replace(/"/g,"")}else{if(ap=="size"){if(am.tagName=="DIV"){am=am.children[0]}an=am.innerHTML}else{if(ap=="style"){an="<"+am.tagName+">"}else{if(ap=="color"){an=P(am.style.backgroundColor)}else{if(ap=="highlight"){an=P(am.style.backgroundColor);if(H){ag="backcolor"}else{ak=true}}}}}}var ah={editor:aj,button:ao,buttonName:ap,popup:af,popupName:ai.popupName,command:ag,value:an,useCSS:ak};if(ai.popupClick&&ai.popupClick(al,ah)===false){return}if(ah.command&&!a(aj,ah.command,ah.value,ah.useCSS,ao)){return false}ae();R(aj)}function r(ai){var ag=1,af=0;for(var ah=0;ah<ai.length;++ah){ag=(ag+ai.charCodeAt(ah))%65521;af=(af+ag)%65521}return(af<<16)|ag}function I(af){af.$area.val("");N(af)}function p(aj,ah,ai,ag,ak){if(g[aj]){return g[aj]}var al=C(D).hide().addClass(G).appendTo("body");if(ag){al.html(ag)}else{if(aj=="color"){var af=ah.colors.split(" ");if(af.length<10){al.width("auto")}C.each(af,function(am,an){C(D).appendTo(al).css(n,"#"+an)});ai=U}else{if(aj=="font"){C.each(ah.fonts.split(","),function(am,an){C(D).appendTo(al).css("fontFamily",an).html(an)})}else{if(aj=="size"){C.each(ah.sizes.split(","),function(am,an){C(D).appendTo(al).html("<font size="+an+">"+an+"</font>")})}else{if(aj=="style"){C.each(ah.styles,function(am,an){C(D).appendTo(al).html(an[1]+an[0]+an[1].replace("<","</"))})}else{if(aj=="url"){al.html('Enter URL:<br><input type=text value="http://" size=35><br><input type=button value="Submit">');ai=c}else{if(aj=="pastetext"){al.html("Paste your content here and click submit.<br /><textarea cols=40 rows=3></textarea><br /><input type=button value=Submit>");ai=c}}}}}}}if(!ai&&!ag){ai=ad}al.addClass(ai);if(H){al.attr(M,"on").find("div,font,p,h1,h2,h3,h4,h5,h6").attr(M,"on")}if(al.hasClass(ad)||ak===true){al.children().hover(X,f)}g[aj]=al[0];return al[0]}function i(ag,af){if(af){ag.$area.attr(z,z);ag.disabled=true}else{ag.$area.removeAttr(z);delete ag.disabled}try{if(H){ag.doc.body.contentEditable=!af}else{ag.doc.designMode=!af?"on":"off"}}catch(ah){}o(ag)}function a(ag,am,aj,al,af){V(ag);if(!H){if(al===undefined||al===null){al=ag.options.useCSS}ag.doc.execCommand("styleWithCSS",0,al.toString())}var ak=true,ai;if(H&&am.toLowerCase()=="inserthtml"){j(ag).pasteHTML(aj)}else{try{ak=ag.doc.execCommand(am,0,aj||null)}catch(ah){ai=ah.description;ak=false}if(!ak){if("cutcopypaste".indexOf(am)>-1){y(ag,"For security reasons, your browser does not support the "+am+" command. Try using the keyboard shortcut or context menu instead.",af)}else{y(ag,(ai?ai:"Error executing the "+am+" command."),af)}}}o(ag);return ak}function R(af){setTimeout(function(){if(h(af)){af.$area.focus()}else{af.$frame[0].contentWindow.focus()}o(af)},0)}function j(af){if(H){return s(af).createRange()}return s(af).getRangeAt(0)}function s(af){if(H){return af.doc.selection}return af.$frame[0].contentWindow.getSelection()}function P(ag){var af=/rgba?\((\d+), (\d+), (\d+)/.exec(ag),ah=ag.split("");if(af){ag=(af[1]<<16|af[2]<<8|af[3]).toString(16);while(ag.length<6){ag="0"+ag}}return"#"+(ag.length==6?ag:ah[1]+ah[1]+ah[2]+ah[2]+ah[3]+ah[3])}function ae(){C.each(g,function(af,ag){C(ag).hide().unbind(Q).removeData(Z)})}function t(){var af="jquery.cleditor.css",ag=C("link[href$='"+af+"']").attr("href");return ag.substr(0,ag.length-af.length)+"images/"}function E(af){return"url("+t()+af+")"}function ac(ah){var aj=ah.$main,ag=ah.options;if(ah.$frame){ah.$frame.remove()}var ai=ah.$frame=C('<iframe frameborder="0" src="javascript:true;">').hide().appendTo(aj);var af=ai[0].contentWindow,ak=ah.doc=af.document,al=C(ak);ak.open();ak.write(ag.docType+"<html>"+((ag.docCSSFile==="")?"":'<head><link rel="stylesheet" type="text/css" href="'+ag.docCSSFile+'" /></head>')+'<body style="'+ag.bodyStyle+'"></body></html>');ak.close();if(H){al.click(function(){R(ah)})}N(ah);if(H){al.bind("beforedeactivate beforeactivate selectionchange keypress",function(am){if(am.type=="beforedeactivate"){ah.inactive=true}else{if(am.type=="beforeactivate"){if(!ah.inactive&&ah.range&&ah.range.length>1){ah.range.shift()}delete ah.inactive}else{if(!ah.inactive){if(!ah.range){ah.range=[]}ah.range.unshift(j(ah));while(ah.range.length>2){ah.range.pop()}}}}});ai.focus(function(){V(ah)})}(C.browser.mozilla?al:C(af)).blur(function(){K(ah,true)});al.click(ae).bind("keyup mouseup",function(){o(ah)});if(l){ah.$area.show()}else{ai.show()}C(function(){var an=ah.$toolbar,ao=an.children("div:last"),ap=aj.width();var am=ao.offset().top+ao.outerHeight()-an.offset().top+1;an.height(am);am=(/%/.test(""+ag.height)?aj.height():parseInt(ag.height))-am;ai.width(ap).height(am);ah.$area.width(ap).height(q?am-2:am);i(ah,ah.disabled);o(ah)})}function o(af){if(!l&&C.browser.webkit&&!af.focused){af.$frame[0].contentWindow.focus();window.focus();af.focused=true}var ag=af.doc;if(H){ag=j(af)}var ah=h(af);C.each(af.$toolbar.find("."+L),function(ai,an){var aj=C(an),al=C.cleditor.buttons[C.data(an,Y)],ap=al.command,ak=true;if(af.disabled){ak=false}else{if(al.getEnabled){var ao={editor:af,button:an,buttonName:al.name,popup:g[al.popupName],popupName:al.popupName,command:al.command,useCSS:af.options.useCSS};ak=al.getEnabled(ao);if(ak===undefined){ak=true}}else{if(((ah||l)&&al.name!="source")||(H&&(ap=="undo"||ap=="redo"))){ak=false}else{if(ap&&ap!="print"){if(H&&ap=="hilitecolor"){ap="backcolor"}if(!H||ap!="inserthtml"){try{ak=ag.queryCommandEnabled(ap)}catch(am){ak=false}}}}}}if(ak){aj.removeClass(ab);aj.removeAttr(z)}else{aj.addClass(ab);aj.attr(z,z)}})}function V(af){if(H&&af.range){af.range[0].select()}}function S(af){setTimeout(function(){if(h(af)){af.$area.select()}else{a(af,"selectall")}},0)}function m(ai){V(ai);var af=j(ai);if(H){return af.htmlText}var ah=C("<layer>")[0];ah.appendChild(af.cloneContents());var ag=ah.innerHTML;ah=null;return ag}function O(af){V(af);if(H){return j(af).text}return s(af).toString()}function y(ah,ai,ag){var af=p("msg",ah.options,W);af.innerHTML=ai;k(ah,af,ag)}function k(am,ah,al){var ak,aj,an,af=C(ah);if(al){var ag=C(al);ak=ag.offset();aj=--ak.left;an=ak.top+ag.height()}else{var ai=am.$toolbar;ak=ai.offset();aj=Math.floor((ai.width()-af.width())/2)+ak.left;an=ak.top+ai.height()-2}ae();af.css({left:aj,top:an}).show();if(al){C.data(ah,Z,al);af.bind(Q,{popup:ah},C.proxy(T,am))}setTimeout(function(){af.find(":text,textarea").eq(0).focus().select()},100)}function h(af){return af.$area.is(":visible")}function N(aj,af){var ak=aj.$area.val(),ag=aj.options,am=ag.updateFrame,al=C(aj.doc.body);if(am){var ai=r(ak);if(af&&aj.areaChecksum==ai){return}aj.areaChecksum=ai}var ah=am?am(ak):ak;ah=ah.replace(/<(?=\/?script)/ig,"&lt;");if(ag.updateTextArea){aj.frameChecksum=r(ah)}if(ah!=al.html()){al.html(ah);C(aj).triggerHandler(A)}}function K(ak,af){var ai=C(ak.doc.body).html(),ah=ak.options,am=ah.updateTextArea,ag=ak.$area;if(am){var aj=r(ai);if(af&&ak.frameChecksum==aj){return}ak.frameChecksum=aj}var al=am?am(ai):ai;if(ah.updateFrame){ak.areaChecksum=r(al)}if(al!=ag.val()){ag.val(al);C(ak).triggerHandler(A)}}})(jQuery);