﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("WindowsSite");

WindowsSite.Ratings = function(element) {
    WindowsSite.Ratings.initializeBase(this, [element]);
}

WindowsSite.Ratings.prototype = {
	/* Parameters */
	Items: null,
	RatingServiceProxy: null,
	ArticleID: null,
	AverageRating: null,
	NormalCss: null,
	HoverCss: null,
	SelectedCss: null,
	SelectedCss: null,
	WaitIndicator: null,
	MouseTimer: null,
	RatingClicked: false,
	StarContainer: null,
	LabelBefore: null,
	LabelAfter: null,

	/* ASP.NET Ajax accessors */
	get_ArticleID: function() { return this.ArticleID; },
	set_ArticleID: function(value) { this.ArticleID = value; },

	get_AverageRating: function() { return this.AverageRating; },
	set_AverageRating: function(value) { this.AverageRating = value; },

	get_NormalCss: function() { return this.NormalCss; },
	set_NormalCss: function(value) { this.NormalCss = value; },

	get_HoverCss: function() { return this.HoverCss; },
	set_HoverCss: function(value) { this.HoverCss = value; },

	get_SelectedCss: function() { return this.SelectedCss; },
	set_SelectedCss: function(value) { this.SelectedCss = value; },

	get_StarContainer: function() { return this.StarContainer; },
	set_StarContainer: function(value) { this.StarContainer = value; },

	get_WaitIndicator: function() { return this.WaitIndicator; },
	set_WaitIndicator: function(value) { this.WaitIndicator = value; },

	get_LabelBefore: function() { return this.LabelBefore; },
	set_LabelBefore: function(value) { this.LabelBefore = value; },

	get_LabelAfter: function() { return this.LabelAfter; },
	set_LabelAfter: function(value) { this.LabelAfter = value; },

	/* Event Handlers */
	_onItemMouseMove: function(args) {
		if (!this.RatingClicked) {
			clearTimeout(this.MouseTimer);
			var cssClass = this.HoverCss;
			for (var i in this.Items) {
				this.Items[i].className = cssClass;
				if (this.Items[i] == args.target) cssClass = this.NormalCss;
			}
		}
	},

	_onItemMouseOut: function(args) {
		if (!this.RatingClicked) {
			this.MouseTimer = setTimeout(this._timerDelegate, 100);
		}
	},

	_onItemClick: function(args) {
		this.WaitIndicator.style.display = 'block';
		this.RatingClicked = true;
		var score = null;
		for (var i in this.Items) {
			this.Items[i].style.cursor = 'default';
			$clearHandlers(this.Items[i]);
			if (this.Items[i] == args.target) score = parseInt(i) + 1;
		}
		if (score != null) this.RatingServiceProxy.SubmitRating(this.ArticleID, score);
	},

	_onMouseTimerCompleted: function() {
		if (!this.RatingClicked) {
			for (var i = 0; i < this.Items.length; i++) {
				this.Items[i].className = (i < this.AverageRating) ? this.SelectedCss : this.NormalCss;
			}
		}
	},

	_onServiceSucceeded: function(result, userContext, methodName) {
		this.WaitIndicator.style.display = 'none';
		this.LabelBefore.style.display = 'none';
		this.LabelAfter.style.display = 'block';
	},

	_onServiceFailed: function(result, userContext, methodName) {
		this.WaitIndicator.style.display = 'none';
	},

	/* Functions */
	getRatingControls: function() {
		var items = new Array();
		var controlChildren = this.StarContainer.childNodes;
		for (var i = 0; i < controlChildren.length; i++) {
			if (controlChildren[i].tagName && controlChildren[i].tagName.toLowerCase() == 'a')
				items.push(controlChildren[i]);
		}
		return items;
	},

	initialize: function() {
		WindowsSite.Ratings.callBaseMethod(this, 'initialize');

		this._mouseMoveDelegate = Function.createDelegate(this, this._onItemMouseMove);
		this._mouseOutDelegate = Function.createDelegate(this, this._onItemMouseOut);
		this._clickDelegate = Function.createDelegate(this, this._onItemClick);
		this._timerDelegate = Function.createDelegate(this, this._onMouseTimerCompleted);
		this._serviceSucceeded = Function.createDelegate(this, this._onServiceSucceeded);
		this._serviceFailed = Function.createDelegate(this, this._onServiceFailed);

		this.RatingServiceProxy = new WindowsSite.Services.RatingService();
		this.RatingServiceProxy.set_defaultSucceededCallback(this._serviceSucceeded);
		this.RatingServiceProxy.set_defaultFailedCallback(this._serviceFailed);

		this.Items = this.getRatingControls();
		for (var i in this.Items) {
			$addHandlers(this.Items[i], {
				'mousemove': this._mouseMoveDelegate,
				'mouseout': this._mouseOutDelegate,
				'click': this._clickDelegate
			});
		}
	},

	dispose: function() {
		this._mouseMoveDelegate = this._mouseOutDelegate = this._clickDelegate = this._timerDelegate = null;
		while (this.Items.length > 0) { $clearHandlers(this.Items.pop()); }
		this.Items = this.RatingServiceProxy = this.ArticleID = this.AverageRating = this.NormalCss = this.HoverCss = this.SelectedCss = this.WaitIndicator = this.MouseTimer = this.RatingClicked = this.StarContainer = this.LabelBefore = this.LabelAfter = null;

		WindowsSite.Ratings.callBaseMethod(this, 'dispose');
	}
}
WindowsSite.Ratings.registerClass('WindowsSite.Ratings', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
