﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("WindowsSite");

WindowsSite.Poll = function(element) {
    WindowsSite.Poll.initializeBase(this, [element]);
}

WindowsSite.Poll.prototype = {
	/* Parameters */
	PollID: null,
	PollServiceProxy: null,
	OptionIDs: null,
	SubmitShowIDs: null,
	SubmitHideIDs: null,
	SubmitButton: null,
	RedoButton: null,
	WaitIndicator: null,
	Total: null,
	MaxBarPixels: null,

	/* ASP.NET Ajax accessors */
	get_PollID: function() { return this.PollID; },
	set_PollID: function(value) { this.PollID = value; },

	get_OptionIDs: function() { return this.OptionIDs; },
	set_OptionIDs: function(value) { this.OptionIDs = value; },

	get_SubmitShowIDs: function() { return this.SubmitShowIDs; },
	set_SubmitShowIDs: function(value) { this.SubmitShowIDs = value; },

	get_SubmitHideIDs: function() { return this.SubmitHideIDs; },
	set_SubmitHideIDs: function(value) { this.SubmitHideIDs = value; },

	get_SubmitButton: function() { return this.SubmitButton; },
	set_SubmitButton: function(value) { this.SubmitButton = value; },

	get_RedoButton: function() { return this.RedoButton; },
	set_RedoButton: function(value) { this.RedoButton = value; },

	get_WaitIndicator: function() { return this.WaitIndicator; },
	set_WaitIndicator: function(value) { this.WaitIndicator = value; },

	get_Total: function() { return this.Total; },
	set_Total: function(value) { this.Total = value; },

	get_MaxBarPixels: function() { return this.MaxBarPixels; },
	set_MaxBarPixels: function(value) { this.MaxBarPixels = value; },

	/* ASP.NET Ajax events */

	/* Event Handlers */
	_onSubmitClicked: function(args) {
		this.WaitIndicator.style.display = 'block';
		this.PollServiceProxy.SubmitPollVote(this.PollID, this.getSelectedID());
	},

	_onRedoClicked: function(args) {
		this.showAndHideRedoElements();
	},

	_onServiceSucceeded: function(result, userContext, methodName) {
		this.Total.innerHTML = result.TotalVotes;
		this.refreshPercentElements(result);
		this.showAndHideSubmitElements();
		this.WaitIndicator.style.display = 'none';
	},

	_onServiceFailed: function(result, userContext, methodName) {
		this.WaitIndicator.style.display = 'none';
	},

	/* Functions */
	refreshPercentElements: function(result) {
		for (var i in this.OptionIDs) {
			var percentageText = $get(this.OptionIDs[i].TextID);
			var percentageBar = $get(this.OptionIDs[i].BarID);
			var answerData = result.Answers[i];
			if (percentageBar && percentageText && answerData) {
				var percent = answerData.Votes / result.TotalVotes;
				percentageText.innerHTML = Math.round(percent * 100) + '%';
				percentageBar.style.width = Math.round(this.MaxBarPixels * percent) + 'px';
			}
		}
	},

	showAndHideSubmitElements: function() {
		for (var i in this.SubmitHideIDs) {
			var el = $get(this.SubmitHideIDs[i]);
			if (el) el.style.display = 'none';
		}
		for (var i in this.SubmitShowIDs) {
			var el = $get(this.SubmitShowIDs[i]);
			if (el) el.style.display = '';
		}
	},

	showAndHideRedoElements: function() {
		for (var i in this.SubmitHideIDs) {
			var el = $get(this.SubmitHideIDs[i]);
			if (el) el.style.display = '';
		}
		for (var i in this.SubmitShowIDs) {
			var el = $get(this.SubmitShowIDs[i]);
			if (el) el.style.display = 'none';
		}
	},

	getSelectedID: function() {
		for (var i in this.OptionIDs) {
			var radioButton = $get(this.OptionIDs[i].InputID);
			if (radioButton && radioButton.checked) return this.OptionIDs[i].AnswerID;
		}
		return null;
	},

	getSelectedText: function() {
		for (var i in this.OptionIDs) {
			var radioButton = $get(this.OptionIDs[i].InputID);
			if (radioButton && radioButton.checked) return this.OptionIDs[i].AnswerText;
		}
		return null;
	},

	initialize: function() {
		WindowsSite.Poll.callBaseMethod(this, 'initialize');

		this._serviceSucceeded = Function.createDelegate(this, this._onServiceSucceeded);
		this._serviceFailed = Function.createDelegate(this, this._onServiceFailed);
		this._submitDelegate = Function.createDelegate(this, this._onSubmitClicked);
		this._redoDelegate = Function.createDelegate(this, this._onRedoClicked);

		this.PollServiceProxy = new WindowsSite.Services.PollService();
		this.PollServiceProxy.set_defaultSucceededCallback(this._serviceSucceeded);
		this.PollServiceProxy.set_defaultFailedCallback(this._serviceFailed);
		$addHandler(this.SubmitButton, 'click', this._submitDelegate);
		$addHandler(this.RedoButton, 'click', this._redoDelegate);
	},
	dispose: function() {
		$clearHandlers(this.SubmitButton);
		$clearHandlers(this.RedoButton);
		this._serviceSucceeded = this._serviceFailed = this._submitDelegate = this._redoDelegate = null;
		while (this.OptionIDs.length > 0) this.OptionIDs.pop();
		while (this.SubmitShowIDs.length > 0) this.SubmitShowIDs.pop();
		while (this.SubmitHideIDs.length > 0) this.SubmitHideIDs.pop();
		this.PollID = this.PollServiceProxy = this.OptionIDs = this.SubmitShowIDs = this.SubmitHideIDs = this.SubmitButton = this.RedoButton = this.WaitIndicator = this.Total = null;

		WindowsSite.Poll.callBaseMethod(this, 'dispose');
	}
}
WindowsSite.Poll.registerClass('WindowsSite.Poll', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
