// JavaScript Document
$(document).ready(function(){					   
  	setup();
});

function setup(){
	
	// Run setup on all argument vote-urls
    $(".argument-package").each(function(){
		// Get URL to save and add "TRUE"  to flag as an ajax request
		var href = $(this).find(".vote-url").attr('href')+'TRUE/';
		var id = $(this).find('.argument-votestats').attr('id');
		$(this).find(".vote-url").click(function () {
			// Get stance from agree-column id name on div column
			stance = $(this).parent().parent().parent().parent().parent().parent().attr('id').replace('-column', '');
			record_vote(href, id, stance, 'ARGUMENT');
   	    });
		
	});
	// Run setup on issue vote-urls
    $(".publicvotingbutton").each(function(){
		// Get URL to save and add "TRUE"  to flag as an ajax request
		var href = $(this).find(".vote-url").attr('href')+'TRUE/';
		var id = $(this).attr('id');
		$(this).find(".vote-url").click(function () {
			//id is either "agree" or "disagree"
			stance = id.replace('public-voting-', '');
			//alert(stance);
			record_vote(href, id, stance, 'ISSUE');
   	    });
	});
	
	// Set all issue and argument vote-urls 
	$(".vote-url").attr('href', '#vote');
}

function record_vote(href, id, stance, type){
	//alert(id);
	$.ajax({
	   type: "POST",
	   url: href,
	   dataType: 'xml',
	   data: 'foo=true',
	   success: function(data, text_status){
		
		var status = $(data).find('response').find('status').text();
		var css_class = $(data).find('response').find('class').text();
		var message = $(data).find('response').find('message').text();
		
		//alert(data);
		
		//Perfrom different actions based on status
		//Also displays message
		act_on_status(status, css_class, message, id, stance, type);
		
	   },
	   error: function(){
		 //alert( 'ERROR' );
	   }
	});	
}
function act_on_status(status, css_class, message, id, stance, type){
	switch (type){
		case 'ARGUMENT':	
			switch (status){
				case 'LOGIN_REQUIRED':
					
				break;
				case 'ALREADY_VOTED':
		
				break;
				case 'ISSUE_EXPIRED':
					//update_arg_votes(id);
				break;
				case 'VOTE_CAST':
					//message = null;
					update_arg_votes(id);
					update_current_results(stance);
				break;
				
				default:
				
				break;
			}
		break;
		case 'ISSUE':
			switch (status){
				case 'ISSUE_EXPIRED':
					
				break
				case 'VOTE_CAST':
					//message = null;
					//update_issue_votes(id);
					update_current_results(stance);
				break;
				
				default:
				
				break;
			}
		break;
	}
	//update_current_results(stance);
	if (message != null){
		$("#message").remove();
		$("#main-content").prepend('<div id="message" class="'+css_class+'">'+message+'</div>');
	}
}
function update_arg_votes(id){
	new_count = $('#'+id).find('.number').text();
	new_count  = parseInt(new_count) + 1;
	$('#' + id).find('.number').text(new_count);
}
/*function update_issue_votes(id){
	new_count = $('#'+id+" .totalvotes").text();
	new_count  = parseInt(new_count) + 1;
	$('#' + id + " .totalvotes").text(new_count);
}*/
function update_current_results(stance){
	//stance  = agree or disagree
	new_count = $('#'+'public-voting-'+stance).find('.number').text();
	new_count  = parseInt(new_count) + 1;
	//alert(new_count);
	$('#'+'public-voting-'+stance).find('.number').text(new_count);
	
	agree_count = parseInt($('#public-voting-'+'agree').find('.number').text());
	disagree_count = parseInt($('#public-voting-'+'disagree').find('.number').text());
	
	total_count = agree_count+disagree_count;
	
	agree_percent = Math.round((agree_count*100)/total_count)+'%';
	disagree_percent = Math.round((disagree_count*100)/total_count)+'%';
	
	$('#public-voting-'+'agree'+' .percent').text(agree_percent);
	$('#public-voting-'+'disagree'+' .percent').text(disagree_percent);
	
	//alert(agree_percent);
	//$new_percent = 
}


