﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("AjaxControls");

AjaxControls.SimpleVote = function(element) {

    AjaxControls.SimpleVote.initializeBase(this, [element]);

    var up_element = element.childNodes[0];
    for (var i = 0; i < up_element.childNodes.length; i++) {

        var node = up_element.childNodes[i];

        if (node.id) {
            if (node.id.match(/02$/)) {
                this._upBtn = $find(node.id);
            } else if (node.id.match(/03$/)) {
                this._downBtn = $find(node.id);
            }
        }

    }

    this._buttonsEnabled = true;

}

AjaxControls.SimpleVote.prototype = {

    //
    // Initializes object.
    //

    initialize: function() {

        AjaxControls.SimpleVote.callBaseMethod(this, 'initialize');

        if (this._upBtn) {
            this._upBtn.add_selected(this._btnSelected);
        }

        if (this._downBtn) {
            this._downBtn.add_selected(this._btnSelected);
        }

    },

    //
    // Gets the postback function.
    //

    get_postback: function() {
        return this._postback;
    },

    //
    // Sets the postback function.
    //
    // params:
    //  value       The new value.

    set_postback: function(value) {

        if (this._postback !== value) {
            this._postback = value;
            this.raisePropertyChanged('postback');
        }

    },

    //
    // Gets the buttonsEnabled value.
    //

    get_buttonsEnabled: function() {
        return this._buttonsEnabled;
    },

    //
    // Sets the buttonsEnabled value.
    //
    // params:
    //  value       The new value.

    set_buttonsEnabled: function(value) {

        if (this._buttonsEnabled !== value) {
            this._buttonsEnabled = value;
            this.raisePropertyChanged('buttonsEnabled');
        }

    },

    //
    // Handles the Selected event.
    //
    // params:
    //  obj         The object that raised the event.
    //  eventArgs   An EventArgs object containing event data.

    _btnSelected: function(obj, eventArgs) {

        var parent = $find(obj.get_parent().get_element().id);
        if (parent._upBtn === obj) {
            parent._downBtn.deselect();
        } else {
            parent._upBtn.deselect();
        }

    },

    //
    // Disposes this object and its references.
    //

    dispose: function() {

        for (var handler in this._handlers) {
            $removeHandler(this.get_element(), handler, this._handlers[handler]);
        }

        this._upBtn.remove_selected(this._btnSelected);
        this._downBtn.remove_selected(this._btnSelected);

        AjaxControls.SimpleVote.callBaseMethod(this, 'dispose');

    }

}

AjaxControls.SimpleVote.registerClass('AjaxControls.SimpleVote', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();