/* This script is based on mooSocialize */
window.addEvent('domready',function(){

	$$('.SocialShare, .SocialShareFooter').each(function(SocialButton, i) {
	
		SocialButton.addEvent('click',function(e){
			
			if(document.getElementById('containerx') == null){
		
				var top;
				
				if (SocialButton.className == 'SocialShareFooter') {
					top	= this.getCoordinates().top.toInt() - 116;
				} else {
					top	= this.getCoordinates().top.toInt();
				}
		
				var height = this.getCoordinates().height.toInt();
				var left = this.getCoordinates().left.toInt() - 163;
				var pos = top+height+6;
				var div = new Element('div');
				div.setStyle('top',pos);
				div.setStyle('left',left);
				div.addClass('soc_container');
				div.setAttribute('id','containerx');
				div.injectInside(document.body);
				
				$(div).addEvent('click',function(){ this.remove(); })
				$(div).addEvent('blur',function(){ this.remove(); })
				
				// close button
				var closeme = new Element('img');
				closeme.src="/wp-content/plugins/wp_mooS/images/close.gif";
				closeme.setAttribute('id','close');
				closeme.injectInside(div);
				closeme.addEvent('click',function(){ div.remove(); })
				
				var title= document.title;
				var url  = this.href;
			
				var left = new Element('div');
				left.addClass('soc_left');
				left.injectInside(div);
				
				// del.icio.us
				var img = new Element('img'); img.alt = 'send to del.icio.us';
				img.src = '/wp-content/plugins/wp_mooS/images/delicious.png'; img.injectInside(left);

				var myLink = new Element('a');
				myLink.href  = 'http://del.icio.us/post?url='+encodeURIComponent(url)+'&title='+title;
				myLink.title = 'send to del.icio.us'; myLink.innerHTML = 'del.icio.us';
				myLink.injectInside(left);
				
				// digg.com
				var img = new Element('img'); img.alt = 'send to digg';
				img.src = '/wp-content/plugins/wp_mooS/images/digg.png'; img.injectInside(left);
				
				var myLink = new Element('a');
				myLink.href  = 'http://digg.com/submit?phase=2&url='+encodeURIComponent(url)+'&title='+title;
				myLink.title = 'send to del.icio.us'; myLink.innerHTML = 'digg';
				myLink.injectInside(left);
				
				// reddit.com
				var img = new Element('img'); img.alt = 'send to reddit.com';
				img.src = '/wp-content/plugins/wp_mooS/images/reddit.png'; img.injectInside(left);
				
				var myLink = new Element('a');
				myLink.href  = 'http://reddit.com/submit?url='+encodeURIComponent(url)+'&title='+title;
				myLink.title = 'send to reddit'; myLink.innerHTML = 'reddit';
				myLink.injectInside(left);
				
				// yahoo.com
				var img = new Element('img'); img.alt = 'send to yahoo.com';
				img.src = '/wp-content/plugins/wp_mooS/images/im_yahoo.gif'; img.injectInside(left);
				
				var myLink = new Element('a');
				myLink.href  = 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u='+encodeURIComponent(url)+'&t='+title
				myLink.title = 'send to yahoo'; myLink.innerHTML = 'yahoo';
				myLink.injectInside(left);
				
				// put in right side container	
				var right = new Element('div');
				right.addClass('soc_left');
				right.injectInside(div);
				
				// stumpleupon.com
				var img = new Element('img');
				img.alt = 'send to stumbleupon';
				img.src = '/wp-content/plugins/wp_mooS/images/stumbleupon.png';
				img.injectInside(right);
				
				var myLink = new Element('a');
				myLink.href  = 'http://www.stumbleupon.com/refer.php?url='+encodeURIComponent(url)+'&title='+title;
				myLink.title = 'send to stumbleupon';
				myLink.innerHTML = 'stumbleupon';
				myLink.injectInside(right);
				
				// google.com
				var img = new Element('img');
				img.alt = 'send to google';
				img.src = '/wp-content/plugins/wp_mooS/images/google.png';
				img.injectInside(right);
				
				var myLink = new Element('a');
				myLink.href  = 'http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk='+encodeURIComponent(url)+'&title='+title;
				myLink.title = 'send to google';
				myLink.innerHTML = 'google';
				myLink.injectInside(right);
				
				// ask.com
				var img = new Element('img');
				img.alt = 'send to ask.com';
				img.src = '/wp-content/plugins/wp_mooS/images/ask.png';
				img.injectInside(right);
				
				var myLink = new Element('a');
				myLink.href  = 'http://mystuff.ask.com/mysearch/QuickWebSave?v=1.2&t=webpages&title='+title+'&url='+encodeURIComponent(url);
				myLink.title = 'send to ask';
				myLink.innerHTML = 'ask';
				myLink.injectInside(right);
				
				// facebook.com
				var img = new Element('img');
				img.alt = 'send to facebook.com';
				img.src = '/wp-content/plugins/wp_mooS/images/facebook.png';
				img.injectInside(right);
				
				var myLink = new Element('a');
				myLink.href  = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(url)+'&t='+title;
				myLink.title = 'send to facebook';
				myLink.innerHTML = 'facebook';
				myLink.injectInside(right);
				
				// fade in
				div.effect('opacity',{duration:500, wait:true, transition:Fx.Transitions.linear }).chain(function(){ $$('.soc_left').setStyle('visibility','visible'); }).start(0,1);	
				
				// open new window via ajax
				$$('.soc_left a').addEvent('click',function(e){
					e = new Event(e).stop();
					window.location = this.href;
				});

			}else{
				$('containerx').effect('opacity', { duration:300, wait:true, transition:Fx.Transitions.linear}).start(1,0);
				$('containerx').remove();
			}
		});
		
	});
})
