// b_allow_toggle and b_allow_change dictate what is allowable after a vote.
function vote_up(url, v_id, b_allow_toggle, b_allow_change) {
    var p = $('div.vote_item.vote_item_' + v_id);
    var o = p.children('div.vote_arrow.vote_up');
    
    // Only allow vote processing on items that allow it.
    if (!(o.hasClass('vote_clickable')))
    	return;
    
    // Disable clicking.
    o.removeClass('vote_clickable');

    var other = p.children('div.vote_arrow.vote_down');
    other.removeClass('vote_clickable');
    
    // Remove existing scores.
    p.children('div.vote_score').addClass('nodisplay');
    
    // Remove existing downvote (if any)
    if (o.hasClass('vote_up_default'))
    {
    	o.removeClass('vote_up_default');
    	other.removeClass('vote_down_default');
    	
    	o.addClass('vote_up_set');
    	other.addClass('vote_down_unset');
    	
    	p.children('div.vote_score.vote_score_up').removeClass('nodisplay');
    	
    	if (b_allow_toggle)
    		o.addClass('vote_clickable');
    	if (b_allow_change)
    		other.addClass('vote_clickable');
    }
    else if (o.hasClass('vote_up_set'))
    {
    	o.removeClass('vote_up_set');
    	other.removeClass('vote_down_unset');
    	
    	o.addClass('vote_up_default')
    	other.addClass('vote_down_default');
    	
    	p.children('div.vote_score.vote_score_default').removeClass('nodisplay');
    	
    	o.addClass('vote_clickable');
    	other.addClass('vote_clickable');
    }
    else if (o.hasClass('vote_up_unset'))
    {
    	o.removeClass('vote_up_unset');
    	other.removeClass('vote_down_set');
    	
    	o.addClass('vote_up_set')
    	other.addClass('vote_down_unset');
    	
    	p.children('div.vote_score.vote_score_up').removeClass('nodisplay');
    	
    	if (b_allow_toggle)
    		o.addClass('vote_clickable');
    	if (b_allow_change)
    		other.addClass('vote_clickable');
    }
   	
  	// Perform a vote up request...
    $.post(url, { action: "vote_up", id: v_id } );
};

function vote_down(url, v_id, b_allow_toggle, b_allow_change) {
    var p = $('div.vote_item.vote_item_' + v_id);
    var o = p.children('div.vote_arrow.vote_down');
    
    // Only allow vote processing on items that allow it.
    if (!(o.hasClass('vote_clickable')))
    	return;
    
    // Disable clicking.
    o.removeClass('vote_clickable');

    var other = p.children('div.vote_arrow.vote_up');
    other.removeClass('vote_clickable');
    
    p.children('div.vote_score').addClass('nodisplay');
    
    // Remove existing downvote (if any)
    if (o.hasClass('vote_down_default'))
    {
    	o.removeClass('vote_down_default');
    	other.removeClass('vote_up_default');
    	
    	o.addClass('vote_down_set');
    	other.addClass('vote_up_unset');
    	
    	p.children('div.vote_score.vote_score_down').removeClass('nodisplay');
    	
    	if (b_allow_toggle)
    		o.addClass('vote_clickable');
    	if (b_allow_change)
    		other.addClass('vote_clickable');
    }
    else if (o.hasClass('vote_down_set'))
    {
    	o.removeClass('vote_down_set');
    	other.removeClass('vote_up_unset');
    	
    	o.addClass('vote_down_default')
    	other.addClass('vote_up_default');
    	
    	p.children('div.vote_score.vote_score_default').removeClass('nodisplay');
    	
    	o.addClass('vote_clickable');
    	other.addClass('vote_clickable');
    }
    else if (o.hasClass('vote_down_unset'))
    {
    	o.removeClass('vote_down_unset');
    	other.removeClass('vote_up_set');
    	
    	o.addClass('vote_down_set')
    	other.addClass('vote_up_unset');
    	
    	p.children('div.vote_score.vote_score_down').removeClass('nodisplay');
    	
    	if (b_allow_toggle)
    		o.addClass('vote_clickable');
    	if (b_allow_change)
    		other.addClass('vote_clickable');
    }
   	
  	// Perform a vote up request...
    $.post(url, { action: "vote_down", id: v_id } );
};

function vote_delete(url, v_id) {
	var t = $('.issue.issue_' + v_id);
	
	t.fadeOut(1000,  function() {
		t.remove() }
	);
		
	// Ajax Call.
	$.post(url, { action: "delete", id: v_id } );
}

function vote_showstateedit(v_id)
{
	var t = $('.issue.issue_' + v_id).children('.issue_info_wrapper');
	var c = t.children('.issue_controls');
	
	c.children('.issue_edit_button').hide();
	t.children('.issue_state').hide();
	
	c.children('.issue_edit_state').show();
}

function vote_setstate(url, v_id, v_state_id) {
	// Get the issue all over the page.
	var t = $('.issue.issue_' + v_id).children('.issue_info_wrapper');
	var c = t.children('.issue_controls');
	
	// Hide the controls...
	c.hide();
	var s = t.children('.issue_state');
	s.children().hide();
	
	// The loader...
	l = t.children('.issue_loader');
	l.show();	
	
	// Prepare the controls for when the ajax is done...
	c.children('.issue_edit_state').hide();
	c.children('.issue_edit_button').show();
	s.children('.issue_state_' + v_state_id).show();
	
	$.post(url, {action: "state", id: v_id, state: v_state_id},
		function(){
			l.hide();
			s.show();
			c.show();
	});
}

function vote_hide(url, v_id) {
	// Get the issue all over the page.
	var t = $('.issue.issue_' + v_id).children('.issue_info_wrapper');
	var c = t.children('.issue_controls');
	
	// Hide the controls...
	c.hide();
	
	// The loader...
	l = t.children('.issue_loader');
	l.show();
	
	// Prepare the updated panel...
	c.children('.issue_display_toggle').children('.issue_show_button').show();
	c.children('.issue_display_toggle').children('.issue_hide_button').hide();
	
	$.post(url, {action: "hide", id: v_id},
		function(){
			l.hide();
			c.show();
			t.children('.issue_name').addClass('issue_name_hidden');
	});
}

function vote_show(url, v_id) {
	// Get the issue all over the page.
	var t = $('.issue.issue_' + v_id).children('.issue_info_wrapper');
	var c = t.children('.issue_controls');
	
	// Hide the controls...
	c.hide();
	
	// The loader...
	l = t.children('.issue_loader');
	l.show();
	
	// Prepare the updated panel...
	c.children('.issue_display_toggle').children('.issue_hide_button').show();
	c.children('.issue_display_toggle').children('.issue_show_button').hide();
	
	$.post(url, {action: "show", id: v_id},
		function(){
			l.hide();
			c.show();
			t.children('.issue_name').removeClass('issue_name_hidden');
	});
}
