﻿/// <reference name="MicrosoftAjax.js"/>

// Register the namespace for the control.
Type.registerNamespace('Snowfriends');

//
// Define the control properties.
//
Snowfriends.Rating = function(element) {
    Snowfriends.Rating.initializeBase(this, [element]);
    this._divForeground = null;
    this._divButtons = null;
    this._currentRating = null;
}

//
// Create the prototype for the control.
//

Snowfriends.Rating.prototype = {


    initialize: function() {
        Snowfriends.Rating.callBaseMethod(this, 'initialize');

        //attach the necessary event handlers
        var buttons = $get(this.get_divButtons(), this.get_element()).getElementsByTagName("A");
        for (var i = 0; i < buttons.length; i++) {
            $addHandler(buttons[i], 'mouseover', Function.createDelegate(this, this._onMouseOver));
            $addHandler(buttons[i], 'click', Function.createDelegate(this, this._onMouseClick));
        }
        $addHandler(this.get_element(), 'mouseout', Function.createDelegate(this, this._onMouseOut));

        //get the current rating (width of the foreground) and store it for mouseout
        var foreground = $get(this.get_divForeground(), this.get_element());
        this.set_currentRating(foreground.style.width);
    },

    dispose: function() {
        $clearHandlers(this.get_element());

        var buttons = $get(this.get_divButtons(), this.get_element()).getElementsByTagName("A");
        for (var i = 0; i < buttons.length; i++) {
            $clearHandlers(buttons[i]);
        }

        Snowfriends.Rating.callBaseMethod(this, 'dispose');
    },

    //update the rating display (set the width of the foreground div)
    updateRating: function(rating) {
        var foreground = $get(this.get_divForeground(), this.get_element());
        foreground.style.width = (rating * 7 + 4) + "px";
    },

    //
    // Event delegates
    //
    _onMouseOver: function(e) {
        if (e.target.getAttribute("rel") == null)
            return;
        var rating = parseInt(e.target.getAttribute("rel"));
        this.updateRating(rating);
    },

    _onMouseOut: function(e) {
    //this.updateRating(this.get_currentRating());
        var foreground = $get(this.get_divForeground(), this.get_element());
        foreground.style.width = this.get_currentRating();
    },

    _onMouseClick: function(e) {
        this.dispose();
    },

    //
    // Control properties
    //
    get_divForeground: function() {
        return this._divForeground;
    },

    set_divForeground: function(value) {
        if (this._divForeground !== value) {
            this._divForeground = value;
            this.raisePropertyChanged('divForeground');
        }
    },

    get_divButtons: function() {
        return this._divButtons;
    },

    set_divButtons: function(value) {
        if (this._divButtons !== value) {
            this._divButtons = value;
            this.raisePropertyChanged('divButtons');
        }
    },

    get_currentRating: function() {
        return this._currentRating;
    },

    set_currentRating: function(value) {
        if (this._currentRating !== value) {
            this._currentRating = value;
            this.raisePropertyChanged('currentRating');
        }
    }
}

// Register the class as a type that inherits from Sys.UI.Control.
Snowfriends.Rating.registerClass('Snowfriends.Rating', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
