/*
*	Poll
*	Manages poll submissions / results through AJAX
*	
*	Requires jQuery library (http://www.jquery.com)
*	
*	Taylan Pince (taylan@trapeze.com) - August 9, 2007
*/

if (typeof trapeze == 'undefined') trapeze = new Object();

trapeze.Poll = {
	
	set_cookie : function(name, val, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		} else {
			var expires = "";
		}
		document.cookie = name + "=" + val + expires + "; path=/";
	},
	
	get_cookie : function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for (var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	
	del_cookie : function(name) {
		this.set_cookie(name, '', -1);
	},
	
	vote : function(form) {
		var id = $(form).find("input[@name=id]").val();
		var params = {
			"ajax"	: "true",
			"id"	: id,
			"vote"	: $(form).find("input[@type=radio][@checked]").val()
		}
		
		$.post('/polls/vote/', params, function(data) {
			if ($("result", data).size() > 0) {
				trapeze.Poll.set_cookie('poll_' + id, 'voted', 15);
				trapeze.Poll.process(data, form, id);
			} else {
				trapeze.Poll.error(form, "There was an error during the process, please try again.");
			}
		});
	},
	
	results : function(id) {
		$.get('/polls/results/' + id + '/xml/', {}, function(data) {
			trapeze.Poll.process(data, $("#Poll > form")[0], id);
		});
	},
	
	go_back : function() {
		$("#Poll").find("label").show();
		$("#Poll").find(".button > input, a.results").show();
		$("#Poll").find(".choice, .percent-wrapper").remove();
		$("#Poll").find("a.back").remove();
	},
	
	process : function(data, form, pid) {
		$(form).find(".error").remove();
		var button = (this.get_cookie("poll_" + pid) != null) ? '' : '<a href="javascript:void(0);" onclick="trapeze.Poll.go_back();" class="back">&laquo; back</a>';
		$(form).find(".button").append(button).find("input, a.results").hide();
        $("#PollChoices").html("<p>Thanks for your vote!</p>");
        /*
		$("result", data).each(function() {
			var id = $(this).attr("id");
			var percent = $(this).attr("percent");
			var w = 204 / 100 * percent;
			var bg = (w == 0) ? "transparent" : "#FFB005";
			var html = '';
			html += '<div class="choice">' + $(this).text() + '</div>';
			html += '<div class="percent-wrapper">';
			html += '<div class="percent" id="PollChoicePercent' + id + '" style="width: 0px; background-color: ' + bg + ';">' + percent + '%</div>';
			html += '</div>';
			$(form).find("#PollChoiceLabel" + id).before(html).hide();
			$(form).find("#PollChoicePercent" + id).animate({width: w}, "fast");
		});
		*/
	},
	
	validate : function(form) {
		if ($(form).find("input[@type=radio][@checked]").size() == 1) {
			return true;
		} else {
			return false;
		}
	},
	
	error : function(form, e) {
		$(form).find(".error").remove();
		$(form).prepend('<p class="error" style="display: none;">' + e + '</p>');
		$(form).find(".error").slideDown();
	},
	
	init : function() {
		if ($("#Poll").size() > 0) {
			var id = $("#Poll > form").find("input[@name=id]").val();
			if (this.get_cookie("poll_" + id) != null) {
				this.results(id);
			} else {
				$("#Poll > form").submit(function() {
					if (trapeze.Poll.validate(this)) {
						trapeze.Poll.vote(this);
					} else {
						trapeze.Poll.error(this, "Please pick an answer first.");
					}
					return false;
				});
				
				$("#Poll").find("a.results").click(function() {
					trapeze.Poll.results(id);
				}).attr("href", "javascript:void(0);");
			}
		}
	}
	
}

$(function() {
	trapeze.Poll.init();
});