"use strict";
const EXPORTED_SYMBOLS = ["core", "resources"];
const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
const resources = {
browser: {
styles: ["/native/fx/bindings.css"]}};
const WIDGET_NAME = "http://bar.yandex.ru/packages/yandexbar#yaru";
const core = {
init: function YaruWidget_init(api) {
this._api = api;
this._logger = api.logger;
this._loadModules();
}
,
finalize: function YaruWidget_finalize() {
delete this.utils;
delete this._api;
delete this._logger;
delete this.__stringBundle;
}
,
buildWidget: function YaruWidget_buildWidget(WIID, item) {
item.setAttribute("yb-native-widget-name",WIDGET_NAME);
item.setAttribute("yb-native-widget-wiid",WIID);
item.module = this;
}
,
destroyWidget: function YaruWidget_destroyWidget(WIID, item, context) {
try {
if (typeof item.yaruDestroy == "function")
item.yaruDestroy();
}
 finally {
item.removeAttribute("yb-native-widget-name");
item.removeAttribute("yb-native-widget-wiid");
}

}
,
get API() {
return this._api;
}
,
onButtonClick: function YaruWidget_onButtonClick(event, widget) {
if (this.yauth.authorized)
{
this.gotoUnread(event);
}
 else
{
let dialogParams = {
name: "yaru",
title: widget.title};
this.yauth.openAuthDialog(event,dialogParams,false,(function () {
this.gotoUnread(event);
}
).bind(this));
}

}
,
counterServiceID: 47,
counterXpathExpr: "number(/s/c/blogs/@v)",
refreshData: function YaruWidget_refreshData(event, callback) {
this.counters.forceUpdate(this.counterServiceID,callback);
}
,
gotoUnread: function YaruWidget_gotoUnread(event) {
this._navToYaruPage("replies_history_unread.xml",this._STAT_CODES.GOTO_UNREAD,event);
}
,
gotoFriends: function YaruWidget_gotoFriends(event) {
this._navToYaruPage("friends.xml",this._STAT_CODES.GOTO_FRIENDS,event);
}
,
postText: function YaruWidget_postText(event) {
this._navToYaruPage("posts_add_text.xml",this._STAT_CODES.POST_TEXT,event);
}
,
postImage: function YaruWidget_postImage(event) {
this._navToYaruPage("posts_add_photo.xml",this._STAT_CODES.POST_IMAGE,event);
}
,
postStatus: function YaruWidget_postStatus(event) {
this._navToYaruPage("posts_add_status.xml",this._STAT_CODES.POST_STATUS,event);
}
,
sharePage: function YaruWidget_sharePage(event) {
this.shareContent({
contentType: "SharePage"},event);
}
,
shareContent: function YaruWidget_shareContent(shareData, event) {
const LINE_BREAK = "<br/><br/>";
const login = this.yauth.username;
const srcChromeWnd = event.target.ownerDocument.defaultView;
const sharedDoc = srcChromeWnd.content.document;
var dataToSend = {
body: "",
title: "",
URL: "",
YARU_retpath: "http://" + login + ".ya.ru/"};
var pageAddress = srcChromeWnd.content.location.href;
var srcLine = this._stringBundle.get("sendContentSource",[pageAddress + LINE_BREAK]);
switch (shareData.contentType) {
case "SharePage":
{
dataToSend.YARU_retpath += "posts_add_link.xml";
dataToSend.URL = pageAddress;
dataToSend.title = sharedDoc.title;
break;
}

case "ShareSelection":
{
dataToSend.YARU_retpath += "posts_add_text.xml";
let imgHintTpl = this._stringBundle.get("sendImageTemplate");
dataToSend.body = srcLine + this.utils.getSelectionString(srcChromeWnd,imgHintTpl);
break;
}

case "ShareImage":
{
dataToSend.YARU_retpath += "posts_add_text.xml";
let imgData = shareData.image;
if (imgData)
{
dataToSend.body = srcLine + "<img src=\"" + imgData.imageURL + "\" alt=\"" + (imgData.imageName || imgData.imageURL) + "\" title=\"" + imgData.imageName + "\" />";
dataToSend.message_type = "html";
}

break;
}

}

this._send("http://my.ya.ru/request_to_session.xml",dataToSend);
}
,
_STAT_CODES: {
GOTO_FRIENDS: 3410,
POST_TEXT: 3420,
POST_IMAGE: 3430,
POST_STATUS: 3440,
GOTO_UNREAD: 3450,
SHARE_CONTENT: 3460},
_MODULES: {
utils: "utils.jsm",
counters: "counters.jsm",
yauth: "yauth.jsm"},
_loadModules: function YaruWidget__loadModules() {
var shAPI = this._api.shareableAPI;
for each(let [moduleName, moduleFileName] in Iterator(this._MODULES)) {
Cu.import(this._api.Package.resolvePath("/native/fx/modules/" + moduleFileName),this);
let module = this[moduleName];
if (typeof module.init == "function")
module.init(shAPI);
}

}
,
__stringBundle: null,
get _stringBundle() {
return this.__stringBundle || (this.__stringBundle = this._api.Localization.createStringBundle("/native/fx/yaru.properties"));
}
,
_navToYaruPage: function YaruWidget__navToYaruPage(page, statAction, origEvent) {
var username = this.yauth.username;
var yaruURL = username ? encodeURIComponent(username) + ".ya.ru/" + page : "wow.ya.ru";
if (statAction)
this.API.Statistics.logShortAction(statAction);
this.API.Controls.navigateBrowser({
url: yaruURL,
eventInfo: origEvent});
}
,
_send: function YaruWidget__send(url, dataToSend) {
var postParams = [];
for(let [paramName, paramValue] in Iterator(dataToSend)) {
if (paramName && paramValue)
postParams.push(paramName + "=" + encodeURIComponent(paramValue.substr(0,1000000)));
}

postParams = postParams.join("&");
var postStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
postStream.setData(postParams,postParams.length);
var postData = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
postData.addHeader("Content-Type","application/x-www-form-urlencoded");
postData.addContentLength = true;
postData.setData(postStream);
this.API.Controls.navigateBrowser({
url: url,
target: "new tab",
postData: postData});
this.API.Statistics.logShortAction(this._STAT_CODES.SHARE_CONTENT);
}
};
