/*
 * File: 2008g13-webmail-notifier-gmail-html.js
 * Date: Revised 2008-07-31
 * Source: webmail4.js (2008-06-25@12-49-00).
 * User script for the Firefox WebMailNotifier extension, available at
 * https://addons.mozilla.org/en-US/firefox/addon/4490.
 * Revisions:
 *   1.  Impose JSLint indentation.
 *   2.  Identify as G/Html to distinguish from webmail4.js (GMail).
 *   3.  Set var inboxOnly manually as webmail4.inboxOnly config var fails.
 *   4.  Set var ar manually as handler.user.split fails: gmail.com or
 *       googlemail.com or other as appropriate.
 *   5.  For gmail, set handler.mailURL to desired access type:
 *       ?ui=mobile (fastest, minimal features);
 *       ?ui=html (fast, some features); [recommended]
 *       ?ui=1 (slower, most features);
 *       ?ui=2 (slowest, all features).
 *   6.  Do not change handler.mailURL to http.  It interferes with polling.
 * Reviser: walt(at)w-gregg.juneau.ak.us
 */

// Declare external variables for JSLint:
/*extern ST_LOGIN_RES, ST_DATA, WebMailHandler */

function initHandler(handler) {
    handler.name = 'G/Html';

//  var inboxOnly = handler.webMailNotifier.prefBranch.getBoolPref("webmail4.inboxOnly");
    var inboxOnly;
    inboxOnly = 1; // 1=true; 0=false // Set this as desired
    handler.queryString = "is%3Aunread" + (inboxOnly?"%20in%3Ainbox":"");
    
// WebmailNotifier handler.user var unavailable; set manually.
//  var ar = handler.user.split("@");
    var ar = new Array(2);
    ar[1] = "gmail.com"; // Set this as appropriate
    
    if (ar[1] === "gmail.com" || ar[1] === "googlemail.com") {
        handler.dataURL = "http://mail.google.com/mail/?ui=1&view=tl&search=query&start=0&q=" + handler.queryString;
        handler.loginData = ["https://www.google.com/accounts/LoginAuth?continue=http%3A%2F%2Fwww.google.com%2Fwebhp",
                          "Email", "Passwd",
                          "continue=http%3A%2F%2Fmail.google.com%2Fwebhp&rmShown=1"];
        handler.mailURL = "https://mail.google.com/mail/?ui=html"; //used in below process!

    } else { //Google Apps
        handler.dataURL = "https://mail.google.com/a/" + ar[1] + "/?ui=1&view=tl&search=query&start=0&q=" + handler.queryString;
        handler.mailURL = "https://mail.google.com/a/" + ar[1]; //used in below process!
        handler.loginData = ["https://www.google.com/a/" + ar[1] + "/LoginAction2?service=mail",
                          "", "Passwd",
                          "continue=" + encodeURIComponent(handler.mailURL) + "&service=mail&Email=" + ar[0]];
    }

    handler.process = function (aHttpChannel, aData) {
        var fnd; // Moved var declaration here per JSLint
        switch (this.stage) {
        case ST_LOGIN_RES:
            fnd = aData.match(/<meta.+?url='(\S+)'/); // No var per JSLint
            if (fnd) {
                fnd = fnd[1].replace(/&amp;/g,"&");
                this.webMailNotifier.getHtml(this,fnd);
                return false;
            }
            ++this.stage;
	    // break intentionally omitted; falls through (JSLint).
        case (ST_LOGIN_RES + 1):
            this.webMailNotifier.getHtml(this,this.mailURL);
            return false;
        case (ST_LOGIN_RES + 2):
            fnd = aData.match(/<meta.+?url='*(\S+)'*\"/i); // No var per JSLint
            if (fnd) {
                fnd = fnd[1].replace(/&amp;/g,"&");
                this.webMailNotifier.getHtml(this,fnd);
                return false;
            }
            // break intentionally omitted; falls through (JSLint).
        case (ST_LOGIN_RES + 3):
            fnd = aData.match(/ID_KEY\s*?:\s*?[\"\'](.+?)[\"\']/); // No var per JSLint
            if (fnd) {
                this.dataURL = this.mailURL + "/?ui=2&ik=" + fnd[1] + "&view=tl&start=0&num=25&rt=h&q=" + this.queryString + "&search=query";
                this.newUI = true;
            } else { // put left brace in, added newline, per JSLint
                this.newUI = false;
            } // put right brace in to balance the left before the preexisting case, sort of per JSLint
            this.stage = ST_DATA;
            break;
        }
        return WebMailHandler.prototype.process.call(this,aHttpChannel, aData);
    };
    handler.getData = function (aData) {
        var fnd;
        if (this.newUI) { // Added {} per JSLint
            fnd = aData.match(/D\(\["ti",.+?,\d*,\d*,(\d+)/); 
        }
        else { // Added {} per JSLint
            fnd = aData.match(/D\(\["ts",\d*,\d*,(\d+)/); 
        } 
        if (fnd) {
            return fnd[1];
        } else {
            return -1;
        }
    };
}
