var mrj = new Object(); 
mrj.menus = new Object();
mrj.helpers = new Object();

mrj.init = function ()
{
	if (document.all != undefined)
	{
		mrj.browser = "ie";	
	}
	else
	{
		mrj.browser = "moz";
	}
	
}


mrj.isInternetExplorer = function ()
{
	return (this.browser == "ie" ? true : false);
}

mrj.isMozilla = function ()
{
	return (this.browser == "moz" ? true : false);
}


mrj.createClass = function (params)
{
	
	var c = params.init;
	for (var i in params)
	{
		if (typeof(params[i]) == "function")
		{
			c.prototype[i] = params[i];
		}
		else
		{
			c[i] = params[i];
		}
	}

	return c;
}

mrj.newClass = function (c, args)
{
	c = new c;
	c.init.apply(c, args);
	return c;
}

mrj.element = function (e)
{
	if (typeof(e) == "string")
	{
		return document.getElementById(e);
	}
	else if (e.__mrj_element != undefined)
	{
		return e.htmlElement;
	}
	
	return e;
}

mrj.isElement = function (obj)
{
	return (obj.__mrj_element ? true : false);
}

mrj.newElement = function (e)
{
	if (mrj.isElement(e))
	{
		return e;
	}
	else
	{
		return new mrj.Element(e);
	}
}

mrj.Element = mrj.createClass(
{
	init: function (e)
	{
		e = mrj.element(e);
		this.htmlElement = e;
		this.htmlElement.mrjElement = this;
		this.__mrj_element = true;
	},
	
	
	
	applyEffect: function (effectName, params)
	{
		mrj.effects[effectName](this, params);
	},
	
	cancelEffect: function (effectName)
	{
		mrj.effects.cancel(this, effectName);
	},
	
	getWidth: function ()
	{
		if (mrj.isInternetExplorer() && this.scrollContainer != undefined && !this.scrollContainer.isVisible())
		{
			this.scrollContainer.show();
			var w = parseInt(this.htmlElement.offsetWidth);
			this.scrollContainer.hide();
		}
		else
		{
			var w = parseInt(this.htmlElement.offsetWidth);
		}
		
			
		/*var s = element.style;
		if (w == 0 && parseInt(s.width) > 0)
		{
			w = parseInt_wd(s.width);
			if (s.borderLeftWidth) w -= parseInt_wd(s.borderLeftWidth);
			if (s.borderRightWidth) w -= parseInt_wd(s.borderRightWidth);
			if (s.paddingLeft) w -= parseInt_wd(s.paddingLeft);
			if (s.paddingRight) w -= parseInt_wd(s.paddingRight);
			if (s.marginLeft) w -= parseInt_wd(s.marginLeft);
			if (s.marginRight) w -= parseInt_wd(s.marginRight);
		}*/
		return w;
	},

	getHeight: function ()
	{
		if (mrj.isInternetExplorer() && this.scrollContainer != undefined && !this.scrollContainer.isVisible())
		{
			this.scrollContainer.show();
			var h = parseInt(this.htmlElement.offsetHeight);
			this.scrollContainer.hide();
		}
		else
		{
			var h = parseInt(this.htmlElement.offsetHeight);
		}
			
		var s = this.htmlElement.style;
		if (h == 0 && parseInt(s.height) > 0)
		{
			h = parseInt_wd(s.height);
			if (s.borderTopWidth) h -= parseInt_wd(s.borderTopWidth);
			if (s.borderBottomWidth) h -= parseInt_wd(s.borderBottomWidth);
			if (s.paddingTop) h -= parseInt_wd(s.paddingTop);
			if (s.paddingBottom) h -= parseInt_wd(s.paddingBottom);
			if (s.marginTop) h -= parseInt_wd(s.marginTop);
			if (s.marginBottom) h -= parseInt_wd(s.marginBottom);		
	
		}	
		return h;
	},
	
	setWidth: function (w, absolute)
	{
		w = (absolute ? this.calcRealWidth(w, true) : parseInt_wd(w));
		this.htmlElement.style.width = w + "px";		
	},

	setHeight: function (h, absolute)
	{
		h = (absolute ? this.calcRealHeight(h, true) : parseInt_wd(h));
		this.htmlElement.style.height = h + "px";
		
		if (mrj.isInternetExplorer())
		{
			if (h == 0) this.hide();
			else this.show();
		}
	},
	
	getPos: function ()
	{
		var left = 0;
		var top = 0;
	
		var curElement = this.htmlElement;
		
		while (curElement != null)
		{
			//if (curElement.style.position == "absolute") break;
			left += curElement.offsetLeft;
			top += curElement.offsetTop;
			curElement = curElement.offsetParent;
		}
		
		return { left: left, top: top };
	},

	setTop: function (top)
	{
		this.htmlElement.style.top = parseInt_wd(top) + "px";	
	},

	setLeft: function (left)
	{
		this.htmlElement.style.left = parseInt_wd(left) + "px";
	},
	
	setPos: function (left, top)
	{
		this.htmlElement.style.left = parseInt_wd(left) + "px";
		this.htmlElement.style.top = parseInt_wd(top) + "px";	
	},
	
	setContent: function (content, append)
	{
		this.htmlElement.innerHTML = (append ? this.htmlElement.innerHTML : "") + content;	
	},
	
	calcRealWidth: function (w, inside, ignoreMargins)
	{
		var s = this.htmlElement.style;
		if (inside == undefined) inside = false;
		if (ignoreMargins == undefined) ignoreMargins = false;
		if (w == undefined) w = s.width;
		w = parseInt_wd(w);
	
		if (!mrj.isInternetExplorer())
		{
			if (inside)
			{
				w -= parseInt_wd(s.borderLeftWidth);
				w -= parseInt_wd(s.borderRightWidth);
				w -= parseInt_wd(s.paddingLeft);
				w -= parseInt_wd(s.paddingRight);
			}
			else
			{
				w += parseInt_wd(s.borderLeftWidth);
				w += parseInt_wd(s.borderRightWidth);
				w += parseInt_wd(s.paddingLeft);
				w += parseInt_wd(s.paddingRight);
			}
			
			if (!ignoreMargins)
			{
				if (inside)
				{
					w -= parseInt_wd(s.marginLeft);
					w -= parseInt_wd(s.marginRight);
				}
				else
				{
					w += parseInt_wd(s.marginLeft);
					w += parseInt_wd(s.marginRight);
				}
			}
		}

		return w;
	
	},

	calcRealHeight: function (h, inside, ignoreMargins)
	{
		var s = this.htmlElement.style;
		if (inside == undefined) inside = true;
		if (ignoreMargins == undefined) ignoreMargins = true;
		if (h == undefined) h = s.height;
		h = parseInt_wd(h);
	
		if (!mrj.isInternetExplorer())
		{
			if (inside)
			{
				h -= parseInt_wd(s.borderTopWidth);
				h -= parseInt_wd(s.borderBottomWidth);
				h -= parseInt_wd(s.paddingTop);
				h -= parseInt_wd(s.paddingBottom);
			}
			else
			{
				h += parseInt_wd(s.borderTopWidth);
				h += parseInt_wd(s.borderBottomWidth);
				h += parseInt_wd(s.paddingTop);
				h += parseInt_wd(s.paddingBottom);
			}
			
			if (!ignoreMargins)
			{
				if (inside)
				{
					h -= parseInt_wd(s.marginTop);
					h -= parseInt_wd(s.marginBottom);
				}
				else
				{
					h += parseInt_wd(s.marginTop);
					h += parseInt_wd(s.marginBottom);
				}
			}
		}
		
		return h;
	
	},

	calcRealSize: function (w, h, inside, ignoreMargins)
	{
		return { 
			width: this.calcRealWidth(w, inside, ignoreMargins), 
			height: this.calcRealHeight(h, inside, ignoreMargins) 
			};
	},
	
	getSize: function ()
	{
		return { width: this.getWidth(), height: this.getHeight() };
	},
	
	setSize: function (width, height, absolute)
	{
		this.setWidth(width, absolute);
		this.setHeight(height, absolute);
	},
	
	createElement: function (elementType, id)
	{
		var e = document.createElement(elementType);
		if (id != undefined) e.id = id;
		this.htmlElement.appendChild(e);
		return new mrj.Element(e);
	},
	
	createNestedElement: function (elementType, oldElement, newId)
	{
		var newElement = new mrj.Element(document.createElement(elementType));
		if (newId != undefined) newElement.id = newId;
		
		oldElement.htmlElement.parentNode.replaceChild(newElement.htmlElement, oldElement.htmlElement);
		newElement.appendElement(oldElement.htmlElement);
		return newElement;
	},
	
	appendElement: function (element)
	{
		var e = mrj.element(element);
		this.htmlElement.appendChild(e);
	},
	
	hide: function ()
	{
		this.htmlElement.style.display = "none";	
	},
	
	show: function ()
	{
		this.htmlElement.style.display = "";	
	},
	
	isVisible: function ()
	{
		return (this.htmlElement.style.display == "none" ? false : true);
	}
});


/*
** Misc functions
*/

function parseInt_wd(obj, defaultVal)
{
	if (defaultVal == undefined)
		defaultVal = 0;
		
	var val = parseInt(obj);
	if (isNaN(val) || val == null || val == undefined || val == "")
		val = defaultVal;
		
	return val;
}

function applyParams(obj, params)
{
	for (var id in params)
	{
		obj[id] = params[id];
	}
}

Object.prototype.merge = function (obj, ignoreNoValues)
{
	for (var i in obj)
	{
		if (!ignoreNoValues || (ignoreNoValues && obj[i]))
		{
			this[i] = obj[i];
		}
	}
}

function isNumber(str)
{
	return /\d/.test(str);
}


mrj.init();

