﻿var Toolkit =
{
	userAgent: navigator.userAgent.toLowerCase(),
	browser:
	{
		version: (navigator.userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/i) || [])[1],
		safari: /webkit/i.test(navigator.userAgent),
		opera: /opera/i.test(navigator.userAgent),
		msie: /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent),
		msie6: false/*@cc_on || @_jscript_version < 5.7@*/,
		mozilla: /mozilla/i.test(navigator.userAgent) && !/(compatible|webkit)/i.test(navigator.userAgent)
	},

	trimString: function(string)
	{
		return (string || "").replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g, "");
	},

	daysInMonth: function(monthDate)
	{
		return new Date(monthDate.getFullYear(), monthDate.getMonth() + 1, 0).getDate();
	},

	objectType: function(obj)
	{
		var objType = String(obj.constructor).match(/function\s+(\w+)/);
		if (objType) return objType[1];
		return "undefined";
	},

	parseEvent: function(e)
	{
		var evt =
			{
				target: (e.target ? e.target : e.srcElement),
				clientX: e.clientX,
				clientY: e.clientY,
				screenX: e.screenX,
				screenY: e.screenY
			};
		return evt;
	},

	addListener: function(element, eventName, listener)
	{
		if (window.attachEvent)
		{
			element.attachEvent("on" + eventName, listener);
		}
		else
		{
			element.addEventListener(eventName, listener, false);
		}
	},

	getPosition: function(element)
	{
		var __isFireFox = navigator.userAgent.match(/gecko/i);
		var res = new Object();
		res.x = 0; res.y = 0;
		if (element !== null)
		{
			res.x = element.offsetLeft;
			res.y = element.offsetTop;

			var offsetParent = element.offsetParent;
			var parentNode = element.parentNode;

			while (offsetParent !== null)
			{
				res.x += offsetParent.offsetLeft;
				res.y += offsetParent.offsetTop;

				if (offsetParent != document.body && offsetParent != document.documentElement)
				{
					res.x -= offsetParent.scrollLeft;
					res.y -= offsetParent.scrollTop;
				}
				//next lines are necessary to support FireFox problem with offsetParent  
				if (__isFireFox)
				{
					while (offsetParent != parentNode && parentNode !== null)
					{
						res.x -= parentNode.scrollLeft;
						res.y -= parentNode.scrollTop;

						parentNode = parentNode.parentNode;
					}
				}
				parentNode = offsetParent.parentNode;
				offsetParent = offsetParent.offsetParent;
			}
		}
		return res;
	},

	/*
	Base, version 1.0.2
	Copyright 2006, Dean Edwards
	License: http://creativecommons.org/licenses/LGPL/2.1/
	*/

	Base: function()
	{
		if (arguments.length)
		{
			if (this == window)
			{ // cast an object to this class
				Toolkit.Base.prototype.extend.call(arguments[0], arguments.callee.prototype);
			} else
			{
				this.extend(arguments[0]);
			}
		}
	}

};

Toolkit.Base.version = "1.0.2";

Toolkit.Base.prototype = {
	extend: function(source, value)
	{
		var extend = Toolkit.Base.prototype.extend;
		if (arguments.length == 2)
		{
			var ancestor = this[source];
			// overriding?
			if ((ancestor instanceof Function) && (value instanceof Function) &&
				ancestor.valueOf() != value.valueOf() && /\bbase\b/.test(value))
			{
				var method = value;
				//	var _prototype = this.constructor.prototype;
				//	var fromPrototype = !Base._prototyping && _prototype[source] == ancestor;
				value = function()
				{
					var previous = this.base;
					//	this.base = fromPrototype ? _prototype[source] : ancestor;
					this.base = ancestor;
					var returnValue = method.apply(this, arguments);
					this.base = previous;
					return returnValue;
				};
				// point to the underlying method
				value.valueOf = function()
				{
					return method;
				};
				value.toString = function()
				{
					return String(method);
				};
			}
			return this[source] = value;
		} else if (source)
		{
			var _prototype = { toSource: null };
			// do the "toString" and other methods manually
			var _protected = ["toString", "valueOf"];
			// if we are prototyping then include the constructor
			if (Toolkit.Base._prototyping) _protected[2] = "constructor";
			for (var i = 0; (name = _protected[i]); i++)
			{
				if (source[name] != _prototype[name])
				{
					extend.call(this, name, source[name]);
				}
			}
			// copy each of the source object's properties to this object
			for (var name in source)
			{
				if (!_prototype[name])
				{
					extend.call(this, name, source[name]);
				}
			}
		}
		return this;
	},

	base: function()
	{
		// call this method from any other method to invoke that method's ancestor
	}
};

Toolkit.Base.extend = function(_instance, _static)
{
	var extend = Toolkit.Base.prototype.extend;
	if (!_instance) _instance = {};
	// build the prototype
	Toolkit.Base._prototyping = true;
	var _prototype = new this;
	extend.call(_prototype, _instance);
	var constructor = _prototype.constructor;
	_prototype.constructor = this;
	delete Toolkit.Base._prototyping;
	// create the wrapper for the constructor function
	var klass = function()
	{
		if (!Toolkit.Base._prototyping) constructor.apply(this, arguments);
		this.constructor = klass;
	};
	klass.prototype = _prototype;
	// build the class interface
	klass.extend = this.extend;
	klass.implement = this.implement;
	klass.toString = function()
	{
		return String(constructor);
	};
	extend.call(klass, _static);
	// single instance
	var object = constructor ? klass : _prototype;
	// class initialisation
	if (object.init instanceof Function) object.init();
	return object;
};

Toolkit.Base.implement = function(_interface)
{
	if (_interface instanceof Function) _interface = _interface.prototype;
	this.prototype.extend(_interface);
};

if (typeof HTMLElement != "undefined" && typeof HTMLElement.prototype.__defineGetter__ != "undefined")
{
	HTMLElement.prototype.__defineGetter__("innerText", function()
	{
		return this.textContent;
	});
	HTMLElement.prototype.__defineSetter__("innerText", function(sText)
	{
		this.textContent = sText;
	});
}
// opera innerText is broken
String.fixText = function($INPUT)
{
	return $INPUT.replace(/\&/g, "&").replace(/\</g, "<").replace(/\>/g, ">");
} 
