var pollVotedID;
$(document).ready(function(){
    var quest_id = $("input[name='questionid']").attr("value");
    if ($.cookie('vote_id') && quest_id == $.cookie('quest_id') && $.cookie('vote_results_height'))
  {
  	$("#poll-container").css({'height': $.cookie('vote_results_height')+'px'});
  }
    $("#poll").submit(voteFormProcess); // setup the submit handler
    if ($("#poll-results").length > 0) {
        animateVoteResults();
    }
    
    if ($.cookie('vote_id') && quest_id == $.cookie('quest_id')) {
        $('#poll').hide();
        showVoteResults();
    }else{
	  $('#poll').show();
    }
});

function showPollsScreen()
{
	if (!$.cookie('vote_id')) {
		$('#poll-results-container').empty();
		$('#poll-container').show();
	}
}

function showVoteResults(){
    $("#poll-container").hide();
    pollVotedID = $.cookie('vote_id');
    $.getJSON("index.php?cl=start&fnc=ajax_getresults", loadVoteResults);
}

function voteFormProcess(event){
    event.preventDefault();
    
    var id = $("input[name='vote']:checked").attr("value");
    var quest_id = $("input[name='questionid']").attr("value");
    
    $("#poll-container").fadeOut("slow", function(){
        $(this).empty();
        
        pollVotedID = id;
        $.getJSON("index.php?cl=start&fnc=vote&calltype=ajax&vote=" + id, loadVoteResults);
        
        $.cookie('vote_id', id, {
            expires: 365
        });
        $.cookie('quest_id', quest_id, {
            expires: 365
        });
        $.cookie('vote_results_height', null, {expires: 365});
    });
}

function animateVoteResults(){
    $("#poll-results div").each(function(){
        var percentage = $(this).next().text();
        $(this).css({
            width: "0%"
        }).animate({
            width: percentage
        }, 'fast');
    });
}

function loadVoteResults(data){

    var total_votes = 0;
    var percent;
    
    for (id in data) {
        total_votes = total_votes + parseInt(data[id]['OXCOUNTER']);
    }
    
    var results_html = "<div id='poll-results' class='clear'>\n<dl class='graph'>\n";
    for (id in data) {
        percent = Math.round((parseInt(data[id]['OXCOUNTER']) / parseInt(total_votes)) * 100);
        if (isNaN(percent)) 
            percent = 0;
        if (data[id]['OXID'] !== pollVotedID) {
            results_html = results_html + "<dt class='bar-title'>" + data[id]['OXTITLE'] + "</dt><dd class='bar-container'><div id='bar" + (id + 1) + "'style='width:0%;'>&nbsp;</div><strong>" + percent + "%</strong></dd>\n";
        }
        else {
            results_html = results_html + "<dt class='bar-title'>" + data[id]['OXTITLE'] + "</dt><dd class='bar-container'><div id='bar" + (id + 1) + "'style='width:0%;background-color:#fea000;'>&nbsp;</div><strong>" + percent + "%</strong></dd>\n";
        }
    }
    
    results_html = results_html + "</dl><p class='totalVotes'>" + POLL_TOTALVOTES + " " + total_votes + "</p></div>\n";
    if (!$.cookie('vote_id')) {
    	results_html = results_html + '<div style="margin-top:5px;"><input type="button" class="submit buttonVote" value="" title="Balsuoti" onclick="showPollsScreen();"></div>';
    }
    $("#poll-results-container").append(results_html).show("fast", function(){
        $("#rightCol").css({'height': 'auto'});
        
        animateVoteResults();
        $("#poll-results-container").css({'height': 'auto'});
        
        $.cookie('vote_results_height', $("#poll-results-container").height(), {expires: 365});
    });
}