/* ================================================================
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","yahoo","stumbleupon"];
		
		//default bookmarks
		this.bookmarks = new Object();
		this.addBookmark('delicious','http://del.icio.us/post?v=4&noui&jump=close&url=%%URL%%','agregar marcador a del.icio.us');
		this.addBookmark('digg','http://digg.com/submit?phase=2&url=%%URL%%&topic=movies','agregar marcador a Digg!');
            this.addBookmark('facebook','http://www.facebook.com/share.php?u=%%URL%%','agregar marcador a Facebook');
            this.addBookmark('googlebuzz','http://www.google.com/buzz/post?url=%%URL%%&imageurl=http://www.datacraft.com.ar/cabecera-datacraft.gif','agregar marcador a Google Buzz');
            this.addBookmark('reddit','http://reddit.com/submit?url=%%URL%%','agregar marcador a Reddit!');
            this.addBookmark('slashdot','http://slashdot.org/bookmark.pl?url=%%URL%%','agregar marcador a Slashdot It!');
            this.addBookmark('technorati','http://technorati.com/faves/?add=%%URL%%','agregar marcador a Technorati');
            this.addBookmark('twitter','http://twitthis.com/twit?url=%%URL%%','agregar marcador a Twitter');
		this.addBookmark('yahoo','http://myweb2.search.yahoo.com/myresults/bookmarklet?u=%%URL%%','agregar marcador a Yahoo');
		this.addBookmark('google','http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk=%%URL%%','agregar marcador a Google');
            this.addBookmark('articulosblog','http://www.articulosblog.com/submit.php?url=%%URL%%','agregar marcador a Articulos Blog');
            this.addBookmark('blogmemes','http://www.blogmemes.com/post?url=%%URL%%','agregar marcador a Blog Memes');
            this.addBookmark('copada','http://copada.net/submit.php?url=%%URL%%','agregar marcador a Copada');
            this.addBookmark('enchilame','http://www.enchilame.com/submit.php?url=%%URL%%','agregar marcador a Enchilame');
            this.addBookmark('flenk','http://www.flenk.com.ar/add.php?url=%%URL%%','agregar marcador a Flenk');
            this.addBookmark('floreame','http://www.floreame.net/submit.php?url=%%URL%%','agregar marcador a Floreame');
            this.addBookmark('fresqui','http://tec.fresqui.com/post?url=%%URL%%','agregar marcador a Fresqui');
		this.addBookmark('meneame','http://meneame.net/submit.php?url=%%URL%%','agregar marcador a Meneame');
            this.addBookmark('rankear','http://www.rankear.com/submit.php?url=%%URL%%','agregar marcador a RankeAr');

		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+'">' + "." + '</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;
	}
};
