﻿function _$(id)
{
    return document.getElementById(id);
};


function goUrl(url, target)
{
	if ((url == null) || (url.length == 0))
		return;

	if (target == null)
		target = "_self";

	window.open(url, target);
};

var Dom =
{
	getPosX: function (element)
	{
		var x = 0;
		do
		{
			x += element.offsetLeft;
		}
		while (element = element.offsetParent);

		return x;
	},
	getPosY: function (element)
	{
		var y = 0;
		do
		{
			y += element.offsetTop;
		}
		while (element = element.offsetParent);

		return y;
	}
};

var FX =
{
	Display:
	{
		cbFade: null,
		fade: function (e, value, max, cb)
		{
			// memorize callback
			if (cb) this.cbFade = cb;

			if (value == null) value = 0;
			if (value > max) value = max;

			if (e.style.opacity != null)
				e.style.opacity = value / 100;
			else
			{
				e.style.filter = "alpha(opacity=" + String(value) + ")";
				value += 5;
			}

			if (value < max)
				window.setTimeout(function () { FX.Display.fade(e, value + 5, max) }, 20);
			else
				if (this.cbFade)
					this.cbFade();

		},
		smoothY: function (e)
		{
			var o = dom.getFirstChild(e);

			var max = e.clientHeight;

			e.style.height = "0";
			o.style.marginTop = String(-max) + "px";
			FX.Display.smoothY_(e, o, max);
		},
		smoothY_: function (e, o, max)
		{
			var my = e.clientHeight;
			var h = max - my;
			if (h == 0)
				return;

			var step = 0;

			if (my < (max >> 1))
				step = my >> 2;
			else
				step = h >> 2;

			if (step == 0)
				if (max > 0)
					step = 1;
				else
					step = -1;

			e.style.height = String(my + step) + "px";
			o.style.marginTop = String(step - h) + "px";

			document.documentElement.offsetLeft; // flush, fix FF
			this.timerHandler = window.setTimeout(function () { FX.Display.smoothY_(e, o, max); }, 10);
		}
	},
	Scroll:
	{
		x: {},
		timerHandler: null,

		scrollX: function (e, max)
		{
			if (this.timerHandler)
				window.clearTimeout(this.timerHandler);
			this._scrollX(e, max);
		},
		_scrollX: function (e, max)
		{

			var x = 0;
			if (e.style.marginLeft)
				x = -parseInt(e.style.marginLeft);

			var w = max - x;
			if (w == 0)
				return;

			var step = w >> 5;

			if (step == 0)
				if (w > 0)
					step = 1;
				else
					step = -1;

			x += step;
			e.style.marginLeft = String(-x) + "px";
			document.documentElement.offsetLeft;
			this.timerHandler = window.setTimeout(function () { FX.Scroll._scrollX(e, max); }, 10);
		},
		scrollY: function (e, max)
		{
			if (this.timerHandler)
				window.clearTimeout(this.timerHandler);
			this.scrollY_(e, max);
		},
		scrollY_: function (e, max)
		{
			var y = 0;
			if (e.style.marginTop)
				y = -parseInt(e.style.marginTop);

			var w = max - y;
			if (w == 0)
				return;

			var step = w >> 4;

			if (step == 0)
				if (w > 0)
					step = 1;
				else
					step = -1;

			y += step;
			e.style.marginTop = String(-y) + "px";
			this.timerHandler = window.setTimeout(function () { FX.Scroll.scrollY_(e, max); }, 10);
		},

		cbScrollBy: null,
		scrollBy: function (max, cb)
		{
			if (cb) this.cbScrollBy = cb;
			var y = document.body.scrollTop;
			if (y == 0)
				y = document.documentElement.scrollTop;

			if (y >= max)
			{
				if (this.cbScrollBy)
					this.cbScrollBy();
				return;
			}
			var step = 0;

			if (y < (max >> 1))
				step = y >> 3;
			else
				step = (max - y) >> 3;

			if (step == 0)
				if (y < max)
					step = 1;
				else
					step = -1;


			window.scrollBy(0, step);

			this.timerHandler = window.setTimeout(function () { FX.Scroll.scrollBy(max); }, 10);
		}
	}
};

