(function($) {
	$(function() {
		$('.share_container, .attach_container').show();

		// External share buttons.
		$('.share_facebook').click(function(event) {
			
			uri='http://www.facebook.com/sharer.php?u='+uri+'&t='+title;
		
			if (window.open(uri,'sharer','toolbar=0,status=0,resizable=1,width=626,height=436')) {
				event.preventDefault();
			}
			$.modal.close();
		});
		
		$('.share_myspace').click(function(event) {
			var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto&t=' + title + '&u=' + uri;
			if (window.open(targetUrl)) {
				event.preventDefault();
			}
			$.modal.close();
		});
		
		$('.share_delicious').click(function(event) {
			if (window.open('http://delicious.com/save?v=5&noui&jump=close&url=' + uri + '&title=' + title, 'delicious','toolbar=no,width=550,height=550')) {
				event.preventDefault();
			}
			$.modal.close();
		});
		
		
		$('.share_bebo').click(function(event) {
			if (window.open('http://www.bebo.com/c/share?Url=' + uri + '&Title='+ title, 'sharer', 'toolbar=0,status=0,width=626,height=436')) {
				event.preventDefault();
			}
			$.modal.close();
		});
		
		$('.share_posterous').click(function(event) {
			if (window.open('http://posterous.com/bookmarklet/iframe_contents?linkto=' + uri + "&title=" + title, 'posterous', 'toolbar=0,status=0,width=590,height=610')) {
				event.preventDefault();
			}
			$.modal.close();
		});

		$(".pass_it_on, .share_email").click(function(e) {
			e.preventDefault();
			$.modal.close();
			$(".pass_it_on_popup").modal({
				closeHTML: "<div class='popup_close'></div>",
				opacity: 25
			});
			}
		);
		
		$('.share_target').click(function(event) {
			var id = $(this).attr('data-id');
			var type = $(this).attr('data-type');
			var target_url = $(this).attr('data-url');
			var name = $(this).text();
			$('.share_form input[name=entry_id]').val(id);
			$('.share_form input[name=attachment]').val(current_id);
			$('.share_form input[name=comment]').val(screen_name + " shared with the " + type);
			
//			$('.share_form input[type=submit]').click();
			var data = $('.share_form').serialize();
			var url = $('.share_form').attr('action');
			
			$('.share_popup .internal, .share_popup .external').hide();
			$('.share_popup .popup_inner').addClass('loading');
			$.post(url, data, function(data, textStatus) {
				$('.share_popup .popup_inner').removeClass('loading');
				$('.share_popup .confirmation .target').attr('href', target_url).text(name);
				$('.share_popup .confirmation').show();
			});
		});
		$('.share_target').mouseover(function(){
			$(this).addClass('share_hover');
		});
		$('.share_target').mouseout(function(){
			$(this).removeClass('share_hover');
		});
		

		$('.attach_src').click(function(event) {
			var id = $(this).attr('data-id');
			$('#comment_form input[name=attachment]').val(id);
			var attach_name = $(this).text();

			if (attach_name.length > 23) {
				attach_name = $.trim(attach_name.substr(0, 20)) + "...";
			}
			$('.current_attachment').text(attach_name);
			$.modal.close();
			$('.attach_remove').show();
		});

		$('.current_attachment').data('empty_text', $('.current_attachment').text());

		$('.attach_remove').click(function(event) {
			$('#comment_form').find('input[name=attachment]').val('');
			$('.current_attachment').text($('.current_attachment').data('empty_text'));
			$('.attach_remove').hide();
		});
	});
})(jQuery);


