$(function()
{	
	$('.users_list li').live('click', function()
	{ 
		if($(this).attr('id') == 'id_everyone')
		{
			$(this).toggleClass('checked').hasClass('checked') ? $(this).siblings().addClass('checked').find(':checkbox').attr('checked', 'checked') : $(this).siblings().removeClass('checked').find(':checkbox').removeAttr('checked');
		} 
		else
		{
			$(this).toggleClass('checked').hasClass('checked') ? $(this).find(':checkbox').attr('checked', 'checked') : $(this).find(':checkbox').removeAttr('checked');
		}
	});
	
//Action Dialog			
	$('.dialog').dialog(
	{
		autoOpen		: false,
		bgiframe		: false,
		width			: 500,
//		height			: 600,
		modal			: true,
		closeOnEscape	: true,
		draggable		: false,
		resizable		: false,
		buttons:		{	
			'Save my comment' 	: function()
			{
				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' 			: function()
			{
				$('#id_title, #id_comment, #id_actions_done input, #id_picture, #id_class_group_id').val('');
				$('ul.users_list input.checkbox, #id_show_anyone input.checkbox').removeAttr('checked');
				$('ul.users_list li').removeClass('checked');
				$('#id_actions_done').hide();
				location.search.match(/comment_(id|reply)=\d+/) ? location.href=location.protocol+'//'+location.hostname+location.pathname : $(this).dialog("close");
				$('.dialog .invalid').removeClass('invalid').find('.error').remove();
			}
		}
	});
	
	$('.dialog_toggle').click(function()
	{ 
		$('#id_done_action_alert').hide();
		$('#post_comment').dialog('open'); 
	})
	.hover(function(){ $(this).addClass("ui-state-hover"); }, function(){ $(this).removeClass("ui-state-hover"); } )
	.mousedown(function(){ $(this).addClass("ui-state-active"); })
	.mouseup(function(){ $(this).removeClass("ui-state-active"); })
	;
//End Action Dialog

//Class Dialog
	$('.dialog_class').dialog(
	{
		autoOpen		: false,
		bgiframe		: false,
		width			: 500,
//		height			: 600,
		modal			: true,
		closeOnEscape	: true,
		draggable		: false,
		resizable		: false,
		buttons:		{	
			'Save my comment' 	: function()
			{
				if(IS_FACEBOOK_USER && !FACEBOOK_PUBLISH_STREAM)
				{
					FB.Facebook.apiClient.users_hasAppPermission('publish_stream', function(result, ex)
					{
						if(result == 1){ $('#post_class_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_class').dialog('close');
										$('#post_class_comment form').submit();
									}
								});
							});
						}
					});
				}
				else
				{
					$('#post_class_comment form').unbind('submit.dialog_class').bind('submit.dialog_class', function(ev)
					{
						if(ev.result === undefined){ $('.dialog_class').dialog('close'); }
					}).submit();
				}
			}, 
			'Cancel' 			: function()
			{
				$('#id_title, #id_comment, #id_actions_done input, #id_picture, #id_class_group_id').val('');
				$('ul.users_list input.checkbox, #id_show_anyone input.checkbox').removeAttr('checked');
				$('ul.users_list li').removeClass('checked');
				$('#id_actions_done').hide();
				location.search.match(/comment_(id|reply)=\d+/) ? location.href=location.protocol+'//'+location.hostname+location.pathname : $(this).dialog("close");
				$('.dialog_class .invalid').removeClass('invalid').find('.error').remove();
			}
		}
	});
	$('.dialog_class_toggle')
		.click(function()
		{ 
			$('#id_class_done_action_alert').hide();
			$('#post_class_comment').dialog('open'); 
			$('#post_class_comment form #id_class_group_id').val(parseInt($(this).attr('id'))); 
		})
		.hover(function(){ $(this).addClass("ui-state-hover"); }, function(){ $(this).removeClass("ui-state-hover"); } )
		.mousedown(function(){ $(this).addClass("ui-state-active"); })
		.mouseup(function(){ $(this).removeClass("ui-state-active"); })
	;
//End Class Dialog
	
//Campaign Dialog
	$('.compaign_dialog').dialog(
	{
		autoOpen		: false,
		bgiframe		: false,
		width			: 800,
//		height			: 600,
		modal			: true,
		closeOnEscape	: true,
		draggable		: false,
		resizable		: false,
		buttons:		{	
			'Save my comment' 	: function()
			{
				$('.compaign_dialog form').unbind('submit.compaign_dialog').bind('submit.compaign_dialog', function(ev)
				{
					if(ev.result === undefined){ $('.compaign_dialog').dialog('close'); }
				}).submit();
			}, 
			'Cancel' 			: function()
			{
				$('#id_title, #id_comment, #id_actions_done input, #id_picture, #id_parent_id').val('');
				$('ul.users_list input.checkbox, #id_show_anyone input.checkbox, #id_actions input.checkbox').removeAttr('checked');
				$('ul.users_list li').removeClass('checked');
				$('#id_actions_done, #id_show_anyone').hide();
				location.search.match(/comment_(id|reply)=\d+/) ? location.href=location.protocol+'//'+location.hostname+location.pathname : $(this).dialog("close");
				$('.compaign_dialog .invalid').removeClass('invalid').find('.error').remove();
			}
		}
	});
	$('.compaign_dialog_toggle, .output_dialog_toggle')
		.click(function(){ $('.compaign_dialog').dialog('open'); return false; })
		.hover(function(){ $(this).addClass("ui-state-hover"); }, function(){ $(this).removeClass("ui-state-hover"); } )
		.mousedown(function(){ $(this).addClass("ui-state-active"); })
		.mouseup(function(){ $(this).removeClass("ui-state-active"); })
	;
//End Campaign Dialog
	
	$('form').each(function(){ new Validator($(this)); });
	
	if ($('.c-anyone :checked').length > 0 || $('.h-parent :hidden').val() > 0){ $('.c-anyone-wrapper').next().show(); }
	
	$('.c-anyone :checkbox').click(function()
	{
		var $el = $(this).parents('.c-anyone-wrapper').next();
		this.checked
			? $el.slideDown().find('input').removeAttr('disabled')
			: $el.slideUp().find('input').attr('disabled', 'disabled')
		;
	});
	
	$(".checklist .checkbox-select").click(function(event)
	{
		var $elem = $(this);
		var block = $elem.attr('rel');
		if($elem.attr('id') == 'id_select_all'){ $elem = $("#id_"+block+".checklist .checkbox-select"); }
		
		$.each($elem, function()
		{
			$(this).parent().addClass("selected");
			var id = $(this).parent().find(":checkbox").attr("checked","checked").val();
			if(id)
			{
				$('#id_'+block+'_'+id+'_done').show();
				$('#id_'+block+'_'+id+'_done'+' input').removeAttr('disabled');
			}
		});
		if(block != 'output_types'){ $('#id_show_anyone').slideDown(); }
		
		if($('#id_all_'+block).length && !$('#id_all_'+block+':checked').length && $elem.parent().parent().find(':checked').not('#id_all_'+block).length == $elem.parent().parent().find(':checkbox').not('#id_all_'+block).length) 
		{
			$('#id_all_'+block).parent().addClass("selected");
			$('#id_all_'+block).attr("checked","checked");
		}
		return false;
	});
	
	$(".checklist .checkbox-deselect").click(function(event)
	{
		var $elem = $(this);
		var block = $elem.attr('rel');
		if($elem.attr('id') == 'id_deselect_all'){ $elem = $("#id_"+block+".checklist .checkbox-deselect"); }
		
		$.each($elem, function()
		{
			$(this).parent().removeClass("selected");
			var id = $(this).parent().find(":checkbox").removeAttr("checked").val();
			if(id)
			{
				$('#id_'+block+'_'+id+'_done').hide();
				$('#id_'+block+'_'+id+'_done'+' input').attr('disabled', 'disabled');
			}
		});
		
		if(block != 'output_types' && !$elem.parent().parent().find(':checked').not('#id_all_'+block).length)
		{
			$('#id_show_anyone').slideUp();
		}

		if($('#id_all_'+block).length && $('#id_all_'+block+':checked').length && $elem.parent().parent().find(':checked').not('#id_all_'+block).length != $elem.parent().parent().find(':checkbox').not('#id_all_'+block).length) 
		{
			$('#id_all_'+block).parent().removeClass("selected");
			$('#id_all_'+block).removeAttr("checked");
		}
		return false;
	});
	
	if($('.checklist :checkbox:checked').length)
	{
		$('.checklist :checkbox:checked').parent().find('.checkbox-select').click();
	}
	
	$('#id_group_id').change(function()
	{
		$.post("/actions/get_users_list/", { group_id: $(this).val() },	function(data){	if(data){ $('ul.users_list').html(data); } });
	});
	
	
	$('.featured.add').live('click', function()
	{
		$elem = $(this);
		$.get($elem.attr('href'), function(result)
		{
			if(result)
			{
				var id = $elem.attr('rel');
				$elem.parents('li').clone(true).appendTo('#comF ul');
				$('#comF ul #add_' + id).removeClass('add').addClass('remove').attr('title', 'Remove from Featured').attr('id', 'remove_' + id).text('Remove');
				$elem.hide();
				$('#featured_tab').parent().show();
			}
		});
		return false;
	});
	
	$('.featured.remove').live('click', function()
	{
		$elem = $(this);
		$.get($elem.attr('href'), function(result)
		{
			if(result)
			{
				var id = $elem.attr('rel');
				$elem.parents('li').remove();
				$('#comA ul #add_' + id).show();
				if( !$('#comF ul li').length )
				{ 
					$('#featured_tab').parent().hide();
					$('#ordinary_tab').click();
				} 
			}
		});
		return false;
	});
	
	
	$('#report_abuse').dialog(
	{
		autoOpen		: false,
		bgiframe		: false,
		width			: 600,
//		height			: 600,
		modal			: true,
		closeOnEscape	: true,
		draggable		: false,
		resizable		: false,
		
		buttons:		{	
			'Close' 			: function(){ $(this).dialog('close'); }
		}
	});
	
	$('.actions a.abused').click(function()
	{
		var $el = $(this);
		var url = $el.attr('href');
		$.get(url, function(result)
		{
			if(result)
			{
				$el.remove(); 
				$('#report_abuse').dialog('open'); 
			}
		});
		return false;
	});
	
});
