new SmoothScroll();

window.addEvent('domready', function(){
	new Tips($$('.tooltipLink'), {
		className: 'playerNameTool'
	});
	
	$('mailLink').innerHTML = ['<','a',' ','h','r','e','f','=','"','m','a','i','l','t','o',':','t','f','2','@','4','f','i','t','e','.','c','o','m','"','>','c','o','n','t','a','c','t','<','/','a>'].join("");
});

BlankingInput = new Class({
	initialize: function (node, originalValue) {
		this.node = $(node);
		this.originalValue = originalValue||this.node.value;

		if (!this.originalValue)	//	no value to default to, don't add focus/blur events
			return;

		this.node.addEvent('focus', this.onFocus.bindAsEventListener(this));
		this.node.addEvent('blur', this.onBlur.bindAsEventListener(this));
	},

	onFocus: function (evt) {
		if (this.node.value == this.originalValue)
			this.node.value = "";
	},

	onBlur: function (evt) {
		if (this.node.value == "")
			this.node.value = this.originalValue;
	}
});

TablePlusColumn = new Class({
	initialize: function (table, th, index, sorter) {
		this.table = table;
		this.th = th;
		this.index = index;
		this.asc = false;
		this.sorter = sorter ? sorter : function (a, b) {
			var av, bv;
			if (!(av = parseFloat(a.value.replace(/,/g,""))))
				av = 0;

			if (!(bv = parseFloat(b.value.replace(/,/g,""))))
				bv = 0;

			return av - bv;
		};

		var a = new Element('a', {
			href: 'javascript:;'
		});

		while (th.childNodes.length)
			a.appendChild(th.childNodes[0]);

		a.addEvent('click', this.go.bindAsEventListener(this));

		th.appendChild(a);
	},

	go: function (evt) {
		if (evt)
		{
			var evt = new Event(evt);
			evt.target.blur();
		}

		var rows = this.table.tbody.childNodes;
		var values = [];
		for (var i = rows.length; i--;)
		{
			var row = rows[i];
			var value = row.getChildren()[this.index].getText();
			values.push({
				value: value,
				row: row
			});
		}

		values.sort(this.sorter);
		if (!this.asc)
			values.reverse();

		for (var i = values.length; i--;)
		{
			var value = values[i];
			this.table.tbody.insertBefore(value.row, this.table.tbody.firstChild);
		}
	}
});

TablePlusRow = new Class({
	initialize: function (table, tr, index) {
		this.table = table;
		this.tr = $(tr);
		this.index = index;

		tr.addEvent('mouseover', this.over.bindAsEventListener(this));
		tr.addEvent('mouseout', this.out.bindAsEventListener(this));
	},

	over: function (evt) {
		this.tr.addClass('over');
	},

	out: function (evt) {
		this.tr.removeClass('over');
	}
});

TablePlusTable = new Class({
	initialize: function (id) {
		var table = this.tableNode = $(id);
		var thead = this.head = table.getElement('thead');
		this.tbody = table.getElement('tbody');

		this.columns = [];
		var ths = thead.getElements('th');
		for (var i = ths.length; i--;)
		{
			var th = ths[i];
			if (th.getProperty('rel') == 'nosort') { continue; }
			if (!th.childNodes.length) { continue; }
			var column = new TablePlusColumn(this, th, i);
			this.columns[i] = column;
		}

		this.rows = [];
		var trs = this.tbody.getElements('tr');
		for (var i = trs.length; i--;)
		{
			var tr = trs[i];
			this.rows.push(new TablePlusRow(this, tr, i));
		}
	}
});

MooForm = new Class({
	initialize: function (form, action, messageNode) {
		this.form = $(form);
		this.form.action = action;
		this.messageNode = $(messageNode);
		this.submitMessage = "Please wait...";
		this.form.addEvent('submit', this.onSubmit.bindAsEventListener(this));
		this.busy = false;
	},

	onSubmit: function (evt) {
		var evt = new Event(evt);
		evt.stop();
		if (this.busy)
			return;

		this.busy = true;

		this.form.send({
				onSuccess: this.onTransportSuccess.bind(this),
				onFailure: this.onTransportFail.bind(this)
			});

		this.disableForm();

		this.message(this.submitMessage);
	},

	disableForm: function () {
		this.disabledFields = [];
		for (var i = this.form.elements.length; i--;)
		{
			var ele = this.form.elements[i];

			ele.disabled = true;
			this.disabledFields.push(ele);
		}
	},

	enableForm: function () {
		for (var i = this.disabledFields.length; i--;)
			this.disabledFields[i].disabled = false;
	},

	onTransportComplete: function () {
		this.message("");
		this.busy = false;
		this.enableForm();
	},

	onTransportSuccess: function (text) {
		try
		{
			var result = Json.evaluate(text);
		}
		catch (e)
		{
			this.onTransportFail();
			return;
		}

		this.onTransportComplete();

		if (result.message)
			this.message(result.message);

		if (result.success)
			this.onActionSuccess(result);
		else
			this.onActionFail(result);
	},

	onTransportFail: function () {
		this.onTransportComplete();
		this.message("An error occurred, try again later.");
	},

	onActionSuccess: function (result) {
	},

	onActionFail: function (result) {
	},

	message: function (text) {
		this.messageNode.setText(text);
	}
});

pinProfile = function (id) {
	var xhr = new XHR({
			method: 'post',
			onSuccess: function(text){
				try { var result = Json.evaluate(text); }
				catch (e) { return; }
				if (result.success)
					location.href = "/";
			}
		});

	xhr.send('/hamster.php', Object.toQueryString({ hamster: 'setMySteamId', remember: 1, steamid: id }));
};

