/*!
 * CLEditor - WYSIWYG HTML Editor
 * version: 1.1 (7/13/2010)
 * @requires jQuery v1.4.2 or later
 *
 * Copyright 2010, Chris Landowski, Premium Software, LLC
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * CLEditor is a simple lightweight jQuery Rich Text (WYSIWYG)
 * HTML editor plugin.
 */
(function(b){function x(a){var c=a.data.buttonID,d=this.htmlMode(),e=i[c];if(c=="html")if(d){this.$area.hide();this.$frame.show();a.target.title="Show HTML"}else{this.$frame.hide();this.$area.show();a.target.title="Show Rich Text"}else if(d)alert("'"+i[c][1]+"' not allowed in HTML view.");else if(c=="font")q(a,this,k);else if(c=="size")q(a,this,l);else if(c=="style")q(a,this,m);else if(c=="color"||c=="highlight")q(a,this,n);else if(c=="image"){a=prompt("Enter Image URL:","http://");a!==null&&a!=""&&
this.execCommand(e[2],a)}else if(c=="link"){if(this.selectedText()==""){alert("A selection is required when inserting a link.");this.focus();return}a=prompt("Enter Link URL:","http://");a!==null&&a!=""&&this.execCommand(e[2],a)}else if(c=="print")this.$frame[0].contentWindow.print();else d||this.execCommand(e[2],e[3]||null);this.focus()}function t(){r(n);r(k);r(l);r(m)}function y(a){if(!this.htmlMode()){var c,d=i[a.data.buttonID][2];if(d=="fontname")c=a.target.style.fontFamily;else if(d=="fontsize")c=
a.target.innerHTML;else if(d=="formatblock")c="<"+a.target.tagName+">";else if(d=="forecolor")c=a.target.style.backgroundColor;else if(d=="hilitecolor"){c=a.target.style.backgroundColor;if(b.browser.msie)d="backcolor";else{this.execCommand("styleWithCSS","true");this.execCommand(d,c);this.execCommand("styleWithCSS","false");this.focus();return}}this.execCommand(d,c);this.focus()}}function z(a){for(;a.target.tagName!="DIV";)a.target=a.target.parentNode;b(a.target).css("backgroundColor","#FFFFCC")}
function A(a){for(;a.target.tagName!="DIV";)a.target=a.target.parentNode;b(a.target).css("backgroundColor","#FFFFFF")}function s(a){a=b.extend({className:"cleditorPopup",hover:true},a);var c=b("<div>").appendTo("body").hide().css("z-index","0").addClass(a.className);if(a.hover){c.addClass("cleditorHover");b(".cleditorPopup.cleditorHover *").live("mouseenter",b.proxy(z,this)).live("mouseleave",b.proxy(A,this))}b.browser.msie&&setTimeout(function(){c.find("div,font,p,h1,h2,h3,h4,h5,h6").attr("unselectable",
"on")},0);return c}function B(){n=s({className:"cleditorColors",hover:false});b.each(settings.colors.split(" "),function(a,c){b("<div>").appendTo(n).css("backgroundColor","#"+c)})}function C(){k=s();b.each(settings.fonts.split(","),function(a,c){b("<div>").appendTo(k).css("font-family",c).html(c)})}function D(){l=s();b.each(settings.sizes.split(","),function(a,c){b("<div>").appendTo(l).html("<font size="+c+">"+c+"</font>")})}function E(){m=s();b.each(settings.styles,function(a,c){b("<div>").appendTo(m).html(c[1]+
c[0]+c[1].replace("<","</"))})}function r(a){if(a!==undefined&&a.button!==undefined){a.unbind("click").hide();a.button=undefined}}function q(a,c,d){if(d.button===undefined||a.target!=d.button){t(a);var e=b(a.target),h=e.offset();d.css({left:--h.left+"px",top:h.top+e.height()+"px"}).bind("click",{$popup:d,buttonID:a.data.buttonID},b.proxy(y,c)).show();d.button=a.target;a.stopPropagation()}}var i={bold:[0,"Bold","bold"],italic:[1,"Italic","italic"],underline:[2,"Underline","underline"],strikethrough:[3,
"Strikethrough","strikethrough"],subscript:[4,"Subscript","subscript"],superscript:[5,"Superscript","superscript"],font:[6,"Font","fontname"],size:[7,"Font Size","fontsize"],style:[8,"Style","formatblock"],color:[9,"Font Color","forecolor"],highlight:[10,"Text Highlight Color","hilitecolor"],removeformat:[11,"Remove Formatting","removeformat"],bullets:[12,"Bullets","insertunorderedlist"],numbering:[13,"Numbering","insertorderedlist"],outdent:[14,"Outdent","outdent"],indent:[15,"Indent","indent"],
alignleft:[16,"Align Text Left","justifyleft"],center:[17,"Center","justifycenter"],alignright:[18,"Align Text Right","justifyright"],justify:[19,"Justify","justifyfull"],undo:[20,"Undo","undo"],redo:[21,"Redo","redo"],rule:[22,"Insert Horizontal Rule","inserthorizontalrule"],image:[23,"Insert Image","insertimage"],link:[24,"Insert Hyperlink","createlink"],unlink:[25,"Remove Hyperlink","unlink"],cut:[26,"Cut","cut",1],copy:[27,"Copy","copy",1],paste:[28,"Paste","paste",1],print:[29,"Print","print"],
html:[30,"Show HTML"]},n,k,l,m,v;b.cleditor=function(a,c){settings=b.extend({name:"cleditor",width:500,height:250,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 | cut copy paste | print html",colors:"FFFFFF FFCCCC FFCC99 FFFF99 FFFFCC 99FF99 99FFFF CCFFFF CCCCFF FFCCFF CCCCCC FF6666 FF9966 FFFF66 FFFF33 66FF99 33FFFF 66FFFF 9999FF FF99FF C0C0C0 FF0000 FF9900 FFCC66 FFFF00 33FF33 66CCCC 33CCFF 6666CC CC66CC 999999 CC0000 FF6600 FFCC33 FFCC00 33CC00 00CCCC 3366FF 6633FF CC33CC 666666 990000 CC6600 CC9933 999900 009900 339999 3333FF 6600CC 993399 333333 660000 993300 996633 666600 006600 336666 000099 333399 663366 000000 330000 663300 663333 333300 003300 003333 000066 330099 330033",
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>"]],mainClass:"cleditorMain",toolbarClass:"cleditorToolbar",groupClass:"cleditorGroup",buttonClass:"cleditorButton",dividerClass:"cleditorDivider",bodyStyle:"margin:4px; font:10pt Arial,Verdana; cursor:text"},
c);var d=/msie\s6/.test(navigator.userAgent.toLowerCase()),e=b("#"+a).hide().keyup(b.proxy(this.updateFrame,this)).mouseup(b.proxy(this.updateFrame,this)),h=b("<div>").insertBefore(e).addClass(settings.mainClass).width(settings.width).height(settings.height),u=b("<div>").appendTo(h).addClass(settings.toolbarClass),o=b("<div>").appendTo(u).addClass(settings.groupClass),p=0;b.each(settings.controls.split(" "),b.proxy(function(F,f){if(f=="|"){var j=b("<div>").appendTo(o).addClass(settings.dividerClass);
p+=j.width();o.width(p);o=b("<div>").appendTo(u).addClass(settings.groupClass);p=0}else if(i[f]){j=i[f][0]*-24;j=b("<div>").appendTo(o).addClass(settings.buttonClass).attr("title",i[f][1]).css("background-position",j+"px").bind("click",{buttonID:f},b.proxy(x,this));p+=j.width();o.width(p);b.browser.msie&&j.attr("unselectable","on");f=="font"&&k===undefined&&C();f=="size"&&l===undefined&&D();f=="style"&&m===undefined&&E();if((f=="color"||f=="highlight")&&n===undefined)B()}},this));h.find("."+settings.buttonClass).hover(function(){b(this).css({backgroundColor:"#ffffff"})},
function(){b(this).css({backgroundColor:"transparent"})});e.appendTo(h);var w=b('<iframe frameborder="0" src="javascript:true;">').appendTo(h),g=w[0].contentWindow.document;g.open();g.write('<html><body style="'+settings.bodyStyle+'">'+e.val()+"</body></html>");g.close();if(d)g.body.contentEditable=true;else g.designMode="on";b(g).keyup(b.proxy(this.updateTextArea,this));b(g).mouseup(b.proxy(this.updateTextArea,this));this.$main=h;this.$toolbar=u;this.$area=e;this.$frame=w;this.settings=settings;
this.doc=g;this.ie6=d;window[settings.name]=this;/auto|%/.test(""+settings.width+settings.height)&&b(window).resize(b.proxy(this.resizeControls,this));this.resizeControls();if(v===undefined){b(document).click(t);b(g).click(t);v=true}};b.cleditor.prototype.execCommand=function(a,c){this.doc.execCommand(a,0,c);this.updateTextArea()};b.cleditor.prototype.focus=function(){setTimeout(b.proxy(function(){this.htmlMode()?this.$area.focus():this.$frame[0].contentWindow.focus()},this),0)};b.cleditor.prototype.htmlMode=
function(){return this.$frame.css("display")=="none"};b.cleditor.prototype.resizeControls=function(){var a=this.$toolbar,c=a.find("div:last");c=c.offset().top+c.outerHeight()-a.offset().top+1;a.height(c);a=this.$main.width();c=/%/.test(""+settings.height)?this.$main.height()-c:parseInt(this.settings.height)-c;this.$frame.width(a).height(c);this.$area.width(a).height(this.ie6?c-2:c)};b.cleditor.prototype.select=function(){setTimeout(b.proxy(function(){this.htmlMode()?this.$area.select():this.execCommand("selectall",
null)},this),0)};b.cleditor.prototype.selectedText=function(){if(b.browser.msie)return this.doc.selection.createRange().text;return this.$frame[0].contentWindow.getSelection()};b.cleditor.prototype.updateFrame=function(){b(this.doc.body).html(this.$area.val())};b.cleditor.prototype.updateTextArea=function(){this.$area.val(b(this.doc.body).html())}})(jQuery);
