/*
	js for the share panel
	Steve 10th August 09
	
	12th August
	- Utilized callback functions to perform smoother transition and effects
*/

$(document).ready(function() {

	var a = {
				'Del.icio.us':{'url':'http://del.icio.us/post?url=','icon':'bookmark-delicious.gif'},
				'Digg'		:{'url':"http://www.digg.com/submit?url=",'icon':'bookmark-digg.gif'},
				'Reddit'	:{'url':'http://www.reddit.com/submit?url=','icon':'bookmark-reddit.gif'},
				'Facebook'	:{'url':'http://facebook.com/sharer.php?u=','icon':'bookmark-facebook.gif'},
				'StumbleUpon':{'url':"http://www.stumbleupon.com/submit?url=",'icon':'bookmark-stumbleupon.gif'},
				'Newsvine'	:{'url':'http://www.newsvine.com/_tools/seed&save?u=','icon':'bookmark-newsvine.gif'},
				'Technorati':{'url':"http://technorati.com/faves?add=",'icon':'bookmark-technorati.gif'},
				'Yahoo! My Web':{'url':'http://myweb2.search.yahoo.com/myresults/bookmarklet?u=','icon':'bookmark-yahoo.gif'},
				'Twitter'	:{'url':'http://twitter.com/home?status=','icon':'bookmark-twitter.gif'},
				'Google'	:{'url':"http://google.com/bookmarks/mark?op=edit&bkmk=",'icon':'bookmark-google.gif'}
			};
			
	// Buttons
	var s = '<ul>';
	var i=0,split=(a.length/2);
	for( var title in a) if(a.hasOwnProperty(title)){
		if(i++&&((i-1)%5)===0){
			s += '</ul><ul>';
		}
		s += '<li><a href="'+a[title]['url']+'" target="_blank"><img src="/static/images/share/'+a[title]['icon']+'" alt="'+title+'"/> '+title+'</a></li>';
	}
	s +='</ul>';
	
	// FORM
	var form=('<form method"post"><input type="text" value="Forward email address" name="targetemail"/><input type="text" value="Your email address" name="email"/><textarea name="message">Message</textarea><button type="submit">SEND</button></form>');

	// Add share buttons to each of the posts
	$('.post').each(function(){
		// Get the title
		var objectId = null;
		try{
			objectId = this.id.match(/[0-9]+/)[0];
		}catch(e){}

		try{
			var url = $('h3 a',this)[0].href, title = $('h3 a',this).html();
		}catch(e){
			var url = window.location.href, title = $('h3',this).html();
		}
		
		$('<button class="share"></button>').toggle(function(){
			// create the area below and expand thetext
			$('<div class="sharepanel"/>').insertAfter(this).animate({height:'160px'}, function(){
				$panel = $(this);
				$(this).append(s + form).children().hide().each(function(){$(this).fadeIn('fast');});

				// Append the current pages URL to each of the a[hrefs]			
				$panel.find('li a').each(function(){
					this.href += encodeURIComponent(url) +"&title="+encodeURIComponent(title);
				});
				// Clear default messages
				$panel.find('input').blur(function(){
					if(!is_email(this.value)){
						$(this).addClass('error');
					}else{
						$(this).removeClass('error');
					}
				});

				$panel.find('input,textarea').each(function(){
					this.alt = this.value;
					$(this).click(function(){if(this.alt===this.value){this.value=''}}).blur(function(){if(this.value==='')this.value=this.alt;});
				});
				
				
				// Add form controls
				$panel.find('form').submit(function(){
					var valid = true;
					$('input',this).each(function(){
						if(!is_email(this.value)){
							valid = false;
							$(this).addClass('error');
						}
					});
					if(!valid){
						return false;
					}
					var form = this;
					$.ajax({ 
						type:'POST',
						url:'/home/share?_frameset=true',
						data:{
							ajax: 1,
							m: 'send_to_friend',
							c: 'offers',
							email_friend: $("input[name=targetemail]",this).val(),
							email_your: $("input[name=email]",this).val(),
							msg: $("textarea",this).val(),
							objectId: objectId,
							url: url,
							title: title},
						success:function(){$(form).html('Thank you');},
						error:function(){$(form).html('Whoops something went wong');}
					});

					return false;
				});			
			});
			
		},function(){
			// fade out the form
			$(this).siblings('.sharepanel').slideUp('fast',function(){
				$(this).remove();
			});
		}).appendTo(this);
	});

	function is_email(email) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   if(reg.test(email) == false) {
		return false;
	   }
	   else {
		return true;
	   }
	}		
	
});



