var poll_id = 0;
var poll_real_id = 0;
var poll_answer_id = ""; 
var is_being_voted = false;

var is_side_nav = false;

pollsL10n.show_loading = parseInt(pollsL10n.show_loading);
pollsL10n.show_fading = parseInt(pollsL10n.show_fading);

function poll_vote(a) {
	
	// Figure our if this is a sideNav thing
	is_side_nav = poll_SideNav(String(a));
	poll_real_id = a;

	if (!is_being_voted) {
		set_is_being_voted(true);
		
		poll_id = a;
		if (is_side_nav) { 
			poll_real_id = parseInt(String(a).substring(5)); 
		}
		
		poll_answer_id = "";
		poll_multiple_ans = 0;
		poll_multiple_ans_count = 0;
		if (jQuery("#poll_multiple_ans_" + poll_id).length) {
			poll_multiple_ans = parseInt(jQuery("#poll_multiple_ans_" + poll_id).val());
		}
		jQuery("#polls_form_" + poll_id + " :checkbox, #polls_form_" + poll_id + " :radio").each(
		function(b) {
			if (jQuery(this).is(":checked")) {
				if (poll_multiple_ans > 0) {
					poll_answer_id = jQuery(this).val() + "," + poll_answer_id;
					poll_multiple_ans_count++;
				} else {
					poll_answer_id = parseInt(jQuery(this).val());
				}
			}
		});
		if (poll_multiple_ans > 0) {
			if (poll_multiple_ans_count > 0 && poll_multiple_ans_count <= poll_multiple_ans) {
				poll_answer_id = poll_answer_id.substring(0, (poll_answer_id.length - 1));
				poll_process();
			} else {
				if (poll_multiple_ans_count == 0) {
					set_is_being_voted(false);
					alert(pollsL10n.text_valid);
				} else {
					set_is_being_voted(false);
					alert(pollsL10n.text_multiple + " " + poll_multiple_ans);
				}
			}
		} else {
			if (poll_answer_id > 0) {
				poll_process();
			} else {
				set_is_being_voted(false);
				alert(pollsL10n.text_valid);
			}
		}
	} else {
		alert(pollsL10n.text_wait);
	}
}
function poll_process() {

	if (pollsL10n.show_fading) {
		jQuery("#polls-" + poll_id).fadeTo("def", 0, function() {
			if (pollsL10n.show_loading) {
				jQuery("#polls-" + poll_id + "-loading").show();
			}
			jQuery.ajax({
				type: "POST",
				url: pollsL10n.ajax_url,
				data: "vote=true&poll_id=" + poll_real_id + "&poll_" + poll_real_id + "=" + poll_answer_id,
				cache: false,
				success: poll_process_success
			})
		})
	} else {
		if (pollsL10n.show_loading) {
			jQuery("#polls-" + poll_id + "-loading").show();
		}
		jQuery.ajax({
			type: "POST",
			url: pollsL10n.ajax_url,
			data: "vote=true&poll_id=" + poll_real_id + "&poll_" + poll_real_id + "=" + poll_answer_id,
			cache: false,
			success: poll_process_success
		})
	}
}

function poll_SideNav(a) { 
	
	if (a.length < 6) { return false; }
	if (a.substring(0, 5)=='99999') { return true; }
	return false; 
	
}

function poll_result(a) {
	
	// Figure our if this is a sideNav thing
	is_side_nav = poll_SideNav(String(a));
	poll_real_id = a;
	
	var strSideNav = '0';
	if (is_side_nav) { 
		poll_real_id = String(a).substring(5); 
		strSideNav = '1';
	}
	
	if (!is_being_voted) {
		set_is_being_voted(true);
		poll_id = a;
		if (pollsL10n.show_fading) {
			jQuery("#polls-" + poll_id).fadeTo("def", 0, function() {
				if (pollsL10n.show_loading) {
					jQuery("#polls-" + poll_id + "-loading").show();
				}
				jQuery.ajax({
					type: "GET",
					url: pollsL10n.ajax_url,
					data: "pollresult=" + poll_real_id+'&is_side_nav='+strSideNav,
					cache: false,
					success: poll_process_success
				})
			});
		} else {
			if (pollsL10n.show_loading) {
				jQuery("#polls-" + poll_id + "-loading").show();
			}
			jQuery.ajax({
				type: "GET",
				url: pollsL10n.ajax_url,
				data: "pollresult=" + poll_real_id+'&is_side_nav='+strSideNav,
				cache: false,
				success: poll_process_success
			});
		}
	} else {
		alert(pollsL10n.text_wait);
	}
}

function poll_booth(a) {
	if (!is_being_voted) {
		set_is_being_voted(true);
		poll_id = a;
		if (pollsL10n.show_fading) {
			jQuery("#polls-" + poll_id).fadeTo("def", 0, function() {
				if (pollsL10n.show_loading) {
					jQuery("#polls-" + poll_id + "-loading").show();
				}
				jQuery.ajax({
					type: "GET",
					url: pollsL10n.ajax_url,
					data: "pollbooth=" + poll_real_id,
					cache: false,
					success: poll_process_success
				});
			});
		} else {
			if (pollsL10n.show_loading) {
				jQuery("#polls-" + poll_id + "-loading").show();
			}
			jQuery.ajax({
				type: "GET",
				url: pollsL10n.ajax_url,
				data: "pollbooth=" + poll_real_id,
				cache: false, success: poll_process_success
			})
		}
	} else {
		alert(pollsL10n.text_wait);
	}
}

function poll_process_success(a) {
	
	jQuery("#polls-" + poll_id).html(a);
	if (pollsL10n.show_loading) {
		jQuery("#polls-" + poll_id + "-loading").hide();
	}
	if (pollsL10n.show_fading) {
		jQuery("#polls-" + poll_id).fadeTo("def", 1, function() {
			set_is_being_voted(false);
		})
	} else { set_is_being_voted(false); }
}

function set_is_being_voted(a) { is_being_voted = a };