var siteurl2;
//siteurl2 = "http://saravanan/NDTV-HINDU/phase2_1/";
siteurl2 = "http://www.ndtv-hindu.com/";
// Global variable definitions
// DB column numbers
var OPT_ID = 0;
var OPT_TITLE = 1;
var OPT_VOTES = 2;
var OPT_Q = 3;

var votedID;

/*$(document).ready(function(){
  $("#poll").submit(formProcess); // setup the submit handler
  
  if ($("#poll-results").length > 0 ) {
    animateResults();
  }
  
  if ($.cookie('vote_id')) {
    $("#poll-container").empty();
    votedID = $.cookie('vote_id');
   // $.getJSON("poll.php?vote=none",loadResults);
    $.getJSON("pollr.php",loadResults);
  }
});

*/
function disp(pid)
{
	// alert(pid);
	//alert("pollr.php?pid="+pid);
	if(pid != "")
	{
  		setTimeout($.getJSON(siteurl2+"pollr.php?pid="+pid,loadResults),2000);
	}else
	{
  		setTimeout($.getJSON(siteurl2+"pollr.php",loadResults),2000);
	}
	document.getElementById("poll-container").innerHTML = "";

}


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(siteurl2+"poll.php?vote="+id,loadResults);
    
    $.cookie('vote_id', id, {expires: 365});
    });
}

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

function loadResults(data) {
  var total_votes = 0;
  var percent;
  //alert(data);
  for (id in data) {
    total_votes = total_votes+parseInt(data[id][OPT_VOTES]);
  }
 
 if(parseInt(total_votes) > 0){
  var results_html = "<div id='poll-results'><h4>Total Votes "+total_votes+"</h4><h3> "+data[0][OPT_Q]+"</h3><dl class='graph'>\n";
  for (id in data) {
    percent = Math.round((parseInt(data[id][OPT_VOTES])/parseInt(total_votes))*100);
	rc = random_color('hex');
    if (data[id][OPT_ID] !== votedID) {
      results_html = results_html+"<dt class='bar-title'><font color='"+rc+"'>"+(parseInt(id)+1)+". </font>"+data[id][OPT_TITLE]+"</dt><dd class='bar-container'><div id='bar"+data[id][OPT_ID]+"'style='width:0%;vertical-align:middle;background-color:"+rc+"'></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></div>\n";
  //<p>Total Votes: "+total_votes+"</p>
  $("#poll-container").append(results_html).fadeIn("slow",function(){
	//document.getElementById("tracediv").innerHTML = document.getElementById("poll-container").innerHTML;																   
	
    animateResults();
	});
 }else
 {
  	var results_html = "<div id='poll-results'><h4>Total Votes "+total_votes+"</h4><dl class='graph'>\n<dt class='bar-title'>Be the first one to vote!</dt></dl></div>\n";
	$("#poll-container").append(results_html).fadeIn("slow","");
 }
}
function random_color(format)
{
 var rint = Math.round(0xffffff * Math.random());
 switch(format)
 {
  case 'hex':
   return ('#0' + rint.toString(16)).replace(/^#0([0-9a-f]{6})$/i, '#$1');
  break;
  
  case 'rgb':
   return 'rgb(' + (rint >> 16) + ',' + (rint >> 8 & 255) + ',' + (rint & 255) + ')';
  break;
  
  case 'def':
   return rint;
  break;
  
  default:
   return rint;
  break;
 }
}
