// Name:	Social xFolk
// Author: 	Nick Peters
// Version: 1.00
// Date: 	2006-12-16
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details:
// <http://www.gnu.org/licenses/gpl.txt>
//
// Revision history:
// 1.0  2006-12-16 Initial version started at SHDH14
//
// ==UserScript==
// @name          Social xFolk
// @namespace     http://www.nickpeters.net
// @description   Add xFolk bookmarks to social bookmarking sites del.icio.us and ma.gnolia with ease.
// @include       *
// ==/UserScript==
( function() {
// getElementsByClassName function taken from http://snook.ca/archives/javascript/your_favourite_1/
var getElementsByClassName = function(node , classname) {
	var a = [];
	var re = new RegExp( '(^| )' + classname + '( |$)' );
	var els = node.getElementsByTagName( "*" );
	for ( var i=0 , j=els.length; i<j; i++ )
		if ( re.test( els[i].className ) ) a.push( els[i] );
	return a;

} // getElementsByClassName

// addBookmarks will parse the microformatted data and generate the buttons
// to add a site to either ma.gnolia or del.icio.us.
var addBookmarks = function(xfolkBookmark) {
	// Get the title and URL of the site
	var urlElements = getElementsByClassName(xfolkBookmark , 'taggedlink');
	if(urlElements.length > 0) {
		var title = (urlElements[0].getAttribute('title') != null) ?
					urlElements[0].getAttribute('title') : urlElements[0].innerHTML;
		var url = (urlElements[0].getAttribute('href') != "") ?
					urlElements[0].getAttribute('href') : "";
	}
	// Get a description of the site from the meta data.
	var descriptions = getElementsByClassName(xfolkBookmark, 'description');
	var description = (descriptions.length > 0) ?
					descriptions[0].innerHTML : "";
	
	// Currently neither ma.gnolia nor del.icio.us support inserting tags
	// via their GET API.  Below is the start of some code to extract tags 
	// in case they either offer support for it in the future, or if I 
	// overlooked something.
	/*var tags = new Array();
	for(var i=0; i < xfolkBookmark.length; i++) {
		if(xfolkBookmark[i].getAttribute('rel') == "tag") {
			tags.push(xfolkBookmark[i]);
		}
	}*/
	
	// Used for debugging purposes.
	//console.log("Title: "+title+"\nURL: "+url+"\nDescription: "+description);
	
	// Insert Line Break
	var brk = document.createElement('br');
	xfolkBookmark.appendChild(brk);
	
	// Insert the del.icio.us button.
	var deliciousLink = document.createElement('a');
	var deliciousImg = document.createElement('img');
	deliciousLink.href="http://del.icio.us/post?v=2;url="+encodeURIComponent(url)+";title="+
	encodeURIComponent(title);
	deliciousImg.src = "http://images.del.icio.us/static/img/bookmarklets/ie/IEpostdel.gif";
	deliciousImg.style.border = '0px';
	deliciousLink.appendChild(deliciousImg);
	xfolkBookmark.appendChild(deliciousLink);
	
	// Insert the Ma.gnolia button.
	var magnoliaLink = document.createElement('a');
	var magnoliaImg = document.createElement('img');
	magnoliaLink.href="http://ma.gnolia.com/bookmarklet/add?url="+encodeURIComponent(url)+
	"&title="+encodeURIComponent(title)+"&description="+encodeURIComponent(description);
	magnoliaImg.src = "http://assets.ma.gnolia.com/images/badges/magnolia_badge.gif?1164757969";
	magnoliaImg.style.border = '0px';
	magnoliaLink.appendChild(magnoliaImg);
	xfolkBookmark.appendChild(magnoliaLink);
} // Addbookmarks

// Create an array of xfolkentry elements and their child nodes 
var bookmarks = getElementsByClassName(document , 'xfolkentry');

// Calls the addBookmarks function to add the buttons on each entry.
for ( var i = 0; i < bookmarks.length; i++ ) {
  addBookmarks(bookmarks[i]);
}
} )();