/*
Share This
usage: <tag class="ice-share-this">whatever</tag>
*/
(function($){
	/*
	Class Definition
	*/
	
	if(!window.ICE)
	{
		window.ICE = {};
	}
	
	ICE.ShareThis = function()
	{
		this.init();
	};
	ICE.ShareThis.IMAGE_PATH = '/ice/share_this/images/';
	ICE.ShareThis.instance = null;
	
	ICE.ShareThis.getInstance = function(){
		if(!ICE.ShareThis.instance)
		{
			ICE.ShareThis.instance = new ICE.ShareThis()
		}
		return ICE.ShareThis.instance;
	};
	
	ICE.ShareThis.bind = function(e){
		$(e).bind('click', function(event){
			$( ICE.ShareThis.getInstance().container ).css({
				top  : $(e).offset().top + $(e).outerHeight(true),
				left : $(e).offset().left
			}).hide().fadeIn(300);
			
			event.stopPropagation();
		});
	};
	
	ICE.ShareThis.DEFAULT = {
		services : {
			meneame : {
				name   : 'Meneame',
				icon   : 'meneame.gif',
				url    : "http://meneame.net/submit.php",
				params : {
					url : window.location
				}
			},
			fresqui : {
				name  : 'Fresqui',
				icon  : 'fresqui.gif',
				url   : "http://tec.fresqui.com/post",
				params : {
					url   : window.location,
					title : $('title').html()
				}
			},
			efectoTequila : {
				name  : 'Efecto Tequila',
				icon  : 'enchilame.gif',
				url   : "http://efectotequila.com/submit.php",
				params : {
					url : window.location
				}
			},
			delicious : {
				name  : 'Delicious',
				icon  : 'delicious.gif',
				url   : "http://del.icio.us/post",
				params : {
					url   : window.location,
					title : $('title').html()
				}
			},
			facebook : {
				name  : 'Facebook',
				icon  : 'facebook.gif',
				url   : "http://www.facebook.com/share.php",
				params : {
					u : window.location,
					t : $('title').html()
				}
			},
			fiendFeed : {
				name  : 'FriendFeed',
				icon  : 'friendfeed.gif',
				url   : "http://friendfeed.com/",
				params : {
					url   : window.location,
					title : $('title').html()
				}
			},
			googleBookmarks : {
				name  : 'Google Bookmarks',
				icon  : 'google_bmarks.gif',
				url   : "http://www.google.com/bookmarks/mark",
				params : {
					op    : 'edit',
					bkmk  : window.location,
					title : $('title').html()
				}
			},
			yahooMyWeb : {
				name  : 'Yahoo! My Web',
				icon  : 'yahoo_myweb.gif',
				url   : "http://myweb2.search.yahoo.com/myresults/bookmarklet",
				params : {
					u : window.location,
					t : $('title').html()
				}
			},
			technorati : {
				name  : 'Technorati',
				icon  : 'technorati.gif',
				url   : "http://www.technorati.com/faves",
				params : {
					add : window.location
				}
			},
			twitter : {
				name  : 'Twitter',
				icon  : 'twitter.gif',
				url   : "http://www.technorati.com/faves",
				params : {
					status : $('title').html() + ': ' + window.location
				}
			}
		}
	};
	ICE.ShareThis.prototype = {
		init : function(){
			$('<div class="unit two-columns ice-share-this-container"></div>').appendTo('.layout');
			this.container = $('.ice-share-this-container');
			var that = this;
			
			$('<img class="close" src="/images/silk/cross.png" />').bind('click', function(){
				that.container.fadeOut(300);
			}).appendTo(this.container);
			
			var services = ICE.ShareThis.DEFAULT.services;
			for (var i in services)
			{
				if(services.hasOwnProperty(i))
				{
					var title = services[i].name;
					var url   = services[i].url;
					var icon  = ICE.ShareThis.IMAGE_PATH + services[i].icon;
					
					var first = true;
					for (var j in services[i].params)
					{
						
						if(services[i].params.hasOwnProperty(j))
						{
							url   += ( (first) ? '?' : '&' ) + j + '=' + escape(services[i].params[j]);
							first  = false;
						}
					}
									
					this.container.append( $('<div class="column"><img src="' + icon + '" /><a href="' + url + '">' + title + '</a></div>') );
				}
			}
			
			this.container.click(function(event){
				event.stopPropagation();
			});
			
			$(document).click(function(){
				ICE.ShareThis.getInstance().hide();
			});
		},
		hide : function(){
			this.container.fadeOut(300);
		}
	};
	
	/*
	Class Binding
	*/
	$(function(){
		var binded = $('.ice-share-this');
		if(binded.length > 0)
		{
			ICE.ShareThis.getInstance();
		}
		binded.each(function(){
			ICE.ShareThis.bind(this);
		});
	});
})(jQuery);


