(function(){"use strict";u8.Base.Help=function(){this.id="help";this.providers=u8.Base.Help.providers;this.debug=u8.debug.level;u8.services.events.bind("u8.services.pages.onPageLoad",this._onPageLoad.bind(this),this.id)};u8.Base.Help.makeClass("Help");$.extend(u8.Base.Help.prototype,{dispose:function(){if(this._isDisposed){return}u8.services.events.unbind("u8.services.pages.onPageLoad",this.id);this._isDisposed=true},show:function(t){if(!t){t={}}if(typeof t=="string"){t={context:this._createContext(t)}}else if(t.uri){t.context=this._createContext(t.uri)}if(!t.context||t.context.constructor!==u8.Base.Help.Context){t.context=this._createContext(t.context)}this._open(t)},getHelp:function(t,e){var n=typeof t=="string"?t:t.uri;if(typeof n=="string"){n=new u8.Base.Uri(n)}if(!n){return e&&e(this,{topics:[]})}var i="Help/"+n.toString_();var r=u8.services.cache.get(i);if(typeof r!="undefined"){return e&&e(this,{topics:r})}var o=this.providers[n.path]||this.providers["default"];if(!o){return e(this,{topics:[]})}var s=this;o.call(this,{uri:n.toString_(),element:t.element},function(t,n){Function.chain(s._loadTopics.curry(n.topics),s._formatTopics.curry(n.topics)).call(s,function(){if(!o.nocache){u8.services.cache.put(i,n.topics||null)}}.chain(e,t,n))})},showTip:function(t){var e=t&&t.element;if(!e){return}var n=$u(".Tooltip",$p(e))[0];if(n){n.show(t)}},addHelp:function(t,e){var n="Help/"+t.uri;u8.services.cache.put(n,t.topics||null);Function.chain(this._loadTopics.curry(t.topics),this._formatTopics.curry(t.topics)).call(this,e)},_open:function(t){var e=t.context;var n=e.toUri();if(u8.debug.level){u8.log.info("Showing help for html:"+e.toPrettyHtml())}var i={helpUri:n.toString_()};var r=u8.services.navigation;var o=r.getPage("Help")&&r.getPageUrl("Help",i);if(!o){o=new u8.Base.Uri({path:u8.resolveUrl("~/crm/help.aspx"),params:i})}var s="resizable,scrollbars,height=800,width=1000";var a=t.windowName||"u8.help";window.open(o.toString_(),a,s)},_createContext:function(t){return new u8.Base.Help.Context(t)},_createTooltipWidget:function(t){if(!t){return}var e=t.element.$;var n=e("<div style='display:none;' />").appendTo(t.element.document.body);n.widget("Tooltip",{parent:t})},_loadTopics:function(t,e){return e()},_formatTopics:function(t,e){if(!t||!t.length){return e()}var n=$w("top").marked;if(n){var i=[];t.forEach(function(t){if(!t.contents){return}if(!t.contentType||t.contentType=="text/x-markdown"){i.push(this._formatTopic.bind(this,t))}}.bind(this));return Function.join(i)(e)}else{return e()}},_onPageLoad:function(t,e){var n=t.element;var i=n.document;var r=this;this._createTooltipWidget(t);if("onhelp"in n){n.onhelp=function(t){r._onHelp(t);return false}}else{i.onkeydown=function(t){var e=t.keyCode==112;if(e){r._onHelp(t);return false}}}},_onHelp:function(t){this.show({windowName:t.shiftKey?"_blank":undefined});t.preventDefault();t.cancelBubble=true},_formatTopic:function(t,e){t.contentType="text/html";t.contentStyle="u8-markdown";var n=marked(t.contents);n.format(this._replaceFieldPlaceholders,function(e){t.contents=e}.chain(e))},_replaceFieldPlaceholders:function(t){return"{"+t+"}"}});u8.Base.Help.providers={default:function(t,e){var n=this;u8.request("HelpChannel.GetHelp",{uri:t.uri},function(t,i){return e&&e(n,i.error?i:{topics:i.response})})},"help://dom":t,"help://texts":n,"help://page":e};function t(t,e){var n,i;var r=t.element;if(r){i=r.getAttribute("data-helpcontenttype");var o=!!r.hasAttribute("data-helpusecontent");n=o?r.textContent:r.getAttribute("data-helptext");var s=o||r.hasAttribute("data-helpexceedselement");if(s){var a=r.hasAttribute("data-helpuseparentoverflow");var u=a?r.parentElement:r;if(u.scrollWidth<=u.offsetWidth&&u.scrollHeight<=u.offsetHeight){n=""}}}var c=[];if(n){c.push({contents:n,contentType:i||"text/plain"})}e(this,{topics:c})}t.nocache=true;function e(t,e){var n=$p(t.element);var i=t.uri.substring(t.uri.indexOf("?")+1);var r=n.$("head > script[data-help-id="+i+"]");if(r.length==0){$error("Could not find page help for '"+t.uri+"'")}var o=r.attr("type")||"text/html";e(this,{topics:[{contents:r.html(),contentType:o}]})}e.nocache=true;function n(t,e){var n=this,i=new u8.Base.Uri(t.uri),r=i.get("textId"),o=i.get("contentType");u8.using({textIds:[r]},function(){e(n,{topics:[{contents:$t(r),contentType:o||"text/plain"}]})})}u8.Base.Help.Context=function(t){this._data={};if(typeof t=="string"){t=new u8.Base.Uri(t)}if(u8.tools.isInstanceOfClass(t,"Uri")){this.fromUri(t)}else if(t){this.add(t)}};$.extend(u8.Base.Help.Context.prototype,{add:function(t){$.extend(this._data,t);return this},toUri:function(){var t=new u8.Base.Uri(this.path||"help://help");t.params=$.extend(t.params,this._data);return t},fromUri:function(t){this.path=t.path;this.add(t.params)},toPrettyHtml:function(){return u8.tools.toPrettyHtml(this._data)}});u8.services.add({name:"help",ctor:u8.Base.Help,scope:"Session"})})();