// this script contains Greasemonkey oriented code snippet.

// === START LICENSE ===
// 
// Copyright 2004-2007 Aaron Boodman
// Contributors: See contributors list in install.rdf
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// Note that this license applies only to the Greasemonkey extension source
// files, not to the user scripts which it runs. User scripts are licensed
// separately by their authors.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// 
// === END LICENSE ===
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

var DEBUG_TRACE = 1; 

function trace() {

	if ( (typeof DEBUG_TRACE != 'undefined') && ! DEBUG_TRACE )
		return;

	var fn;
	if ( typeof Firebug == 'undefined' ) {
		fn = console;
	} else if ( Firebug && Firebug.Console && Firebug.Console.log  ) {
		fn = Firebug.Console;
	}

	if ( fn ) {
		if ( arguments.length == 1 ) {
			fn.log(arguments[0]);
		} else {
			var args = [];
			for (var i = 0; i < arguments.length; i++) {
				args.push(arguments[i]);
			}
			fn.log(args);
		}
	}
}


{

	var XHR = function (opts) {
try {
		var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].
					createInstance(Components.interfaces.nsIXMLHttpRequest);

		if ( opts.onload ) {
			req.onload = function () {
				  var responseState = {
					// can't support responseXML because security won't
					// let the browser call properties on it
					responseText:req.responseText,
					readyState:req.readyState,
					responseHeaders:(req.readyState == 4 ?
									 req.getAllResponseHeaders() :
									 ''),
					status:(req.readyState == 4 ? req.status : 0),
					statusText:(req.readyState == 4 ? req.statusText : ''),
					finalUrl:(req.readyState == 4 ? req.channel.URI.spec : '')
				  };
				return opts.onload.call(this, responseState);
			}
		}
		if ( opts.onerror ) {
			req.onerror = function () {
				  var responseState = {
					// can't support responseXML because security won't
					// let the browser call properties on it
					responseText:req.responseText,
					readyState:req.readyState,
					responseHeaders:(req.readyState == 4 ?
									 req.getAllResponseHeaders() :
									 ''),
					status:(req.readyState == 4 ? req.status : 0),
					statusText:(req.readyState == 4 ? req.statusText : ''),
					finalUrl:(req.readyState == 4 ? req.channel.URI.spec : '')
				  };
				return opts.onerror.call(this, req);
			}
		}
		req.open(opts.method || "GET", opts.url, true);
		if ( opts.contentType ) {
			req.setRequestHeader("Content-Type", opts.contentType);
		} else {
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		req.send(opts.data || null);
		return req;
} catch(e) {
	alert(e.fileName + e.lineNumber + ":" + e.message);
}
	};

	var querystring = function (q) {
		var p = [];
		for ( var i in q ) {
			p.push(encodeURIComponent(i)+"="+encodeURIComponent(q[i]));
		}
		return p.join("&")
	}

}

function getCookieFromManager(host, name) {
	var iter = Components.classes["@mozilla.org/cookiemanager;1"]
							.getService(Components.interfaces.nsICookieManager).enumerator;

	while (iter.hasMoreElements()){
		var cookie = iter.getNext();
		if (cookie instanceof Components.interfaces.nsICookie){
			if ( cookie.host == host && cookie.name == name )
				return cookie.value;
		}
	}
	return null;
}

if ( gContextMenu ) {
	var e = gContextMenu.target;
	var u;
	if ( e.tagName.toLowerCase() == 'a' ) {
		u = e.href;
	} else {
		u = e.ownerDocument.location.href;
	}

	trace(gContextMenu, u);
}

var serviceAdapters = [
	{
		service: 'delicious',
		url: /del.icio.us\/(\w+)\b/,
		params: ['username']
	},
	{
		service: "blog",
		url: /(.*)()/,
		params: ['url', 'author']
	}
];

var adapter;
var m;

for ( var i = 0; i < serviceAdapters.length; i++ ) {
	adapter = serviceAdapters[i];
	if ( m = u.match(adapter.url) )
		break;
}

var params = {
	at: getCookieFromManager("friendfeed.com", "AT"),
	userid: "",
	service: adapter.service,
};

adapter.params.forEach( function (name, i) {
	params[name] = m[i + 1];
} );

trace(adapter, m, params);

var r = XHR( {
	url: "http://friendfeed.com/settings/configureservice",
	data: querystring(params),
	method: "POST",
	onload: function (res) { trace(res) }
} );



