// JavaScript Document

/* ================================================================
Free for use

The original version of this short url script
is available at http://www.nuff-respec.com/technology/social-bookmarking-javascript
Copyright (c) 2005-2007 Daniel Bulli. All rights reserved.
This code may be modified in any way to fit your requirements.
=================================================================== */

var SocialBookmarking = 
{
	'initted':false,
	'cssfile':"c/default.css", //default css file
	
	/**
	 * init SocialBookmarking 
	 * (no need to call directly, unless not using default css above)
	 *
	 * @access	public
	 * @param	boolean	whether or not to add css file
	 * @param	string	path to css file
	 * @return	
	 */	
	init:function(dontAddCss,cssFile) 
	{
		this.initted = true;
		
		//default set + order
		this.whichones =  ["delicious","digg","technorati","reddit","stumbleupon"];
		
		//default bookmarks
		this.bookmarks = new Object();
		this.addBookmark('favorites','javascript:addToFavorites(document.title, location.href)','Favorites');
		this.addBookmark('delicious','http://del.icio.us/post?v=4&noui&jump=close&url=%%URL%%&title=%%TITLE%%','del.icio.us');
		this.addBookmark('digg','http://digg.com/submit?phase=2&url=%%URL%%&&title=%%TITLE%%&topic=television','Digg!');
		this.addBookmark('technorati','http://technorati.com/faves/?add=%%URL%%','Technorati');
		this.addBookmark('stumbleupon','http://www.stumbleupon.com/submit?url=%%URL%%&title=%%TITLE%%','StumbleUpon');
		this.addBookmark('slashdot','http://slashdot.org/bookmark.pl?url=%%URL%%&title=%%TITLE%%','Slashdot');
		this.addBookmark('facebook','http://www.facebook.com/share.php?u=%%URL%%&t=%%TITLE%%','Facebook');

		if(!dontAddCss)
		{		
			cssFile = (cssFile) ? cssFile : this.cssfile;
			this.addCSS(cssFile);
		}
	},

	/**
	 * add a new css file to document
	 *
	 * @access	public
	 * @param	string	path to css file
	 * @return	
	 */	
	addCSS: function(cssFile)
	{
		if(!this.initted) this.init();
		
		//no css provided
		if (!cssFile) return;		
		
		try
		{
			var headID = document.getElementsByTagName("head")[0];         
			var cssNode = document.createElement('link');
			cssNode.type = 'text/css';
			cssNode.rel = 'stylesheet';
			cssNode.href = cssFile;
			cssNode.media = 'screen';
			headID.appendChild(cssNode);
		}
		catch(e){}
	},

	/**
	 * replace a div with certain ID
	 *
	 * @access	public
	 * @param	string	div id
	 * @param	string	url of page (only set if different than current page)
	 * @param	string	title of page (only set if different than current page)
	 * @param	array	array of strings of bookmarks to show (only set if different than default)
	 * @return	
	 */	
	replaceDIV: function(divID,url,title,whichones)
	{
		if(!this.initted) this.init();	
		divID = (divID) ? divID : 'social_bookmarking';
		if(document.getElementById && document.getElementById(divID))
		{
			document.getElementById(divID).innerHTML = this.getBookmarks(url,title,whichones);
		}
	},
	
	

	/**
	 * Writes out unordered list of bookmarks
	 *
	 * @access	public
	 * @param	string	url of page (only set if different than current page)
	 * @param	string	title of page (only set if different than current page)
	 * @param	array	array of strings of bookmarks to show (only set if different than default OR 'ALL')
	 * @return	
	 */	
	writeBookmarks: function(url,title,whichones)
	{
		if(!this.initted) this.init();
		document.write(this.getBookmarks(url,title,whichones));
	},
	
	
	/**
	 * Retrieved HTML for unodered list
	 *
	 * @access	public
	 * @param	string	url of page (only set if different than current page)
	 * @param	string	title of page (only set if different than current page)
	 * @param	array	array of strings of bookmarks to show (only set if different than default)
	 * @return	HTML Unordered list
	 */	
	getBookmarks: function(url,title,whichones,classname)
	{
		if(!this.initted) this.init();
		var classname = (classname) ? classname : 'social_bookmarks'
		var out = '<ul class="'+ classname + '">';
		
		whichones_u = (whichones && whichones != 'all') ? whichones : this.whichones;

		var l = whichones_u.length;
	
		if(whichones != 'all')
		{
			for( var i=0; i < l; i++ ) 
			{
				if(this.bookmarks[whichones_u[i]])
				{
					out += '<li>'+this.getBookmarkItem(this.bookmarks[whichones_u[i]],url,title)+'</li>';
				}
			}
		}
		else
		{
			for(var bookmark in this.bookmarks) 
			{
				out += '<li>'+this.getBookmarkItem(this.bookmarks[bookmark],url,title)+'</li>';
			}		
		}
		out += '</ul>';
		return out;
	},
	

	/**
	 * Retrieve bookmarkitem
	 *
	 * @access	public
	 * @param	object	bookmark object
	 * @param	string	url of page (only set if different than current page)
	 * @param	string	title of page (only set if different than current page)
	 * @return	HTML href link
	 */		
	getBookmarkItem: function(bookmark,url,title)
	{
		if(!this.initted) this.init();
		
		var url   = (url) ? encodeURIComponent(url) : encodeURIComponent(location.href);
		var title = (title) ? encodeURIComponent(title) : encodeURIComponent(document.title);
		
		var out =  '<a href="' + bookmark.url + '" class="' + bookmark.css + '" title="'+bookmark.title+'">' + bookmark.title + '</a>';
		out = out.replace('%%TITLE%%',title);
		out = out.replace('%%URL%%',url);
		return out;
	},	
	
	/**
	 * Add a new bookmark object to default list
	 *
	 * @access	public
	 * @param	string	bookmark identifier
	 * @param	string	bookmark url
	 * @param	string	bookmark title
	 * @param	string	bookmark css file (if blank uses which)
	 * @return	
	 */	
	addBookmark: function(which,url,title,css)
	{
		if(!this.initted) this.init();
		var info = new Object();
		info.which	= (which)  ? which : '';
		info.title	= (title) ? title : '';
		info.url	= (url)   ? url : null;
		info.css	= (css)   ? css : info.which;		
		this.bookmarks[which] =  info;
	}
};