
var votedID;

$(document).ready(function(){
    $("#poll").submit(formProcess); // setup the submit handler
    var poll_id = $("#poll_id").val();

    if ($("#poll-results").length > 0 ) {
        animateResults();
    }
    //alert($.cookie('vote_id'+poll_id));
    if ($.cookie('vote_id'+poll_id)) {
        $("#poll-container").empty();
        var lang = $("#lang").val();
        votedID = $.cookie('vote_id');
        //$.getJSON("poll.php?vote=none",loadResults);
        $.post('../includes/getJSONPoll.php', {
            vote: 'none',
            lang: lang
        }, function(response){
            loadResults(response, lang);
        }, 'json');
    }
});

function formProcess(event){
    event.preventDefault();
    var id = $("input[@name='poll']:checked", '#poll').attr("value");
    var poll_id = $("#poll_id").val();
    var lang = $("#lang").val();
    id = id.replace("opt",'');
    $("#poll-container").fadeOut("slow",function(){
        $(this).empty();            
        var params = {
            vote:id,
            poll_id: poll_id,
            lang: lang
        };

        $.post('../includes/getJSONPoll.php', params, function(response){
            loadResults(response, lang);
        }, 'json');
    
        $.cookie('vote_id'+poll_id, poll_id, {
            expires: 365
        });
    });
}

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

function loadResults(data, lang) {   
    var total_votes = 0;
    var percent;
    var row = null;
    
    for (var i = 0 ; i < data.length; i++) {
        row = data[i];
        total_votes = total_votes+parseInt(row.votes);
    }

    var results_html = "<div id='poll-results' style='margin-top:5px;'><br style='clear:both;'><h3>Poll Results</h3>\n<dl class='graph'>\n";

    if(lang == 'np'){
        results_html = "<div id='poll-results' style='margin-top:5px;'><br style='clear:both;'><h3>&#2346;&#2379;&#2354; &#2344;&#2340;&#2367;&#2332;&#2366;</h3>\n<dl class='graph'>\n";
    }       
    
    for (i = 0 ; i < data.length; i++) {
        row = data[i];
        
        percent = Math.round((parseInt(row.votes)/parseInt(total_votes))*100);
        if (row.poll_option_id !== votedID) {
            results_html = results_html+"<dt class='bar-title'>"+row.vote_option+"</dt><dd class='bar-container'><div id='bar"+row.poll_option_id+"'style='width:0%;'>&nbsp;</div><strong>"+percent+"%</strong></dd>\n";
        } else {
            results_html = results_html+"<dt class='bar-title'>"+row.vote_option+"</dt><dd class='bar-container'><div id='bar"+row.poll_option_id+"'style='width:0%;background-color:#0066cc;'>&nbsp;</div><strong>"+percent+"%</strong></dd>\n";
        }
    }
  
    
    if(lang == 'np'){
        results_html = results_html+"</dl><p>&#2332;&#2350;&#2381;&#2350;&#2366; &#2350;&#2340;: "+total_votes+"</p></div>\n";
    }else{
        results_html = results_html+"</dl><p>Total Votes: "+total_votes+"</p></div>\n";
    }
  
    $("#poll-container").append(results_html).fadeIn("slow",function(){
        animateResults();
    });
}
