if(jQuery){(function($){$.fn.postEditor=function(targetText,options){var defaults={buttonBold:"",buttonItalic:"",buttonUnderline:"",buttonLink:"",selectSmilies:"",defaultText:"Add comment..."};var options=$.extend(defaults,options);smiliesfocus=0;return this.each(function(){var opts={elem:$(this),field:targetText,btBold:options.buttonBold,btItalic:options.buttonItalic,btUnderline:options.buttonUnderline,btLink:options.buttonLink,selSmilies:options.selectSmilies,txt:options.defaultText};if(opts.btBold!=""){$(opts.elem).find("."+opts.btBold).click(function(){var pos=$.postEditor.findPositions(opts);var txt=$("#"+opts.field).val();if(pos[0]!=pos[1]){var str=txt.substring(pos[0],pos[1]);str="[b]"+str+"[/b]";$.postEditor.formatString(opts,txt,pos,str)}else{if(txt==opts.txt){txt=""}var str="[b][/b]";$.postEditor.formatString(opts,txt,pos,str)}return false})}if(opts.btItalic!=""){$(opts.elem).find("."+opts.btItalic).click(function(){var pos=$.postEditor.findPositions(opts);var txt=$("#"+opts.field).val();if(pos[0]!=pos[1]){var str=txt.substring(pos[0],pos[1]);str="[i]"+str+"[/i]";$.postEditor.formatString(opts,txt,pos,str)}else{if(txt==opts.txt){txt=""}var str="[i][/i]";$.postEditor.formatString(opts,txt,pos,str)}return false})}if(opts.btUnderline!=""){$(opts.elem).find("."+opts.btUnderline).click(function(){var pos=$.postEditor.findPositions(opts);var txt=$("#"+opts.field).val();if(pos[0]!=pos[1]){var str=txt.substring(pos[0],pos[1]);str="[u]"+str+"[/u]";$.postEditor.formatString(opts,txt,pos,str)}else{if(txt==opts.txt){txt=""}var str="[u][/u]";$.postEditor.formatString(opts,txt,pos,str)}return false})}if(opts.btLink!=""){$(opts.elem).find("."+opts.btLink).click(function(){var pos=$.postEditor.findPositions(opts);var txt=$("#"+opts.field).val();if(pos[0]!=pos[1]){var str=txt.substring(pos[0],pos[1]);var lnk=prompt("Please type the address","http://");str='<a href="'+lnk+'" target="_blank">'+str+"</a>";$.postEditor.formatString(opts,txt,pos,str)}return false})}if(opts.selSmilies!=""){$(opts.elem).find("."+opts.selSmilies).find("div").mouseover(function(){$(this).addClass("hover")}).mouseout(function(){$(this).removeClass("hover")}).click(function(){if($(this).hasClass("active")){smiliesfocus=0;$(this).removeClass("active");$(this).parent().find("ul").hide()}else{$(this).addClass("active");$(this).parent().find("ul").show()}return false}).focus(function(){$(this).addClass("focus")}).blur(function(){$(this).removeClass("focus");if($(this).hasClass("active")&&smiliesfocus>0){}else{smiliesfocus=0;$(this).removeClass("active");$(this).parent().find("ul").hide();$("#"+opts.field).focus()}});$(opts.elem).find("."+opts.selSmilies).find("li").mouseover(function(){smiliesfocus=1}).mouseout(function(){smiliesfocus=-1}).click(function(){var pos=$.postEditor.findPositions(opts);var txt=$("#"+opts.field).val();if(pos[0]!=pos[1]){var str=txt.substring(pos[0],pos[1])}else{if(txt==opts.txt){txt=""}}str=" "+$(this).attr("title")+" ";$.postEditor.formatString(opts,txt,pos,str);smiliesfocus=0;$(this).parent().parent().find("div").removeClass("active");$(this).parent().hide();$("#"+opts.field).focus()});$(document).click(function(e){if($(e.target).parent().attr("class")!==opts.selSmilies){smiliesfocus=0;$("."+opts.selSmilies).find("div").removeClass("active");$("."+opts.selSmilies).find("ul").hide()}})}})};$.extend({postEditor:{findPositions:function(opts){var text=document.getElementById(opts.field).value;var s=0;var e=0;if(document.selection){var range=document.selection.createRange();var dpl=range.duplicate();if(range.text.length>0){dpl.moveToElementText(document.getElementById(opts.field));dpl.setEndPoint("EndToEnd",range);s=dpl.text.length-range.text.length;e=s+range.text.length}}else{s=document.getElementById(opts.field).selectionStart;e=document.getElementById(opts.field).selectionEnd}return[s,e]},formatString:function(opts,txt,pos,str){var str1=txt.substring(0,pos[0]);var str2=txt.substring(pos[0],pos[1]);var str3=txt.substring(pos[1],txt.length);if(str1==opts.txt){str1="";pos[0]=0}str2=str;$("#"+opts.field).val(str1+str2+str3);$.postEditor.setCaretPosition(opts.field,(pos[0]+3))},setCaretPosition:function(elemId,caretPos){var elem=document.getElementById(elemId);if(elem!=null){if(elem.createTextRange){var range=elem.createTextRange();range.move("character",caretPos);range.select()}else{if(elem.selectionStart){elem.focus();elem.setSelectionRange(caretPos,caretPos)}else{elem.focus()}}}}}})})(jQuery)};