﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="../Common/AjaxCommonBase.js" />

Type.registerNamespace("Crown.Web.Ajax.Controls");

// --------------------------------------------------------------------------
// Vote Ctrl
// --------------------------------------------------------------------------
Crown.Web.Ajax.Controls.Vote = function(element) 
{
    Crown.Web.Ajax.Controls.Vote.initializeBase(this, [element]);
    this._answers = null;
    this._voteButton = null;
    this._showResultsButton = null;
    this._voteClickHandler = Function.createDelegate(this, this._onVoteClick);
    this._showResultsClickHandler = Function.createDelegate(this, this._onShowResultsClick);
    this._answerChangeHandler = Function.createDelegate(this, this._onAnswerChange);
    this._resultResponseHandler = Function.createDelegate(this, this._onResultResponse);
}
Crown.Web.Ajax.Controls.Vote.prototype = 
{
    initialize: function() 
    {
		Crown.Web.Ajax.Controls.Vote.callBaseMethod(this, 'initialize');
        
        // Get a handle to our vote button and setup the onclick handler
        if ((this._voteButton = $get(this.get_id() + "_VoteButton", this.get_element())) != null)
			$addHandler(this._voteButton, "click", this._voteClickHandler);
			
		// Get a handle to our show results and setup the onclick handler
        if ((this._showResultsButton = $get(this.get_id() + "_ShowResults", this.get_element())) != null)
			$addHandler(this._showResultsButton, "click", this._showResultsClickHandler);
    },
    dispose: function() 
    {
		delete this._voteClickHandler;
		delete this._showResultsClickHandler;
		delete this._answerChangeHandler;
		delete this._serverResponseHandler;
		
        Crown.Web.Ajax.Controls.Vote.callBaseMethod(this, 'dispose');
    },
    
    // Gets the list of vote answers for the control
    get_Answers: function()
    {
        // Create an array to hold the answers if one does not exist
        if (this._answers == null)
            this._answers = [];
        
        // Return the list of answers
        return this._answers;
    },
    
    // Called when the user changes the selected answers
    _onAnswerChange: function(eventArgs)
    {
		var answersSelected = false;
		
		// Determine if we have any selected answers
		for (var index = 0; index < this.get_Answers().length; index ++)
		{
			if (this.get_Answers()[index].get_Selected() == true)
			{
				answersSelected = true;
				break;
			}
		}
		
		// Enable / disable our vote button
		this._voteButton.disabled = !answersSelected;
    },
    
    // Called when the user clicks on the vote button
    _onVoteClick: function(eventArgs)
    {
        var selectedAnswers = [];
        
        // Add each answer value to our array of selected answers
        for (var index = 0; index < this.get_Answers().length; index ++)
        {
            // Do not add non-selected answers to the response
            if (this.get_Answers()[index].get_Selected())
                Array.add(selectedAnswers, this.get_Answers()[index].get_Value());
        }
        
        // Send the selected answers to the server
        this.performCallback("VoteCommand", selectedAnswers, this._resultResponseHandler);
        eventArgs.preventDefault();
        eventArgs.stopPropagation();
    },
    
    // Called when the user clicks on the show results button
    _onShowResultsClick : function(eventArgs)
    {
		// Send the selected answers to the server
        this.performCallback("ShowResults", null, this._resultResponseHandler);
        eventArgs.preventDefault();
        eventArgs.stopPropagation();
    },
    
    _onResultResponse: function(graphUrl)
    {
		var element = null;
		
		// Hide the vote container
        if ((element = $get(this.get_id() + "_VoteContainer", this.get_element())) != null)
			element.style.display = "none";
			
		// Show the result container
		if ((element = $get(this.get_id() + "_ResultContainer", this.get_element())) != null)
		{
			element.style.display = "block";
			
			// Set the image url for our graph
			if ((element = $get(this.get_id() + "_ResultImage", this.get_element())) != null)
				element.src = graphUrl;
		}
    }
}
Crown.Web.Ajax.Controls.Vote.registerClass('Crown.Web.Ajax.Controls.Vote', Crown.Web.Ajax.Controls.AjaxControlBase);

// --------------------------------------------------------------------------
// Vote Answer
// --------------------------------------------------------------------------
Crown.Web.Ajax.Controls.VoteAnswerCtrl = function(element) 
{
    Crown.Web.Ajax.Controls.VoteAnswerCtrl.initializeBase(this, [element]);
    this._displayText = "";
    this._value = "";
    this._voteCount = 0;
    this._graphColour = "";
    this._selected = false;
    this._owner = null;
    this._clickHandler = Function.createDelegate(this, this._onClick);
}
Crown.Web.Ajax.Controls.VoteAnswerCtrl.prototype = 
{
    initialize: function() 
    {
        Crown.Web.Ajax.Controls.VoteAnswerCtrl.callBaseMethod(this, 'initialize');
        
        // Add this control to the owner's collection
        Array.add(this.get_owner().get_Answers(), this);
        
        // Setup the owner control's event handler for the check changed event
        this.add_checkChanged(this.get_owner()._answerChangeHandler);
        
        // Add an property change event handler so we can update whether we are selected or not
        $addHandler(this.get_element(), "click", this._clickHandler);
    },
    dispose: function() 
    {   
		var element = this.get_element();
		
		// Remove our DOM event handlers
		if (element != null)
			$removeHandler(element, "click", this._clickHandler);
		
		// Remove our checkChanged event handler
		this.remove_checkChanged(this.get_owner()._answerChangeHandler);
		
        Crown.Web.Ajax.Controls.VoteAnswerCtrl.callBaseMethod(this, 'dispose');
    },
    // Gets or sets the text for the answer
    get_Text: function () { return this._displayText; },
    set_Text: function (value) { this._displayText = value; },
    
    // Gets or sets the value for the answer
    get_Value: function () { return this._value; },
    set_Value: function (value) { this._value = value; },
    
    // Gets or sets the vote count for the answer
    get_Votes: function () { return this._voteCount; },
    set_Votes: function (value) { this._voteCount = value; },
    
    // Gets or sets the graph colour for the answer
    get_GraphColour: function () { return this._graphColour; },
    set_GraphColour: function (value) { this._graphColour = value; },
    
    // Gets or sets whether the answer is selected
    get_Selected: function () { return this._selected; },
    set_Selected: function (value) 
    {
		// Do nothing if we're not changing the value
		if (this._selected == value)
			return;
			 
		// Save the new value and raise the check changed event
		this._selected = value; 
		this.raiseCheckChanged();
	},
    
    // Gets or sets the control to which this class belongs
    get_owner: function () { return this._owner; },
    set_owner: function (value) { this._owner = value; },
    
    // Gets or sets the oncheckchanged event handler
    add_checkChanged : function(handler) { this.get_events().addHandler("checkChanged", handler); },
    remove_checkChanged : function(handler) { this.get_events().removeHandler("checkChanged", handler); },
    raiseCheckChanged : function() 
    {
		var handler = null;
		
		// Raise the event if we have any listeners
        if ((handler = this.get_events().getHandler("checkChanged")) != null)
            handler(this, Sys.EventArgs.Empty);
    },
    
    // Handles the UI onclick event
    _onClick: function (eventArgs)
    {
        this.set_Selected(eventArgs.target.checked);
    }
}
Crown.Web.Ajax.Controls.VoteAnswerCtrl.registerClass('Crown.Web.Ajax.Controls.VoteAnswerCtrl', Crown.Web.Ajax.Controls.AjaxControlBase);

// --------------------------------------------------------------------------
// Script registration
// --------------------------------------------------------------------------
if (typeof(Sys) !== 'undefined') 
	Sys.Application.notifyScriptLoaded();
Type.registerNamespace('Crown.Web.Ajax.Controls');Crown.Web.Ajax.Controls.Resource={"ShowResultsText":"Show Results"};
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();