var media_base = settings.mediaBase || '/m';
var sharingServicesList = [
	{
		'title' : 'Digg',
		'url' : 'http://digg.com/submit?phase=2&amp;url=<URL>',
		'icon' : media_base + '/img/sociable/digg.png'
	},	
	{
		'title' : 'del.icio.us',
		'url' : 'http://del.icio.us/post?url=<URL>',
		'icon' : media_base + '/img/sociable/delicious.png'
	},	
	{
		'title' : 'Facebook',
		'url' : 'http://www.facebook.com/sharer.php?u=<URL>',
		'icon' : media_base + '/img/sociable/facebook.png'
	},	
	{
		'title' : 'StumbleUpon',
		'url' : 'http://www.stumbleupon.com/submit?url=<URL>',
		'icon' : media_base + '/img/sociable/stumbleupon.png'
	},	
	{
		'title' : 'Sphinn',
		'url' : 'http://sphinn.com/submit.php?url=<URL>',
		'icon' : media_base + '/img/sociable/sphinn.gif'
	},	
	{
		'title' : 'SphereIt',
		'url' : 'http://www.sphere.com/search?q=sphereit:<URL>',
		'icon' : media_base + '/img/sociable/sphere.png'
	},	
	{
		'title' : 'Technorati',
		'url' : 'http://technorati.com/faves?add=<URL>',
		'icon' : media_base + '/img/sociable/technorati.png'
	},	

];




(function($) {
 
	$.fn.easyShare = function(settings) {
	var config = {
		'topTemplate' : '<div class="sociable-tagline" style="display:none;"><strong>Share me:</strong><a href="javascript:void(0);" class="sociable-close">Close</a><ul>',
		'itemTemplate' : '<li><a rel="nofollow" href="<URL>" title="<TITLE>"><img src="<ICON>" title="<TITLE>" alt="<TITLE>" class="sociable-hovers" /><TITLE></a></li>',
		'bottomTemplate' : '</ul><form action="" method="GET"><label for="sharelink">Link:&nbsp;</label><input id="sharelink" type="text" name="embed" value="<URL>" /></form></div>',
		'services' : sharingServicesList
	};
 
	if (settings) $.extend(config, settings);
	/**
	 * Wrap function with default arguments.
	 * Usage:
	 * var f_orig = function(a, b){ ... };
	 * var a = 1;
	 * var b = 2;
	 * var f_wrapped = Globa.wrapFunc(f_orig, a);
	 * 
	 * f_wrapped(b); // this execute f_orig and pass a as first argument and b as second 
	 * 
	 * @param {Function} func function to wrap
	 * @return {Function} wrapped function
	 */
	var wrapFunc = function(func){
			var makeArray = Array.prototype.slice;
			var wrapArguments = makeArray.call(arguments, 1);					
			return function(){ return func.apply(this, wrapArguments.concat(makeArray.call(arguments))); };							
	};	
		
	var templateItem = function(tpl, source, destination){	
		return tpl.replace(new RegExp('[\<]' + source + '[\>]', 'g'), destination);
	};
	
	var template = function(tpl, source){
		for(var i in source){
			tpl = templateItem(tpl, i, source[i]);
		}
		return tpl;
	}
	
	/**
	 * This method display a layer with social networks list
	 */ 
	var displaySocial = function(url, title, e){
	    var prefix = "http://";
	    if(url.substring(0, prefix.length) != prefix){
	        url = 'http://' + location.host + url;
	    }
	    
		var offset = $(this).offset();
		var result = [template(config.topTemplate, { 'URL' : url, 'TITLE' : title })];
		for(var s in config.services){
			var services = config.services[s];
			result.push(template(config.itemTemplate, {
				'URL' : templateItem(services.url, 'URL', url), // build sharing url
				'TITLE' : services.title,
				'ICON' : services.icon
			}));
		}
		
		result.push(template(config.bottomTemplate, { 'URL' : url, 'TITLE' : title }));
		
		var obj = $(result.join("\n"));	
		obj.css({
			'top' : (offset.top - 50) + 'px',
			'left' : (offset.left - 50) + 'px',
		});
		
		// link text input field
		var input = obj.find('input:text');
		input.click(function(){ $(this).select(); });
		
		// close easyShare
		var close = obj.find('a.sociable-close:first');
		close.click(function(){
			obj.slideUp('fast', function(){
				obj.remove();
			});
		});
		
		$('body').append(obj);
		
		
		// show easyShare
		obj.slideDown('fast', function(){
		
			// if positin of opened easyShare block
			// is not in current window area
			// pick it up
			var bottomPosition = offset.top  + obj.height();
			var windowOffset = $(window).height() + $(window).scrollTop();
			if( bottomPosition > windowOffset){
			
				obj.animate({
					'top' : (offset.top - (bottomPosition - windowOffset ) - obj.height()/2) + 'px'
				}, 'fast');
			}		
		});
				
		return false;	
	}
	
	this.each(function() {
		var o = $(this);
		var url = o.attr('href');
		var title = o.attr('title');
		o.click(wrapFunc(displaySocial, url, title));
	});
 
	return this; 
	};
 })(jQuery);
 
