<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bindings SYSTEM "../entities.dtd">

<bindings xmlns="http://www.mozilla.org/xbl"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
		  xmlns:html="http://www.w3.org/1999/xhtml">
		  
	<binding id="toolbarbutton" extends="/-common/content/basewidget.xbl#toolbarbuttonbase">
		<resources>
			<stylesheet src="main.css" />
		</resources>
		<content>
			<xul:toolbarbutton type="button" class="toolbarbutton-1 widget-button" item-href="main" item-command="auth"/>
            <xul:toolbarbutton type="menu-button" anonid="button-main" item-href="main" class="toolbarbutton-1 widget-button bold-menu-counter">
				<xul:menupopup anonid="menu">
					<xul:menuitem item-command="update" item-param="main" label="&menu.refresh;" />
					<xul:menuseparator/>
					<xul:menuitem class="menuitem-iconic" item-href="news" label="&menu.lenta;" />
					<xul:menuitem class="menuitem-iconic" item-href="wall" label="&menu.wall;" />
					<xul:menuitem class="menuitem-iconic" item-href="friends" label="&menu.friends;" anonid="mi-friends" />
					<xul:menuitem class="menuitem-iconic" item-href="messages" label="&menu.messages;" anonid="mi-messages" />
					<xul:menuitem class="menuitem-iconic" item-href="notifications" label="&menu.notif;" anonid="mi-notifications" />
					<xul:menuseparator/>
					<xul:menuitem class="menuitem-iconic" item-command="share" label="&menu.share;" anonid="mi-share" />
					<xul:menuitem class="menuitem-iconic" item-command="post" label="&menu.setstatus;" />
					<xul:menuseparator/>
					<xul:menuitem class="menuitem-iconic" item-href="media" label="&menu.fotovideo;" />
					<xul:menuitem class="menuitem-iconic" item-href="notes" label="&menu.notes;" />
					<xul:menuitem class="menuitem-iconic" item-href="events" label="&menu.events;" />
					<xul:menuseparator/>
					<xul:menuitem item-command="logout" label="&menu.logout;" />
				</xul:menupopup>
			</xul:toolbarbutton>
			<xul:toolbarbutton type="menu-button" item-href="friends" item-param="req" anonid="button-friends" class="toolbarbutton-1 widget-button">
				<xul:menupopup anonid="menu-friends">
					<xul:menuitem class="menuitem-iconic" item-command="update" item-param="friends" label="&menu.refresh;" />
					<xul:menuseparator anonid="sep-friends" />
				</xul:menupopup>
			</xul:toolbarbutton>
			<xul:toolbarbutton type="menu-button" item-href="messages" anonid="button-messages" class="toolbarbutton-1 widget-button">
				<xul:menupopup anonid="menu-messages">
					<xul:menuitem class="menuitem-iconic" item-command="update" item-param="messages" label="&menu.refresh;" />
					<xul:menuseparator anonid="sep-messages" />
				</xul:menupopup>
			</xul:toolbarbutton>
			<xul:toolbarbutton type="menu-button" item-href="notifications" anonid="button-notifications" class="toolbarbutton-1 widget-button">
				<xul:menupopup anonid="menu-notifications">
					<xul:menuitem class="menuitem-iconic" item-command="update" item-param="notifications" label="&menu.refresh;" />
					<xul:menuseparator anonid="sep-notifications" />
				</xul:menupopup>
			</xul:toolbarbutton>
			<xul:toolbarbutton item-command="share" anonid="button-share" class="toolbarbutton-1 widget-button" tooltiptext="&share.tooltip;" />
		</content>
		<implementation implements="nsISupports, nsIDOMEventListener">
			<field name="btnAuth">document.getAnonymousElementByAttribute(this, "item-command", "auth")</field>
            <field name="mi_logout">document.getAnonymousElementByAttribute(this, "item-command", "logout")</field>
			<field name="buttons">({ })</field>
            <field name="menus">({ })</field>
			<field name="menuitems">({ })</field>
            <field name="btn_ids">([ "main", "friends", "messages", "notifications", "share" ])</field>
            <field name="counter_ids">([ "friends", "messages", "notifications" ])</field>

			<field name="observers"><![CDATA[
			({
				"showitems": function() {
					this.showItems();
                    this.updateFresh();
				},
				"throbber": function(topic, data) {
					var ids = data ? [data] : this.btn_ids;
                    for (var i=0; i<ids.length; ++i) {
                        if (this.buttons[ids[i]]) {
                            this.buttons[ids[i]].setAttribute("throbber", !!data);
                        }
                    }
				},
				"display": function(topic) {
					this.log(topic);
					this.updateView();
                    this.updateFresh();
				}
			})
			]]></field>

			<constructor><![CDATA[
				this.log('constructor');
				var ent = this.application.entities;
				this.btnAuth.setAttribute("tooltiptext", ent.text("main.tooltip") + '\n' + ent.text("please.auth"));
				var i, ids = this.btn_ids;
				for (i=0; i<ids.length; i++) {
                    var id = ids[i];
					this.buttons[id] = document.getAnonymousElementByAttribute(this, "anonid", "button-" + id);
					this.menus[id] = document.getAnonymousElementByAttribute(this, "anonid", "menu-" + id);
					this.menuitems[id] = document.getAnonymousElementByAttribute(this, "anonid", "mi-" + id);;
				}
				this.showItems();
				this.updateView();
                this.updateFresh();
			]]></constructor>
			
			<method name="updateView">
				<body><![CDATA[
					var data = this.application.getUserData();
                    this.setAttribute("auth", !!data);
                    
                    if (data) {
                        var ents = this.application.entities;
                        var user = data.user;
                        var items = this.counter_ids;
                        var itemMap = {friends: "friend_requests"};
                        
                        this.mi_logout.setAttribute("label", ents.text("menu.logout") + (user&&user.name ? " (" + user.name + ")" : ""));
                        for (var i=0; i<items.length; ++i) {
                            var itemName = items[i];
                            var itemCName = itemMap[itemName] || itemName;
                            var count = data[itemCName + "_count"] || "";
                            this.buttons[itemName].setAttribute("counter", count);
                            this.setMenuitemCounter(this.menuitems[itemName], count);
                        }
                        
                        // tooltips
                        var mainTip = (user&&user.name ? user.name + " - " : "") + ents.text("main.tooltip");
                        
                        this.buttons.main.setAttribute("tooltiptext", mainTip + (data.error ? "\n" + ents.text("inet.error.main") : ""));
                        this.buttons.messages.setAttribute("tooltiptext", ents.text("messages.tooltip") + (data.error ? "\n" + ents.text("inet.error.msg") : ""));
                        this.buttons.friends.setAttribute("tooltiptext", ents.text("friend.tooltip") + (data.error ? "\n" + ents.text("inet.error.friend") : ""));
                        this.buttons.notifications.setAttribute("tooltiptext", ents.text("notif.tooltip") + (data.error ? "\n" + ents.text("inet.error.notif") : ""));
                        
                        // update menus
                        this.updatePopupMenu("friends", data.friend_requests, function(req) {
                            return req._profile && {
                                "label": ents.text("menu.friend.req").replace("{username}", req._profile.name),
                                "item-href": "profile",
                                "item-param": req.uid_from
                            };
                        });
                        this.updatePopupMenu("messages", data.messages, function(msg) {
                            var tpl = msg.subject ? "{username}: " + msg.subject : ents.text("menu.message.from");
                            return msg._profile && {
                                "label": tpl.replace("{username}", msg._profile.name),
                                "item-href": "message_thread",
                                "item-param": msg.snippet_author  // (/^\d+$/.test(msg.thread_id) ? "id." + msg.thread_id : msg.thread_id)
                            };
                        });
                        this.updatePopupMenu("notifications", data.notifications, function(notif) {
                            return {
                                "label": notif.title_text,
                                "item-href": "notification"
                            };
                        });
                    }
				]]></body>
			</method>
			<method name="updatePopupMenu">
                <parameter name="id"/>
                <parameter name="items"/>
                <parameter name="func"/>
                <body><![CDATA[
                    var menu = this.menus[id],
                        sep  = document.getAnonymousElementByAttribute(this, "anonid", "sep-" + id);
                        
                    while (sep.nextSibling && /menuitem/i.test(sep.nextSibling.localName)) {
                        menu.removeChild(sep.nextSibling);
                    }
                    if (items) {
                        for(i=0; i<items.length; ++i) {
                            var conf = func.call(this, items[i]);
                            if (conf) {
                                menu.appendChild(this.createNode(null, 'menuitem', conf));
                            }
                        }
                    }
                ]]></body>
            </method>
            <method name="updateFresh">
				<body><![CDATA[
					var fresh = false;
                    for (var i=0; (i<this.counter_ids.length) && !fresh; ++i) {
                        var itemName = this.counter_ids[i];
                        fresh = (this.buttons[itemName].getAttribute("counter") && !this.application.api.Settings.getValue("show-" + itemName + "-button"));
                    }
                    this.buttons.main.setAttribute("fresh", !!fresh);
				]]></body>
			</method>
			<method name="showItems">
				<body><![CDATA[
					var ids = this.btn_ids;
                    for (var i=1; i<ids.length; ++i) {
                        var show = this.application.api.Settings.getValue("show-" + ids[i] + "-button");
                        this.buttons[ids[i]].setAttribute("hidden", !show);
                        this.menuitems[ids[i]].setAttribute("hidden", show);
                    }
                    this.updateFresh();
				]]></body>
			</method>
		</implementation>
	</binding>
</bindings>