/* File:	bookmarklets.js
This file is used to define and display the bookmarklets available on the site 

Section:	Version History
10/02/2009 (DJO) - Created File
*/

// define the avialable services
var	CONST_DEFAULTSERVICES = "rss,delicious,digg,stumbleupon,reddit,technorati,twitter";

// initialise bookmarklet object
var bookmarklets = {};

// initialise top level objects
bookmarklets.services = {};
bookmarklets.fn = {};

// define configuration information for each of the services
bookmarklets.services.rss = {title: "RSS Feed", url: "http://feeds2.feedburner.com/ConceptualAdvantage" };
bookmarklets.services.delicious = {title: "Save to Delicious", url: "http://delicious.com/save?", urlParam: "url", titleParam: "title" };
bookmarklets.services.digg = {title: "Digg This", url: "http://digg.com/submit?", urlParam: "url", titleParam: "title" };
bookmarklets.services.technorati = {title: "Favorite This!", url: "http://technorati.com/faves?sub=favthis&", urlParam: "add" };
bookmarklets.services.stumbleupon = {title: "Add to Stumble Upon", url: "http://www.stumbleupon.com/submit?", urlParam: "url", titleParam: "title" };
bookmarklets.services.reddit = {title: "reddit this!", url: "http://www.reddit.com/submit?", urlParam: "url" };
bookmarklets.services.furl = {title: "Furl", url: "http://www.furl.net/items/new?p=0&", urlParam: "u", titleParam: "t" };

// define twitter using a custom function
bookmarklets.services.twitter = {
	title: "Twitter", 
	getUrlFn: function(url, title) {
		return "http://twitter.com/home?status=" + encodeURIComponent("Check out: " + title + " @ " + url);
	}};

/* bookmarklet functions */

bookmarklets.fn.getService = function(strId) {
	// initialise variables
	var fnresult = null;
	
	// if the service is defined, then update the function result
	if (bookmarklets.services[strId]) {
		fnresult = bookmarklets.services[strId];
		fnresult.id = strId;
	} // if
	
	return fnresult;	
} // getService

/*
Function:	getDestUrl
This function is used to get the bookmarking url for the defined, service

Parameters:
strId - the service id
url - the url to bookmark
title - the page title to bookmark
*/
bookmarklets.fn.getDestUrl = function(strId, url, title) {
	// initialise variables
	var fnresult = "";

	// find the service definition
	var serviceDef = bookmarklets.fn.getService(strId);

	// if the service has a url defined with it, then start adding
	if (serviceDef) {
		// if the a custom get url function is defined, use that
		if (serviceDef.getUrlFn) {
			fnresult = serviceDef.getUrlFn(url, title)
		}
		// otherwise, if the url for the service is defined use that
		else if (serviceDef.url) {
			fnresult = serviceDef.url;
			
			// if the service has a url parameter defined, then pass the url
			if (serviceDef.urlParam) {
				fnresult += serviceDef.urlParam + "=" + encodeURIComponent(url) + '&';
			} // if
			
			// if the service has a title parameter defined, then pass the page title
			if (serviceDef.titleParam) {
				fnresult += serviceDef.titleParam + "=" + encodeURIComponent(title) + '&';
			} // if
		} // if
	} // if
	
	return fnresult; 
} // getDestUrl

/* 
Plugin:  jQuery bookmarklets
This plugin is designed to drop a bunch of bookmarklets on the matched elements
*/
jQuery.fn.bookmarklets = function(serviceList) {
	// if the service list is undefined, use the default service list
	if (! serviceList) {
		serviceList = CONST_DEFAULTSERVICES;
	} // if
	
	// create the service array
	var	serviceArray = serviceList.split(",");

	// iterate through the service list and create the required buttons
	for (var ii = 0; ii < serviceArray.length; ii++) {
		// initialise variables
		var serviceDef = bookmarklets.fn.getService(serviceArray[ii]);
		
		// if the service is defined, then create the button
		if (serviceDef) {
			// get the destination url
			var destUrl = bookmarklets.fn.getDestUrl(serviceDef.id, window.location.href, document.title);	
			
			// if the destination url is defined, then create the link
			if (destUrl) {
				// create the required button
				$(this).append(
					"<a class='bookmarklet' service='" + serviceDef.id + "' href='" + destUrl + "' target='_blank' title='" + serviceDef.title + "'>" + 
					"<img src='" + (typeof(wpTemplatePath) != "undefined" ? wpTemplatePath + "/" : "") + "images/" + serviceDef.id + ".png' border='0' alt='" + serviceDef.title + "' title='" + serviceDef.title + "' />" + 
					"</a>\n");
			}
		} // if 
	} // for 
	
	// attach on click events to each of the bookmarklets
	$(".bookmarklet").click(function() {
		// if ga is enabled, then track the click
		if (pageTracker) {
			//  send event tracking to google
			pageTracker._trackEvent("Bookmarklets", "Saved to " + $(this).attr("service"), "Saved " + document.title);
		} // if
	});
} // bookmarklets	  