﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("AjaxControls");

AjaxControls.SimpleVoteButton = function(element) {

    AjaxControls.SimpleVoteButton.initializeBase(this, [element]);

    this._hoverCss = null;
    this._normalCss = null;
    this._clickedCss = null;
    this._isSelected = false;
    this._enabled = true;
    this._value = 0;
    this._link = null;
    this._postback = null;
    this._buttonType = null;
    this._size = null;
    this._hoverDiv = null;

    this._delegates = {
        mouseover: Function.createDelegate(this, this._mouseoverHandler),
        mouseout: Function.createDelegate(this, this._mouseoutHandler),
        mouseup: Function.createDelegate(this, this._mouseupHandler),
        mousemove: Function.createDelegate(this, this._mousemoveHandler)
    };

    $addHandlers(element, this._delegates);

}

AjaxControls.SimpleVoteButton.prototype = {

    // 
    // Initializes the control.
    //

    initialize: function() {

        /*
        // used for debugging purposes
        for (var i in this) {
        document.write(i + ":" + this[i] + "<br/>");
        }
        */

        AjaxControls.SimpleVoteButton.callBaseMethod(this, 'initialize');

        this._hoverCss = 'simpleVote_' + this._size + 'Button simpleVote_' + this._size + 'Text simpleVote_' + this._size + '' + this._buttonType + 'MouseOver';
        this._normalCss = 'simpleVote_' + this._size + 'Button simpleVote_' + this._size + 'Text simpleVote_' + this._size + '' + this._buttonType + 'Default';
        this._clickedCss = 'simpleVote_' + this._size + 'Button simpleVote_' + this._size + 'Text simpleVote_' + this._size + '' + this._buttonType + 'Clicked';

        this._link = $get(this.get_element().id + '_text');
        this._hoverDiv = $get(this.get_id() + '_hoverText');

        if (this._isSelected) {
            this.get_element().className = this._clickedCss;
        }

    },

    // 
    // Gets the button state.
    //

    get_enabled: function() {
        return this._enabled;
    },

    // 
    // Sets the button state.
    //
    // params:
    //  value       The new value.
    //

    set_enabled: function(value) {
        if (this._enabled !== Boolean(value)) {
            this._enabled = Boolean(value);
            this.raisePropertyChanged('enabled');
        }
    },

    // 
    // Gets the button size.
    //

    get_size: function() {
        return this._size;
    },

    // 
    // Sets the button size.
    //
    // params:
    //  value       The new value.
    //

    set_size: function(value) {
        if (this._size !== value) {
            this._size = value;
            this.raisePropertyChanged('size');
        }
    },

    // 
    // Gets the button type.
    //

    get_buttonType: function() {
        return this._buttonType;
    },

    // 
    // Sets the button type.
    //
    // params:
    //  value       The new value.
    //

    set_buttonType: function(value) {
        if (this._buttonType !== value) {

            this._buttonType = value;
            this.raisePropertyChanged('buttonType');

        }
    },

    //
    // 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 a value indicating whether or not this button is selected.
    //

    get_isSelected: function() {
        return this._isSelected;
    },

    //
    // Sets the selection state for this button. 
    //
    // params:
    //  value       The new value.
    //

    set_isSelected: function(value) {
        if (this._isSelected !== value) {

            this._isSelected = value;
            this.raisePropertyChanged('isSelected');

        }
    },

    //
    // Gets the postback function.
    //

    get_value: function() {
        return this._value;
    },

    //
    // Sets the value function.
    //
    // params:
    //  value       The new value.
    //

    set_value: function(value) {
        if (this._value !== value) {
            this._value = value;
            this.raisePropertyChanged('value');
        }
    },

    // 
    // Handles the mouseover event.
    //

    _mouseoverHandler: function(e) {

        if (!this._isSelected && this._enabled && this.get_element().className != this._hoverCss) {
            this.get_element().className = this._hoverCss;
        }
        
    },

    //
    // Handles the mouseout event.
    //

    _mouseoutHandler: function(e) {

        if (!this._isSelected && this._enabled) {
            this.get_element().className = this._normalCss;
        }

        //var data = this.get_element_data();

//        if (e.target.id == this.get_element().id ||
//           (e.clientX < data.left || e.clientX > (data.left + data.width)) ||
//           (e.clientY < data.top || e.clientY > (data.top + data.height))) {

            this._hoverDiv.style.display = 'none';

//        }

    },

    //
    // Gets element data.
    //

    get_element_data: function() {

        // grab left offset value:
        var left = 0;
        var top = 0;
        var width = this.get_element().offsetWidth;
        var height = this.get_element().offsetHeight;
        var obj = this.get_element();

        while (obj) {
            left += obj.offsetLeft;
            top += obj.offsetTop;
            obj = obj.offsetParent;
        }

        return {
            left: left,
            top: top,
            width: width,
            height: height
        };

    },

    //
    // Handles the mouseup event.
    //

    _mouseupHandler: function(e) {

        if (!this._isSelected && this._enabled) {

            this.get_element().className = this._clickedCss;
            this._isSelected = true;
            this._value++;
            this._link.innerHTML = (this._value > 9999) ? 9999 : this._value;
            this.onSelected(Sys.EventArgs.Empty);

            // we want to hide the hover text only when a selection is made
            this._hoverDiv.style.display = 'none';
            
            eval(this._postback);

        }

    },

    //
    // Handles the mousemove event.
    //

    _mousemoveHandler: function(e) {

        if (!(/^\s*$/.test(this._hoverDiv.childNodes[0].innerHTML))) {

            // used for debugging purposes
            /*
            for (var i in e) {
            document.write(i + ":" + e[i] + "<br/>");
            }
            */

            var data = this.get_element_data();

            this._hoverDiv.style.display = 'inline';
            this._hoverDiv.style.top = document.documentElement.scrollTop + data.top +((e.clientY - data.top)) - 31 + 'px';
            this._hoverDiv.style.left = data.left + ((e.clientX - data.left)) - 21 + 'px';
            //this._hoverDiv.innerHTML = 'scroll: ' + document.documentElement.scrollTop + '; left: ' + data.left + '; top: ' + data.top + '; width: ' + this.get_element().offsetWidth + '; offsetY: ' + e.offsetY + '; screenY: ' + e.screenY + '; clientX : ' + e.clientX + '; clientY: ' + e.clientY + ':' + this.get_element().offsetTop + '<br/>' + e.target.id + '<br />' + this.get_element().id;

        }

    },

    //
    // Adds a handler for the selected event.
    //
    // params:
    //  handler     The handler that is to be added.
    //

    add_selected: function(handler) {
        this.get_events().addHandler('selected', handler);
    },

    //
    // Removes the handler from the selected event.
    //
    // params:
    //  handler     The handler that is to be removed.
    //

    remove_selected: function(handler) {
        this.get_events().removeHandler('selected', handler);
    },

    //
    // Raises the Selected event.
    //

    onSelected: function(eventArgs) {

        var handler = this.get_events().getHandler('selected');

        if (handler) {
            handler(this, eventArgs);
        }

    },

    //
    // Puts the current object in a deselected state.
    //

    deselect: function() {
        if (this._isSelected) {
            this._isSelected = false;
            this._value--;
            this._link.innerHTML = (this._value > 9999) ? 9999 : this._value;
            this.get_element().className = this._normalCss;
        }
    },

    //
    // Disposes the current object and its references.
    //

    dispose: function() {

        for (var handler in this._handlers) {
            $removeHandler(this.get_element(), handler, this._handlers[handler]);
        }

        AjaxControls.SimpleVoteButton.callBaseMethod(this, 'dispose');

    }

}

AjaxControls.SimpleVoteButton.registerClass('AjaxControls.SimpleVoteButton', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();