var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;

function submitPoll(pid, isSmall){
	var contId = isSmall ? "smallpoll_" + pid : "poll_" + pid;
	var poll = Dom.get(contId);
	var response = '';
	var callback = 
	{ 
		success: function(o) {
			showResult(pid, o.responseText,isSmall);
		}, 
		failure: function(o){
			alert("Unable to submit poll. Please try again. Error:" + o.statusText)
		}
	}
	var pollform = isSmall ? document.smallqpform.elements : document.quickpollform.elements;
	var optId = isSmall ? "smallpolla_" + pid : "polla_" + pid;

	for(var i=0;i< pollform.length;i++){
		if (pollform[i].name == optId){
			if (pollform[i].checked){
				response = pollform[i].value;
				break;
			}
		}
	}
	
	if (response == ''){
		alert("Please enter a choice!");
		return;
	}
	
	var data = "pid=" + pid + "&response=" + response;
	
	//show loading
	poll.innerHTML = "<div class='smaller'>submitting..<img src='/images/loading.gif'/></div>"
	YAHOO.util.Connect.asyncRequest('POST', '/pointmaker/submit_poll.php', callback, data);
}

function showResult(pid, result, isSmall){
	var contId = isSmall ? "smallpoll_" + pid : "poll_" + pid;
	var poll = Dom.get(contId);
	if (result == undefined){
		poll.innerHTML = "<div class='smaller'>loading..<img src='/images/loading.gif'/></div>"
		var callback = 
		{ 
			success: function(o) {
				showResult(pid, o.responseText,isSmall);
			}, 
			failure: function(o){
				alert("Unable to get poll results. Please try again. Error:" + o.statusText)
			}
		}
		var data = "pid=" + pid;
		YAHOO.util.Connect.asyncRequest('POST', '/pointmaker/submit_poll.php', callback, data);
	}
	else{
		var graph = '';
		var results = result.split("|");
		
		//results[0] is the total vote; results[1] is the max count
		var total = parseInt(results[0]);
		var max = parseInt(results[1]);
		var percentageSum = 0;
		var percentages = new Array();
		
		//cal the percentages
		for (var i=2;i<results.length;i++){
			info = results[i].split('>>'); //info[0]=id;[1]=question;2=count
			percentages[i] = Math.round(info[2]/total*100);
			percentageSum += percentages[i];
		}
		while (percentageSum != 100){ //we have to address the rounding error
			if (percentageSum > 100){
				for (var i in percentages){
					if (percentages[i] > 0){
						percentages[i]--; //minus one to balance the sum
						percentageSum--;
						break;
					}
				}
			}
			else if (percentageSum < 100){
				for (var i in percentages){
					if (percentages[i] > 0){
						percentages[i]++; //minus one to balance the sum
						percentageSum++;
						break;
					}
				}
			}
			else break; //something wrong with the number, break to avoid loop
		}
		
		for (var i=2;i<results.length;i++){
			info = results[i].split('>>'); //info[0]=id;[1]=question;2=count
			var width = 0;
			if (max > 0) width = Math.round(info[2]/max*100);
			if (width==0) width=1;
			graph += "<div>"+info[1]+"</div>";
			graph += "<li><span class='bar' style='width: "+width+"%;'>100</span>";
			
			graph += total != 0 ? "<span class='barLabel'>"+percentages[i]+"%</span></li>" : "<span class='barLabel'>0%</span></li>";
		}
		poll.innerHTML = "<ol class='barchart'>" + graph +"</ol>";
	}
	
}

