<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<!DOCTYPE bindings SYSTEM "chrome://unplug/locale/unplug_result.dtd" >
<!--
/*
 *         _        ___
 *    /\ / /___    / _ \ /\ /\  _ ___
 *   / // // _ \  / // // // // // _ \
 *  / // // // / / ___// // // // // /
 *  \___//_//_/ /_/   /_/ \___/ \_  /
 *                             \___/
 * 
 *  Compunach UnPlug
 *  Copyright (C) 2009 David Batley <unplug@dbatley.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
-->
<bindings xmlns="http://www.mozilla.org/xbl" xmlns:xbl="http://www.mozilla.org/xbl" xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
	<!--
		a result to display in searchpage.xul
		these are created as we find the search results
	-->	
	<binding id="unplug_result">
		<implementation>
			<constructor>
				callback = (function (evt, responsename, responsedata) {
					var reference;
					var unplug_result_element = document.getBindingParent(evt.target);
					try {
						reference = unplug_result_element.getAttribute("reference");
						UnPlug2SearchPage.widgetresponse(reference, responsename, responsedata);
					} catch(e) {
						// pass
					}
					evt.stopPropagation();
				});
			</constructor>
			<method name="initResult">
				<parameter name="result"/>
				<parameter name="preferred_downloaders"/>
				<body>
					/*
					 * Set up the item
					 * TODO - remove capabilities from here (use widgetavailable instead)
					 */
					var that = this;
					var getelem = (function(elem_id) {
						return document.getAnonymousElementByAttribute(that, "id", elem_id);
					});
					var setattr = (function(elem_id, attr_name, attr_value) {
						getelem(elem_id).setAttribute(attr_name, attr_value);
					});
					var isavailable = (function(widgetname) {
						return UnPlug2SearchPage.widgetavailable(result, widgetname);
					});
					var hideifnotavail = (function(elem_id, widgetname) {
						if (isavailable(widgetname)) {
							getelem(elem_id).style.display = "block";
						} else {
							getelem(elem_id).style.display = "none";
						}
					});
					var disableifnotavail = (function(elem_id, widgetname) {
						if (isavailable(widgetname)) {
							setattr(elem_id, "disabled", false);
						} else {
							setattr(elem_id, "disabled", true);
						}	
					});
					var set_default_downloader = (function () {
						// set the default value for the download button
						preferred_downloaders = preferred_downloaders || [];
						preferred_downloaders.push("fallback");
						for (var i = 0; i &lt; preferred_downloaders.length; i++) {
							switch (preferred_downloaders[i]) {
								case "fallback":
									if (true) {
										setattr("dl", "oncommand", "callback(event, 'fallback', null)");
										setattr("dl", "tooltiptext", "&dl.nodefault.tooltip;");
										getelem("dl").className = "fallback";
										return;
									}
									break;
								case "saveas":
									if (isavailable("saveas")) {
										setattr("dl", "oncommand", "callback(event, 'saveas', null)");
										setattr("dl", "tooltiptext", "&dl.saveas.tooltip;");
										getelem("dl").className = "saveas";
										return;
									}
									break;
								case "downthemall":
									if (isavailable("downthemall")) {
										setattr("dl", "oncommand", "callback(event, 'downthemall', null)");
										setattr("dl", "tooltiptext", "&dl.dta.tooltip;");
										getelem("dl").className = "dta";
										return;
									}
									break;
								case "flashgot":
									if (isavailable("flashgot")) {
										setattr("dl", "oncommand", "callback(event, 'flashgot', null)");
										setattr("dl", "tooltiptext", "&dl.flashgot.tooltip;");
										getelem("dl").className = "flashgot";
										return;
									}
									break;
								case "openover":
									if (isavailable("openover")) {
										setattr("dl", "oncommand", "callback(event, 'openover', null)");
										setattr("dl", "tooltiptext", "&open.over.tooltip;");
										getelem("dl").className = "openover";
										return;
									}
									break;
							}
						}
					});
					
					// debug
					setattr("contentbox", "tooltiptext", result.download.toSource() + "\n" + result.details.trace);
					
					// basic information
					setattr("name", "value", result.details.name || "name missing!");
					setattr("host", "value", result.details.host || "host missing!");
					setattr("protocol", "value", result.details.protocol || "proocol missing!");
					setattr("description", "value", result.details.description || "");
					
					// thumbnail
					if (result.details.thumbnail) {
						setattr("thumbnail", "src", result.details.thumbnail);
						setattr("thumbnail", "tooltiptext", result.details.thumbnail);
					}
					
					// copy url
					disableifnotavail("copyurl",     "copyurl");
					
					// openers
					disableifnotavail("open_tab",    "opentab");
					disableifnotavail("open_new",    "opennew");
					disableifnotavail("open_over",   "openover");
					
					// downloaders
					disableifnotavail("dl_saveas",   "saveas");
					disableifnotavail("dl_dta",      "downthemall");
					disableifnotavail("dl_flashgot", "flashgot");
					
					// det default downloader on dl button
					set_default_downloader();
				</body>
			</method>
			<method name="addDuplicate">
				<parameter name="result"/>
				<body>
					/*
					 * Duplicates are downloaded in the same way, but may have different
					 * details associated with them.
					 */
					var that = this;
					var getelem = function(elem_id) {
						return document.getAnonymousElementByAttribute(that, "id", elem_id);
					}
					var getattr = function(elem_id, attr_name) {
						return getelem(elem_id).getAttribute(attr_name);
					}
					var setattr = function(elem_id, attr_name, attr_value) {
						getelem(elem_id).setAttribute(attr_name, attr_value);
					}
					
					// debug
					setattr("contentbox", "tooltiptext", getattr("contentbox", "tooltiptext") + "\n" + result.details.trace);
					
					// basic information
					if (result.details.description) {
						setattr("description", "value", result.details.description);
					}
					
					// thumbnail
					if (result.details.thumbnail) {
						setattr("thumbnail", "src", result.details.thumbnail);
						setattr("thumbnail", "tooltiptext", result.details.thumbnail);
					}
				</body>
			</method>
			
			<!-- when callbacks are made, this value is supplied -->
			<field name="reference" />
		</implementation>
		<content>
			<xul:vbox id="contentbox" flex="1" style="border: 1px black solid">
				<xul:toolbar id="test-toolbar" flex="1">
					<xul:image id="thumbnail" />
					<xul:vbox flex="1" style="overflow: hidden;">
						<xul:hbox>
							<xul:label id="host" />
							<xul:label id="name" />
						</xul:hbox>
						<xul:hbox>
							<xul:label id="protocol" class="unplug-sub-heading" />
							<xul:label id="description" class="unplug-sub-heading" />
						</xul:hbox>
					</xul:vbox>
					<!--
						we want these to be drop-down buttons
						ie Open / Open in a new window
						and Download / Dowload with dTa / download with XXX
						and Play / Play in vlc / Play in totem / Look for media players
					-->
					<!-- aternative is type="menu" -->
					<xul:toolbarbutton
						id="copyurl"
						accesskey="&copyurl.accesskey;"
						label="&copyurl.label;"
						tooltiptext="&copyurl.tooltip;"
						class="menuitem-iconic copyurl"
						type="button"
						disabled="true"
						oncommand="callback(event, 'copyurl', null);"
						tabindex="1"
						/>
					<xul:toolbarbutton
						id="dl"
						accesskey="&dl.accesskey;"
						label="&dl.label;"
						type="menu-button"
						tabindex="1"
						onkeydown="if (event.keyCode == 32) { this.open = true; }"
						>
						<xul:menupopup>
							<xul:menuitem
								id="dl_saveas"
								accesskey="&dl.saveas.accesskey;"
								label="&dl.saveas.label;"
								tooltiptext="&dl.saveas.tooltip;"
								class="menuitem-iconic saveas"
								oncommand="callback(event, 'saveas', null);"
								/>
							<xul:menuitem
								id="dl_dta"
								accesskey="&dl.dta.accesskey;"
								label="&dl.dta.label;"
								tooltiptext="&dl.dta.tooltip;"
								class="menuitem-iconic dta"
								oncommand="callback(event, 'downthemall', null);"
								/>
							<xul:menuitem
								id="dl_flashgot"
								accesskey="&dl.flashgot.accesskey;"
								label="&dl.flashgot.label;"
								tooltiptext="&dl.flashgot.tooltip;"
								class="menuitem-iconic flashgot"
								oncommand="callback(event, 'flashgot', null);"
								/>
							<xul:menuseparator />
							<xul:menuitem
								id="open_tab"
								accesskey="&open.tab.accesskey;"
								label="&open.tab.label;"
								tooltiptext="&open.tab.tooltip;"
								class="menuitem-iconic opentab"
								oncommand="callback(event, 'opentab', null);"
								/>
							<xul:menuitem
								id="open_new"
								accesskey="&open.new.accesskey;"
								label="&open.new.label;"
								tooltiptext="&open.new.tooltip;"
								class="menuitem-iconic opennew"
								oncommand="callback(event, 'opennew', null);"
								/>
							<xul:menuitem
								id="open_over"
								accesskey="&open.over.accesskey;"
								label="&open.over.label;"
								tooltiptext="&open.over.tooltip;"
								class="menuitem-iconic openover"
								oncommand="callback(event, 'openover', null);"
								/>
							<xul:menuseparator />
							<xul:menuitem
								accesskey="&config.accesskey;"
								label="&config.label;"
								tooltiptext="&config.tooltip;"
								class="menuitem-iconic config"
								oncommand="callback(event, 'config', 'downloader');"
								/>
						</xul:menupopup>
					</xul:toolbarbutton>
				</xul:toolbar>
			</xul:vbox>
		</content>
	</binding>

</bindings>

