(function(jQuery) {
	jQuery.fn.easyShare = function(options) {
		// Initialisation des variables sous forme de tableaux afin de gérer plusieurs animation sur une même page
		var settings = Array();
		var sites = Array();
		var element;
		var position;		
		var elementH;
		var elementW;
		var url;
		var title;
		
		// Paramètres des sites
		sites = {
			'delicious': {
				name: 'del.icio.us',
				target: 'http://del.icio.us/post?url={url}&amp;title={title}',				
				icone: 'delicious_16.png',
				icone_big: 'delicious_32.png'
			},			
			'digg': {
				name: 'Digg',
				target: 'http://digg.com/submit?phase=2&amp;url={url}&amp;title={title}',				
				icone: 'digg_16.png',
				icone_big: 'digg_32.png'
			},			
			'email': {
				name: 'E-mail',
				target: 'mailto:?subject={title}&body={url}',				
				icone: 'email_16.png',
				icone_big: 'email_32.png'
			},
			'facebook': {
				name: 'Facebook',
				target: 'http://www.facebook.com/sharer.php?u={url}&amp;t={title}',				
				icone: 'facebook_16.png',
				icone_big: 'facebook_32.png'
			},
			'favoris': {
				name: 'Favoris',
				target: '',				
				icone: 'bookmarks_16.png',
				icone_big: 'bookmarks_32.png'
			},
			'google': {
				name: 'Google',
				target: 'http://www.google.com/bookmarks/mark?op=edit&amp;bkmk={url}&amp;title={title}',				
				icone: 'google_16.png',
				icone_big: 'google_32.png'
			},
			'live': {
				name: 'Live',
				target: 'https://skydrive.live.com/sharefavorite.aspx/.SharedFavorites?url={url}&amp;title={title}',				
				icone: 'live_16.png',
				icone_big: 'live_32.png'
			},
			'myspace': {
				name: 'MySpace',
				target: 'http://www.myspace.com/Modules/PostTo/Pages/?c={url}&amp;t={title}',				
				icone: 'myspace_16.png',
				icone_big: 'myspace_32.png'
			},			
			'scoopeo': {
				name: 'Scoopeo',
				target: 'http://www.scoopeo.com/scoop/new?newurl={url}&amp;title={title}',				
				icone: 'scoopeo_16.png',
				icone_big: 'scoopeo_32.png'
			},
			'twitter': {
				name: 'Twitter',				
				target: 'http://twitter.com/timeline/home?status=RT+@maviedemaman+:+{url}',
				icone: 'twitter2_16.png',
				icone_big: 'twitter_32.png'
			},
			'yahoo': {
				name: 'Yahoo!',
				target: 'http://bookmarks.yahoo.com/myresults/bookmarklet?u={url}&amp;t={title}',				
				icone: 'yahoo_16.png',
				icone_big: 'yahoo_32.png'
			},
			'yahoobuzz': {
				name: 'Yahoo! Buzz',
				target: 'http://fr.buzz.yahoo.com/buzz?targetUrl={url}&amp;t={title}',				
				icone: 'yahoobuzz_16.png',
				icone_big: 'yahoobuzz_32.png'
			}		
		};
		
		// Paramètres par défaut
		settings = {			
			className: '',
			title: 'easyShare this with...',
			sites: [ 'delicious', 'digg', 'email', 'facebook', 'favoris', 'google', 'live', 'myspace', 'scoopeo', 'twitter', 'yahoo' ],
			imagePath: '',
			mode: 'normal',
			hover: false
		};
		
		if(options) {
			jQuery.extend(settings, options);
		}
	
		if(settings.mode == 'big') {
			var elementLien = $(this);
			var url = elementLien.attr('href') != ''?elementLien.attr('href'):window.location;			
			var alt = elementLien.attr('alt');
			var title = elementLien.attr('title') != ''?elementLien.attr('title'):'Maviedemaman.com, tout l\'univers enfant.';
			
			content = '<ul class="easyShareBox">';
			for(key in settings.sites) {
				if(key != 'indexOf') {				
					lien = sites[settings.sites[key]].target.replace(/{url}/, url).replace(/{title}/, title);
					image = '<img src="'+settings.imagePath+sites[settings.sites[key]].icone_big+'" alt="'+sites[settings.sites[key]].name+'" />';				
					lienDisplay = '<a href="'+lien+'" target="blank" title="'+sites[settings.sites[key]].name+'">'+image+'</a>';
					content += '<li class="'+settings.sites[key]+'">'+lienDisplay+'</li>';
				}
			}
			content += '</ul><div class="clear"></div>';
			
			elementLien.after(content);
			elementLien.remove();
		}else {
			$(this).live('click', function() {
				if($('#easyShare').length) {
					$('#easyShare').fadeOut(function() {
						$(this).remove();
						$('body').unbind('click');
					});
				}else {
					element = $(this);
					setContent();
				}			
				
				return false;
			});
		}
		
		// Fermeture auto après un clique
		$('#easyShare li.favoris a, .easyShareBox li.favoris a').live('click', function() {
			if(window.opera) {  
				if ($(this).attr("rel") != ""){ // don't overwrite the rel attrib if already set  
					$(this).attr("rel","sidebar");  
				}  
			} 
			
			if (window.sidebar) { // Firefox 
				window.sidebar.addPanel(title, url,"");  
			} else if( document.all ) { // IE
				window.external.AddFavorite(url, title);  
			} else if(window.opera) { // Opera 7+  		
				return false; // do nothing - the rel="sidebar" should do the trick  
			} else { // Safari, chrome, konqueror et autres navigateurs ne supportant pas l'ajout par javascript
				alert('Veuillez presser "Ctrl+D" ou "Cmd+D" pour Mac afin d\'ajouter cette page à vos favoris');  
			}
			return false;
		});
		
		// Favoris
		$('#easyShare a').live('click', function() {
			$('#easyShare').fadeOut(function() {
				$(this).remove();
				$('body').unbind('click');
			});
		});
		
		setContent = function() {
			// Fermeture quand click à l'exterieur
			// On bind l'event à la volée pour de meilleurs performances
			$('body').click(function() {
				if(!$(this).parents('#easyShare').length) {
					$('#easyShare').fadeOut(function() {
						$(this).remove();
						$('body').unbind('click');
					});
				}
			});
		
			url = $(element).attr('href') != ''?$(element).attr('href'):window.location;
			title = $(element).attr('title') != ''?unescape( encodeURIComponent($(element).attr('title'))):'easyShare';
			paddingTop = $(element).css('padding-top').replace('px');
			paddingBottom = $(element).css('padding-bottom').replace('px');
			elementH = parseInt($(element).height()) + parseInt(paddingTop) + parseInt(paddingBottom);
			elementW = $(element).width();
			position = getPosition($(element));
			
			content = '<div id="easyShare"><div class="title">'+settings.title+'</div><ul>';
			
			for(key in settings.sites) {
				if(key != 'indexOf') {
					lien = sites[settings.sites[key]].target.replace(/{url}/, url).replace(/{title}/, title);
					image = '<img src="'+settings.imagePath+sites[settings.sites[key]].icone+'" alt="'+sites[settings.sites[key]].name+'" />';
					if(settings.mode == 'normal') {
						lienDisplay = image+'<a href="'+lien+'" target="blank">'+sites[settings.sites[key]].name+'</a>';
					}else if(settings.mode == 'compact') {
						lienDisplay = '<a href="'+lien+'" target="blank">'+image+'</a>';
					}else {
						lienDisplay = image+'<a href="'+lien+'" target="blank">'+sites[settings.sites[key]].name+'</a>';
					}
					content += '<li class="'+settings.sites[key]+'">'+lienDisplay+'</li>';
				}
			}				
			
			content += '</ul></div>';
			$('body').append(content);
			
			$('#easyShare').css({					
				top: parseInt(position.y) + elementH,
				left: position.x
			}).fadeIn();
		};
		
		getPosition = function(obj) {
			obj = $(obj).get(0);
			curleft = obj.offsetLeft || 0;
			curtop = obj.offsetTop || 0;
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
			return {x:curleft,y:curtop};
		};

		// Standalone functionnalities
		function easyShare() {

		}
		$.extend(easyShare.prototype, {
			addSite: function(id, name, target, icone) {
				sites[id] = {name: name, target: target, icone: icone};
			}
		});
		$.easyShare = new easyShare();		
	};
})(jQuery);
