﻿(function ()
{
	var window = this, undefined;

	// *** Extend System Objects ***

	// Node.contains( [childNode] )
	if (window.Node && Node.prototype && !Node.prototype.contains)
	{
		Node.prototype.contains = function (arg)
		{
			return !!(this.compareDocumentPosition(arg) & 16);
		};
	}

	// String.trim()
	if (!String.prototype.trim)
	{
		String.prototype.trim = function ()
		{
			return this.replace(/^(\s|\&nbsp\;)+/, '').replace (/(\s|\&nbsp\;)+$/, '');
		};
	}

	// String.right( [length] )
	if (!String.prototype.right)
	{
		String.prototype.right = function (len)
		{
			if (len <= 0)
			{
				return '';
			}
			else if (len > this.length)
			{
				return this;
			}
			else
			{
				return this.substr(this.length - len);
			}
		};
	}

	// Array.indexOf( [obj] )
	if (!Array.prototype.indexOf)
	{
		Array.prototype.indexOf = function (obj, fromIndex)
		{
			if (fromIndex == null)
			{
				fromIndex = 0;
			}
			else if (fromIndex < 0)
			{
				fromIndex = Math.max(0, this.length + fromIndex);
			}

			for (var i = fromIndex, j = this.length; i < j; i++)
			{
				if (this[i] === obj) { return i; }
			}
			return -1;
		};
	}

	// *** Building the Global Object ***

	// $( selector, [context] )
	// $.addEvent( obj, type, context )
	// $.removeEvent( obj, type, fn )
	// $.returnEvent( e, retValue )
	// $.setUnselectable( elem )
	// $.setOpacity( elem, [opacity] )
	// $.getAbsPos( elem )
	// $.getBgXOffset ( elem)
	// $.getBgYOffset ( elem)
	// $.setBgXOffset ( elem, xOffset )
	// $.setBgYOffset ( elem, yOffset )
	// $.isArray ( obj )
	// $.rand = function ( min, max )
	// $.getStyle = function ( elem, style )

	window["$"] = function (selector, context)
	{
		return (context || document).getElementById(selector);
	}

	window["$"].addEvent = function (obj, type, fn)
	{
		if (obj.attachEvent)
		{
			obj[type + fn] = function() { fn.call(obj, window.event); }
			obj.attachEvent('on' + type, obj[type + fn]);
		}
		else
		{
			obj.addEventListener(type, fn, false);
		}
	}

	window["$"].removeEvent = function (obj, type, fn)
	{
		if (obj.detachEvent)
		{
			obj.detachEvent('on' + type, obj[type + fn]);
			obj[type + fn] = null;
		}
		else
		{
			obj.removeEventListener(type, fn, false);
		}
	}

	window["$"].returnEvent = function (e, retValue)
	{
		if (retValue) { return true; }

		if (e.preventDefault) e.preventDefault();
		else e.returnResult = false;
		if (e.stopPropagation) e.stopPropagation();
		else e.cancelBubble = true;
		return false;
	}

	window["$"].setUnselectable = function (elem)
	{
		if (elem == undefined) { return; }
		this.addEvent(elem, "selectstart", function () { return false; });
		elem.style.MozUserSelect = "none";
		elem.style.KhtmlUserSelect = "none";
		elem.style.userSelect = "none";
		elem.unselectable = "on";
	}

	window["$"].setOpacity = function (elem, opacity)
	{
		elem.style.opacity = opacity || '1.0';
		elem.style.MozOpacity = opacity || '1.0';
		elem.style.KhtmlOpacity = opacity || '1.0';

		/*
			The IE can't handle Alpha-PNGs together with Alpha-Opacity, so we don't use Opacity at all.
			elem.style.filter = (opacity === undefined ? 'none': 'Alpha(style=0, opacity=' + Math.round(opacity * 100) + ')');
		*/
	}

	window["$"].getAbsPos = function (elem)
	{
		var x = 0, y = 0;
		while (elem != null)
		{
			x += elem.offsetLeft;
			y += elem.offsetTop;
			elem = elem.offsetParent;
		}

		return { "x":x, "y":y };
	}

	window["$"].getBgXOffset = function (elem)
	{
		try
		{
			return parseInt(/([-0-9a-zA-Z]+)\s.+/.exec($.getStyle(elem, 'background-position'))[1]);
		}
		catch (e)
		{
			return 0;
		}
	}

	window["$"].getBgYOffset = function (elem)
	{
		try
		{
			return parseInt(/.+\s([-0-9a-zA-Z]+)/.exec($.getStyle(elem, 'background-position'))[1]);
		}
		catch (e)
		{
			return 0;
		}
	}

	window["$"].setBgXOffset = function (elem, xOffset)
	{
		if ($.getStyle(elem, 'background-position'))
		{
			elem.style.backgroundPosition =  xOffset + 'px ' + /.+\s([-0-9a-zA-Z]+)/.exec($.getStyle(elem, 'background-position'))[1]
		}
	}

	window["$"].setBgYOffset = function (elem, yOffset)
	{
		if ($.getStyle(elem, 'background-position'))
		{
			elem.style.backgroundPosition = /([-0-9a-zA-Z]+)\s.+/.exec($.getStyle(elem, 'background-position'))[1] + ' ' + yOffset + 'px'
		}
	}

	window["$"].isArray = function (obj)
	{
		return Object.prototype.toString.call(obj) === "[object Array]";
	}

	window["$"].rand = function (min, max)
	{
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}

	window["$"].getStyle = function (elem, style)
	{
		style = (style == 'float') ? 'cssFloat' : style.replace(/\-(\w)/g, function (strMatch, p1)
		{
			return p1.toUpperCase();
		});

		var value = elem.style[style];
		if (!value || value == 'auto')
		{
			if (document.defaultView)
			{
				var css = document.defaultView.getComputedStyle(elem, null);
				value = css ? css[style] : null;
			}
			else if (style = 'backgroundPosition' && elem.currentStyle && elem.currentStyle.backgroundPositionX && elem.currentStyle.backgroundPositionY)
			{
				// IE
				value = elem.currentStyle.backgroundPositionX  + ' ' + elem.currentStyle.backgroundPositionY;
			}
		}

		return value;
	}
})();
