function populatePoll(data,destination) {
	pollID = $("input.poll_id", destination).attr("value");
	destination.empty().append($("<ul class='results'></ul>"));
	currTotal = 0;
    for(i=0;i<data.options.data.length;i++) {
		itemTotal = Math.floor((data.options.data[i].RESPONSES/data.poll.data[0].RESPONSES)*100);
		currTotal += itemTotal;
		if(i==data.options.data.length-1) {
			itemTotal += 100 - currTotal;
		}
        $("ul", destination).append($("<li><span></span><div class='result'>&nbsp;</div><div class='stat'>&nbsp;</div></li>"));
        $("ul", destination)
        .find("li").eq(i).find("span").html(data.options.data[i].TITLE)
        .end()
        .find("div.result").width(Math.floor((data.options.data[i].RESPONSES/data.poll.data[0].RESPONSES)*280) + "px")
        .end()
        .find("div.stat").html(itemTotal + "%");
    }
	$("li", destination).show();
	$("div.rounded_left_corner_button", destination).remove();
	destination.append($("<div class='again'><a href='#'><span>Vote Again</span></a></div>"));
	//console.log($("div.again a", destination));
	$("div.again a", destination).bind("click", function() {
		parent = $(this).parents("div.poll");
		$.ajax({
			type: "GET",
			url: "/modules/fetch_module.php",
			data: {
				_file : "polls",
				_function : "poll_choices",
				poll_id : pollID,
				limit : 1,
				offset : 0,
				dataType: "jsonp",
				jsonp: "jsonp_callback"
			},
			success: function(data){
				data = eval(data);
				$("div.panel", parent).remove();
				$("div.title", parent).after($(unescape(data.data)));
				setUpVoting();
			}
		});
		return false;
	});
    //$("div.poll div.panel").empty().append(htmlFrag)/*.append($("<p class='results'>Total Responses: " + data.poll.data[0].RESPONSES + "</p>"))*/;
}

function setUpVoting() {
	/*$("div.poll div.wide label").each(function () {
		$(this).bind("mouseover", function () {
			thisIndex = $(this).parents("div.poll").find("label").index(this) + 1;
			$(this).parents("div.poll").find("div.thin li").removeClass("active").eq(thisIndex).addClass("active");
		}).bind("mouseout", function () {
			thisIndex = 0;
			$(this).parents("div.poll").find("div.thin li").removeClass("active").eq(thisIndex).addClass("active");
		});
	});
	$("div.poll div.btnlcround a").bind("click", function () {
		destination = $(this).parents("div.poll").find("div.panel");
		$.ajax({
			type: "GET",
			url: "/db/fetch_data.php",
			data: {
				_file : "polls",
				_function : "post_vote_get_poll_results",
				poll_id : $(this).parents("div.poll").find("input.poll_id").attr("value"),
				poll_option_id : $(this).parents("div.poll").find("input:checked").attr("value"),
				random : generateID(),
				dataType: "jsonp",
				jsonp: "jsonp_callback"
			},
			success: function(data){
				data = eval(data);
				populatePoll(data,destination);
			}
		});
		return false;
	});*/
	$("div.large_poll").each(function () {
		$("div.wide label", this).unbind().each(function () {
			$(this).bind("mouseover", function () {
				thisIndex = $(this).parents("div.large_poll").find("label").index(this) + 1;
				$(this).parents("div.large_poll").find("div.thin li").removeClass("active").eq(thisIndex).addClass("active");
			}).bind("mouseout", function () {
				thisIndex = 0;
				$(this).parents("div.large_poll").find("div.thin li").removeClass("active").eq(thisIndex).addClass("active");
			});
		});
		$("a.vote", this).unbind().bind("click", function () {
			page['current_poll'] = $(this).parents("div.large_poll");
			$.ajax({
				type: "GET",
				url: "/db/fetch_data.php",
				data: {
					_file : "polls",
					_function : "post_vote_get_poll_results",
					poll_id : page['current_poll'].find("input.poll_id").attr("value"),
					poll_option_id : page['current_poll'].find("input:checked").attr("value"),
					random : generateID(),
					dataType: "jsonp",
					jsonp: "jsonp_callback"
				},
				success: function(data){
					data = eval(data);
					$.ajax({
						type: "GET",
						url: "/modules/fetch_module.php",
						data: {
							_file : "polls",
							_function : "poll_content",
							poll_id : data['poll']['data'][0]['POLL_ID'],
							show_results : 'true',
							results_width : 190,
							image_size : '200x200',
							random : generateID(),
							dataType: "jsonp",
							jsonp: "jsonp_callback"
						},
						success: function(data){
							data = eval(data);
							page['current_poll'].empty().html($(unescape(data.data)));
							setUpVoting();
						}
					});
				}
			});
			return false;
		});
		/*$("a.results", this).unbind().bind("click", function () {
			page['current_poll'] = $(this).parents("div.large_poll");
			$.ajax({
				type: "GET",
				url: "/modules/fetch_module.php",
				data: {
					_file : "polls",
					_function : "poll_content",
					poll_id : page['current_poll'].find("input.poll_id").attr("value"),
					poll_title : page['current_poll'].find("h4").text(),
					show_results : 'true',
					results_width : 190,
					image_size : '200x200',
					random : generateID(),
					dataType: "jsonp",
					jsonp: "jsonp_callback"
				},
				success: function(data){
					data = eval(data);
					page['current_poll'].empty().html($(unescape(data.data)));
					setUpVoting();
				}
			});
			return false;
		});*/
	});
	$("div.poll").each(function () {	
		$("div.wide label", this).unbind().each(function () {
			$(this).bind("mouseover", function () {
				thisIndex = $(this).parents("div.poll").find("label").index(this) + 1;
				$(this).parents("div.poll").find("div.thin li").removeClass("active").eq(thisIndex).addClass("active");
			}).bind("mouseout", function () {
				thisIndex = 0;
				$(this).parents("div.poll").find("div.thin li").removeClass("active").eq(thisIndex).addClass("active");
			});
		});
		$("a.vote", this).unbind().bind("click", function () {
			page['current_poll'] = $(this).parents("div.poll");
			$.ajax({
				type: "GET",
				url: "/db/fetch_data.php",
				data: {
					_file : "polls",
					_function : "post_vote_get_poll_results",
					poll_id : page['current_poll'].find("input.poll_id").attr("value"),
					poll_option_id : page['current_poll'].find("input:checked").attr("value"),
					random : generateID(),
					dataType: "jsonp",
					jsonp: "jsonp_callback"
				},
				success: function(data){
					if($("body").attr("id") == "home") {
						window.location = "/polls/results";
					} else {
						data = eval(data);
						$.ajax({
							type: "GET",
							url: "/modules/fetch_module.php",
							data: {
								_file : "polls",
								_function : "poll_content",
								poll_id : page['current_poll'].find("input.poll_id").attr("value"),
								poll_title : page['current_poll'].find("h4").text(),
								show_results : 'true',
								results_width : 116,
								image_size : '120x120',
								random : generateID(),
								dataType: "jsonp",
								jsonp: "jsonp_callback"
							},
							success: function(data){
								data = eval(data);
								page['current_poll'].empty().html($(unescape(data.data)));
								setUpVoting();
							}
						});
					}
				}
			});
			return false;
		});
		/*$("a.results", this).unbind().bind("click", function () {
			page['current_poll'] = $(this).parents("div.poll");
			$.ajax({
				type: "GET",
				url: "/modules/fetch_module.php",
				data: {
					_file : "polls",
					_function : "poll_content",
					poll_id : page['current_poll'].find("input.poll_id").attr("value"),
					poll_title : page['current_poll'].find("h4").text(),
					show_results : 'true',
					results_width : 116,
					image_size : '120x120',
					random : generateID(),
					dataType: "jsonp",
					jsonp: "jsonp_callback"
				},
				success: function(data){
					data = eval(data);
					page['current_poll'].empty().html($(unescape(data.data)));
					setUpVoting();
				}
			});
			return false;
		});*/
	});
	$("div.large_poll a.again").unbind().bind("click", function() {
		$parent = $(this).parents("div.large_poll");
		$.ajax({
			type: "GET",
			url: "/modules/fetch_module.php",
			data: {
				_file : "polls",
				_function : "poll_content",
				poll_id : $parent.find("input.poll_id").attr("value"),
				poll_title : $parent.find("h4").text(),
				show_results : 'false',
				results_width : 190,
				image_size : '200x200',
				limit : 1,
				offset : 0,
				dataType: "jsonp",
				jsonp: "jsonp_callback"
			},
			success: function(data){
				data = eval(data);
				$parent.empty().html($(unescape(data.data)));
				setUpVoting();
			}
		});
		return false;
	});
	$("div.poll a.again").unbind().bind("click", function() {
		$parent = $(this).parents("div.poll");
		$.ajax({
			type: "GET",
			url: "/modules/fetch_module.php",
			data: {
				_file : "polls",
				_function : "poll_content",
				poll_id : $parent.find("input.poll_id").attr("value"),
				poll_title : $parent.find("h4").text(),
				show_results : 'false',
				results_width : 116,
				image_size : '120x120',
				limit : 1,
				offset : 0,
				dataType: "jsonp",
				jsonp: "jsonp_callback"
			},
			success: function(data){
				data = eval(data);
				$parent.empty().html($(unescape(data.data)));
				setUpVoting();
			}
		});
		return false;
	});
}

function gotoPoll(data) {
	data = eval(data);
	switch($("body").attr("id")) {
		case "home":
			window.location = "/polls/?results=true";
		break;
		default:
			if($("input.poll_id[value=" + data['poll']['data'][0]['POLL_ID'] + "]").parents("div.poll").find("div.panel")['length'] == 0) {
				destination = $("input.poll_id[value=" + data['poll']['data'][0]['POLL_ID'] + "]").parents("div.large_poll").find("div.panel");
			} else {
				destination = $("input.poll_id[value=" + data['poll']['data'][0]['POLL_ID'] + "]").parents("div.poll").find("div.panel");
			}
			populatePoll(data,destination);
		break;
	}
}

$(document).ready(function () {
	/*$("div.poll div.wide span").bind("mouseover",function () {
		console.group("poll_option");
		console.log($(this).parents("div.poll").find("span").size());
		console.log($(this).parents("div.poll").find("div.thin li").size());
		thisIndex = $(this).parents("div.poll").find("span").index(this);
		console.log(thisIndex);
		$(this).parents("div.poll").find("div.thin li").removeClass("active").eq(thisIndex).addClass("active");
	}).bind("mouseout",function () {
		thisIndex = 0;
		console.log(thisIndex);
		$(this).parents("div.poll").find("div.thin li").removeClass("active").eq(thisIndex).addClass("active");
		console.groupEnd();
	});
	$("div.poll div.wide span input").bind("click",function () {
		$(this).parents("div.poll").find("div.wide span").unbind();
	});*/
	setUpVoting();
});