/*! doctop - v1.2.0 - 2015-08-01
* https://github.com/times/doctop
* Copyright (c) 2015 Ændrew Rininsland; Licensed MIT */
!function(a){a.doctop=function(b){this.options=a.extend({},a.doctop.options,b),this._cleanGDoc=function(b){var c;return c=this.options.staticExport?a(b).not("meta").not("style").not("title"):a(b).filter("#contents").children().not("style")},this._parseAndCleanDOM=function(b){var c;if(c=this._cleanGDoc(b),this.options.preserveFormatting||this.options.fancyOutput){var d=this.options.staticExport?a(b).filter("style")[0].innerHTML:a(b).filter("#contents").children("style")[0].innerHTML,e=/(\.[a-z0-9]+?)\{[^{}]*?font-weight:bold[^{}]*?\}/gi.exec(d),f=/(\.[a-z0-9]+?)\{[^{}]*?font-style:italic[^{}]*?\}/gi.exec(d);e&&e.length>0&&c.find("span"+e[1]).each(function(b,c){a(c).replaceWith(""+c.innerHTML+"")}),f&&f.length>0&&c.find("span"+f[1]).each(function(b,c){a(c).replaceWith(""+c.innerHTML+"")})}return a.grep(c.find("span"),function(b){return a(b).text().length>0?(a(b).replaceWith(b.innerHTML),!0):void 0}),c.each(function(a,b){b.innerHTML=b.innerHTML.replace(/(?:\x0A| )/gi," ")}),a("span:not(:has(*))").remove(),c},this._parseDOMIntoTree=function(b){for(var c,d,e,f=this.options,g=function(a,b){return f.fancyOutput?{index:Object.keys(a).length,content:b.textContent.trim(),depth:Number(b.tagName.toUpperCase().replace("H",""))-1,tag:b.tagName,children:{}}:{}},h=function(b,c){return f.fancyOutput?{content:a(b).text(),content_html:b.innerHTML,index:Object.keys(c).length,tag:b.tagName}:f.preserveFormatting?b.innerHTML:a(b).text()},i=function(a,b){for(var c=0;"undefined"!=typeof b[a];)a=a+"_"+c,c++;return a},j={},k=j,l=1,m=0,n=0,o=b[0];o&&1===o.nodeType;){switch(c=o.tagName.toLowerCase()){case"h1":case"h2":case"h3":case"h4":case"h5":case"h6":d=f.simpleKeys?c+"_"+m:o.textContent.trim(),"h1"===c?(d=i(d,j),j[d]=g(j,o),k=f.fancyOutput?j[d].children:j[d],e=k,m++):l>=Number(c.substr(1))?(d=i(d,e),e[d]=g(e,o),k=f.fancyOutput?e[d].children:e[d]):l"!==o.innerHTML&&(n=Object.keys(k).length>0?Object.keys(k).length:0,d=c+"_"+n,k[d]=h(o,k))}o=o.nextElementSibling}return j},this._parseArchieML=function(b){var c={_base:function(b){var d,e="";return"undefined"!=typeof b.tagName?b.children.length?a.each(b.children,function(a,b){(d=c[b.tagName.toLowerCase()])&&(e+=d(b))}):e+=a(b).text():b.each(function(a,b){(d=c[b.tagName.toLowerCase()])&&(e+=d(b))}),e},text:function(b){return a(b).text()},span:function(a){return c._base(a)},p:function(a){return c._base(a)+"\n"},a:function(b){var c=a(b).attr("href");if(void 0===c)return"";c&&b.search.indexOf("?q=")>-1&&(c=b.search.substr(b.search.indexOf("q=")+2,b.search.indexOf("&")>-1?b.search.indexOf("&")-3:void 0),c=decodeURIComponent(c));var d='';return d+=a(b).text(),d+=""},li:function(a){return"* "+c._base(a)+"\n"}};return["ul","ol"].forEach(function(a){c[a]=c.span}),["h1","h2","h3","h4","h5","h6"].forEach(function(a){c[a]=c.p}),c._base(b)},this._doCallbacks=function(b){if("undefined"!=typeof this.options.tabletop_url&&"undefined"!=typeof Tabletop){var c=new a.Deferred;Tabletop.init({key:this.options.tabletop_url,simpleSheet:this.options.tabletop_simplesheet,proxy:this.options.tabletop_proxy,callback:function(a,b){c.resolve({data:a,tabletop:b})}}),a.when(c).done(a.proxy(function(a){this.options.callback.call(b,{copy:b,data:a})},this))}else this.options.callback.call(b,{copy:b})},a.ajax({context:this,url:this.options.url,type:"GET",cache:this.options.cache,crossDomain:!0,success:function(a){var b,c=this._parseAndCleanDOM(a),d=this._parseDOMIntoTree(c);this.options.archieml&&"object"==typeof window.archieml&&(b=this._parseArchieML(this._cleanGDoc(a)),b=b.replace(/<[^<>]*>/g,function(a){return a.replace(/”|“/g,'"').replace(/‘|’/g,"'")}),d.archie=archieml.load(b)),this._doCallbacks(d)}})},a.doctop.options={callback:function(a){console.log("You forgot to specify a callback..."),console.dir(a)},url:"",tabletop_url:void 0,tabletop_proxy:void 0,tabletop_simplesheet:!1,preserveFormatting:!0,simpleKeys:!1,cache:!0,staticExport:!1,fancyOutput:!1,archieml:!1}}(jQuery);