!function($){$.fn.markItUp=function(settings,extraSettings){var method,params,options,ctrlKey,shiftKey,altKey;ctrlKey=shiftKey=altKey=!1,"string"==typeof settings&&(method=settings,params=extraSettings),options={id:"",nameSpace:"",root:"",previewHandler:!1,previewInWindow:"",previewInElement:"",previewAutoRefresh:!0,previewPosition:"after",previewTemplatePath:"~/templates/preview.html",previewParser:!1,previewParserPath:"",previewParserVar:"data",previewParserAjaxType:"POST",resizeHandle:!0,beforeInsert:"",afterInsert:"",onEnter:{},onShiftEnter:{},onCtrlEnter:{},onTab:{},markupSet:[{}]},$.extend(options,settings,extraSettings),options.root||$("script").each(function(a,b){miuScript=$(b).get(0).src.match(/(.*)jquery\.markitup(\.pack)?\.js$/),null!==miuScript&&(options.root=miuScript[1])});var uaMatch=function(a){a=a.toLowerCase();var b=/(chrome)[ \/]([\w.]+)/.exec(a)||/(webkit)[ \/]([\w.]+)/.exec(a)||/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(a)||/(msie) ([\w.]+)/.exec(a)||a.indexOf("compatible")<0&&/(mozilla)(?:.*? rv:([\w.]+)|)/.exec(a)||[];return{browser:b[1]||"",version:b[2]||"0"}},matched=uaMatch(navigator.userAgent),browser={};return matched.browser&&(browser[matched.browser]=!0,browser.version=matched.version),browser.chrome?browser.webkit=!0:browser.webkit&&(browser.safari=!0),this.each(function(){function localize(a,b){return b?a.replace(/("|')~\//g,"$1"+options.root):a.replace(/^~\//,options.root)}function init(){id="",nameSpace="",options.id?id='id="'+options.id+'"':$$.attr("id")&&(id='id="markItUp'+$$.attr("id").substr(0,1).toUpperCase()+$$.attr("id").substr(1)+'"'),options.nameSpace&&(nameSpace='class="'+options.nameSpace+'"'),$$.wrap("<div "+nameSpace+"></div>"),$$.wrap("<div "+id+' class="markItUp"></div>'),$$.wrap('<div class="markItUpContainer"></div>'),$$.addClass("markItUpEditor"),header=$('<div class="markItUpHeader"></div>').insertBefore($$),$(dropMenus(options.markupSet)).appendTo(header),footer=$('<div class="markItUpFooter"></div>').insertAfter($$),options.resizeHandle===!0&&browser.safari!==!0&&(resizeHandle=$('<div class="markItUpResizeHandle"></div>').insertAfter($$).bind("mousedown.markItUp",function(a){var d,e,b=$$.height(),c=a.clientY;d=function(a){return $$.css("height",Math.max(20,a.clientY+b-c)+"px"),!1},e=function(a){return $("html").unbind("mousemove.markItUp",d).unbind("mouseup.markItUp",e),!1},$("html").bind("mousemove.markItUp",d).bind("mouseup.markItUp",e)}),footer.append(resizeHandle)),$$.bind("keydown.markItUp",keyPressed).bind("keyup",keyPressed),$$.bind("insertion.markItUp",function(a,b){b.target!==!1&&get(),textarea===$.markItUp.focused&&markup(b)}),$$.bind("focus.markItUp",function(){$.markItUp.focused=this}),options.previewInElement&&refreshPreview()}function dropMenus(markupSet){var ul=$("<ul></ul>"),i=0;return $("li:hover > ul",ul).css("display","block"),$.each(markupSet,function(){var button=this,t="",title,li,j;if(title=button.title?button.key?(button.title||"")+" [Ctrl+"+button.key+"]":button.title||"":button.key?(button.name||"")+" [Ctrl+"+button.key+"]":button.name||"",key=button.key?'accesskey="'+button.key+'"':"",button.separator)li=$('<li class="markItUpSeparator">'+(button.separator||"")+"</li>").appendTo(ul);else{for(i++,j=levels.length-1;j>=0;j--)t+=levels[j]+"-";li=$('<li class="markItUpButton markItUpButton'+t+i+" "+(button.className||"")+'"><a href="#" '+key+' title="'+title+'">'+(button.name||"")+"</a></li>").bind("contextmenu.markItUp",function(){return!1}).bind("click.markItUp",function(a){a.preventDefault()}).bind("focusin.markItUp",function(){$$.focus()}).bind("mouseup",function(e){return button.call&&eval(button.call)(e),setTimeout(function(){markup(button)},1),!1}).bind("mouseenter.markItUp",function(){$("> ul",this).show(),$(document).one("click",function(){$("ul ul",header).hide()})}).bind("mouseleave.markItUp",function(){$("> ul",this).hide()}).appendTo(ul),button.dropMenu&&(levels.push(i),$(li).addClass("markItUpDropMenu").append(dropMenus(button.dropMenu)))}}),levels.pop(),ul}function magicMarkups(a){return a?(a=a.toString(),a=a.replace(/\(\!\(([\s\S]*?)\)\!\)/g,function(a,b){var c=b.split("|!|");return altKey===!0?void 0!==c[1]?c[1]:c[0]:void 0===c[1]?"":c[0]}),a=a.replace(/\[\!\[([\s\S]*?)\]\!\]/g,function(a,b){var c=b.split(":!:");return abort!==!0&&(value=prompt(c[0],c[1]?c[1]:""),null===value&&(abort=!0),value)})):""}function prepare(a){return $.isFunction(a)&&(a=a(hash)),magicMarkups(a)}function build(a){var b=prepare(clicked.openWith),c=prepare(clicked.placeHolder),d=prepare(clicked.replaceWith),e=prepare(clicked.closeWith),f=prepare(clicked.openBlockWith),g=prepare(clicked.closeBlockWith),h=clicked.multiline;if(""!==d)block=b+d+e;else if(""===selection&&""!==c)block=b+c+e;else{a=a||selection;var i=[a],j=[];h===!0&&(i=a.split(/\r?\n/));for(var k=0;k<i.length;k++){line=i[k];var l;(l=line.match(/ *$/))?j.push(b+line.replace(/ *$/g,"")+e+l):j.push(b+line+e)}block=j.join("\n")}return block=f+block+g,{block:block,openBlockWith:f,openWith:b,replaceWith:d,placeHolder:c,closeWith:e,closeBlockWith:g}}function markup(a){var b,c,d,e;if(hash=clicked=a,get(),$.extend(hash,{line:"",root:options.root,textarea:textarea,selection:selection||"",caretPosition:caretPosition,ctrlKey:ctrlKey,shiftKey:shiftKey,altKey:altKey}),prepare(options.beforeInsert),prepare(clicked.beforeInsert),(ctrlKey===!0&&shiftKey===!0||a.multiline===!0)&&prepare(clicked.beforeMultiInsert),$.extend(hash,{line:1}),ctrlKey===!0&&shiftKey===!0){for(lines=selection.split(/\r?\n/),c=0,d=lines.length,e=0;e<d;e++)""!==$.trim(lines[e])?($.extend(hash,{line:++c,selection:lines[e]}),lines[e]=build(lines[e]).block):lines[e]="";string={block:lines.join("\n")},start=caretPosition,b=string.block.length+(browser.opera?d-1:0)}else ctrlKey===!0?(string=build(selection),start=caretPosition+string.openWith.length,b=string.block.length-string.openWith.length-string.closeWith.length,b-=string.block.match(/ $/)?1:0,b-=fixIeBug(string.block)):shiftKey===!0?(string=build(selection),start=caretPosition,b=string.block.length,b-=fixIeBug(string.block)):(string=build(selection),start=caretPosition+string.block.length,b=0,start-=fixIeBug(string.block));""===selection&&""===string.replaceWith&&(caretOffset+=fixOperaBug(string.block),start=caretPosition+string.openBlockWith.length+string.openWith.length,b=string.block.length-string.openBlockWith.length-string.openWith.length-string.closeWith.length-string.closeBlockWith.length,caretOffset=$$.val().substring(caretPosition,$$.val().length).length,caretOffset-=fixOperaBug($$.val().substring(0,caretPosition))),$.extend(hash,{caretPosition:caretPosition,scrollPosition:scrollPosition}),string.block!==selection&&abort===!1?(insert(string.block),set(start,b)):caretOffset=-1,get(),$.extend(hash,{line:"",selection:selection}),(ctrlKey===!0&&shiftKey===!0||a.multiline===!0)&&prepare(clicked.afterMultiInsert),prepare(clicked.afterInsert),prepare(options.afterInsert),previewWindow&&options.previewAutoRefresh&&refreshPreview(),shiftKey=altKey=ctrlKey=abort=!1}function fixOperaBug(a){return browser.opera?a.length-a.replace(/\n*/g,"").length:0}function fixIeBug(a){return browser.msie?a.length-a.replace(/\r*/g,"").length:0}function insert(a){if(document.selection){var b=document.selection.createRange();b.text=a}else textarea.value=textarea.value.substring(0,caretPosition)+a+textarea.value.substring(caretPosition+selection.length,textarea.value.length)}function set(a,b){if(textarea.createTextRange){if(browser.opera&&browser.version>=9.5&&0==b)return!1;range=textarea.createTextRange(),range.collapse(!0),range.moveStart("character",a),range.moveEnd("character",b),range.select()}else textarea.setSelectionRange&&textarea.setSelectionRange(a,a+b);textarea.scrollTop=scrollPosition,textarea.focus()}function get(){if(textarea.focus(),scrollPosition=textarea.scrollTop,document.selection)if(selection=document.selection.createRange().text,browser.msie){var a=document.selection.createRange(),b=a.duplicate();for(b.moveToElementText(textarea),caretPosition=-1;b.inRange(a);)b.moveStart("character"),caretPosition++}else caretPosition=textarea.selectionStart;else caretPosition=textarea.selectionStart,selection=textarea.value.substring(caretPosition,textarea.selectionEnd);return selection}function preview(){"function"==typeof options.previewHandler?previewWindow=!0:options.previewInElement?previewWindow=$(options.previewInElement):!previewWindow||previewWindow.closed?options.previewInWindow?(previewWindow=window.open("","preview",options.previewInWindow),$(window).unload(function(){previewWindow.close()})):(iFrame=$('<iframe class="markItUpPreviewFrame"></iframe>'),"after"==options.previewPosition?iFrame.insertAfter(footer):iFrame.insertBefore(header),previewWindow=iFrame[iFrame.length-1].contentWindow||frame[iFrame.length-1]):altKey===!0&&(iFrame?iFrame.remove():previewWindow.close(),previewWindow=iFrame=!1),options.previewAutoRefresh||refreshPreview(),options.previewInWindow&&previewWindow.focus()}function refreshPreview(){renderPreview()}function renderPreview(){if(options.previewHandler&&"function"==typeof options.previewHandler)options.previewHandler($$.val());else if(options.previewParser&&"function"==typeof options.previewParser){var b=options.previewParser($$.val());writeInPreview(localize(b,1))}else""!==options.previewParserPath?$.ajax({type:options.previewParserAjaxType,dataType:"text",global:!1,url:options.previewParserPath,data:options.previewParserVar+"="+encodeURIComponent($$.val()),success:function(a){writeInPreview(localize(a,1))}}):template||$.ajax({url:options.previewTemplatePath,dataType:"text",global:!1,success:function(a){writeInPreview(localize(a,1).replace(/<!-- content -->/g,$$.val()))}});return!1}function writeInPreview(a){if(options.previewInElement)$(options.previewInElement).html(a);else if(previewWindow&&previewWindow.document){try{sp=previewWindow.document.documentElement.scrollTop}catch(a){sp=0}previewWindow.document.open(),previewWindow.document.write(a),previewWindow.document.close(),previewWindow.document.documentElement.scrollTop=sp}}function keyPressed(a){if(shiftKey=a.shiftKey,altKey=a.altKey,ctrlKey=(!a.altKey||!a.ctrlKey)&&(a.ctrlKey||a.metaKey),"keydown"===a.type){if(ctrlKey===!0&&(li=$('a[accesskey="'+(13==a.keyCode?"\\n":String.fromCharCode(a.keyCode))+'"]',header).parent("li"),0!==li.length))return ctrlKey=!1,setTimeout(function(){li.triggerHandler("mouseup")},1),!1;if(13===a.keyCode||10===a.keyCode)return ctrlKey===!0?(ctrlKey=!1,markup(options.onCtrlEnter),options.onCtrlEnter.keepDefault):shiftKey===!0?(shiftKey=!1,markup(options.onShiftEnter),options.onShiftEnter.keepDefault):(markup(options.onEnter),options.onEnter.keepDefault);if(9===a.keyCode)return 1!=shiftKey&&1!=ctrlKey&&1!=altKey&&(caretOffset!==-1?(get(),caretOffset=$$.val().length-caretOffset,set(caretOffset,0),caretOffset=-1,!1):(markup(options.onTab),options.onTab.keepDefault))}}function remove(){$$.unbind(".markItUp").removeClass("markItUpEditor"),$$.parent("div").parent("div.markItUp").parent("div").replaceWith($$);var a=$$.parent("div").parent("div.markItUp").parent("div");a.length&&a.replaceWith($$),$$.data("markItUp",null)}var $$,textarea,levels,scrollPosition,caretPosition,caretOffset,clicked,hash,header,footer,previewWindow,template,iFrame,abort;if($$=$(this),textarea=this,levels=[],abort=!1,scrollPosition=caretPosition=0,caretOffset=-1,options.previewParserPath=localize(options.previewParserPath),options.previewTemplatePath=localize(options.previewTemplatePath),method)switch(method){case"remove":remove();break;case"insert":markup(params);break;default:$.error("Method "+method+" does not exist on jQuery.markItUp")}else init()})},$.fn.markItUpRemove=function(){return this.each(function(){$(this).markItUp("remove")})},$.markItUp=function(a){var b={target:!1};return $.extend(b,a),b.target?$(b.target).each(function(){$(this).focus(),$(this).trigger("insertion",[b])}):void $("textarea").trigger("insertion",[b])}}(jQuery);