$(function()
{
	var global_id = null;
	
	$('#see_all_hidden_action_on_profile').click(function()
	{
		var url = $(this).attr('href');
		$.get(url, function(result)
		{
//			if(result){ $('table.action_tracker').html(result); }
			if(result){ $('table.action_tracker tbody').html(result); }
		});
		return false;
	});

	$.fn.outerHTML = function(){ return $('<div>').append( this.eq(0).clone() ).html(); };
	/*
	$('.dialog').dialog(
	{
		autoOpen		: false,
		bgiframe		: false,
		width			: 600,
//		height			: 600,
		modal			: true,
		closeOnEscape	: true,
		draggable		: false,
		resizable		: false,
		
		buttons			:
		{	
			'Yes Please' 			: function(){ $(this).children('form').submit(); }, 
			'No Thanks' 			: function(){ $(this).dialog('close'); }
		}
	});
	*/
	$('#decide_what, #decide_what_class').dialog(
	{
		autoOpen		: false,
		bgiframe		: false,
		width			: 550,
//		height			: 600,
		modal			: true,
		closeOnEscape	: true,
		draggable		: false,
		resizable		: false,
		
		buttons			:
		{	
			'Yes Please' 			: function(){ $(this).dialog('option', 'buttons', {}); location.href = '/actions/view/' + global_id + '/?open_comment=1'; },
			'No Thanks' 			: function(){ $(this).dialog('close'); }
		}
	});

	
	if($('#post_comment').length > 0){ var buttons = $('#post_comment').dialog('option', 'buttons'); }
	
	function action_comment_load(id)
	{
		$('#post_comment').dialog('option', 'width', 550);
		$('#post_comment').dialog('option', 'buttons', 
		{
			'Post my comment' : buttons['Save my comment'],
			'Post and see the action' : function()
			{
				$('#id_redirect_to_action').val(1);
				if(IS_FACEBOOK_USER && !FACEBOOK_PUBLISH_STREAM)
				{
					FB.Facebook.apiClient.users_hasAppPermission('publish_stream', function(result, ex)
					{
						if(result == 1){ $('#post_comment form').submit(); }
						else
						{
							FB.Connect.showPermissionDialog('publish_stream', function(permission, e)
							{
								$.get('/users/facebook_permission/', {'permission': (permission == 'publish_stream' ? 1 : 2)}, function(ans)
								{
									if(ans)
									{
										$('.dialog').dialog('close');
										$('#post_comment form').submit();
									}
								});
							});
						}
					});
				}
				else
				{
					$('#post_comment form').unbind('submit.dialog').bind('submit.dialog', function(ev)
					{
						if(ev.result === undefined){ $('.dialog').dialog('close'); }
					}).submit();
				}
			},
			'Cancel' : buttons['Cancel']
		});
		$('#post_comment').dialog('open'); 
		
		$.get('/actions/ajax_get/'+id+'/?hide_loader', {}, function(res)
		{
			if(res)
			{
				
				$('#id_action_id').val(id);
				$('#action_info').addClass('active');
				$('#action_image').html(res.action_image);
				$('#action_button').html(res.action_button);
				$('#action_number').html(res.action_number);
				$('#action_title').html(res.action_title);
				$('#action_user').html(res.action_user);
				$('#action_description').html(res.action_description);
	
			}
		}, 'json');
	}

	if(typeof load_comment != 'undefined'){ action_comment_load(parseInt(load_comment)); }
	
	$('.action_tracker .ajax_done').live('click', function()
	{
		var id = global_id = $(this).attr('id').substring(10);
		var elem = $(this);
		var $action_count = $('#ajax_count_increment_' + id);
		
		if(elem.data('show_more_feeds') == 1){ return false; }
		
		elem.data('show_more_feeds', 1);
		
//		overlay.show();
		$.get('/actions/done/' + id + '/?hide_loader', {}, function(data)
		{
			elem.data('show_more_feeds', 0);
			if(data)
			{
				/*
				if(data.alert_id)
				{
					$('#action_alert_message').html(data.message);
					$('#action_alert').dialog('option', 'buttons', 
					{'Ok' : function()
						{ 
							$(this).dialog('close'); 
						//	location.href = '/actions/view/' + global_id + '/?open_comment=1';
							action_comment_load(id);
						} 
					});
					$('#action_alert').dialog('open');
				}
				else
				{
				//	location.href = '/actions/view/' + global_id + '/?open_comment=1';
				}
				*/
				action_comment_load(id);
			}
			else { alert('Check your connection!'); }
			
			if($action_count.length>0)
			{
				var new_count = parseInt($action_count.html()) + 1;
				$action_count.html(new_count);
			}
			
			var $update_all = $('#id_all_done');
			if($update_all.length>0)
			{
				var all_done = parseInt($update_all.html()) + 1;
				$update_all.html(all_done);
				if (all_done == 2){ $('#id_all_done_multiple').html('s'); }
			}

			var $header_update_all = $('#id_header_all_done');
			if($header_update_all.length>0)
			{
				var header_all_done = parseInt($header_update_all.html()) + 1;
				$header_update_all.html(header_all_done);
				if (header_all_done == 2){ $('#id_header_all_done_multiple').html('s'); }
				header_pulsate($header_update_all);
			}
			
		}, 'json');

		return false;
	});
	
	$('.action_tracker .ajax_done_increment').live('click', function()
	{
		var id = global_id = $(this).attr('id').substring(20);
		var elem = $(this);
		var $action_count = $('#ajax_count_increment_' + id);
		if(elem.data('show_more_feeds') == 1){ return false; }
		
		elem.data('show_more_feeds', 1);
		
		if($action_count.length>0){ $action_count.parent('.ajax-counts').addClass('loading'); }
		//overlay.show();
		$.get('/actions/done/' + id + '/?hide_loader', {}, function(result)
		{
			elem.data('show_more_feeds', 0);
			if(result)
			{
				var $update_elem = $('#done_' + id);
				if($update_elem.length>0)
				{
					var new_done = parseInt($update_elem.html()) + 1;
					$update_elem.html(new_done);
					if (new_done == 2){ $('#multiple_' + id).html('s'); }
				}
				else 
				{
					var $never_done = $('#done_text_' + id + '.never_done');
					if($never_done.length>0)
					{
						$never_done.removeClass('never_done');
						$never_done.html('I\'ve done this <span id="done_'+ id +'">1</span> time<span id="multiple_'+ id +'"></span>');
					}
				}
				
				var $update_all = $('#id_all_done');
				if($update_all.length>0)
				{
					var all_done = parseInt($update_all.html()) + 1;
					$update_all.html(all_done);
					if (all_done == 2){ $('#id_all_done_multiple').html('s'); }
				}
				
				if($action_count.length>0)
				{
					var new_count = parseInt($action_count.html()) + 1;
					$action_count.html(new_count);
				}
				
				var $header_update_all = $('#id_header_all_done');
				if($header_update_all.length>0)
				{
					var header_all_done = parseInt($header_update_all.html()) + 1;
					$header_update_all.html(header_all_done);
					if (header_all_done == 2){ $('#id_header_all_done_multiple').html('s'); }
					header_pulsate($header_update_all);
				}
				
				if(result.alert_id)
				{
					$('#action_alert_message').html(result.message);
					$('#action_alert').dialog('option', 'buttons', 
					{	
						'Yes Please' 			: function()
						{ 
							$(this).dialog('option', 'buttons', {}); 
							$(this).dialog('close'); 
						//	location.href = '/actions/view/' + global_id + '/?open_comment=1';
							action_comment_load(id);
						},
						'No Thanks' 			: function(){ $(this).dialog('close'); }
					});					
					$('#action_alert').dialog('open');
				}
			}
			else { alert('Check your connection!'); }
			if($action_count.length>0){ $action_count.parent('.ajax-counts').removeClass('loading'); }
		}, 'json');

		return false;
	});

	$('#sidebar .ajax_like').click(function()
	{
		var id = $(this).attr('id').substring(10);
		var $element = $(this);
		
//		overlay.show();
		$.get('/actions/like/' + id + '/?hide_loader', function(data)
		{
			if (data)
			{
				$element.fadeOut();
				$('<tr>'
				+	'<td class="image_column">'
				+		'<a class="button remove_action" href="/users/remove_action/'+ id +'/">Remove</a>' 
				+ 		($element.attr('rev') ? '<a href="' + $element.attr('href') +'" class="image_link"><img src="/public/files/actions/' + $element.attr('rel') + '/50x50/' + id + '.' + $element.attr('rev') + '" class="image" /></a>' : '') 
				+	'</td>'
				+	'<td class="title">' + $element.text().split(' ', 1) + '<a class="title" href="' + $element.attr('href') +'" title="' + $element.text() +'">' + $element.attr('title') + '</a>'
				+		'<div id="done_text_'+ id +'" class="never_done">'
				+			'I haven\'t done this yet'
				+		'</div>'
				+	'</td>'
				+	'<td class="buttons">'
				+		'<a class="button ajax_done" id="ajax_done_'+id+'" href="#">I\'ve done this</a>'
				+		'<div class="ajax-counts"><span class="ajax_count_increment" id="ajax_count_increment_'+id+'">0</span></div>'
				+		'<a class="button ajax_done_increment" id="ajax_done_increment_'+id+'" href="#">+</a>'
				+	'</td>'
				+'</tr>').prependTo($('.action_tracker')).hide().fadeIn();
				
				if($('#header_actions').children('li').length == 6){ $('#header_actions').children('li:last').fadeOut().remove(); }
				$('<li>'
				+	'<a href="' + $element.attr('href') +'" title="' + $element.text() +'">'
				+ 		'<img src="/public/files/actions/' + $element.attr('rel') + '/50x50/' + id + '.' + $element.attr('rev') + '" />'
				+	'</a>'
				+'</li>').prependTo($('#header_actions')).hide().fadeIn();
				
				$('#no-tracker-text').slideUp();
				
				var $update_all = $('#id_all_track');
				if($update_all.length>0)
				{
					var all_done = parseInt($update_all.html()) + 1;
					$update_all.html(all_done);
					if (all_done == 2){ $('#id_all_track_multiple').html('s'); }
				}
				
				var $header_update_all = $('#id_header_all_track');
				if($header_update_all.length>0)
				{
					var header_all_done = parseInt($header_update_all.html()) + 1;
					$header_update_all.html(header_all_done);
					if (header_all_done == 2){ $('#id_header_all_track_multiple').html('s'); }
					header_pulsate($header_update_all);
				}
				
				header_bounce();
			}
		});
						
		return false;
	});
	
	if($('#id_photo').length)
	{
		$('#id_photo').uploadify({
			'uploader'  	: __rootPath+'back/uploadify.swf',
			'scriptData'	: {session_id: session_id, 'upload_croppable[photo]': 1},
			//'script'    	: __basePath+'users/profile_picture/',
			//'script'    	: __basePath+'users/profile/edit/' + FE_LOGGED_USER_ID + '/',
			'script'    	: __basePath+'users/edit_photo/',
			'fileDataName'	: 'photo',
			'fileDesc'		: 'Images',
			'fileExt'		: '*.jpg;*.jpeg;*.png;*.gif',
			'cancelImg' 	: __rootPath+'back/styles/icons/delete.png',
			'auto'			: true,
			'multi'			: false,
			'buttonText'	: __globals.browse_button,
			'simUploadLimit': 1,
			'sizeLimit'		: 4194304,
			'onSelect'		: function(e){ $(e.target).parents('.croppable').find('.crop-loader').show(); },
			'onComplete'	: function(e, id, file, responce, data){ $('body').append(responce); $('#id_photo').uploadifyClearQueue(); }
		});
		//$('#id_upload_croppable_photo_').click(function(){ $('#id_photo').uploadifyUpload(); return false; });
		$('.button.upload').hide();
	}
	
	$('.delete_object_file').live('click', function()
	{
		var $elem = $(this);
		var url = $elem.attr('href')+'/';
		
		$.get(url, function(){ var p = $elem.parents('tr'); p.fadeOut(function(){p.remove()}); });
		return false;
	});
	
	$('#id_publish_stream').click(function()
	{
		var $e = $(this);
		if($e.attr('rel') == 1)
		{
			FB.Connect.showPermissionDialog('publish_stream', function(result, ex)
			{
				if(result == 'publish_stream')
				{
					$.get('/users/facebook_permission/', {'permission': 1}, function(ans)
					{ 
						if(ans == 1)
						{ 
							$e.attr('rel', '2');
							$e.html('Disallow publishing to my feed');
							$('#publish_stream_text').html('You are currently allowing publishing on your Facebook feed.');
							$('#publish_stream_text').parent('p').removeClass('not_linked').addClass('linked');
						} 
					}); 
				}
			});
		}
		else 
		{ 
			$.get('/users/facebook_permission/', {'permission': 2}, function(ans)
			{ 
				if(ans == 2)
				{ 
					$e.attr('rel', '1');	
					$e.html('Allow publishing to my feed');
					$('#publish_stream_text').html('You are currently not allowing publishing on your Facebook feed.'); 
					$('#publish_stream_text').parent('p').removeClass('linked').addClass('not_linked');
				} 
			}); 
		}
		
		return false;
	});
	
	$('#dialog_remove_action').dialog(
	{
		autoOpen		: false,
		bgiframe		: false,
		width			: 550,
//		height			: 600,
		modal			: true,
		closeOnEscape	: true,
		draggable		: false,
		resizable		: false,
		
		buttons			:
		{	
			'Yes Please' 			: function(){ $(this).dialog('option', 'buttons', {}); var url_link = url; url = null; location.href = url_link; }, 
			'No Thanks' 			: function(){ $(this).dialog('close'); }
		}
	});
	
	var url = null;
	$('.action_tracker .remove_action').click(function()
	{
		url = $(this).attr('href');
		$('#dialog_remove_action').dialog('open');
		
		return false;
	});
	
	$('.facebook_disconnect_dialog').dialog(
	{
		autoOpen		: false,
		bgiframe		: false,
		width			: 550,
//		height			: 600,
		modal			: true,
		closeOnEscape	: true,
		draggable		: false,
		resizable		: false,
		
		buttons			:
		{	
			'Disconnect' 			: function(){ $(this).children('form').submit(); $(this).dialog('option', 'buttons', {}); }, 
			'Cancel'	 			: function(){ $(this).dialog('close'); }
		}
	});
	
	$('#fb_disconnect').click(function()
	{
		$('.facebook_disconnect_dialog').dialog('open');
		return false;
	});
	
	$('.avatar li input[type=radio]').click(function(){ $('.jcrop-holder').hide(); });
	
});