$(function() {
	$('#impact-box .button-share').live('click', function() {
		$('#impact-box .share-box').slideDown();
		return false;
	});
	
	$('.infobox .close').live('click', function() {
		$(this).parents('.infobox:eq(0)').slideUp();
	});
	
	$('.ico-directlink .field').live('click', function() {
		$(this).select();
	});
	
	$('.main-form .row').click(function() {
		$('.main-form .row').removeClass('selected-row');
		$(this).toggleClass('selected-row');
	});
	$('.main-form .row .field').focus(function() {
		$('.main-form .row').removeClass('selected-row');
		$(this).parents('.row:eq(0)').addClass('selected-row');
	});
	
	$('.radio-types .radio-box .radio')
		.live('click', function() {
			$(this).parents('.radio-types:eq(0)').find('.radio-box').removeClass('radio-box-active').find('.radio-content').slideUp();
			$(this).parents('.radio-box').addClass('radio-box-active').find('.radio-content').slideDown();
			
			$(this).parents('.radio-types:eq(0)').find('input').val(parseInt($(this).parents('.radio-box').attr('id')));
			$('#donationtype').html($(this).parents('.radio-box').attr('title'));
		});
	$('.freqtype').change(function() {
			var freq = $(this).find("option:selected").attr('title');
			$(this).parents('.radio-box').attr('title',freq+' recurring donation');
			$('#donationtype').html($('.radio-box-active').attr('title'));
		});
	
	$('#donatesrc')
		.live('keyup', function() {
			$('#donateamt').html($(this).val());
		});
	
	$('#print')
		.live('click', function() {
			$('#receipt').jqprint();
			return false;
		});
		
	$('#fbsl')
		.live('click', function() 
		{
			$('#popup').show();
		
			FB_RequireFeatures(["XFBML"], function()
			{
				FB.Facebook.init("9fe90cb3db79f8e74e4320a6da28b29f", "http://www.createafund.com/xd_receiver.htm");
				FB.Connect.requireSession();
			});
			

			return false;
		});
	
	$('#facebookconnectcomment')
		.live('click', function(e) 
		{
			e.preventDefault();

			RepositionLBComment();	
			FB_RequireFeatures(["XFBML"], function()
			{
				FB.Facebook.init("9fe90cb3db79f8e74e4320a6da28b29f", "http://www.createafund.com/xd_receiver.htm");
				FB.Connect.requireSession(facebookComment);
			});
			return false;
		});
	
	
	
	$('.page-comments')
		.live('click', function() {
		
			if($(this).hasClass('unactive'))
				return false;
			
			var html = $.ajax(
			{url: 'index.php?route=donate/comments&f='+$('#fundid').val()+'&ci='+parseInt($(this).attr('id')), 
			async: false, 
			type:"GET"}).responseText;
			
			$('#comments').html(html);

			return false;
		});
	$('.page-donations')
		.live('click', function() {
		
			if($(this).hasClass('unactive'))
				return false;
			
			var html = $.ajax(
			{url: 'index.php?route=donate/recentdonations&f='+$('#fundid').val()+'&ci='+parseInt($(this).attr('id')), 
			async: false, 
			type:"GET"}).responseText;
			
			$('#recentdonations').html(html);

			return false;
		});
	
	
		
	if($('#comments').length > 0 ) 
	{
		var html = $.ajax(
		{url: 'index.php?route=donate/comments&f='+$('#fundid').val()+'&ci=0', 
		async: false, 
		type:"GET"}).responseText;
		
		$('#comments').html(html);
	}
	
	if($('#recentdonations').length > 0 ) 
	{
		var html = $.ajax(
		{url: 'index.php?route=donate/recentdonations&f='+$('#fundid').val()+'&ci=0&embed='+$('#embed').val(), 
		async: false, 
		type:"GET"}).responseText;
		
		$('#recentdonations').html(html);
	}
	
	
	$('.submit-facebook')
		.live('click', function() {
		
			//alert($('fbname').html());
		
			$('#commentpicurl_fb').val($('facebookpreview img').attr('src'));
			$('#commentfirstname_fb').val($('fbname').html());

			return false;
		});
		
	//ORG DONATIONS
	$('.orgdonate')
		.live('click', function() {
		
			//find sibling money
			var amt=$(this).parents().eq(0).find('.orgdonateamt').val();
			if(amt=="" || amt<1 || amt=='undefined' || amt==null)
			{
				alert('Please enter the amount  you wish to give.'+amt);
				return false;
			}
			
			window.location = $(this).attr('href')+'&amt='+amt;

			return false;
		});
	$('.orgdonate2')
		.live('click', function() {
		
			//find sibling money
			var amt=$('.fieldb').val();
			
			window.location = $('.hidden').val()+'&per='+amt;

			return false;
		});
		
	//UPDATES
	$('.show-all-updates')
		.live('click', function() {
		
			$('.hidden-update').slideToggle();
			$('.show-all-updates').toggle();

			return false;
		});
	
	
});

function facebookComment()
{
	$('#facebookcomment').show();
	$('#nonfacebook').hide();
	
	var uid=FB.Facebook.apiClient.get_session().uid;
	
	$('#fbname').html("<fb:name uid='"+uid+"' linked='false' useyou='false' capitalize='false' shownetwork='false'></fb:name>");
	$('#facebookpreview').html("<fb:profile-pic uid='"+uid+"' size='thumb'></fb:profile-pic>");
	$('#facebookid').val(uid);
	
	FB.XFBML.Host.parseDomTree(); 
}                    

function RepositionLB()
{
	clearTimeout(fb_position);
	if($('#RES_ID_fb_pop_dialog_table').length>0)
	{
		$('#RES_ID_fb_pop_dialog_table').css('top','50px');	
	}
	else
	{
		fb_position =setTimeout('RepositionLB()',250);
	}
}

function RepositionLBComment()
{
	clearTimeout(fb_position);
	if($('#RES_ID_fb_pop_dialog_table').length>0)
	{
		
		$('#RES_ID_fb_pop_dialog_table').css('top',Math.max(20,$.getDocHeight()-800)+'px');
		
	}
	else
	{
		fb_position =setTimeout('RepositionLBComment()',250);
	}
}

var fb_position = null;