// Global variable definitions
// DB column numbers
var OPT_ID = 0;
var OPT_TITLE = 1;
var OPT_VOTES = 2;

var votedID;

function getDialogButton( dialog_selector, button_name )
{
  var buttons = $('.ui-dialog-buttonset button' );

  for ( var i = 0; i < buttons.length; ++i )
  {
     var jButton = $( buttons[i] );
	
     if ( jButton.text() == button_name )
     {
         return jButton;
     }
  }

  return null;
}

$(document).ready(function(){

	
	$( "#dialog-confirm" ).dialog({
			resizable: false,
			height:300,
			width:400,
			modal: true,
			closeOnEscape: true,
			autoOpen: false,
			buttons: [{
                                id:"btn-poll",
                                text: "Oy Ver",
                                click: function() {
                                         $('#poll').submit();
                                }
                        },{
                                id:"btn-close",
                                text: "Kapat",
                                click: function() {
                                        $(this).dialog("close");
                                }
                        }]
		});
	if ($.cookie('vote_id')) {
		$( "#dialog-confirm" ).dialog( "disable" );
	}
	else
	{
		$( "#dialog-confirm" ).dialog( "open" );
	}
  $("#poll").submit(formProcess); // setup the submit handler
  
   //$("#btn-poll").hide();

  
   
  if ($("#poll-results").length > 0 ) {
    animateResults();
	
  }
  //$.cookie("vote_id", null);
  if ($.cookie('vote_id')) {
    $("#poll-container").empty();
    votedID = $.cookie('vote_id');
    $.getJSON("/poll/poll.php?vote=none",loadResults);
  }
});

function formProcess(event){
  event.preventDefault();
  
  var id = $("input[@name='poll']:checked").attr("value");
  id = id.replace("opt",'');
  
  $("#poll-container").fadeOut("slow",function(){
    $(this).empty();
    
    votedID = id;
    $.getJSON("/poll/poll.php?vote="+id,loadResults);
    
    $.cookie('vote_id', 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) {
  var total_votes = 0;
  var percent;
  
  var button = getDialogButton( '#dialog-confirm', 'Oy Ver' );
	if ( button )
	{
	  button.hide();
	}
  
  for (id in data) {
    total_votes = total_votes+parseInt(data[id][OPT_VOTES]);
  }
  
  var results_html = "<div id='poll-results'><h3>Anket Sonuçları</h3><br /><dl class='graph'>\n";
  for (id in data) {
    percent = Math.round((parseInt(data[id][OPT_VOTES])/parseInt(total_votes))*100);
    if (data[id][OPT_ID] !== votedID) {
      results_html = results_html+"<dt class='bar-title'>"+data[id][OPT_TITLE]+"</dt><dd class='bar-container'><div id='bar"+data[id][OPT_ID]+"'style='width:0%;'>&nbsp;</div><strong>"+percent+"%</strong></dd>\n";
    } else {
      results_html = results_html+"<dt class='bar-title'>"+data[id][OPT_TITLE]+"</dt><dd class='bar-container'><div id='bar"+data[id][OPT_ID]+"'style='width:0%;background-color:#0066cc;'>&nbsp;</div><strong>"+percent+"%</strong></dd>\n";
    }
  }
  
  //results_html = results_html+"</dl><p>Toplam Oy: "+total_votes+"</p></div>\n";
  
  $("#poll-container").append(results_html).fadeIn("slow",function(){
    animateResults();});
}
