jQuery.cookie=function(B,I,L){if(typeof I!="undefined"){L=L||{};if(I===null){I="";L.expires=-1}var E="";if(L.expires&&(typeof L.expires=="number"||L.expires.toUTCString)){var F;if(typeof L.expires=="number"){F=new Date();F.setTime(F.getTime()+(L.expires*24*60*60*1000))}else{F=L.expires}E="; expires="+F.toUTCString()}var K=L.path?"; path="+(L.path):"";var G=L.domain?"; domain="+(L.domain):"";var A=L.secure?"; secure":"";document.cookie=[B,"=",encodeURIComponent(I),E,K,G,A].join("")}else{var D=null;if(document.cookie&&document.cookie!=""){var J=document.cookie.split(";");for(var H=0;H<J.length;H++){var C=jQuery.trim(J[H]);if(C.substring(0,B.length+1)==(B+"=")){D=decodeURIComponent(C.substring(B.length+1));break}}}return D}};

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

var votedID;
var votePID;
var res_message;

$(document).ready(function(){
	
	votePID = $("input[name='pid']").attr("value");
	
  $("#poll").submit(formProcess); // setup the submit handler

	$("#results_link").click(function(){
		var pid = $("input[name='pid']").attr("value");
		$.getJSON("poll/poll.php?pid="+pid+"&vote=none",loadResults);
		return false;
	});
   
  if ($("#poll-results").length > 0 ) {
    animateResults();
  }
  
  
  if ($.cookie('vote_pid_'+votePID)) {
    //$("#poll-container").empty();
    votedID = $.cookie('vote_id');
    var pid = $("input[name='pid']").attr("value");
    var res = parseInt($("input[name='res']").attr("value"));
    
    if(res == 1)
    	$.getJSON("poll/poll.php?pid="+pid+"&vote=none",loadResults);
    else
    {
    	res_message = $("input[name='res_message']").attr("value");
    	$.getJSON("poll/poll.php?pid="+pid+"&vote=none",loadMessage);
    }
    
  }
  
  
});

function formProcess(event){
  event.preventDefault();
  
  var pid = $("input[name='pid']").attr("value");
  var res = parseInt($("input[name='res']").attr("value"));
  var id = $("input[name='poll']:checked").attr("value");
  //id = id.replace("opt",'');
  
  if(id == undefined)
  	return false;
  
  $("#poll-container").fadeOut("slow",function(){
    //$(this).empty();
    
    votedID = id;
    
    if(res == 1)
    	$.getJSON("poll/poll.php?pid="+pid+"&vote="+id,loadResults);
    else
    {
    	res_message = $("input[name='res_message']").attr("value");
    	$.getJSON("poll/poll.php?pid="+pid+"&vote="+id,loadMessage);
    }
    
    var date = new Date();
    date.setTime(date.getTime() + (30 * 60 * 1000));
    
    $.cookie('vote_id', id, {expires: 1});
    $.cookie('vote_pid_'+pid, 1, {expires: date});
  });
}

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 ptitle;
  
  for (id in data) {
    total_votes = total_votes+parseInt(data[id]);
  }
  
  var results_html = "<div id='poll-results'><h3>Umfrage Ergebnis</h3>\n<dl class='graph'>\n";
  
  //for (id in data) {
  $("#poll input[name='poll']").each(function(index){
  	
  	id = this.id;
  	
  	if(data[id] === undefined)
  		data[id] = 0;
  	
  	
  	ptitle = $("#poll label[for='"+id+"']").html();

  	//ptitle = ptitle.replace("&nbsp;",'');
  	
  	if(data[id] == 0)
  		percent = 0;
  	else
    	percent = Math.round((parseInt(data[id])/parseInt(total_votes))*100);
    
    
    if (id !== votedID) {
      results_html = results_html+"<dt class='bar-title'>"+ptitle+"</dt><dd class='bar-container'><div id='bar"+id+"'style='width:0%;'>&nbsp;</div><strong>"+percent+"%</strong></dd>\n";
    } else {
      results_html = results_html+"<dt class='bar-title'>"+ptitle+"</dt><dd class='bar-container'><div id='bar"+id+"'style='width:0%;background-color:#0066cc;'>&nbsp;</div><strong>"+percent+"%</strong></dd>\n";
    }
  });
  
  //results_html = results_html+"</dl><p style='clear:both;'><br />Stimmen: "+total_votes+"</p></div>\n";
  results_html = results_html+"</dl><p style='clear:both;'></p></div>\n";
  
  //$("#poll-container").empty();
  
  $("#poll-container").html(results_html).fadeIn("slow",function(){
    animateResults();});
}


function loadMessage(data) {
	results_html = "<h3>"+res_message+"</h3>";
	$("#poll-container").html(results_html).fadeIn("slow");
}
