/* eko.js // 20090330 */

var edt = false;

var eko = {

    /** Core **/

    init: function() {
	eko.dom = d.location.protocol;
	eko.dom += '//' + d.location.host;
	eko.title = d.title.sub(0, d.title.lastIndexOf(' ') + 1);
	if (edt) edt.enable();
	if (!boot.fs) {
	    eko.disp();
	    eko.parse();
	    eko.track();
	    eko.xs = { };
	}
	// IE6 check
	if (d.all && !w.XMLHttpRequest)
	    c(gi('box'), 'ie');
    },

    track: function() {
	this.hash = d.location.href.sub(this.dom.length);
	if (this.hash.indexOf('#') != -1)
	    this.hash = this.hash.sub(0, this.hash.indexOf('#'));
	setInterval("eko.check()", 50);
    },

    parse: function() {
	var i, es;
	this.mns = { };
	es = gts(gi('menu'), 'a');
	for (i = 0; i < es.length; i++) {
	    this.mns[es[i].href] = es[i];
	}
    },

    disp: function() {
	var i, n, m, c, es;
	this.bks = { };
	es = gcs(gi('body'), 'hidden');
	for (i = 0; i < es.length; i++) {
	    if (nn(es[i]) != 'div')
		continue;
	    n = ga(es[i], 'name');
	    m = parseInt(ga(es[i], 'timeout'));
	    c = ga(es[i], 'coords').split(',');
	    this.bks[n] = { e: es[i],
			   m: m,
			   x: c[0] + '%',
			   y: c[1] + '%',
			   r: parseInt(c[3]) };
	    ss(es[i], { width: c[2] + 'px' });
	    if (ga(es[i], 'close'))
		es[i].onclick = function() { return eko.close(this); }
	}
	es = gts(gi('body'), 'a');
	for (i = 0; i < es.length; i++)
	    if (es[i].href)
		if (ga(es[i], 'ref'))
		    es[i].onclick = function() { return eko.jump(this); }
		else if (ga(es[i], 'tab'))
		    es[i].onclick = function() { return eko.tab(this); }
		else
		    es[i].onclick = function() { return eko.open(this); }
	setTimeout("eko.calc()", 200);
    },

    /** Public **/

    close: function(e) {
	var n = ga(e, 'name');
	this.fx(n, -1, true);
	return false;
    },

    open: function(e) {
	e.blur();
	if (boot.fs) return true;
	if (e.href.indexOf(this.dom) == -1 ||
	    e.href.sub(-4) == '.pdf')
	    return pop(e);
	if (e.href.indexOf('/admin') != -1 ||
	    e.href.indexOf('/webstat') != -1)
	    return true;
	var b, m, n, i;
	i = e.href.indexOf('#');
	if (i != -1) {
	    n = e.href.sub(i + 1);
	    for (m in this.bks) {
		b = this.bks[m];
		if (tpf(b) || m == n)
		    continue;
		if (ga(b.e, 'auto')) {
		    ss(b.e, { zIndex: 1 });
		    this.fx(m, -1, true);
		}
	    }
	    if (this.bks[n]) {
		ss(this.bks[n].e, { zIndex: 2 });
		this.fx(n, 1, true);
	    }
	} else {
	    for (n in this.bks)
		if (nf(this.bks[n]))
		    this.fx(n, -1, true);
	    this.ok = false;
	    this.dyn(e.href);
	    setTimeout("eko.ready()", 500);
	}
	return false;
    },

    tab: function(e) {
	e.blur();
	var v = pn(gpn(e));
	var ds = gxs(v, 'tab');
	var n, u, t = ga(e, 'tab');
	for (var i = 0; i < ds.length; i++) {
	    u = ga(ds[i], 'tab');
	    n = (nn(ds[i]) == 'div')? 'none': '';
	    c(ds[i], (u == t)? 'active': n);
	}
	return false;
    },

    jump: function(e) {
	e.blur();
	sa(gi('refs'), 'left', (900 -parseInt(ga(e, 'ref')) * 130));
	this.scrolling();
	return this.open(e);
    },

    /** Private **/

    calc: function() {
	var n, b, s, t;
	for (n in this.bks) {
	    b = this.bks[n];
	    if (tpf(b)) continue;
	    b.merge(gd(b.e, 2));
	    if (c(fc(b.e)) == 'box')
		u = { w: 0, h: 20 };
	    else u = { w: 0, h: 0 };
	    sd(fc(b.e), ds({ w: b.w, h: b.h }, u));
	    sd(b.e, { w: b.w, h: b.h });
	    s = (b.r >= 2)? -1: 1;
	    if (b.r % 2)
		t = { marginLeft: (s * b.w) + 'px' };
	    else t = { marginTop: (s * b.h) + 'px' };
	    sa(fc(b.e), 'fx', s * ((b.r % 2)? b.w: b.h));
	    ss(fc(b.e), t);
	    ss(b.e,
	       { left:       b.x,
		 top:        b.y,
	         marginLeft: -rn(b.w / 2) + 'px',
		 marginTop:  -rn(b.h / 2) + 'px' });
	    c(b.e, 'wrap');
	    if (b.m) setTimeout("eko.fx('" + n + "', 1, true)", b.m);
	}
	this.ok = true;
	return true;
    },

    fx: function(n, o, f) {
	var b = this.bks[n];
	if (!b || !b.e) return false;
	if (!ga(b.e, 'busy') || f)
	    sa(b.e, 'busy', o);
	else if (parseInt(ga(b.e, 'busy')) != o)
	    return false;
	var l = 'margin' + ((b.r % 2)? 'Left': 'Top');
	var v = parseFloat(ga(fc(b.e), 'fx'));
	var t = (o < 0)? ((b.r % 2)? b.w: b.h): 0;
	if (b.r >= 2) t = -t;
	var a = (o < 0)? .3: .08;
	var w = v + a * (t - v);
	if (Math.abs(t - w) < 1) w = t;
	var s = { };
	s[l] = rn(w) + 'px';
	sa(fc(b.e), 'fx', w);
	ss(fc(b.e), s);
	if (w != t)
	    setTimeout("eko.fx('" + n + "', " + o + ")", 20);
	else ra(b.e, 'busy');
	return true;
    },

    dyn: function(href) {
	if (this.r)
	    return false;
	var i, x = href.sub(this.dom.length + 1);
	if (!x) x = 'home';
	this.hash = '/' + x;
	d.location.href = '#/' + x;
	for (i in this.mns)
	    if (nf(this.mns[i]))
		c(pn(this.mns[i]), 'active', href.indexOf(i) != -1);
	if (this.xs[x])
	    return this.upd(x);
	this.r = false;
	if (w.XMLHttpRequest)
	    this.r = new XMLHttpRequest();
	else if (w.ActiveXObject)
	    this.r = new ActiveXObject('Microsoft.XMLHTTP');
	if (!this.r) return false;
	this.rx = x;
	this.r.onreadystatechange = function() { eko.proc(); }
	this.r.open('GET', '/xml/' + x, true);
	this.r.send(null);
	return true;
    },

    proc: function() {
	if (!this.r ||
	    this.r.readyState != 4 ||
	    this.r.status != 200)
	    return false;
	this.xs[this.rx] = fromXML(gt(this.r.responseXML, 'document'));
	this.upd(this.rx);
	this.r = false;
	return true;
    },

    upd: function(x) {
	if (!this.ok) {
	    setTimeout("eko.upd('" + x + "')", 50);
	    return false;
	}
	var cs;
	var e = this.xs[x];
	ih(gi('body'), e.body);
	d.title = this.title + e.title;
	cs = e.page.split('/').shift();
	if (!cs) cs = 'home';
	if (e.color)
	    cs += ' ' + e.color;
	else cs += ' white';
	c(d.body, cs);
	this.disp();
	c(gi('wait'), 'none', true);
	return true;
    },

    ready: function() {
	c(gi('wait'), 'none', false);
	this.ok = true;
    },
    
    check: function() {
	var h = d.location.hash.sub(1);
	if (!h || h.charAt(0) != '/')
	    return;
	if (this.hash != h)
	    this.dyn(this.dom + h);
    },

    redir: function(href) {
	d.location.href = href;
	return true;
    },

    scrolling: function() {
	var t = parseInt(ga(gi('refs'), 'left'));
	var ti = gi('refs').style.marginLeft;
	if (!ti) ti = 0;
	else ti = parseInt(ti);
	var tf = ti - rn(.2 * (ti - t));
	gi('refs').style.marginLeft = tf + 'px';
	if (tf != ti)
	    setTimeout("eko.scrolling()", 20);
    }

}

function fromXML(e, a) {
    var i, j, v, cx, n, xa, x = { };
    if (!e || !nl(e))
	return false;
    for (i = 0; i < nl(e); i++) {
	cx = cn(e, i);
	n = nn(cx);
	if (!n) continue;
	if (a) n = i;
	v = nv(fc(cx));
	switch (ga(cx, 'type')) {
	case 'arr':
	case 'array': x[n] = fromXML(cx, true); break;
	case 'obj':
	case 'object': x[n] = fromXML(cx); break;
	case 'bool':
	case 'boolean': x[n] = (v == 'true'); break;
	case 'num':
	case 'int':
	case 'float':
	case 'number': x[n] = parseFloat(v); break;
	default:
	    if (v && ga(cx, 'type') != tp(v))
		dbg('v', 'xml',
		    '<> ' + [ ga(cx, 'type'), tp(v), n ]);
	    if (!v) v = '';
	    x[n] = v.decode();
	}
    }
    if (!a) return x;
    xa = [ ];
    for (i in x)
	if (nf(x[i]))
	    xa.push(x[i]);
    delete(x);
    return xa;
}

w.onload = eko.init;