﻿function $C(tag)
{
	return document.createElement(tag);
}

Object.extend
(
	Event,
	{
		wheel: function(event)
		{
			var delta = 0;
			if (!event) event = window.event;
			if (event.preventDefault)
			{
				event.preventDefault();
				event.stopPropagation();
			}
			else
			{
				event.cancelBubble = true;
				event.returnValue = false;
			}
			if (event.wheelDelta)
			{
				delta = event.wheelDelta/120;
				if (window.opera) delta = -delta;
			}
			else if (event.detail)
			{
				delta = -event.detail/3;
			}
			return Math.round(delta); //Safari Round
		}
	}
);

Element.addMethods(
	{
		setWidth: function(element, w) {
			element = $(element);
			element.style.width = w + "px";
		},
		setHeight: function(element, h) {
			element = $(element);
			element.style.height = h + "px";
		},
		getLeft: function(element) {
			element = $(element);
			if (element.offsetLeft) {
				return element.offsetLeft;
			}
			if (element.style.left != '') {
				return parseInt(element.style.left);
			}
			return 0;
		},
		setLeft: function(element, t) {
			element = $(element);
			element.style.left = t + "px";
		},
		getTop: function(element) {
			element = $(element);
			if (element.offsetTop) {
				return element.offsetTop;
			}
			if (element.style.top != '') {
				return parseInt(element.style.top);
			}
			return 0;
		},
		setTop: function(element, t) {
			element = $(element);
			element.style.top = t + "px";
		}
	}
);

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
};

// Removes starting whitespaces
String.prototype.lTrim = function() {
	return this.replace(/\s*((\S+\s*)*)/, "$1");
};

// Removes ending whitespaces
String.prototype.rTrim = function() {
	return this.replace(/((\s*\S+)*)\s*/, "$1");
};

String.prototype.stripSlash = function() {
	var str = this;
	str = str.replace(/&lt;|%3C/g, '<');
  str = str.replace(/&gt;|%3E/g, '>');
  str = str.replace(/%20/g, ' ');
  str = str.replace(/%22/g, '\"');
  str = str.replace(/%27/g, '\'');
  return str;
};

Function.prototype.delay = function() {
	var __method = this, args = $A(arguments), ms = args.shift(), obj = args.shift();
	var fnc = function () {
		__method.apply(obj || __method, args);
	};
	this.timer = setTimeout(fnc, ms);
};

Function.prototype.periodical = function() {
	var __method = this, args = $A(arguments), ms = args.shift(), obj = args.shift();
	var fnc = function () {
		__method.apply(obj || __method, args);
	};
	this.timer = setInterval(fnc, ms);
};

Function.prototype.stop = function() {
	if (!this.timer) return;
	clearTimeout(this.timer);
	clearInterval(this.timer);
	this.timer = null;
};

/*
 * end of extension
 */

// Get base url
var url = document.location.href;
var baseUrl = url.substring(0, url.lastIndexOf("/") + 1);
var rootUrl = window.location.protocol + "//" + window.location.host;

function getAbsoluteUrl(url)
{
	if (url.substring(0, 1) == '/') {
		url = rootUrl + url;
	}
	// Does URL begin with http?
	else if (url.substring(0, 4) != 'http') {
		url = baseUrl + url;
	}
	return url;
}

function getHTML(url, pars, el, callback)
{
	var complete = function()
	{
		if (callback && typeof(callback)=="function")
			callback();
	};

	if (url.substring(0, 1) == '/') {
		url = rootUrl + url;
	}
	// Does URL begin with http?
	else if (url.substring(0, 4) != 'http') {
		url = baseUrl + url;
	}

	var reportError = function(request) {
		try {
			$(el).innerHTML = "Sorry. There was an error.";
		}
		catch (e) {
			//alert(request.status+ " | " + request.statusText);
			alert("Sorry. There was an error.");
		}
	};

	if ($(el))
	{
		new Ajax.Updater(
			{success: el},
			url,
			{
				//method: 'get',
				//parameters: pars,
				method: 'post',
				postBody: pars,
				evalScripts: true,
				//asynchronous: false,
				onFailure: reportError,
				onComplete: complete
			}
		);
	}
	else
	{
		new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: pars,
				evalScripts: true,
				//asynchronous: false,
				onComplete: complete,
				onFailure:reportError
			}
		);
	}
}
