<?xml version="1.0"?>
<!DOCTYPE bindings SYSTEM "/twitter/entities.dtd">
<bindings xmlns="http://www.mozilla.org/xbl" xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
	<binding id="microbrowser">
		<content>
			<xul:vbox flex="1">
				<xul:browser type="content-primary" anonid="browser" flex="1" disablesecurity="true" disablehistory="true" />
			</xul:vbox>
		</content>
		<implementation>
			<field name="browser">document.getAnonymousElementByAttribute(this, "anonid", "browser")</field>
			<constructor>
				var defaultTitle = "&twitter.dialog.auth.default-title;"
				var shareTitle = "&twitter.dialog.share.title;"
				var postTitle = "&twitter.dialog.post.title;"
				<![CDATA[
				var args = window.arguments[0];
				var browser0 = this.browser;
				
				browser0.docShell.allowPlugins = false;
				
				if (args.init) {
					args.init.call(this, this.browser);
				}

				if (args.inject) {
					browser0.addEventListener("DOMContentLoaded", function () {
						args.inject(browser0.contentDocument);
					}, true, true);
				}
				
				if (args.onload) {
					window.addEventListener("load", args.onload, false);
				}
				
				if (args.onclose) {
					window.addEventListener("close", args.onclose, false);
				}
				
				if (args.progressListener) {
					browser0.addProgressListener(args.progressListener, args.notifyMask);
				}
				
				if (args.htmlOnly) {
					browser0.docShell.allowImages = false;
				}
				
				if (args.title) {
					var title = args.title;
					switch (title) {
						case "&twitter.dialog.share.title;":
							title = shareTitle;
							break
						case "&twitter.dialog.post.title;":
							title = postTitle;
							break
					}
					document.title = title;
				} else {
					this.titleTimer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
					
					this.titleTimer.initWithCallback(function () {
						document.title = browser0.contentDocument.title || defaultTitle;
					}, 100, titleTimer.TYPE_REPEATING_SLACK);
				}

				var observer = {
					observe: function (subj, topic, data) {
						//browser0.loadURIWithFlags(args.url, 64); // 64 - no history
						browser0.loadURI(args.url);
						window.focus();
					}
				};
				
				var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
				var app_observer = {
					QueryInterface: function () { return this; },
					observe: function (subject, topic, data) {
						if (topic == "ru.yandex.bar.twitter") {
							switch (data) {
								case "finalize": {
									if (args.onclose) {
										args.onclose();
									}
									window.close();
									break;
								}
							}
						}
					}
				}
				
				observerService.addObserver(app_observer, "ru.yandex.bar.twitter", false);
				
				var timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
				timer.init(observer, 10, timer.TYPE_ONE_SHOT);
			]]></constructor>
			<destructor><![CDATA[
				var args = window.arguments[0];
				if (args.onclose) {
					try {
						window.removeEventListener("close", args.onclose, false);
					} catch (e) {
					}
				}				
				if (args.onload) {
					try {
						window.removeEventListener("load", args.onload, false);
					} catch (e) {
					}
				}
				try {
					this.titleTimer.cancel();
				} catch (e) {}
			]]></destructor>
		</implementation>
	</binding>
</bindings>