// XPCOM stuff
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

const YOONO_SCHEME = "yoono";

const nsISupports = Components.interfaces.nsISupports;
const nsIIOService = Components.interfaces.nsIIOService;
const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler;
const nsIURI = Components.interfaces.nsIURI;

const CC = Components.classes;
const CI = Components.interfaces;

const WMED = CC['@mozilla.org/appshell/window-mediator;1'].getService(CI.nsIWindowMediator);

const CONSOLESERVICE = CC["@mozilla.org/consoleservice;1"].getService(CI.nsIConsoleService);

function Protocol() {

}

Protocol.prototype =
{
  classDescription: "Yoono Protocol to handle yoono: urls",
  classID:          Components.ID("{5cd52995-7113-9bd6-8d65-364c4f7fea08}"),
  contractID:       "@mozilla.org/network/protocol;1?name=" + YOONO_SCHEME,
  QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIProtocolHandler]),

  scheme: YOONO_SCHEME,
  defaultPort: -1,
  protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
                 nsIProtocolHandler.URI_NOAUTH,

  allowPort: function(port, scheme)
  {
    return false;
  },

  newURI: function(spec, charset, baseURI)
  {
    var uri = Components.classes["@mozilla.org/network/simple-uri;1"].createInstance(nsIURI);
    uri.spec = spec;
    return uri;
  },

  newChannel: function(aURI)
  {
    var prefService = Components.classes["@mozilla.org/preferences-service;1"]
                                .getService(Components.interfaces.nsIPrefService);

    var prefBranch = prefService.getBranch('extensions.yoono.');
    var allow = false;
    try {
      allow = prefBranch.getBoolPref('admin');
    } catch(e) {
      
    }

    var url = '';
    // aURI is a nsIUri, so get a string from it using .spec
    var feature = aURI.spec;
    // strip away the SCHEME: part
    feature = feature.substring(feature.indexOf(":") + 1, feature.length);
    switch(feature) {
      case '':
        url = "chrome://yoono/content/protocol.html";
      break;
      case 'about':
        url = "chrome://yoono/content/aboutyoono.xul";
      break;
      case 'admin':
        if(allow) {
          url = "chrome://yoono/content/admin.xul";         
        } else {
          url = '';
        }
      break;
      case 'debug':
        url = "chrome://yoono/content/aboutYoonoHelp.xul";
      break;

      case 'home':
        url = "http://www.yoono.com";
      break;
      case 'blog':
        url = "http://blog.yoono.com/";
      break;
      case 'help':
        url = "http://help.yoono.com/help.html";
      break;

      case 'update':
        url = "http://cache.yoono.com/download/ff/yoono_latest.xpi";
      break;
      case 'socialzine':
        url = "chrome://yoonosb/content/friends2/socialzine/page.html";
        var mainWin = WMED.getMostRecentWindow("navigator:browser");
        var gBrowser = mainWin.gBrowser;
        var browsers = gBrowser.browsers;
        // select if already opened
        for(var idx=0; idx < browsers.length; idx++) {
          if(browsers[idx].contentWindow.document.URL == 'yoono:socialzine') {
            if(browsers[idx] == gBrowser.mCurrentBrowser) break; // reload
            gBrowser.selectTabAtIndex(idx);
            url = 'about:blank';
            aURI = url;
            break;
          }
        }
      break;
      case 'columns':
        url = "chrome://yoonosb/content/friends2/multicolumn/columns.html";
        var mainWin = WMED.getMostRecentWindow("navigator:browser");
        var gBrowser = mainWin.gBrowser;
        var browsers = gBrowser.browsers
        // select if already opened
        for(var idx=0; idx < browsers.length; idx++) {
          if(browsers[idx].contentWindow.document.URL == 'yoono:columns') {
            gBrowser.selectTabAtIndex(idx);
            url = 'about:blank';
            aURI = url;
            break;
          }
        }
      break;
    }
    var ios = CC["@mozilla.org/network/io-service;1"].getService(CI.nsIIOService);
    var channel = ios.newChannel(
      url,
      null,
      null
    );

    channel.originalURI = aURI;

    return channel;
  }
}



/**
* XPCOMUtils.generateNSGetFactory was introduced in Mozilla 2 (Firefox 4).
* XPCOMUtils.generateNSGetModule is for Mozilla 1.9.2 (Firefox 3.6).
*/
if (XPCOMUtils.generateNSGetFactory)
    var NSGetFactory = XPCOMUtils.generateNSGetFactory([Protocol]);
else
    var NSGetModule = XPCOMUtils.generateNSGetModule([Protocol]);
