/*!
 * Chili - the jQuery plugin for highlighting code
 * http://noteslog.com/chili/
 * 
 * Copyright 2010 Andrea Ercolino
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 * 
 * VERSION: NEXT - 20100405 1932
 */

(function(e){function r(a){return e.chili.dynamic.origin+"jquery.chili.recipes."+a+".js"}function s(a){return a.match(/\bjquery\.chili\.recipes\.([\w-]+)\.js$/i)[1]}function E(a){var b=e.chili.codeLanguage(a);if(""!=b){var c=r(b);if(e.chili.dynamic.active&&!e.chili.recipes[b]){e.chili.queue[c]||t(c,u,[c]);e.chili.queue[c].push(a)}else{e(a).trigger("chili.before_coloring",[b]);u.apply(a,[c]);e(a).trigger("chili.after_coloring",[b])}}}function u(a){a=s(a);if(e.chili.recipes[a]){var b=e(this).text();
if(b){b=v(b);w.apply({selector:this,subject:b,module:a,context:{}});F(this);G(this)}}}function w(){H(this);var a=x(this.subject,this.module,this.context);a=I(this,a);a=y(a);e(this.selector)[0].innerHTML=a}function J(a,b,c){if(""!=c)return"applyStep";if(""!=b)return"applyBlock";if(""!=a)return"applyRecipe";return""}function K(a,b){if(a){var c=(a||"").match(/^(?!(?:\/$|.+\/$|.+\/\/$|.+\/\/.))([^\/]*)(?:\/([^\/]*)(?:\/([^\/]+))?)?$/);if(c){var d=c[1]||"",f=c[2]||"";c=c[3]||"";var g=J(d,f,c),h=L(d,b);
return{action:g,recipeName:d,blockName:f,stepName:c,recipe:h,module:a,context:b}}}}function L(a,b){var c=null;return c=""==a?b.recipe:e.chili.recipes[a]}function t(a,b,c){e.chili.queue[a]=[];e.getScript(a,function(){for(var d=s(a),f=e.chili.queue[a],g=0,h=f.length;g<h;g++){var i=f[g];if("undefined"!=typeof i.selector)i=e(i.selector)[0];e(i).trigger("chili.before_coloring",[d]);b.apply(f[g],c);e(i).trigger("chili.after_coloring",[d])}})}function M(a,b){return result=p(a,b.recipe)}function N(a,b){var c=
b.blockName;b=b.recipe;return result=c in b?p(a,b,c):m(a)}function O(a,b){var c=b.blockName,d=b.stepName,f=b.recipe;b=b.context;if(""==c)c=b.blockName;return result=!(c in f)||!(d in f[c])?m(a):p(a,f,c,d)}function P(a,b){var c="";switch(b.action){case "applyRecipe":c=M(a,b);break;case "applyBlock":c=N(a,b);break;case "applyStep":c=O(a,b);break;default:break}return c}function Q(a,b){var c=r(b.recipeName);e.chili.queue[c]||t(c,w);var d="chili_"+z();e.chili.queue[c].push({selector:"#"+d,subject:a,module:b.module,
context:b.context});return result='<span id="'+d+'">'+result+"</span>"}function x(a,b,c){var d="";b=K(b,c);return d=typeof b=="undefined"?m(a):b.recipe?P(a,b):e.chili.dynamic.active?Q(a,b):m(a)}function z(a){for(var b=(new Date).valueOf();a&&a.indexOf(b)>-1;);return b=(new Date).valueOf()}function R(a,b){var c=[],d=a[b];for(var f in d){var g=A(a,b,f);c.push(g)}return c}function S(a){return(a.replace(/\\./g,"%").replace(/\[.*?\]/g,"%").match(/\((?!\?)/g)||[]).length}function A(a,b,c){var d=a[b][c],
f=typeof d._match=="string"?d._match:d._match.source;d=d._replace?d._replace:'<span class="$0">$$</span>';return{recipe:a,blockName:b,stepName:c,exp:"("+f+")",length:S(f)+1,replacement:d}}function T(a){for(var b=1,c=[],d=0,f=a.length;d<f;d++){var g=a[d].exp;g=g.replace(/\\\\|\\(\d+)/g,function(h,i){return!i?h:"\\"+(b+1+parseInt(i,10))});c.push(g);b+=a[d].length}return c}function U(a,b){a="(?:"+T(a).join("|")+")";a="((?:\\s|\\S)*?)"+a+"|((?:\\s|\\S)+)";return new RegExp(a,b)}function V(a,b){return b.replace(/(<span\s+class\s*=\s*(["']))((?:(?!__)\w)+\2\s*>)/ig,
"$1"+a+"__$3")}function W(a,b){for(var c=2,d=0,f=a.length;d<f;d++){var g=a[d];if(b[c])break;c+=g.length}a=b.slice(c,c+g.length);a.push(b.index);a.push(b.input);return{step:g,matches:a}}function X(a){return a.step.replacement.apply({x:function(b,c){return x(b,c,a.step)}},a.matches)}function Y(a){return a.step.replacement.replace(/(\\\$)|(?:\$\$)|(?:\$(\d+))/g,function(b,c,d){b="";return b=c?"$":d?d=="0"?a.step.stepName:m(a.matches[d]):m(a.matches[0])})}function Z(a,b){var c="";if(!b[0])return c;if(c=
b[b.length-1])return c=m(c);a=W(a,b);c=e.isFunction(a.step.replacement)?X(a):Y(a);b=b[1];b=m(b);c=V(a.step.recipe._name,c);return c=b+c}function $(a,b,c){b=U(c,b._case?"g":"gi");for(var d=[],f;(f=b.exec(a))!=null&&f[0]!="";){var g=Z(c,f);d.push(g)}return d=d.join("")}function p(a,b,c,d){if(d)c=[A(b,c,d)];else{if(!c){c="_main";aa(b)}if(!c in b)return m(a);c=R(b,c)}return $(a,b,c)}function ba(a,b){return"."+a+"\n{\n\t"+b+"\n}\n"}function ca(a){var b=a._name,c=["/* Chili -- "+b+" */"];for(var d in a)if(!(d.search(/^_(?!main\b)/)>=
0)){var f=a[d];for(var g in f){var h=f[g];if(!(false in h)){h=h._style;if(typeof h=="string"){var i={};i[g]=h;h=i}for(var j in h){i=ba(b+"__"+j,h[j]);c.push(i)}}}}return c.join("\n")}function aa(a){var b=a._name;if(!e.chili.queue[b]){a=ca(a);e.chili.loadStylesheetInline(a);e.chili.queue[b]=true}}function da(a,b){for(var c="",d=0;d<b;d++)c+=a;return c}function m(a){return a.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function ea(a){return a.replace(/&amp;/g,"&").replace(/&lt;/g,
"<").replace(/&gt;/g,">")}function q(a,b,c){c=c||[];for(var d=/([\w\W]*?)(?:(<\w+[^>]*\/>)|(<\w+[^>]*>)|(<\/\w+[^>]*>))|([\w\W]+)/ig,f=function(h,i,j,o,k,l){h=g.index;if(l)l=n("text",l,h);else{l=n("text",i,h);b.apply(l,c);h+=i.length;if(j)l=n("empty",j,h);else if(o)l=n("open",o,h);else if(k)l=n("close",k,h)}b.apply(l,c)},g;(g=d.exec(a))!=null&&g[0]!="";)f.apply({},g)}function fa(a){return a.replace(/ /g,e.chili.whiteSpace.writingSpace)}function ga(a){return a.replace(/\t/g,e.chili.whiteSpace.writingTab)}
function ha(a){return a.replace(/\n/g,e.chili.whiteSpace.writingNewLine)}function ia(a){a=a;a=a.replace(/&nbsp;<BR>/ig,"\n");return a=a.replace(/\r\n?/g,"\n")}function ja(a){e.chili.whiteSpace.writingSpace="&#160;";e.chili.whiteSpace.writingTab=da("&#160;",e.chili.whiteSpace.tabWidth);e.chili.whiteSpace.writingNewLine="\n";if(/\r\n?/.test(a))e.chili.whiteSpace.writingNewLine=e.browser.msie?"&#160;<br>":/\r\n/.test(a)?"\r\n":"\r"}function v(a){ja(a);a=ia(a);if(e.chili.whiteSpace.no1stLine)a=a.replace(/^\n/,
"");return a}function y(a){var b=[];q(a,function(){var c=this.value;if(this.type=="text"){c=fa(c);c=ga(c);c=ha(c)}b.push(c)});return b=b.join("")}function ka(a,b){var c=[],d=b.join("");q(a,function(){if(this.type=="open")b.push(this.value);else this.type=="close"&&b.pop()});for(var f=0,g=b.length;f<g;f++){var h=b[f].replace(/^<(\w+)[^>]*>$/,"</$1>");c.unshift(h)}c=c.join("");a=d+a+c;return{line:a,open:b}}function la(a){var b=[];a=a.replace(/(.*)\n/g,function(c,d){c=ka(d,b);b=c.open;return d=c.line?
"<li>"+c.line+"</li>":"<li> </li>"});return a="<ol>"+a+"</ol>"}function ma(a,b,c){var d=parseInt(b,10);if(c){a=e("."+a);b=a.index(this);a.slice(0,b).each(function(){d+=e(this).find("li").length})}e(this).find("ol").attr("start",d);e("body").width(e("body").width()-1).width(e("body").width()+1)}function B(a){var b=e(a).html();b=v(b);b=la(b);b=y(b);a.innerHTML=b}function G(a){var b=e.chili.codeLineNumbers(a);if(b){B(a);ma.apply(a,b)}else e.chili.decoration.lineNumbers&&B(a)}function H(a){var b=a.subject;
if(/{:\w+\(/.test(b)){var c=0;b=m(b);b=b.replace(/({:(\w+)\((|(?:(['"])[^\4\n]*(?:\\.[^\4\n]*)*\4)(?:\s*,\s*((['"])[^\6\n]*(?:\\.[^\6\n]*)*\6))*)\)\[)((?:.|\n)*?)(\]\2:})/g,function(d,f,g,h,i,j,o,k,l,na){eval("args = ["+h+"];");d={original:k,start:na-c,count:k.length,callback:g,args:h};c+=f.length+l.length;if(e.isArray(a.filters))a.filters.push(d);else a.filters=[d];return k});b=ea(b);a.subject=b}}function n(a,b,c){return{type:a,value:b,start:c}}function oa(a){var b=[],c=0;q(a,function(){switch(this.type){case "empty":case "open":case "close":c+=
this.value.length;break;case "text":this.start-=c;this.end=this.start+this.value.length;break;default:throw"no type case for '"+this.type+"'";}b.push(this)});return b}function pa(a){return a.replace(/<span[^>]+><\/span>/g,"")}function C(a){for(var b=[],c=0,d=a.length;c<d;c++)b.push(a[c].value);b=b.join("");return b=pa(b)}function qa(a,b,c){c=b+c;for(var d=-1,f=-1,g="",h="",i="",j=0,o=a.length;j<o;j++){var k=a[j];if(k.type=="open")g=k.value;else if(k.type=="close")g="";else{if(k.start<=b&&b<k.end){d=
j;h=g}if(k.start<=c&&c<k.end){f=j;i=g}if(d!=-1&&f!=-1)break}}return{first:{position:d,span:h},last:{position:f,span:i}}}function D(a,b,c){var d=a.value.substr(0,b),f=n("text",d,a.start);f.end=a.start+d.length;d=a.value.substr(b);var g=n("text",d,a.start+b);g.end=a.start+b+d.length;a={first:[f],second:[g]};if(c){a.first.push(n("close","</span>"));a.second.unshift(n("open",c))}return a}function ra(a,b,c){var d=b+c,f=qa(a,b,c);c=a.slice(0,f.first.position);var g=a[f.first.position],h=a.slice(f.first.position+
1,f.last.position),i=a[f.last.position];a=a.slice(f.last.position+1);g=D(g,b-g.start,f.first.span);d=D(i,d-i.start,f.last.span);h=[].concat(g.second,h,d.first);h=C(h);b=n("html",h,b);a=[].concat(c,g.first,b,d.second,a);return{tokens:a,position:c.length+g.first.length}}function I(a,b){var c=b;if(!a.filters)return c;for(var d=[],f=0,g=a.filters.length;f<g;f++){var h=a.filters[f],i=e.chili.filters&&e.chili.filters[h.callback];if(i&&e.isFunction(i)){if(0==d.length)d=oa(b);var j=ra(d,h.start,h.count);
d=j.tokens;j=j.position;h=i.apply({text:h.original,html:d[j].value},h.args);d[j].value=h}}if(0<d.length)c=C(d);return c}function sa(){e.browser.msie?document.selection.empty():window.getSelection().removeAllRanges()}function ta(){element=this;sa()}function ua(){var a;if(e.browser.msie)a=document.selection.createRange().htmlText;else{a=window.getSelection();var b=a.getRangeAt(0);a=a.toString();b=b.toString();a=/\n/.test(a)?a:b}return a}function va(a){var b=z(a),c="";if(/<br\b/i.test(a)||/<li\b/i.test(a)){if(/<br\b/i.test(a))a=
a.replace(/\<br[^>]*?\>/ig,b);else if(/<li\b/i.test(a))a=a.replace(/<ol[^>]*?>|<\/ol>|<li[^>]*?>/ig,"").replace(/<\/li>/ig,b);var d=e("<pre>").appendTo("body").hide()[0];d.innerHTML=a;c=e(d).text().replace(new RegExp(b,"g"),"\r\n");e(d).remove()}return c}function wa(a){return e.browser.msie?va(a):a.replace(/\r/g,"").replace(/^# ?/g,"").replace(/\n# ?/g,"\n")}function xa(a,b){var c=e.chili.selection.box;a=e(e.browser.msie?'<textarea style="'+c.style+'">':'<pre style="'+c.style+'">').appendTo("body").text(a).attr("id",
"chili_selection").click(function(){e(this).remove()});var d=c.top(b.pageX,b.pageY,a.width(),a.height());b=c.left(b.pageX,b.pageY,a.width(),a.height());a.css({top:d,left:b});return a}function ya(a){if(e.browser.msie){a[0].focus();a[0].select()}else{var b=window.getSelection();b.removeAllRanges();var c=document.createRange();c.selectNodeContents(a[0]);b.addRange(c)}}function za(a){if(element&&element==this){element=null;var b=ua();if(""!=b){b=wa(b);a=xa(b,a);ya(a)}}}function F(a){if(e.chili.selection.active&&
(e.browser.msie||e.browser.mozilla))e(a).parents().filter("pre").bind("mousedown",ta).bind("mouseup",za)}e.extend({chili:{options:{whiteSpace:{tabWidth:4,no1stLine:true},automatic:{active:true,selector:"code",context:document},dynamic:{active:true,origin:""},decoration:{lineNumbers:false},selection:{active:true,box:{style:"position:absolute; z-index:3000; overflow:scroll; width:16em; height:9em; border:1px solid gray; padding:1em; background-color:white;",top:function(a,b,c,d){return b-Math.round(d/
2)},left:function(a){return a}}}}}});e(function(){e.chili.loadStylesheetInline(".chili-ln-off {list-style-type: none;}");e.extend(e.chili,e.chili.options);e.chili.automatic.active&&e(e.chili.automatic.selector,e.chili.automatic.context).chili()});e.extend(e.chili,{version:"next",codeLanguage:function(a){return(a=e(a).attr("class").match(/\bchili-lang-(\w+)/))?a[1]:""},codeLineNumbers:function(a){a=e(a).attr("class").match(/\bchili-ln-(\d+)-([\w][\w\-]*)|\bchili-ln-(\d+)/);return!a?null:a[3]?[a[0],
a[3],""]:[a[0],a[1],a[2]]},revealChars:function(a){for(var b=[],c=0,d=a.length;c<d;c++)b.push(a[c]+" <- "+a.charCodeAt(c));return b=b.join("\n")},loadStylesheetInline:function(a){if(document.createElement){var b=document.createElement("style");b.type="text/css";if(b.styleSheet)b.styleSheet.cssText=a;else{a=document.createTextNode(a);b.appendChild(a)}document.getElementsByTagName("head")[0].appendChild(b)}},queue:{},recipes:{},filters:{off:function(){return this.text}}});e.fn.chili=function(a){var b=
e.extend({},e.chili);e.chili=e.extend(true,e.chili,a||{});this.each(function(){E(this)});e.chili=b;return this}})(jQuery);

