var WebApp = (function() {
	var L2R = +1;
	var R2L = -1;

	var HOME = 0;
	var BACK = 1;
	var LEFT = 2;
	var RIGHT = 3;
	var TITLE = 4;

	var _def, _headView, _head;
	var _webapp, _group;

	var _loader		= [];
	var _fading		= [];
	var _header		= [];
	var _async		= [];
	var _location	= [];
	var _history	= [];
	var _historyPos	= -1;
	var _prev		= -1;
	var _initialNav	= history.length;
	var _sliding	= false;
	var _hold		= false;
	var _baseTitle	= null;
	var _lockCnt	= 0;
	var _width		= 0;
	var _dialog		= null;

	var _handler	= {};
	_handler.load				= [];
	_handler.beginslide			= [];
	_handler.endslide			= [];
	_handler.orientationchange	= [];
	_handler.error				= [];

/* Public */
	__p = {
		SetDefault: function(layer) { _def = layer },

		HideBar: function() {
			window.scrollTo(0, 1);
			return false;
		},

		Header: function(show, what) {
			Buttons(show);
			Display(_headView, 0);
			_headView = $(what);
			Display(_headView, !show);
			return false;
		},

		Tab: function(id, active) {
			var o = $(id);
			ShowTab(o, $$("li", o)[active]);
		},

		AddEventListener: function(evt, handler) {
			if (typeof _handler[evt] != "undefined")
				if (_handler[evt].indexOf(handler) == -1)
					_handler[evt].push(handler);
		},

		Toggle: function() {
			if (_history.length > 1) {
				if (_historyPos == _history.length - 1)
					history.back();
				else
					history.forward();
			}
			return false;
		},

		Back: function() {
			if (_hold)
				return (_hold = false);

			if (history.length - _initialNav == _historyPos)
				history.back();
			else
				window.location = _location[_historyPos - 1] || "#";
			return false;
		},

		Home: function() {
			if (history.length - _initialNav == _historyPos)
				history.go(-_historyPos);
			else
				window.location = "#";
			return (_hold = false);
		},

		Form: function(frm) {
			var s, a, b, c, o, f;

			a = $(frm);
			b = $(_history[_historyPos]);
			s = (a.style.display != "block");

			f = $Name(a) == "form" ? a : $Parent(a, "form");
			o = f.onsubmit;
			if (!s) {
				f.onsubmit = f.onsubmit(null, true);
			} else {
				a.style.top = parseInt($("__wa_shadow").style.top) - 2 + "px";

				f.onsubmit = function(e, b) {
					if (b) return o;
					if (o) o(e);

					e.preventDefault();
					__p.Submit(this);
				};
			}

			AdjustView();
			Shadow(s);
			Display(a, s);
			SetTitle(s ? $$("legend", a)[0].innerHTML : 
							(_historyPos ? b.title : null) );

			_dialog = (s) ? a : null;
			if (s) { c = a; a = b; b = c }

			DelLayerButtons(a);
			AddLayerButtons(b, s);

			if (s)	__p.Header(s);
			else	Buttons(!s);

			return false;
		},

		Submit: function(frm, submiter) {
			var e = arguments[2];
			var a = submiter;

			var f = $(frm);
			if(f && $Name(f) != "form") f = $Parent(f, "form");
			if (f) {
				var _ = function(i, f) {
					var q = "";
					for (var n = 0; n < i.length; n++)
						if (f ? f(i[n]) : 1)
							q += "&" + i[n].name + "=" + escape(i[n].value);
					return q;
				}

				var q  = _($$("input", f),
					function(i) {
						with(i) return (name && !disabled &&
										(["text", "password", "hidden", "search"].indexOf(type) != -1 ||
										(["radio", "checkbox"].indexOf(type) != -1 && checked)))
					});
					q += _($$("select", f));

				e  = e || event;
				a  = !a ? $Link(e.target) : a;
				if (!a) FocusFixer();
				q += "&" + (a && a.id ? a.id : "__submit") + "=1";
				q  = q.substr(1);
				BasicAsync(f.action || self.location.href, null, q);
				if (_dialog) __p.Form(_dialog);
			}
			return false;
		},

		Postable: function(keys, values) {
			var q = "";
			for (var i = 1; i < values.length && i <= keys.length; i++)
				q += "&" + keys[i - 1] + "=" + escape(values[i]);
			return q.replace(/&=/g, "&").substr(1);
		},

		Request: function(url, prms, cb, async, loader) {
			cb = cb == -1 ? DefaultCallback() : cb;

			var o = new XMLHttpRequest();
			var c = function() { __callback(o, cb, loader) };
			var m = prms ? "post" : "get";

			async = (!async ? false : true);
			if (loader) __p.Loader(loader, true);
			if (_handler.error.length > 0)
				_async[_async.length] = [o, url, prms];

			url = AddParam(url, "__async", "true");
			url = AddParam(url, "__source", _history[_historyPos]);
			o.open(m, url, async);
			if (prms) o.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			o.onreadystatechange = (async) ? c : null;
			o.send(prms);

			if (!async) c();
		},

		Loader: function(obj, show) {
			var o = obj;
			var h = $Has(o, "__lod");
			if (h == show)
				return h;

			if (show) {
				$Add(o, "__lod");
				_loader.push(o);
			} else $Remove(o, "__lod");

			ApplyMore(o);
			return h;
		},
		
		Player: function(src) {
			src = src || $Link(event.target).href;
			if (!IsMobile()) {
				window.open(src);
			} else {
				var o = $("__wa_media");
				if (o) delete _webapp.removeChild(o);

				o = $New("iframe");
				o.id = "__wa_media";
				o.src = src;			/* must be before appendChild to prevent Safari weird behavior */

				_webapp.appendChild(o);
			}
			return false;
		}
	}

	function ApplyMore(o) {
		if ($Has(o, "iMore")) {
			var a = $$("a", o)[0];
			if (a.title) {
				o = a.innerHTML;
				a.innerHTML = a.title;
				a.title = o;
			}
		}
	}

	function FocusFixer() {
		var i = $New("input");
		_group.appendChild(i);
		i.type = "text";
		i.focus();
		Display(i, 0);
		setTimeout(function() { delete _group.removeChild(i) }, 5);
	}

	function Buttons(s) {
		for (var i = 0; i < _header.length; i++)
			Display(_header[i], s);
		
		Display(_header[BACK], s && !_header[LEFT] && _historyPos);
		Display(_header[HOME], s && !_header[RIGHT] && _historyPos > 1);
	}

	function AddLayerButtons(lay, ignore) {
		var a = $$("a", lay);
		var p = RIGHT;

		for (var i = 0; i < a.length && p >= LEFT; i++) {
			if (_header[p] && !ignore) { i--; p--; continue; }

			if (["action", "back"].indexOf(a[i].rel) != -1) {
				$Add(a[i], p == RIGHT ? "iRightButton" : "iLeftButton");
				Display(a[i], 1);
				_header[p--] = a[i];
				_head.appendChild(a[i--]);
			}
		}
	}

	function DelLayerButtons(lay) {
		for (var i = LEFT; i <= RIGHT; i++) {
			var a = _header[i];
			if (a && ["action", "back"].indexOf(a.rel) != -1) {
				Display(a, 0);
				$Remove(a, i == RIGHT ? "iRightButton" : "iLeftButton");
				lay.insertBefore(a, lay.firstChild);
			}
		}
		_header[RIGHT] = $("waRightButton");
		_header[LEFT] = $("waLeftButton");
	}

/* Private */
	function $(i) { return typeof i == "string" ? document.getElementById(i) : i; }
	function $$(t, o) { return (o || document).getElementsByTagName(t); }
	function $Async(o) { return o.rev == "async" || o.rev == "async:np"; }
	function $New(t) { return document.createElement(t); }
	function $Has(o, c) { return ($Class(o).indexOf(c) != -1); }
	function $Class(o) { return o.className.split(" "); }
	function $Link(o) { return $Name(o) == "a" ? o : $Parent(o, "a") }
	function $Name(o) { return o.localName.toLowerCase() }
	function $Add(o, c) {
		var h = $Has(o, c);
		if (!h) o.className += " " + c;
		return h;
	}
	function $Remove(o) {
		var c = $Class(o);
		var a = arguments;
		for (var i = 1; i < a.length; i++) {
			var p = c.indexOf(a[i]);
			if (p != -1) c.splice(p, 1);
		}
		o.className = c.join(" ");
	}
	function $Parent(o, t) {
		while ((o = o.parentNode) && (o.nodeType != 1 || $Name(o) != t));
		return o;
	}	
	function $Any(o, c) {
		while ((o = o.parentNode) && (o.nodeType != 1 || !$Has(o, c)));
		return o;
	}	

	function $Text(o) {
		var o = o.childNodes;
		for (var i = 0; i < o.length; i++)
			if (o[i].nodeType == 3)
				return o[i].nodeValue.replace(/^\s+|\s+$/g, "");
		return null;
	}

	function Vars() {
		_webapp	= $("WebApp");
		_group	= $("iGroup");
		_header[BACK] = $("waBackButton");
		_header[HOME] = $("waHomeButton");
		_header[RIGHT] = $("waRightButton");
		_header[LEFT] = $("waLeftButton");
		_header[TITLE] = $("waHeadTitle");
	}

	function Display(o, s) { if (o) o.style.display = s ? "block" : "none" }

	function Show(o) {
		Display(o, 1);
		o.style.width = "100%";
	}

	function Shadow(s) {
		Display($("__wa_shadow"), s);
	}

	function Lock() {
		if (!_lockCnt++)
			Display($("__wa_noclick"), 1);
	}
	function Unlock() {
		if (!--_lockCnt)
			Display($("__wa_noclick"), 0);
	}

	function Historize(o, l) {
		if (o) {
			_history.splice(++_historyPos, _history.length);
			_history.push(o);

			_location.splice(_historyPos, _location.length);
			_location.push(l ? window.location.href : null);
		}
	}

	function Cleanup() {
		var s, i, c;

		while (s = _loader.pop())
			__p.Loader(s, 0);

		s = $$("li");
		for (i = 0; i < s.length; i++)
			if ($Has(s[i], "__sel"))
				$Remove(s[i], "__sel");
	}

	function ParseParams(s, np) {
		var ed = s.indexOf("#_");
		if (ed == -1)
			return null;

		var rs = "";
		var bs = SplitUrl(s);
		if (!np) for (var i = 0; i < bs[1].length; i++)
			rs += "/" + bs[1][i].split("=").pop();

		return bs[2] + rs;
	}

	function FadeItem(o, show, next) {
		if (!next) {
			if (!o || _fading.indexOf(o) != -1)
				return;
			_fading.push(o);
		}

		with (o.style) {
			if ((!show && opacity > 0) || (show && opacity < 1)) {
				if (show) display = "block";
				opacity = parseFloat(opacity) + (show ? +0.5 : -0.5);
				setTimeout(function() { FadeItem(o, show, 1) }, 0);
			} else {
				display = (opacity == 0) ? "none" : "block";
				_fading.splice(_fading.indexOf(o), 1);
			}
		}
	}

	function IsMobile() {
		with (navigator.userAgent)
			return (indexOf("iPhone") + indexOf("iPod") > -2);
	}

	function Resizer() {
		if (_sliding)
			return;
			
		var w = (window.innerWidth >= 480) ? 480 : 320;
		if (w != _width) {
			_width = w;
			_webapp.style.minHeight = ((w == 320) ? 417 : 269) + "px";
			_webapp.className = (w == 320) ? "portrait" : "landscape";
			CallListeners("orientationchange");
		}
	}

	function Locator() {
		if (_sliding || _hold)
			return;

		var act = GetActive();
		if (act == null)
			act = _history[0];

		var pos = _history.indexOf(act);
		var cur = _history[_historyPos];

		if (act == cur)
			return;

		if (pos != -1 && pos < _historyPos) {
			_historyPos = pos + 1;
			InitSlide(cur, act, L2R);
		} else {
			SlideTo(act);
		}
	}

	function CallListeners(evt, ctx) {
		var e = {};
		e.type = evt;
		e.context = ctx || Explode(window.location.href);
		e.windowWidth = _width;
		e.windowHeight = _webapp.offsetHeight;

		var k = true;
		for (var i = 0; i < _handler[evt].length; i++)
			k = k && (_handler[evt][i](e) == false ? false : true);
		return k;
	}

	function Init() {
		Vars();
		InitCheck();
		InitRadio();
		InitTab();
		InitHeader();
		InitLock();
		InitShadow();

		var l = $("iLoader");
		if (l) Display(l, 0);

		if (!_def)
			_def = GetLayers()[0].id;
		Historize(_def);

		var a = GetActive();
		if (a != _def) Historize(a, true);
		if (!a) a = _def;

		Show($(a));
		AddLayerButtons($(a));

		Display(_header[BACK], (!_header[LEFT] && _historyPos));
		Display(_header[HOME], (!_header[RIGHT] && _historyPos > 1 && a != _def));

		if (_header[TITLE]) {
			_baseTitle = _header[TITLE].innerHTML;
			_header[TITLE].innerHTML = GetTitle($(a));
		}

		setInterval(Resizer, 100);
		setInterval(Locator, 100);
		setTimeout(function() { CallListeners("load") }, 100);
		setTimeout(AdjustView, 500);
	}

/* Event */
	function ShowTab(ul, li, h) {
		var al = $$("li", ul);
		for (var i = 0; i < al.length; i++) {
			Display($(ul.id + i), (!h && al[i] == li));
			$Remove(al[i], "__act");
		}
		$Add(li, "__act");
	}

	function ListenClick(e, b) {
		if (_sliding) {
			e.preventDefault();
			return;
		}

		/* Checkbox */
		var o = e.target;
		var n = $Name(o);
		if (n == "label") {
			var f = $(o.getAttribute("for"));
			if ($Has(f, "iToggle")) {
				setTimeout(function() {
					FlipCheck(f.previousSibling.childNodes[1], true) }, 1);
			}
			return;
		}

		/* Radio parent */
		var li = $Parent(o, "li");		
		if (li && $Has(li, "iRadio")) {
			$Add(li, "__sel");
			ShowRadio(li);
			return;
		}

		/* Tab */
		var ul = $Parent(o, "ul");
		var pr = !ul ? null : ul.parentNode;
		var a  = $Link(o);
		if (ul && $Has(pr, "iTab")) {
			var h = $(ul.id + "-loader");
			Display(h, 0);

			if ($Async(a)) {
				Display(h, 1);
				BasicAsync(a, function(o) {
					Display(h, 0);
					ShowTab(ul, li, null);
					Display(ShowAsync(o)[0], 1);
				});

			} else { h = null }
			ShowTab(ul, li, h);
			e.preventDefault();
			return;
		}

		/* Radio list */
		if (ul && $Has(ul, "iCheck")) {
			var al = $$("li", ul);
			for (var i = 0; i < al.length; i++)
				$Remove(al[i], "__act", "__sel");
			$Add(li, "__act __sel");
			setTimeout(function() { $Remove(li, "__sel") }, 1000);
			e.preventDefault();
			return;
		}

		/* Menu and list */
		if (ul && 
			(($Has(ul, "iMenu") || $Has(pr, "iMenu")) ||
			 ($Has(ul, "iList") || $Has(pr, "iList"))) ) {

			if (a && !$Has(a, "iButton")) {
				var c = $Add(li, "__sel");
				if ($Async(a)) {
					if (!c) BasicAsync(a);
					e.preventDefault();
				}
				if (a.rev == "media") {
					__p.Player(a.href);
					setTimeout(function() { $Remove(li, "__sel") }, 500);
					e.preventDefault();
				}
			}
			return;
		}

		/* More */
		var dv = $Any(o, "iMore");
		if (dv) {
			if (!__p.Loader(dv, 1) && $Async(o))
				BasicAsync(o, MoreAsync);
			e.preventDefault();
			return;
		}

		/* Top form button */
		if (a && _dialog && !a.onclick) {
			if (a.rel == "back")
				__p.Form(_dialog, a);
			if (a.rel == "action")
				__p.Submit(_dialog, a);
			e.preventDefault();
			return;
		}

		/* Media player */
		if (a && a.rev == "media") {
			__p.Player(a.href);
			e.preventDefault();
		}

		/* Basic link */
		if (n == "a" && $Async(o)) {
			BasicAsync(o);
			e.preventDefault();
		}
	}

/* Animate */
	function SlideTo(to) {
		if (_history[_historyPos] != to)
			InitSlide(_history[_historyPos], to);
		return false;
	}

	function InitSlide(src, dst, dir) {
		if (_sliding)
			return;

		AdjustView();
		Lock();

		if (dst == _history[0])
			_initialNav = history.length;

		dir = dir || R2L;
		src = $(src);
		dst = $(dst);

		FadeItem(_head, false);
		Display($("iFooter"), false);
		Display(_headView, 0);

		_sliding = true;
		DoSlide(src, dst, dir);
	}
	
	function SlideInfo(d) {
		return [Explode(_location[_prev]), Explode(window.location.href), d];
	}

	function DoSlide(src, dst, dir) {
		if (_fading.length > 0) {

			setTimeout(function() { DoSlide(src, dst, dir) }, 5);
			return;
		}

		_prev = _historyPos;

		CallListeners("beginslide", SlideInfo(dir));

		DelLayerButtons(src);

		AddLayerButtons(dst);

		

		Show(src);

		Show(dst);

		_webapp.style.width = "200%";
		src.style.width = "50%";
		dst.style.width = "50%";

		var s = 0;
		var w = src.offsetWidth;

		if (dir == R2L) {
			src.parentNode.insertBefore(src, dst);
			Historize(dst.id, true);
		} else {
			dst.parentNode.insertBefore(dst, src);
			while (_historyPos && _history[--_historyPos] != dst.id);
			window.scrollTo(w, 1);
		}

		setTimeout(function() {
			while (s <= w) {
				s += Math.max((w - s) / 16, 4);
				window.scrollTo((w + w * dir) / 2 - Math.min(s, w) * dir, 1);
			}
			var c = dst.cloneNode(true);
			dst.parentNode.insertBefore(c, dst);
			Display(src, 0);

			setTimeout(function() {
				var o = c.parentNode.removeChild(c);
				delete o;
				EndSlide(src, dst, dir);
			}, 0);
		}, 5);
	}

	function EndSlide(src, dst, dir) {
		Cleanup();

		if (_header[BACK]) {				
			if (dir == R2L)
				_header[BACK].innerHTML = src.title;
			else if (_historyPos)
				_header[BACK].innerHTML = $(_history[_historyPos - 1]).title;
		}

		Display(_header[BACK], !_header[LEFT] && _historyPos);
		Display(_header[HOME], !_header[RIGHT] && _historyPos > 1);
		Display(_header[LEFT], 1);
		Display(_header[RIGHT], 1);
		SetTitle(GetTitle(dst));
		FadeItem(_head, 1);
		Display($("iFooter"), 1);

		_webapp.style.width = "100%";
		Show(dst);
		CleanSlide();
		CallListeners("endslide", SlideInfo(dir));
		_prev = -1;
	}

	function CleanSlide() {
		if (_fading.length > 0) {
			setTimeout(CleanSlide, 5);
			return;
		}
		Unlock();
		AdjustView();
		_sliding = false;
	}

	function SetTitle(title) {
		var o;
		if (o = _header[TITLE]) {
			o.innerHTML = title || GetTitle(GetActive());
			Display(o, 1);
		}
	}

	function FlipCheck(o, dontChange) {
		var i = $(o.parentNode.title);

		var txt = i.title.split("|");
		if (txt.length != 2)
			txt = ["ON", "OFF"];

		if (!dontChange)
			i.click();

		o.nextSibling.innerHTML = txt[i.checked ? 0 : 1];
		if (i.checked) {
			o.style.left = "";
			o.style.right = "-1px";
			o.parentNode.className = "iToggleOn";
			o.nextSibling.style.left = "0";
			o.nextSibling.style.right = "";
		} else {
			o.style.left = "-1px";
			o.style.right = "";
			o.parentNode.className = "iToggle";
			o.nextSibling.style.left = "";
			o.nextSibling.style.right = "0";
		}
	}

	function AdjustView() {
		while (window.pageYOffset > 1)
			window.scrollTo(0,
					Math.max(window.pageYOffset - window.pageYOffset / 10, 1));
		__p.HideBar();
	}	

	function Explode(loc) {
		if (loc) {
			var pos = loc.indexOf("#_");
			var vis = [];
	
			if (pos != -1) {
				loc = loc.substring(pos + 2).split("/");
				vis = GetLayers().filter(
						function(l) { return l.id == "wa" + loc[0] });
			}
			if (vis.length) {
				loc[0] = vis[0].id;
				return loc;
			}
		}
		return [];
	}

	function GetLayers() {
		var lay = [];
		var src = _group.childNodes;
		for (var i = 0; i < src.length; i++)
			if (src[i].nodeType == 1 && $Has(src[i], "iLayer"))
				lay.push(src[i]);
		return lay;
	}

	function GetTitle(o) {
		return (!_historyPos && _baseTitle) ? _baseTitle : o.title;
	}

	function GetActive() {
		return Explode(window.location.href)[0];
	}

	function SplitUrl(u) {
		var s, q, d;

		s = u.replace(/&amp;/g, "&");
		d = s.indexOf("#");
		d = s.substr(d != -1 ? d : s.length);
		s = s.substr(0, s.length - d.length);
		q = s.indexOf("?");
		q = s.substr(q != -1 ? q : s.length);
		s = s.substr(0, s.length - q.length);
		q = !q ? [] : q.substr(1).split("&");

		return [s, q, d];
	}

	function AddParam(u, k, v) {
		u = SplitUrl(u);
		var q = u[1].filter(
				function(o) { return o && o.indexOf(k + "=") != 0 });	/* != 0 ??? */
		q.push(k + "=" + escape(v));
		return u[0] + "?" + q.join("&") + u[2];
	}

	function BasicAsync(item, cb, q) {
		var h, o, u;

		u = (typeof item == "object" ? item.href : item);
		o = $Parent(item, "li");

		if (!cb) cb = DefaultCallback(u, item.rev == "async:np");
		__p.Request(u, q, cb, true, o);
	}

	function DefaultCallback(i, np) {
		return function(o) {
			var u = i ? ParseParams(i, np) : null;
			var g = ShowAsync(o);
			if (g && (g[1] || u)) window.location = g[1] || u;
			return null;
		};
	}

	function ShowAsync(o) {
		if (o.responseXML)
		with (o.responseXML.documentElement) {
			var dst = getElementsByTagName("destination")[0];
			var mod = dst.getAttribute("mode");
			var txt = getElementsByTagName("data")[0].firstChild.nodeValue;
			var i = $(dst.firstChild.nodeValue);
			var g = dst.getAttribute("go");
			if (mod == "append")
				i.innerHTML += txt;
			else
				i.innerHTML = txt;
			return [i, g ? "#_" + g.substr(2) : null];
		}
		throw "Invalid asynchronous response received.";
	}

	function MoreAsync(o) {
		ShowAsync(o);	/* TODO */
		// update more content ?
	}

	function __callback(o, cb, lr) {
		if (o.readyState != 4)
			return;

		var er, ld, ob;
		er = (o.status != 200 && o.status != 0);
		if (!er) {
//			if (lr) lr.className += " __sel";
			try { if (cb) ld = cb(o, lr); } catch (ex) { er = ex; console.error(er); }
			//if (typeof ld == "object") lr = ld;
		}
		ob = _async.filter(function(a) { return o == a[0] })[0];
		if (lr) __p.Loader(lr, 0);
		if (er) { if (lr) $Remove(lr, "__sel"); CallListeners("error", ob) }
		if (ob) _async.splice(_async.indexOf(ob), 1);
		delete o;
	}

/* Render */
	function InitHeader() {
		var dv = $New("div");
		var hd = $("iHeader");
		dv.style.opacity = 1;
		while (hd.hasChildNodes())
			dv.appendChild(hd.firstChild);
		hd.appendChild(dv);
		_head = dv;
	}

	function InitTab() {
		var o = $$("ul");
		for(var i = 0; i < o.length; i++) {
			var p = o[i].parentNode;
			if (p && $Has(p, "iTab")) {
				$Add($$("li", o[i])[0], "__act");
				if (p = $(o[i].id + "0"))
					Display(p, 1);
			}
		}
	}

	function InitRadio() {
		var o = $$("li");

		for (var i = 0; i < o.length; i++) {
			if ($Has(o[i], "iRadio")) {
				var lnk = $New("a");
				var sel = $New("span");
				var cpy = o[i].childNodes;
				var inp = $$("input", o[i]);
				for (var j = 0; j < inp.length; j++) {
					with (inp[j]) if (type == "radio" && checked) {
						sel.innerHTML = $Text(parentNode);
						break;
					}					
				}
				lnk.appendChild(sel);
				while (o[i].hasChildNodes())
					lnk.appendChild(o[i].firstChild);
				o[i].appendChild(lnk);
				lnk.href = "#";
				lnk.onclick = function() { _hold = 1; return SlideTo("waRadio") };
			}
		}

		var d = $New("div");
		d.className = "iLayer";
		d.id = "waRadio";
		_group.appendChild(d);
	}

	function ClickRadio(a, p, u) {
		var x = $$("input", p);
		var y = $$("a", u);
		for (var i = 0; i < y.length; i++)
			if (y[i] == a) {
				x[i].checked = true;
				$$("span", p)[0].innerHTML = $Text(x[i].parentNode);
				break;
			}
	}

	function ShowRadio(p) {
		var o = $$("input", p);
		var dv = $New("div");
		var ul = $New("ul");
		ul.className = "iCheck";

		for (var i = 0; i < o.length; i++) {
			if (o[i].type == "radio") {
				var li = $New("li");
				var a = $New("a");
				a.innerHTML = o[i].nextSibling.nodeValue;
				a.href = "#";
				a.onclick = function() { ClickRadio(this, p, ul) };
				li.appendChild(a);
				ul.appendChild(li);

				if (o[i].checked) li.className = "__act";
			}
		}

		dv.className = "iMenu";
		dv.appendChild(ul);

		o = $("waRadio");
		if (o.firstChild)
			delete o.removeChild(o.firstChild);
		o.title = $Text(p.firstChild);
		o.appendChild(dv);
	}

	function InitLock() { InitObj("div", "__wa_noclick") }
	function InitShadow() {
		var o = InitObj("div", "__wa_shadow");
		var h = $("iHeader");
		o.style.top = (h ? (h.offsetTop + h.offsetHeight) : 0) + "px";
	}	
	function InitObj(t, i) {
		var o = $New(t);
		o.id = i;
		_webapp.appendChild(o);
		return o;
	}

	function InitCheck() {
		var o = $$("input");

		for (var i = 0; i < o.length; i++) {
			if (o[i].type == "checkbox" && $Has(o[i], "iToggle")) {
				var txt = o[i].title.split("|");
				if (txt.length != 2)
					txt = ["ON", "OFF"];

				var b1 = $New("b");
				var b2 = $New("b");
				var i1 = $New("i");
				b1.className = "iToggle";
				b1.title = o[i].id;
				b1.innerHTML = "&nbsp;";
				i1.innerHTML = txt[1];
				b1.appendChild(b2);
				b1.appendChild(i1);
				o[i].parentNode.insertBefore(b1, o[i]);
				b2.onclick = function() { FlipCheck(this); };
				FlipCheck(b2, true);
			}
		}
	}

/* PreLoad */
	addEventListener("load", Init, true);
	addEventListener("click", ListenClick, true);

/* Static */
	return __p;
})();

var WA = WebApp;