home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 March / Chip_2011.03_CD.iso / Tools / yabar.msi / filF0298B0202F77D777DC8C37B07F238F1 < prev    next >
Encoding:
Text File  |  2010-04-16  |  43.8 KB  |  1,301 lines

  1. var Ya = {
  2.   observerService:    Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService),
  3.   nsIYa:              Cc["@yandex.ru/yasearch;1"].getService(Ci.nsIYaSearch).wrappedJSObject,
  4.   
  5.   sContextMenu:       null,
  6.   lastWord:           null,
  7.   
  8.   yaContextSearchId:         "yasearchMenuIdPrefix-yandex-search-menu",
  9.   yaContextSearchServicesId: "yasearchMenuIdPrefix-yandex-menu",
  10.   
  11.   onLoad: function() {
  12.     if (typeof(FeedHandler) === "object" && typeof(FeedHandler.updateFeeds) === "function") {
  13.       var origUpdateFeeds = FeedHandler.updateFeeds;
  14.       
  15.       FeedHandler.updateFeeds = function() {
  16.         Ya.updateFeedsAttribute();
  17.         return origUpdateFeeds.apply(this, arguments);
  18.       }
  19.     }
  20.     
  21.     if (typeof(BrowserToolboxCustomizeDone) === "function") {
  22.       var origBrowserToolboxCustomizeDone = BrowserToolboxCustomizeDone;
  23.       
  24.       BrowserToolboxCustomizeDone = function() {
  25.         Ya.realizeLoginBehaviour();
  26.         return origBrowserToolboxCustomizeDone.apply(this, arguments);
  27.       }
  28.     }
  29.     
  30.     this.stringBundleSet     = document.getElementById("yasearch-string-bundle-global");
  31.     this.sContextMenu        = document.getElementById("contentAreaContextMenu");
  32.     
  33.     if (this.nsIYa.getBoolPref("yasearch.default.search")) {
  34.       let menuElement = document.getElementById("context-searchselect");
  35.       if (menuElement)
  36.         menuElement.setAttribute("style", "display:none !important");
  37.     }
  38.     
  39.     this.realizeLoginBehaviour();
  40.     
  41.     this.observerService.addObserver(this, "Ya-Refresh-Services", false);
  42.     this.observerService.addObserver(this, "Ya-Refresh-Login-Status", false);
  43.     this.observerService.addObserver(this, "Ya-Refresh-Data", false);
  44.     this.observerService.addObserver(this, "Ya-Refresh-Busy-State", false);
  45.     
  46.     this.redrawContextMenu();
  47.     this.sContextMenu.addEventListener("popupshowing", function(e) { Ya.showContextMenu(e) }, false);
  48.     this.sContextMenu.addEventListener("popuphidden",  function(e) { Ya.hideContextMenu(e) }, false);
  49.     
  50.     setTimeout(function(){
  51.       Ya._mailIntegration.defaultOnClick =
  52.           document.getElementById("content").onclick ||
  53.           function() {
  54.             return window.contentAreaClick(event, false);
  55.           };
  56.       
  57.       Ya._mailIntegration.defaultWinObject = window.MailIntegration;
  58.       Ya.mailIntegration = Ya.nsIYa.getBoolPref("yasearch.general.ui.mail.integration");
  59.     }, 10);
  60.     
  61.     this.observerService.notifyObservers(null, "Ya-Browser-OpenClose", "open");
  62.     
  63.     // partner item
  64.     let partnerPaletteNode =
  65.         Array.slice.call(document.getElementById("navigator-toolbox").palette.childNodes)
  66.              .filter(function(aNode){return aNode.id == "yasearch-partner"})[0];
  67.     
  68.     if (partnerPaletteNode) {
  69.       var partnerExists = false;
  70.       
  71.       if (this.nsIYa.yaPartner) {
  72.         let toolbaritemContent = this.nsIYa.yaPartner.toolbaritemContent(document);
  73.         if (toolbaritemContent) {
  74.           partnerExists = true;
  75.           
  76.           ["title", "label"].forEach(function(aAttrName) {
  77.             if (toolbaritemContent.hasAttribute(aAttrName))
  78.               partnerPaletteNode.setAttribute(aAttrName, toolbaritemContent.getAttribute(aAttrName));
  79.           })
  80.         }
  81.       }
  82.       
  83.       if (!partnerExists)
  84.         partnerPaletteNode.parentNode.removeChild(partnerPaletteNode);
  85.     }
  86.     
  87.     // XB widgets custom actions helper
  88.     this.log("Installing XB widgets custom actions helper");
  89.     try {
  90.       window.yasearchOverlayController.guiBuilder.registerExtActionsHelper(this);
  91.     }
  92.     catch (e) {
  93.       this.log(e.message);
  94.       this.log(e.stack);
  95.     }
  96.   },
  97.   
  98.   unLoad: function() {
  99.     try {
  100.       window.yasearchOverlayController.guiBuilder.unregisterExtActionsHelper(this);
  101.     }
  102.     catch (e) {}
  103.     
  104.     this.observerService.removeObserver(this, "Ya-Refresh-Services");
  105.     this.observerService.removeObserver(this, "Ya-Refresh-Login-Status");
  106.     this.observerService.removeObserver(this, "Ya-Refresh-Data");
  107.     this.observerService.removeObserver(this, "Ya-Refresh-Busy-State");
  108.     
  109.     this.observerService.notifyObservers(null, "Ya-Browser-OpenClose", "close");
  110.   },
  111.   
  112.   // called by XULBuilder
  113.   handleXBAction: function yso_handleXBAction(aWidgetId, aAction) {
  114.     let widget = window.yasearchOverlayController.windowEngine.getWidget(aWidgetId);
  115.     let widgetProtoId = widget.getPrototype().getID();
  116.     
  117.     if (widgetProtoId == "A78155FC-41D5-4304-BD60-A01D8C1FB9FA" /* quotes widget */) {
  118.       switch (aAction) {
  119.         case "setup":
  120.           var QIDSettingNode = widget.findSetting("quote-id");
  121.           var QListNode = widget.findData("quotes list");
  122.           var quoteID = {value: QIDSettingNode.getValue()};
  123.           window.openDialog(YaUtils.CHROME_CONTENT + "custombar/quote-setup-dialog.xul",
  124.                             "",
  125.                             "centerscreen,chrome,dialog,modal,resizable,dependent",
  126.                             quoteID,
  127.                             QListNode);
  128.           QIDSettingNode.setValue(quoteID.value);
  129.           break;
  130.         
  131.         case "add":
  132.           var defaultQuoteID = widget.findSetting("quote-id").getDefaultValue();
  133.           var quoteID = {value: defaultQuoteID};
  134.           var QListNode = widget.findData("quotes list");
  135.           window.openDialog(YaUtils.CHROME_CONTENT + "custombar/quote-setup-dialog.xul",
  136.                             "",
  137.                             "centerscreen,chrome,dialog,modal,resizable,dependent",
  138.                             quoteID,
  139.                             QListNode);
  140.           if (quoteID.value === undefined) break;
  141.           window.yasearchOverlayController._barCore.application._overlayProvider
  142.             .insertWidget(widgetProtoId, widget, true, undefined, {"quote-id": quoteID.value});
  143.           break;
  144.         
  145.         case "delete":
  146.           window.yasearchOverlayController._barCore.application._overlayProvider.removeWidget(widget, true);
  147.           /*
  148.           var toolbarItemId = "yasearch.cb-" + widgetProtoId + "-inst-" + widget.getID();
  149.           var widgetToolbarItem = window.document.getElementById(toolbarItemId);
  150.           widgetToolbarItem.parentNode.removeChild(widgetToolbarItem);
  151.           var toolbar = widgetToolbarItem.parentNode;
  152.           while ( toolbar && (toolbar.localName !== "toolbar") ) {
  153.             toolbar = toolbar.parentNode;
  154.           }
  155.           if (toolbar) {
  156.             toolbar.setAttribute("currentset", toolbar.currentSet);
  157.             toolbar.ownerDocument.persist(toolbar.id, "currentset");
  158.           }
  159.           window.yasearchOverlayController.guiBuilder.onWidgetRemoved(widgetToolbarItem);
  160.           */
  161.           break;
  162.         
  163.         default:
  164.           throw new Error("Unsupported action");
  165.       }
  166.     }
  167.   },
  168.   
  169.   redrawSearchBoxPopup: function() {
  170.     let searchTB = document.getElementById("yasearch-textbox");
  171.     if (searchTB)
  172.       searchTB.rebuildPopupServices();
  173.   },
  174.   
  175.   //[2do] for mainKeyset
  176.   focusSearch: function() {
  177.     let searchTB = document.getElementById("yasearchbox");
  178.     if (searchTB) {
  179.       searchTB.focus();
  180.       searchTB.select();
  181.     }
  182.   },
  183.   
  184.   get buttonsObject() {
  185.     let buttons = {
  186.       get navigElements() {
  187.         return !!(this.bloggers || this.cy || this.spam);
  188.       },
  189.       
  190.       get isAnyVisibleAuthElement() {
  191.         //...["lenta", "mail", "money", "fotki", "yaru", "moikrug"]
  192.       }
  193.     };
  194.     
  195.     ["cy", "spam", "bloggers", "lenta", "mail", "money", "fotki", "yaru", "translate", "moikrug"]
  196.     .forEach(function(aButtonName) {
  197.       this[aButtonName] = !!document.getElementById("yasearch-" + aButtonName);
  198.     }, buttons);
  199.     
  200.     return buttons;
  201.   },
  202.   
  203.   buildFeedsMenu: function() {
  204.     var XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  205.     
  206.     let addFeedsMenu = document.createElementNS(XULNS, "menulist");
  207.     addFeedsMenu.appendChild(document.createElementNS(XULNS, "menupopup"));
  208.     
  209.     let feeds = ("feeds" in gBrowser.mCurrentBrowser) ? gBrowser.mCurrentBrowser.feeds : null;
  210.     
  211.     if (!feeds || feeds.length == 0)
  212.       return false;
  213.     
  214.     for each (var feed in feeds) {
  215.       let newFeed = document.createElementNS(XULNS, "menuitem");
  216.       let baseTitle = feed.title || feed.href;
  217.       newFeed.setAttribute("label", baseTitle);
  218.       newFeed.setAttribute("tooltiptext", feed.href);
  219.       addFeedsMenu.firstChild.appendChild(newFeed);
  220.     }
  221.     
  222.     window.openDialog(YaUtils.CHROME_CONTENT + "feeds/add-dialog.xul", "",
  223.                       "centerscreen,chrome,dialog,resizable,dependent",
  224.                       addFeedsMenu, gBrowser.mCurrentBrowser.contentTitle);
  225.     
  226.     return true;
  227.   },
  228.   
  229.   updateFeedsAttribute: function() {
  230.     let lentaTBItem = document.getElementById("yasearch-lenta");
  231.     if (lentaTBItem) {
  232.       setTimeout(function(me) {
  233.         let feeds = ("feeds" in gBrowser.mCurrentBrowser) ? gBrowser.mCurrentBrowser.feeds : null;
  234.         lentaTBItem.setAttribute("hasFeeds", !(!feeds || feeds.length == 0));
  235.         
  236.         if (lentaTBItem.firstChild.open)
  237.           me.onLentaPopupshowing();
  238.       }, 0, this);
  239.     }
  240.   },
  241.   
  242.   onLentaPopupshowing: function() {
  243.     let lentaTBItem = document.getElementById("yasearch-lenta");
  244.     let lentaMenuPopup = document.getElementById("yasearch-lenta-menupopup");
  245.     
  246.     if (lentaTBItem && lentaMenuPopup)
  247.       lentaMenuPopup.lastChild.previousSibling
  248.                     .setAttribute("disabled", !(lentaTBItem.getAttribute("hasFeeds") == "true"));
  249.   },
  250.   
  251.   addBookmark: function(aFolderId, aExistId) {
  252.     window.openDialog(YaUtils.CHROME_CONTENT + "bookmarks/add-dialog.xul", "",
  253.                       "centerscreen,chrome,dialog,resizable,dependent",
  254.                       gBrowser.mCurrentBrowser, aFolderId, aExistId);
  255.   },
  256.   
  257.   loadCY: function(aEvent, aType) {
  258.     let location = gBrowser.selectedBrowser.webNavigation.currentURI;
  259.     
  260.     try {
  261.       if (location.scheme && (location.scheme == "http" || location.scheme == "https") && location.host.length >= 3) {
  262.         switch (aType) {
  263.           case "similar":
  264.             this.loadURI("http://yandex.ru/yandsearch?ds=" + encodeURIComponent(location.spec), aEvent);
  265.             break;
  266.           
  267.           case "all":
  268.             this.loadURI("http://yandex.ru/yandsearch?serverurl=" + encodeURIComponent(location.host), aEvent);
  269.             break;
  270.           
  271.           default:
  272.             this.loadURI("http://www.yandex.ru/cy?host=" + encodeURIComponent(location.host) + "&base=0",
  273.                           aEvent, {action:3200});
  274.             break;
  275.         }
  276.       }
  277.     } catch(e) {}
  278.   },
  279.   
  280.   setCY: function(aCYData) {
  281.     let spamButton = document.getElementById("yasearch-spam-button");
  282.     if (spamButton) {
  283.       if (aCYData && aCYData.spam) {
  284.         spamButton.disabled = false;
  285.         
  286.         let onCommandTxt = "Ya.confirmCYSpamPromt('" + aCYData.spam + "', event.target)";
  287.         spamButton.setAttribute("oncommand", "if (event.target == this) " + onCommandTxt);
  288.         document.getElementById("yasearch-spam-button-confirm-item").setAttribute("oncommand", onCommandTxt);
  289.       } else {
  290.         spamButton.disabled = true;
  291.       }
  292.     }
  293.     
  294.     let cyButton = document.getElementById("yasearch-cy-button");
  295.     if (cyButton)
  296.       cyButton.setCYData(aCYData);
  297.   },
  298.   
  299.   confirmCYSpamPromt: function(aUrl, aElement) {
  300.     let rv = this.nsIYa.promptService.confirm(window,
  301.                         this.stringBundleSet.getString("ConfirmCYSpamTitle"),
  302.                         this.stringBundleSet.getString("ConfirmCYSpamMessage"));
  303.     if (rv)
  304.       this.nsIYa.confirmCYSpam(aUrl, aElement);
  305.   },
  306.   
  307.   get personalToolbarItems() {
  308.     return YaUtils.evaluateXPath(document, "//xul:toolbaritem[starts-with(@class,'yasearch-login')]");
  309.   },
  310.   
  311.   realizeLoginBehaviour: function() {
  312.     let isLogin = this.nsIYa.isLogin;
  313.     
  314.     let reg  = isLogin ? /(\s|^)yasearch\-login\-not(\s|$)/ : /(\s|^)yasearch\-login(\s|$)/;
  315.     let repl = "$1yasearch-login" + (isLogin ? "" : "-not") + "$2";
  316.     
  317.     this.personalToolbarItems.forEach(function(item) {
  318.       item.className = item.className.replace(reg, repl);
  319.       
  320.       let ch, i = 0;
  321.       
  322.       while ((ch = item.childNodes.item(i++))) {
  323.         if (ch.hasAttribute("yaAuthDisabled"))
  324.           ch.setAttribute("yaAuthDisabled", !isLogin);
  325.       }
  326.     });
  327.     
  328.     this.updateServicesWork();
  329.   },
  330.   
  331.   refreshLoginButton: function() {
  332.     let lButton = document.getElementById("yasearch-login-button");
  333.     if (lButton) {
  334.       let login = this.nsIYa.session.login;
  335.       let tooltiptext = login ? this.stringBundleSet.getString("user") + ": " + login :
  336.                                 lButton.getAttribute("_deftooltiptext");
  337.       
  338.       lButton.setAttribute("yaLabel", login || "0");
  339.       lButton.setAttribute("tooltiptext", tooltiptext);
  340.     }
  341.   },
  342.   
  343.   checkOnCommandEvent: function(aEvent) {
  344.     let target = aEvent.target;
  345.     let currentTarget = aEvent.currentTarget;
  346.     
  347.     if (!(target == currentTarget || target.localName == "toolbarbutton"))
  348.       return false;
  349.     
  350.     if (currentTarget.localName !== "toolbaritem" && currentTarget.localName !== "toolbarbutton") {
  351.       target = null;
  352.       
  353.       let currentTargetId = currentTarget.getAttribute("id");
  354.       if (currentTargetId && currentTargetId.indexOf("-yachevron-clone") > 0) {
  355.         let chevronCloned = currentTargetId.split("-yachevron-clone")[0];
  356.         target = document.getElementById(chevronCloned);
  357.       }
  358.       
  359.       if (!target)
  360.         return false;
  361.     }
  362.     
  363.     if (target.disabled && !this.nsIYa.isLogin) {
  364.       let parentNode = target.parentNode;
  365.       
  366.       while (parentNode && parentNode.localName != "toolbaritem")
  367.         parentNode = parentNode.parentNode;
  368.       
  369.       if (parentNode && /yasearch\-login/.test(parentNode.getAttribute("class"))) {
  370.         var [,serviceType] = parentNode.getAttribute("id").split("-");
  371.         this.openWelcomeWindow(serviceType, target.getAttribute("id"));
  372.         return false;
  373.       }
  374.     }
  375.     
  376.     return true;
  377.   },
  378.   
  379.   updateServicesWork: function() {
  380.     this.refreshLoginButton();
  381.     this.redrawServicesButton();
  382.     this.refreshMailData();
  383.     this.refreshCounterData("fotki");
  384.     this.refreshCounterData("yaru");
  385.     this.refreshCounterData("moikrug");
  386.     this.refreshLentaData();
  387.     this.refreshMoneyData();
  388.   },
  389.   
  390.   redrawServices: function() {
  391.     this.redrawServicesButton();
  392.     this.redrawContextMenu();
  393.     this.redrawSearchBoxPopup();
  394.   },
  395.   
  396.   redrawServicesButton: function() {
  397.     let sButton = document.getElementById("yasearch-search");
  398.     if (!sButton)
  399.       return;
  400.     
  401.     if (sButton.firstChild)
  402.       sButton.removeChild(sButton.firstChild);
  403.     
  404.     this.nsIYa.DOMUtils.appendNode(this.nsIYa.getDOMDocContent("bar-services"), sButton);
  405.   },
  406.   
  407.   showQuickTranslateItems: function(aLabel) {
  408.     if (!this.nsIYa.getBoolPref("yasearch.general.ui.quick.search"))
  409.       aLabel = false;
  410.     
  411.     for each (var qtItemId in ["lingvo"]) {
  412.       var mItem = document.getElementById("yasearchQTranslateItem-" + qtItemId);
  413.       if (mItem) {
  414.         mItem.hidden = aLabel ? false : true;
  415.         if (aLabel)
  416.           mItem.setAttribute("label", this.stringBundleSet.getFormattedString("search_" + qtItemId, [aLabel]));
  417.       }
  418.     }
  419.   },
  420.   
  421.   redrawContextMenu: function() {
  422.     let oldMenu = document.getElementById(this.yaContextSearchServicesId);
  423.     if (oldMenu) {
  424.       let newMenu = this.nsIYa.getDOMDocContent("menu-popup");
  425.       this.nsIYa.DOMUtils.replaceNode(newMenu, oldMenu);
  426.     }
  427.   },
  428.   
  429.   getBrowserSelection: function() {
  430.     let focusedWindow = document.commandDispatcher.focusedWindow;
  431.     let selection = focusedWindow.getSelection().toString();
  432.     
  433.     return this._cropSelectionText(selection);
  434.   },
  435.   
  436.   _cropSelectionText: function(aString) {
  437.     const CHAR_LEN = 100;
  438.     
  439.     if (aString) {
  440.       if (aString.length > CHAR_LEN) {
  441.         var pattern = new RegExp("^(?:\\s*.){0," + CHAR_LEN + "}");
  442.         pattern.test(aString);
  443.         aString = RegExp.lastMatch;
  444.       }
  445.       
  446.       aString = aString.replace(/^\s+/, "").replace(/\s+$/, "").replace(/\s+/g, " ");
  447.       
  448.       if (aString.length > CHAR_LEN)
  449.         aString = aString.substr(0, CHAR_LEN);
  450.     }
  451.     
  452.     return aString;
  453.   },
  454.   
  455.   showContextMenu: function(evt) {
  456.     if (evt.target.id != this.sContextMenu.id)
  457.       return;
  458.     
  459.     let yaContextMenu = document.getElementById(this.yaContextSearchId);
  460.     let yaContextServicesMenu = document.getElementById(this.yaContextSearchServicesId);
  461.     
  462.     let selectedText = "";
  463.     
  464.     let target = gContextMenu.target;
  465.     
  466.     if (gContextMenu.isContentSelected) {
  467.       selectedText = this.getBrowserSelection();
  468.     } else if (gContextMenu.onTextInput && !target.readOnly && target.type != "password") {
  469.       if (target.value) {
  470.         selectedText = target.value.toString().slice(target.selectionStart, target.selectionEnd);
  471.         selectedText = this._cropSelectionText(selectedText);
  472.       }
  473.     }
  474.     
  475.     this.lastWord = selectedText;
  476.     
  477.     if (selectedText) {
  478.       if (selectedText.length > 15)
  479.         selectedText = selectedText.substr(0, 15) + "...";
  480.       
  481.       yaContextMenu.setAttribute("label", this.stringBundleSet.getFormattedString("search_yandex", [selectedText]));
  482.       yaContextServicesMenu.setAttribute("label", this.stringBundleSet.getFormattedString("search_services", [selectedText]));
  483.     }
  484.     
  485.     this.showQuickTranslateItems(selectedText || false);
  486.     
  487.     let hidden = !selectedText;
  488.     yaContextMenu.hidden = hidden;
  489.     yaContextServicesMenu.hidden = hidden;
  490.     
  491.     let sendToItemsType = "page";
  492.     if (!!this.pageDataSender.getImageDataForContextMenu()) {
  493.       sendToItemsType = "image";
  494.     } else if (gContextMenu.isTextSelected) {
  495.       sendToItemsType = "selected";
  496.     }
  497.     
  498.     let hiddenSendService = /^(chrome|file|about):$/.test(target.ownerDocument.location.protocol);
  499.     
  500.     ["Mail", "Yaru"].forEach(function(quickSendService) {
  501.       let item = document.getElementById("yasearchQSendTo" + quickSendService);
  502.       if (item) {
  503.         item.setAttribute("yaPageDataSendType", sendToItemsType);
  504.         item.setAttribute("label", item.getAttribute("yaLabelSend_" + sendToItemsType));
  505.         item.setAttribute("hidden", hiddenSendService || !document.getElementById("yasearch-" + quickSendService.toLowerCase()));
  506.       }
  507.     });
  508.   },
  509.   
  510.   hideContextMenu: function(aEvent) {
  511.     if (aEvent.target.id != this.sContextMenu.id)
  512.       return;
  513.     
  514.     this.lastWord = null;
  515.   },
  516.   
  517.   contentAreaClick: function(event, fieldNormalClicks) {
  518.     if (event.isTrusted && event.button == 0 &&
  519.         !event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey) {
  520.       
  521.       var linkNode,
  522.           target = event.target;
  523.       
  524.       if (target instanceof HTMLAnchorElement ||
  525.           target instanceof HTMLAreaElement ||
  526.           target instanceof HTMLLinkElement) {
  527.         if (target.hasAttribute("href"))
  528.           linkNode = target;
  529.       } else {
  530.         linkNode = event.originalTarget;
  531.         while (linkNode && !(linkNode instanceof HTMLAnchorElement))
  532.           linkNode = linkNode.parentNode;
  533.         if (linkNode && !linkNode.hasAttribute("href"))
  534.           linkNode = null;
  535.       }
  536.       
  537.       if (linkNode && linkNode.href) {
  538.         var hrefStr = linkNode.href;
  539.         if (hrefStr.indexOf("mailto:") > -1 && hrefStr.indexOf("mailto:") < 7 &&
  540.             this.composeEmail(hrefStr))
  541.           return false;
  542.       }
  543.     }
  544.     
  545.     return window.contentAreaClick(event, fieldNormalClicks);
  546.   },
  547.   
  548.   pageDataSender: {
  549.     _storedDataToSend: null,
  550.     
  551.     sendToService: function(aDestination, aEvent, aStatData) {
  552.       const LINE_BREAK = aDestination === "yaru" ? "<br/><br/>" : "\n\n";
  553.       
  554.       this._storedDataToSend = null;
  555.       
  556.       var sendType = "page";
  557.       
  558.       if (aEvent && aEvent.target && aEvent.target.hasAttribute("yaPageDataSendType"))
  559.         sendType = aEvent.target.getAttribute("yaPageDataSendType");
  560.       
  561.       var dataToSend = { body: "", subject: "", title: "", URL: "" };
  562.       
  563.       switch (sendType) {
  564.         case "page":
  565.         case "page-link":
  566.           dataToSend.subject = window.content.document.title;
  567.           
  568.           if (sendType == "page") {
  569.             let metaNodes = window.content.document.getElementsByTagName("meta");
  570.             for (let i = 0, len = metaNodes.length; i < len; i++) {
  571.               let metaName = (metaNodes[i].name || "").toLowerCase();
  572.               if (metaName == "description" && metaNodes[i].content) {
  573.                 dataToSend.body = metaNodes[i].content + LINE_BREAK;
  574.                 break;
  575.               }
  576.             }
  577.           }
  578.           
  579.           dataToSend.body += window.content.location.href;
  580.           
  581.           break;
  582.         
  583.         case "selected":
  584.           dataToSend.body = this.getSelectedText(aEvent);
  585.           break;
  586.         
  587.         case "image":
  588.           let imgData = this.getImageDataForContextMenu();
  589.           if (imgData) {
  590.             dataToSend.body = '<img src="' + imgData.imageURL +
  591.                               '" alt="' + (imgData.imageName || imgData.imageURL) +
  592.                               '" title="' + imgData.imageName + '" />';
  593.             dataToSend.message_type = "html";
  594.           }
  595.           
  596.           break;
  597.         
  598.         default:
  599.           break;
  600.       }
  601.       
  602.       if (!(dataToSend.body || dataToSend.subject))
  603.         return false;
  604.       
  605.       if (!(sendType == "page" || sendType == "page-link")) {
  606.         let wp = gBrowser.currentURI;
  607.         if (wp && wp.spec && typeof wp.spec != "undefined" && !/^about/.test(wp.spec))
  608.           dataToSend.body = "╨ÿ╤ü╤é╨╛╤ç╨╜╨╕╨║: " + wp.spec + LINE_BREAK + dataToSend.body;
  609.       
  610.       } else if (/^about/.test(dataToSend.body)) {
  611.         dataToSend.body = null;
  612.       }
  613.       
  614.       if (aDestination == "yaru") {
  615.         if (sendType == "page") {
  616.           dataToSend.URL = window.content.location.href;
  617.           delete dataToSend.body;
  618.         }
  619.         
  620.         dataToSend.title = dataToSend.subject;
  621.         delete dataToSend.subject;
  622.       }
  623.       
  624.       var sendObject = {
  625.         sendType: sendType,
  626.         destination: aDestination,
  627.         dataToSend: dataToSend,
  628.         statData: aStatData,
  629.         sessionRestarted: false
  630.       };
  631.       
  632.       if (Ya.nsIYa.username) {
  633.         this._sendToServiceHepler(sendObject);
  634.       } else {
  635.         sendObject.sessionRestarted = true;
  636.         this._storedDataToSend = sendObject;
  637.         
  638.         var welcomeWindow = Ya.openWelcomeWindow(aDestination);
  639.         if (welcomeWindow) {
  640.           function addUnloadListener(aTarget) {
  641.             aTarget.addEventListener("unload", function(aUnloadEvent) {
  642.               aUnloadEvent.currentTarget.removeEventListener("unload", arguments.callee, false);
  643.               if (Ya && Ya.pageDataSender)
  644.                 Ya.pageDataSender.checkNeedSendData();
  645.             }, false);
  646.           }
  647.           
  648.           if (welcomeWindow.document instanceof Ci.nsIDOMXULDocument) {
  649.             addUnloadListener(welcomeWindow);
  650.           } else {//window not ready
  651.             welcomeWindow.addEventListener("load", function(aLoadEvent) {
  652.               aLoadEvent.currentTarget.removeEventListener("load", arguments.callee, false);
  653.               addUnloadListener(aLoadEvent.currentTarget);
  654.             }, false);
  655.           }
  656.         }
  657.       }
  658.     },
  659.     
  660.     checkNeedSendData: function() {
  661.       if (this._storedDataToSend) {
  662.         this._sendToServiceHepler(this._storedDataToSend);
  663.         this._storedDataToSend = null;
  664.       }
  665.     },
  666.     
  667.     _sendToServiceHepler: function(aSendObject) {
  668.       var login = Ya.nsIYa.username;
  669.       
  670.       if (!login || typeof aSendObject !== "object" || !aSendObject.dataToSend)
  671.         return false;
  672.       
  673.       var url = false;
  674.       
  675.       switch (aSendObject.destination) {
  676.         case "mail":
  677.           url = "http://" + Ya.nsIYa.getLocaleDependedUrl("MailHost") + "/compose?mailto=";
  678.           break;
  679.         
  680.         case "yaru":
  681.           url = "http://" + login + ".ya.ru/posts_add_";
  682.           url += (aSendObject.sendType == "page" ? "link" : "text") + ".xml";
  683.           break;
  684.         
  685.         default:
  686.           break;
  687.       }
  688.       
  689.       if (!url)
  690.         return false;
  691.       
  692.       aSendObject.url = url;
  693.       
  694.       if (aSendObject.destination == "yaru") {
  695.         if (aSendObject.sessionRestarted)
  696.           aSendObject.url = "http://my.ya.ru/request_to_session.xml";
  697.         
  698.         aSendObject.dataToSend.YARU_retpath = url;
  699.         
  700.         Ya.nsIYa.xmlHttpRequest(
  701.           "http://pass.yandex.ru/?retpath=" + encodeURIComponent("http://" + login + ".ya.ru/"),
  702.           { callbackFunc: function() { if (Ya && Ya.pageDataSender) Ya.pageDataSender._sendData(aSendObject); } }
  703.         );
  704.         
  705.         return true;
  706.       }
  707.       
  708.       return this._sendData(aSendObject);
  709.     },
  710.     
  711.     getSelectedText: function(aEvent) {
  712.       var focusedWindow = document.commandDispatcher.focusedWindow;
  713.       if (focusedWindow == window)
  714.         focusedWindow = content;
  715.       
  716.       if (!focusedWindow)
  717.         return false;
  718.       
  719.       var selection = focusedWindow.getSelection();
  720.       if (selection.isCollapsed || !selection.rangeCount)
  721.         return false;
  722.       
  723.       var range = selection.getRangeAt(0);
  724.       
  725.       var container = range && range.commonAncestorContainer ? range.commonAncestorContainer : null;
  726.       
  727.       var imgCollection = (container && container.getElementsByTagNameNS) ?
  728.                               Array.slice(container.getElementsByTagNameNS(container.namespaceURI, "img")) :
  729.                               [];
  730.       
  731.       const TMP_ALT_ATTR_NAME = "__yasearchTmpAlt";
  732.       
  733.       let i = 0;
  734.       
  735.       if (imgCollection.length) {
  736.         while (imgCollection[i]) {
  737.           let img = imgCollection[i];
  738.           img.setAttribute(TMP_ALT_ATTR_NAME, img.getAttribute("alt"));
  739.           img.setAttribute("alt", "[ ╨╕╨╖╨╛╨▒╤Ç╨░╨╢╨╡╨╜╨╕╨╡: " + img.src + " ]");
  740.           i++;
  741.         }
  742.       }
  743.       
  744.       let selectedText = selection.toString();
  745.       
  746.       if (imgCollection.length) {
  747.         i = 0;
  748.         while (imgCollection[i]) {
  749.           let img = imgCollection[i];
  750.           img.setAttribute("alt", img.getAttribute(TMP_ALT_ATTR_NAME));
  751.           img.removeAttribute(TMP_ALT_ATTR_NAME);
  752.           i++;
  753.         }
  754.       }
  755.       
  756.       return selectedText ? selectedText : false;
  757.     },
  758.     
  759.     getImageDataForContextMenu: function() {
  760.       return this.getImageDataForNode(document.popupNode);
  761.     },
  762.     
  763.     getImageDataForNode: function(aNode) {
  764.       if (aNode && aNode.nodeType == 1 && (aNode instanceof Ci.nsIImageLoadingContent) && aNode.currentURI) {
  765.         var imageURI = aNode.currentURI;
  766.         
  767.         if (/file|https?/.test(imageURI.scheme)) {
  768.           var altText = (aNode.hasAttribute && aNode.hasAttribute("alt")) ? aNode.getAttribute("alt") : "";
  769.           var titleText = (aNode.hasAttribute && aNode.hasAttribute("title")) ? aNode.getAttribute("title") : "";
  770.           
  771.           if (altText == imageURI.spec)   altText   = "";
  772.           if (titleText == imageURI.spec) titleText = "";
  773.           
  774.           var imageName = (titleText.length > altText.length) ? titleText : altText;
  775.           
  776.           if (imageName.length > 1000)
  777.             imageName = imageName.substr(0, 1000);
  778.           
  779.           return {imageURL: imageURI.spec, imageName: imageName};
  780.         }
  781.         
  782.         return null;
  783.       }
  784.       
  785.       return false;
  786.     },
  787.     
  788.     _sendData: function(aSendObject) {
  789.       if (!aSendObject)
  790.         return false;
  791.       
  792.       var url = aSendObject.url;
  793.       
  794.       var dataToSend = [];
  795.       
  796.       for (var [paramName, paramValue] in Iterator(aSendObject.dataToSend)) {
  797.         if (paramName && paramValue)
  798.           dataToSend.push(paramName + "=" + encodeURIComponent(paramValue.substr(0, 1000000)));
  799.       }
  800.       
  801.       var postData = null;
  802.       
  803.       if (dataToSend.length) {
  804.         dataToSend = dataToSend.join("&");
  805.         
  806.         if (dataToSend.length > 8192 && /^https?:\/\/mail\.yandex\./.test(url)) {
  807.           url = url.replace("/compose?", "/classic/compose?");
  808.         }
  809.         
  810.         var stringStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
  811.         stringStream.setData(dataToSend, dataToSend.length);
  812.         
  813.         postData = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
  814.         postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
  815.         postData.addContentLength = true;
  816.         postData.setData(stringStream);
  817.       }
  818.       
  819.       Ya.loadURI(url, "tab", aSendObject.statData, postData);
  820.     }
  821.   },
  822.   
  823.   _mailIntegration: {
  824.     enabled: false,
  825.     
  826.     get mailUrl() {
  827.       return "http://" + Ya.nsIYa.getLocaleDependedUrl("MailHost") + "/compose?mailto=";
  828.     },
  829.     
  830.     defaultOnClick: function(aEvent) {
  831.       return window.contentAreaClick(aEvent, false);
  832.     },
  833.     
  834.     yaOnClick: function(aEvent) {
  835.       return Ya.contentAreaClick(aEvent, false);
  836.     },
  837.     
  838.     defaultWinObject: window.MailIntegration,
  839.     
  840.     _sendText: function(aBody, aSubject) {
  841.       var dataToSend = [];
  842.       
  843.       if (aBody)
  844.         dataToSend.push("body=" + encodeURIComponent(aBody.substr(0, 1000000)));
  845.       
  846.       if (aSubject)
  847.         dataToSend.push("subject=" + encodeURIComponent(aSubject.substr(0, 100000)));
  848.       
  849.       var postData = null;
  850.       if (dataToSend.length) {
  851.         dataToSend = dataToSend.join("&");
  852.         
  853.         var stringStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
  854.         stringStream.setData(dataToSend, dataToSend.length);
  855.         
  856.         postData = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
  857.         postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
  858.         postData.addContentLength = true;
  859.         postData.setData(stringStream);
  860.       }
  861.       
  862.       Ya.loadURI(this.mailUrl, "tab", null, postData);
  863.     },
  864.     
  865.     yaWinObject: {
  866.       sendLinkForWindow: function(aWindow) {
  867.         this.sendMessage(aWindow.location.href,
  868.                          aWindow.document.title);
  869.       },
  870.       
  871.       sendMessage: function(aBody, aSubject) {
  872.         Ya._mailIntegration._sendText(aBody, aSubject);
  873.       },
  874.       
  875.       _launchExternalUrl: function(aURL) {
  876.         Ya._mailIntegration.defaultWinObject._launchExternalUrl(aURL);
  877.       }
  878.     }
  879.   },
  880.   
  881.   composeEmail: function(aMailAddress) {
  882.     if (this.nsIYa.getBoolPref("yasearch.general.ui.mail.integration.helper.show")) {
  883.       this.nsIYa.setBoolPref("yasearch.general.ui.mail.integration.helper.show", false);
  884.       
  885.       setTimeout(function(me) {
  886.         me.nsIYa.promptService.alert(window,
  887.           me.stringBundleSet.getString("MailIntegrationHelperTitle"),
  888.           me.stringBundleSet.getString("MailIntegrationHelperMessage")
  889.         )
  890.       }, 100, this);
  891.     }
  892.     
  893.     let mailUrl = this._mailIntegration.mailUrl + encodeURIComponent(aMailAddress.replace(/^mailto\:/, ""));
  894.     this.loadURI(mailUrl, "tab", {action:1021});
  895.     
  896.     return true;
  897.   },
  898.   
  899.   get mailIntegration() {
  900.     return this._mailIntegration.enabled;
  901.   },
  902.   
  903.   set mailIntegration(val) {
  904.     let type = val ? "ya" : "default";
  905.     this._mailIntegration.enabled = !!val;
  906.     document.getElementById("content").onclick = this._mailIntegration[type + "OnClick"];
  907.     window.MailIntegration = this._mailIntegration[type + "WinObject"];
  908.   },
  909.   
  910.   search: function(engineId, evt) {
  911.     if (!this.lastWord || this.lastWord == "")
  912.       return false;
  913.     
  914.     var searchUrl = this.nsIYa.getSearchEngineUrl(engineId, this.lastWord, null, {clid:10});
  915.     if (!searchUrl)
  916.       return false;
  917.     
  918.     if (this.nsIYa.prefs.searchHistoryEnabled) {
  919.       Cc["@mozilla.org/satchel/form-history;1"]
  920.         .getService(Ci.nsIFormHistory2)
  921.         .addEntry("yasearch-history", this.lastWord);
  922.     }
  923.     
  924.     this.loadURI(searchUrl.url, "tab", searchUrl.statData, null, true);
  925.     
  926.     return true;
  927.   },
  928.   
  929.   loadService: function(aId, aURL, aEvent, aStatData) {
  930.     this.loadURI(aURL, aEvent, aStatData);
  931.     this.nsIYa.setServiceTimestamp(aId);
  932.   },
  933.   
  934.   loadConditionalURI: function(aType, aEvent, aStatData) {
  935.     var url;
  936.     
  937.     switch (aType) {
  938.       case "feeds"://timed.alert.js
  939.       case "lenta":
  940.         url = "lenta.yandex.ru/" + (this.nsIYa.getBoolPref("yasearch.feeds.uri.open.new") ? "un" : "") + "read.xml";
  941.         break;
  942.       
  943.       case "mail":
  944.         url = this.nsIYa.getLocaleDependedUrl("MailHost") + "/messages" +
  945.               (this.nsIYa.getBoolPref("yasearch.mail.uri.open.new") ? "?extra_cond=only_new" : "");
  946.         break;
  947.       
  948.       case "fotki":
  949.         var login = this.nsIYa.username;
  950.         url = "fotki.yandex.ru" + (login ? ("/users/" + encodeURIComponent(login) + "/favorites") : "");
  951.         break;
  952.       
  953.       case "yaru":
  954.         var login = this.nsIYa.username;
  955.         url = login ? ("http://" + encodeURIComponent(login) + ".ya.ru/replies_history_unread.xml") : "wow.ya.ru";
  956.         break;
  957.       
  958.       default:
  959.         return false;
  960.     }
  961.     
  962.     return this.loadURI(url, aEvent, aStatData);
  963.   },
  964.   
  965.   checkForClick: function(aEvent) {
  966.     var node = aEvent.target;
  967.     if (node.getAttribute("disabled") == "true" || !node.hasAttribute("oncommand"))
  968.       return;
  969.     
  970.     var fn = new Function("event", node.getAttribute("oncommand"));
  971.     fn.call(node, aEvent);
  972.     
  973.     this.closeMenus(node);
  974.   },
  975.   
  976.   closeMenus: function(aNode) {
  977.     if (aNode && "tagName" in aNode) {
  978.       if (aNode.namespaceURI == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  979.           && (aNode.tagName == "menupopup" || aNode.tagName == "popup"))
  980.         if (aNode.hidePopup)
  981.            aNode.hidePopup();
  982.       this.closeMenus(aNode.parentNode);
  983.     }
  984.   },
  985.   
  986.   getTypeByEvent: function(evt) {
  987.     if (evt) {
  988.       if (evt == "tab" || evt == "window" || evt == "new-tab") return evt;
  989.       if (evt.shiftKey) return "window";
  990.       if (evt.ctrlKey || evt.metaKey || this.nsIYa.prefs.commandOpenTab) return "tab";
  991.       if (evt.type == "click" && evt.button == 1) return "tab";
  992.     }
  993.     return "none";
  994.   },
  995.   
  996.   canLoadUrlInCurrentTab: function(aTab, aUrl) {
  997.     if (aTab.hasAttribute("busy"))
  998.       return false;
  999.     
  1000.     var linkedBrowser = aTab && aTab.linkedBrowser ? aTab.linkedBrowser : null;
  1001.     
  1002.     try {
  1003.       if (!linkedBrowser || linkedBrowser.currentURI.host == "yasearch")
  1004.         return true;
  1005.     } catch(e) {}
  1006.     
  1007.     try {
  1008.       var sh = linkedBrowser.sessionHistory;
  1009.       if (!sh || sh.index < 0 || (sh.count < 2 && (!linkedBrowser.currentURI || linkedBrowser.currentURI.spec == "about:blank")))
  1010.         return true;
  1011.     } catch(e) {}
  1012.     
  1013.     function getHostFromURL(url) {
  1014.       const IO_SERVICE = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
  1015.       
  1016.       try {
  1017.         return IO_SERVICE.newURI(url, null, null).host;
  1018.       } catch(e) {}
  1019.       
  1020.       return null;
  1021.     }
  1022.     
  1023.     var currentHost = getHostFromURL(linkedBrowser.currentURI.spec);
  1024.     var host2Load = getHostFromURL(aUrl);
  1025.     
  1026.     if (!(currentHost && (currentHost === host2Load)))
  1027.       return false;
  1028.     
  1029.     function modifTextareasInWindow(aWin) {
  1030.       var doc = aWin.document;
  1031.       if (doc && ("body" in doc)) {
  1032.         var i=0, textareas = doc.getElementsByTagName("textarea");
  1033.         while (textareas[i]) {
  1034.           var textarea = textareas[i++];
  1035.           if (textarea instanceof Ci.nsIDOMHTMLTextAreaElement && textarea.defaultValue != textarea.value)
  1036.             return true;
  1037.         }
  1038.       }
  1039.       return false;
  1040.     }
  1041.     
  1042.     return modifTextareasInWindow(linkedBrowser.contentWindow) ? false : true;
  1043.   },
  1044.   
  1045.   loadURI: function(url, evt, aStatData, postData, aRespectLoadTabPref) {
  1046.     this.nsIYa.checkNeedSendGuid();
  1047.     
  1048.     if (/^mailto\:/.test(url))
  1049.       return this.mailIntegration ? this.composeEmail(url) : false;
  1050.     
  1051.     var type = this.getTypeByEvent(evt);
  1052.     
  1053.     if (!/^[A-z]+\:\/\//.test(url))
  1054.       url = "http://" + url;
  1055.     
  1056.     if (aStatData)
  1057.       url = this.nsIYa.appendStatData2Url(url, aStatData);
  1058.     
  1059.     var currentTab;
  1060.     
  1061.     switch (type) {
  1062.       case "window":
  1063.         openNewWindowWith(url);
  1064.         break;
  1065.       
  1066.       case "new-tab":
  1067.       case "tab":
  1068.         currentTab = gBrowser.mCurrentTab;
  1069.         
  1070.         if (!(type == "new-tab" || evt.ctrlKey || evt.metaKey || (evt.type == "click" && evt.button == 1)) &&
  1071.             this.canLoadUrlInCurrentTab(currentTab, url))
  1072.         {
  1073.           currentTab.linkedBrowser.loadURIWithFlags(url, Ci.nsIWebNavigation.LOAD_FLAGS_NONE, null, null, postData);
  1074.         } else {
  1075.           currentTab = gBrowser.loadOneTab(url, null, null, postData, (aRespectLoadTabPref ? null : false));
  1076.         }
  1077.         
  1078.         break;
  1079.       
  1080.       default:
  1081.         currentTab = gBrowser.mCurrentTab;
  1082.         loadURI(url, null, postData);
  1083.     }
  1084.     
  1085.     if (aStatData && aStatData.action && currentTab && currentTab.linkedBrowser.yaSearchTHandler)
  1086.       currentTab.linkedBrowser.yaSearchTHandler.actionValue = aStatData.action;
  1087.     
  1088.     return true;
  1089.   },
  1090.   
  1091.   fixupWebURI: function(aString) {
  1092.     const nsIURIFixup = Ci.nsIURIFixup;
  1093.     let fixup = Cc["@mozilla.org/docshell/urifixup;1"].getService(nsIURIFixup);
  1094.     let uri = null;
  1095.     try {
  1096.       uri = fixup.createFixupURI(aString, nsIURIFixup.FIXUP_FLAG_NONE);
  1097.       uri = uri && uri.spec && /^(ftp|https?)/.test(uri.scheme) ? uri : null;
  1098.     } catch(e) {}
  1099.     
  1100.     return uri;
  1101.   },
  1102.   
  1103.   loadURIsInTabs: function(aURIsArray) {
  1104.     for each (let uriString in aURIsArray) {
  1105.       let uri = this.fixupWebURI(uriString);
  1106.       if (uri)
  1107.         gBrowser.addTab(uri.spec);
  1108.     }
  1109.   },
  1110.   
  1111.   refreshMailData: function() {
  1112.     var mb = document.getElementById("yasearch-mail-menupopup");
  1113.     if (!mb)
  1114.       return;
  1115.     
  1116.     while (mb.lastChild.nodeName != "menuseparator")
  1117.       mb.removeChild(mb.lastChild);
  1118.     
  1119.     var data = this.nsIYa.mailData;
  1120.     
  1121.     mb.lastChild.style.display = (data.count > 0 && data.nodes) ? "" : "none";
  1122.     
  1123.     if (data.nodes)
  1124.       while (data.nodes.hasChildNodes())
  1125.         this.nsIYa.DOMUtils.appendNode(data.nodes.firstChild, mb);
  1126.     
  1127.     mb.parentNode.setAttribute("yaLabel", data.count > 0 ? data.count : 0);
  1128.   },
  1129.   
  1130.   clickMail: function(aEvent) {
  1131.     let mailId = aEvent.target.id;
  1132.     this.nsIYa.decMailCounter(mailId);
  1133.     this.loadURI(mailId, aEvent, {action:1030});
  1134.     return false;
  1135.   },
  1136.   
  1137.   refreshCounterData: function(aType) {
  1138.     let menupopup = document.getElementById("yasearch-" + aType + "-menupopup");
  1139.     
  1140.     if (menupopup) {
  1141.       let tbb = this.nsIYa.Counters.getXULNode(aType);
  1142.       menupopup.parentNode.setAttribute("yaLabel", tbb.getAttribute("yaLabel"));
  1143.       
  1144.       while (menupopup.firstChild)
  1145.         menupopup.removeChild(menupopup.firstChild);
  1146.       
  1147.       let data = tbb.firstChild;
  1148.       while (data.hasChildNodes())
  1149.         this.nsIYa.DOMUtils.appendNode(data.firstChild, menupopup);
  1150.     }
  1151.   },
  1152.   
  1153.   refreshLentaData: function() {
  1154.     let fb = document.getElementById("yasearch-lenta-button");
  1155.     if (fb) {
  1156.       let data = this.nsIYa.Counters.getCount("lenta");
  1157.       fb.setAttribute("yaLabel", data > 0 ? data : 0);
  1158.     }
  1159.   },
  1160.   
  1161.   refreshMoneyData: function() {
  1162.     let mb = document.getElementById("yasearch-money-button");
  1163.     if (mb) {
  1164.       let data = this.nsIYa.Counters.getCount("money");
  1165.       mb.setAttribute("yaLabel", data > 0 ? data : 0);
  1166.     }
  1167.   },
  1168.   
  1169.   observe: function(aSubject, aTopic, aState) {
  1170.     switch (aTopic) {
  1171.       case "Ya-Refresh-Services":
  1172.         this.redrawServicesButton();
  1173.         break;
  1174.       
  1175.       case "Ya-Refresh-Login-Status":
  1176.         this.realizeLoginBehaviour();
  1177.         break;
  1178.       
  1179.       case "Ya-Refresh-Data":
  1180.         switch (aState) {
  1181.           //~ case "mail":
  1182.           case "mailList":
  1183.             this.refreshMailData();
  1184.             break;
  1185.           
  1186.           case "allServices":
  1187.           case "mailAndFeeds":
  1188.             this.refreshLentaData();
  1189.             this.refreshMoneyData();
  1190.             this.refreshCounterData("fotki");
  1191.             this.refreshCounterData("yaru");
  1192.             this.refreshCounterData("moikrug");
  1193.             break;
  1194.           
  1195.           case "fotki":
  1196.           case "yaru":
  1197.           case "moikrug":
  1198.             this.refreshCounterData(aState);
  1199.             break;
  1200.           
  1201.           case "lenta":
  1202.             this.refreshLentaData();
  1203.             break;
  1204.           
  1205.           case "money":
  1206.             this.refreshMoneyData();
  1207.             break;
  1208.           
  1209.           case "mail-integration":
  1210.             this.mailIntegration = this.nsIYa.getBoolPref("yasearch.general.ui.mail.integration");
  1211.             break;
  1212.           
  1213.           case "services":
  1214.             this.redrawServices();
  1215.             break;
  1216.           
  1217.           default:
  1218.             break;
  1219.         }
  1220.         
  1221.         break;
  1222.         
  1223.       case "Ya-Refresh-Busy-State":
  1224.         let data = aState.split(":");
  1225.         let state = !!(data[0] === "true");
  1226.         let types = [data[1]];
  1227.         
  1228.         for each (var type in types) {
  1229.           window.setTimeout(function() {
  1230.             let btn = YaUtils.evaluateXPath(document, '//xul:toolbaritem[starts-with(@id,"' + type + '")]')[0];
  1231.             if (btn)
  1232.               btn.setAttribute("busy", state);
  1233.           }, state ? 0 : 700);
  1234.         }
  1235.         
  1236.         break;
  1237.     }
  1238.   },
  1239.   
  1240.   openWelcomeWindow: function(aServiceType, aCallbackElementId) {
  1241.     const winType = "Yasearch:WelcomeDialog";
  1242.     
  1243.     let welcomeWindow = this.nsIYa.getWindow(winType);
  1244.     if (welcomeWindow) {
  1245.       welcomeWindow.focus();
  1246.     } else {
  1247.       let flags = "chrome,alwaysRaised,dependent=yes";
  1248.       
  1249.       if (!this.nsIYa.AppInfo.browser.isGreaterThenFx35 || this.nsIYa.AppInfo.OS.isMacOS)
  1250.         flags += ",titlebar=no";
  1251.       
  1252.       welcomeWindow = window.openDialog(YaUtils.CHROME_CONTENT + "dialogs/welcome.xul",
  1253.                                         winType, flags, aServiceType, aCallbackElementId);
  1254.     }
  1255.     
  1256.     return welcomeWindow;
  1257.   },
  1258.   
  1259.   openPrefWindow: function() {
  1260.     const winType = "Yasearch:PreferencesWindow";
  1261.     
  1262.     let prefWindow = this.nsIYa.getWindow(winType);
  1263.     if (prefWindow) {
  1264.       prefWindow.focus();
  1265.     } else {
  1266.       var instantApply = this.nsIYa.getBoolPref("browser.preferences.instantApply");
  1267.       var features = "chrome,titlebar,toolbar,centerscreen," + (instantApply ? "dialog=no" : "modal");
  1268.       prefWindow = window.openDialog(YaUtils.CHROME_CONTENT + "preferences/preferences.xul", winType, features);
  1269.     }
  1270.     
  1271.     return prefWindow;
  1272.   },
  1273.   
  1274.   openAboutWindow: function() {
  1275.     const winType = "Yasearch:AboutDialog";
  1276.     
  1277.     let aboutWindow = this.nsIYa.getWindow(winType);
  1278.     if (aboutWindow)
  1279.       aboutWindow.focus();
  1280.     else
  1281.       aboutWindow = window.openDialog(YaUtils.CHROME_CONTENT + "about/about.xul", winType,
  1282.                                       "chrome,resizable=no,dependent=yes,centerscreen");
  1283.     
  1284.     return aboutWindow;
  1285.   },
  1286.   
  1287.   log: function(msg) { this.nsIYa.log("Ya: " + msg); },
  1288.   dump: function(aObj) { this.nsIYa.dump(aObj); }
  1289. };
  1290.  
  1291. window.addEventListener("load", function(aLoadEvent) {
  1292.   aLoadEvent.currentTarget.removeEventListener("load", arguments.callee, false);
  1293.   
  1294.   aLoadEvent.currentTarget.addEventListener("unload", function(aUnloadEvent) {
  1295.     aUnloadEvent.currentTarget.removeEventListener("unload", arguments.callee, false);
  1296.     Ya.unLoad();
  1297.   }, false);
  1298.   
  1299.   Ya.onLoad();
  1300. }, false);
  1301.