var Poll = {
	Manage: {
		selected: null,
		select: function(callback) 
		{
			var dialog = $('<div>Loading...</div>');
			
			dialog.dialog({ 
			    title: 'Manage Polls',
			    modal: true, 
			    width: 640,
			    height: 480,
			    close: function() {
			    	$(this).dialog('destroy').remove();
			    } 
			}).load('/poll/manage/', {}, function(){
				$('#poll-selection-button').bind('click', function(){
					callback(Poll.Manage.selected, $('#poll-item-name-' + Poll.Manage.selected).text());
					dialog.dialog('close');
				});
			});
		},
		add: function()
		{
			$.post('/poll/add/', $('#poll-create').serializeArray(), function(poll){
				$('#poll-manage-list').append('<li id="poll-item-'+poll.id+'" class="ui-state-default ui-corner-all"><a href="#" onclick="Poll.Manage.remove('+poll.id+');return false;"><span class="ui-icon ui-icon-close">{X}</span></a> <a id="poll-item-name-'+poll.id+'" href="#" onclick="Poll.Manage.edit('+poll.id+');return false;">'+poll.title+'</a></li>');
				$('#poll-create input[name=\'poll\[title\]\']').val('');
			}, 'json');
		},
		addOption: function(id)
		{
			$.post('/poll/option/add/'+id+'/', $('#option-create').serializeArray(), function(option){
				$('#poll-options').append('<li id="poll-option-'+option.id+'" class="ui-state-default ui-corner-all"><a href="#" onclick="Poll.Manage.removeOption('+option.id+');return false;"><span class="ui-icon ui-icon-close">{X}</span></a>'+option.value+'</li>');
				$('#option-create input[name=\'option\[value\]\']').val('');
			}, 'json');
		},
		removeOption: function(option)
		{
			if (confirm('Are you sure you wish to remove this option? (Removing options will reset the poll)')) {
				$.getJSON('/poll/option/delete/'+option+'/');
				$('#poll-option-' + option).remove();
			}
		},
		remove: function(id)
		{
			if (confirm('Are you sure you wish to delete this poll?')) {
				$.getJSON('/poll/delete/'+id+'/', {}, function(poll){
				});
			
				$('#poll-item-' + id).remove();
			
				if (Poll.Manage.selected == id) {
					$('#poll-manage-select').hide();
					$('#poll-manage-edit').html("Select a poll on the left to continue.");
				}
			}
		},
		edit: function(id)
		{
			$('#poll-manage-list li').removeClass("ui-state-active");
			$('#poll-item-' + id).addClass("ui-state-active");
			$('#poll-manage-select').show();
			
			if (id == 0) { 
				$('#poll-manage-edit').html('');
			} else {
				$('#poll-manage-edit').html('Loading...');
				$('#poll-manage-edit').load('/poll/edit/' + id + '/');
			}
			
			Poll.Manage.selected = id;
			
			$('#poll-manage-add').show();
		},
		save: function(id)
		{
			$("#poll-save").attr('disabled', 'disabled');
			$("#poll-save").val('Saving...');
			
			$('#poll-item-name-' + id).text($('#poll-edit input[name=\'poll\[title\]\']').val());
			$.post('/poll/save/'+id+'/', $('#poll-edit').serializeArray(), function(poll){
				$("#poll-save").val('Save');
				$("#poll-save").removeAttr('disabled');
			}, 'json');
		},
		selectGroup: function(pollId)
		{
			return function(id, name){
				$('#poll-privacy').html('Viewable by: ' + name);
				$('input[name=\'poll\[groupId\]\']').val(id);
				$.post('/poll/save/'+pollId+'/group/', {'poll[groupId]': id}, function(){}, 'json');
			}
		}
	},
	
	vote: function(id, optionId)
	{
		$('#poll-' + id + ' button') . attr('disabled', 'disabled');
		$.post('/poll/vote/'+optionId+'/', {}, function(poll){
			$('#poll-' + id) . load('/poll/results/'+id+'/');
		}, 'json');		
	},
	
	results: function(id)
	{
		$('#poll-' + id) . html('Loading...') . load('/poll/results/'+id+'/');
	}
};