/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Adblock Plus.
 *
 * The Initial Developer of the Original Code is
 * Wladimir Palant.
 * Portions created by the Initial Developer are Copyright (C) 2006-2009
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */

var subscription = null;
var result = null;
var autoAdd = false;

function init() {
  if (window.arguments)
  {
    // In K-Meleon we might get the arguments wrapped
    for (var i = 0; i < window.arguments.length; i++)
      if (window.arguments[i] && "wrappedJSObject" in window.arguments[i])
        window.arguments[i] = window.arguments[i].wrappedJSObject;

    [subscription, result] = window.arguments;
  }

  autoAdd = !result;
  if (!result)
    result = {};

  if (subscription)
  {
    document.getElementById("location").value = subscription.url;
    document.getElementById("title").value = subscription.title;
  }
  else
    document.getElementById("title").value = "";

  if (subscription instanceof aup.Subscription)
  {
    document.getElementById("enabled").setAttribute("checked", !subscription.disabled);
    if (subscription instanceof aup.ExternalSubscription)
    {
      document.getElementById("location").setAttribute("disabled", "true");
      document.getElementById("external-description").hidden = false;
      document.getElementById("autodownload").setAttribute("checked", "true");
      document.getElementById("autodownload").setAttribute("disabled", "true");
    }
    else
    {
      document.getElementById("edit-description").hidden = false;
      document.getElementById("autodownload").setAttribute("checked", subscription.autoDownload);
    }
  }
  else
  {
    document.title = document.documentElement.getAttribute("newtitle");
    document.getElementById("new-description").hidden = false;
    document.getElementById("enabled").setAttribute("checked", "true");
    document.getElementById("autodownload").setAttribute("checked", "true");
  }
}

function saveSubscription()
{
  var add = !(subscription instanceof aup.Subscription);
  var url = (add || subscription instanceof aup.DownloadableSubscription ? document.getElementById("location").value.replace(/^\s+/, "").replace(/\s+$/, "").replace(/^~+/, "") : subscription.url);
  if (!url)
  {
    alert(aup.getString("subscription_no_location"));
    document.getElementById("location").focus();
    return false;
  }

  if (add || subscription.url != url)
  {
    var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
    try {
      file.initWithPath(url);
      var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); 
      url = ioService.newFileURI(file).spec;
    } catch (e2) {
      try {
        var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
        uri.spec = url;
        url = uri.spec;
      } catch (e) {
        alert(aup.getString("subscription_invalid_location"));
        document.getElementById("location").focus();
        return false;
      }
    }
  }

  result.url = url;
  result.title = document.getElementById("title").value.replace(/^\s+/, "").replace(/\s+$/, "");
  if (!result.title)
    result.title = result.url;

  result.autoDownload = document.getElementById("autodownload").checked;
  result.disabled = !document.getElementById("enabled").checked;

  if (autoAdd)
    aup.addSubscription(result.url, result.title, result.autoDownload, result.disabled);

  return true;
}
