﻿Type.registerNamespace("PitchPuff.UI");

// ----------------------------------------------------------------------------

PitchPuff.UI.CategorySelect = function(associatedElement) {
	PitchPuff.UI.CategorySelect.initializeBase(this, [associatedElement]);
}
PitchPuff.UI.CategorySelect.prototype = {

	_spaces: "                             ",
	_hasDefaultText: false,
	_previousIndex: 0,
	_selectionIndent: 0,

	initialize: function() {
			PitchPuff.UI.CategorySelect.callBaseMethod(this, "initialize");

			// if there is a default text remove on first change
			var options;
			if(options = this.get_element().options)
				if(options.length > 0 && options[0].className == "defaultText")
					this._hasDefaultText = true;

			// add handler to remove default /
			$addHandlers(this.get_element(), {
					change: this._selectionChanged
				}, this);

			this._selectionChanged(null, true);
		},
	dispose: function() {
			$clearHandlers(this.get_element());
			PitchPuff.UI.CategorySelect.callBaseMethod(this, "dispose");
		},

	_selectionChanged: function(e, init) {
			var element = this.get_element();

			if(this._hasDefaultText && !init) {
				element.removeChild(element.options[0]);
				this._hasDefaultText = false;
			}

			// restore 'indent' of previous selection
			if(this._selectionIndent > 0)
				element.options[this._previousIndex].text = this._spaces.substr(0, this._selectionIndent) + element.options[this._previousIndex].text;

			// save indent and index of current selection
			this._previousIndex = this.get_element().selectedIndex;
			var indent = this._textOffset(element.options[this._previousIndex].text);
			if(indent > 0) {
				this._selectionIndent = indent;
				element.options[this._previousIndex].text = element.options[this._previousIndex].text.substr(indent);
			}
			else
				this._selectionIndent = 0;
		},

	_textOffset: function(str) { // because of safari has poor regex support (see: http://aptana.com/reference/api/RegExp.html)
			var i = 0;
			while(i < str.length && (str.charCodeAt(i) == 160 || str.charCodeAt(i) == 32))
				i++;
			return i;
		}
}
PitchPuff.UI.CategorySelect.registerClass("PitchPuff.UI.CategorySelect", Sys.UI.Control);

// ----------------------------------------------------------------------------

PitchPuff.UI.UsernameCheck = function(associatedElement) {
	PitchPuff.UI.UsernameCheck.initializeBase(this, [associatedElement]);
}
PitchPuff.UI.UsernameCheck.prototype = {

	ControlToValidate: null,
	_controlToValidate: null,
	LinkID: null,
	_link: null,

	initialize: function() {
			PitchPuff.UI.UsernameCheck.callBaseMethod(this, "initialize");

			this._controlToValidate = $get(this.ControlToValidate);
			this._link = $get(this.LinkID);

			$addHandlers(this._link, { click: this._checkClicked }, this);
		},

	dispose: function() {
			$clearHandlers(this.get_element());
			PitchPuff.UI.UsernameCheck.callBaseMethod(this, "dispose");
		},

	_checkClicked: function(e) {
			// run any other validators first
			var valid = true, o;
			if(typeof(o = this._controlToValidate.Validators) != 'undefined') {
				for(i=0; i < o.length; i++)
				{
					ValidatorValidate(o[i], null, e);
					if(!(valid = o[i].isvalid))
						break;
				}
			}

			if(valid)
				PageMethods.UserExists(this._controlToValidate.value, this._checkClicked_Callback, null, this);
		},

	_checkClicked_Callback: function(result, context) {
			if(result)
				context._setText("Username is unavailable.", "red");
			else
				context._setText("Username is available.", "green");
		},

	_setText: function(text, color) {
			this.get_element().style.color = color;
			this.get_element().innerText = text;
			this.get_element().style.display = "";
		}
}
PitchPuff.UI.UsernameCheck.registerClass("PitchPuff.UI.UsernameCheck", Sys.UI.Behavior);

// ----------------------------------------------------------------------------

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
