/* eko.script // 20090407 */

var edt = false;

var eko = {

    /** Core **/
    
    photos: { },
    full: false,
    last: false,

    init: function() {
	if (gi('photosBox')) {
	    eko.photos = { box: gi('photosBox'),
			   pvw: gi('photosPreview'),
			   lst: false };
	    if (ie)
		gi('photosAlbum').style.marginRight = '220px';
	    eko.view(fc(gfc(eko.photos.box)), 0);
	}
	eko.adjust();
	eko.handler();
	if (edt) edt.enable();
    },

    /** Public **/

    roll: function(e, i) {
	e.blur();
	gi('body').scrollTop += 25 - 50 * i;
	if (gi('body').scrollTop > 50)
	    gi('header').style.marginTop = (50 - gi('body').scrollTop) + "px";
	else gi('header').style.marginTop = 0;
	return false;
    },

    slide: function(e, i) {
	e.blur();
	this.scroll(this.photos.box.scrollLeft + 400 * i);
	return false;
    },

    view: function(e, i) {
	e.blur();
	this.full = (w.innerWidth || d.body.clientWidth || 640) > 1100 &&
	            (w.innerHeight || d.body.clientHeight || 480) > 800;
	if (this.photos.lst)
	    c(this.photos.lst, 'active', false);
	c(e, 'active', true);
	this.scroll(130 * (i - 2));
	this.photos.lst = e;
	var j = e.href.lastIndexOf('/');
	var pic = new Image();
	var size;
	size = (this.full)? '/.large': '/.medium';
	pic.onload = function() { eko.disp(this); };
	pic.src = e.href.sub(0, j) + size + e.href.sub(j);
	return false;
    },

    /** Private **/

    scroll: function(t) {
	var b = ga(this.photos.box, 'sc');
	sa(this.photos.box, 'sc', t);
	if (!b) this.scrolling();
	return false;
    },

    scrolling: function() {
	var t = parseInt(ga(this.photos.box, 'sc'));
	var ti = this.photos.box.scrollLeft;
	var tf = ti - rn(.2 * (ti - t));
	this.photos.box.scrollLeft = tf;
	if (this.photos.box.scrollLeft == ti)
	    ra(this.photos.box, 'sc');
	else setTimeout("eko.scrolling()", 20);
    },

    disp: function(e) {
	var img = fc(this.photos.pvw);
	var w = this.full? 320: 240;
	var h = this.full? 240: 160;
	if (this.full != this.last)
	    this.adjust();
	this.photos.pvw.style.height = (this.full? 520: 350) + "px";
	img.src = e.src;
	img.style.marginRight = (w - rn(e.width / 2)) + 'px';
	img.style.marginTop = (h - rn(e.height / 2)) + 'px';
	this.last = this.full;
    },

    adjust: function() {
	if (gi('photosBox')) {
	    var mg;
	    eko.width = gi('photosAlbum').offsetWidth - 60;
	    eko.height = gi('main').offsetHeight;
	    eko.photos.box.style.width = eko.width + 'px';
	    mg = Math.round((eko.height - 200 - (this.full? 520: 350)) / 2);
	    // mg = Math.max(0, mg);
	    eko.photos.pvw.style.marginTop = mg + "px";
	    eko.photos.pvw.style.marginBottom = mg + "px";
	} else {
	    eko.height = gi('main').offsetHeight;
	    eko.body = gi('body').offsetHeight;
	    eko.padding = max(0, rn((eko.height - eko.body) / 2));
	    if (eko.body > eko.height - 120)
		gi('body').style.height = (eko.height - 120) + 'px';
	    if (eko.body > eko.height)
		gi('arrows').style.display = 'block';
	    else gi('arrows').style.display = 'none';
	    gi('body').style.paddingTop = eko.padding + 'px';
	}
	gi('body').style.visibility = 'visible';
    },

    handler: function() {
	if (ie) {
	    d.attachEvent('onmousewheel',
			  function() { eko.wheel(w.event); });
	    w.attachEvent('onresize',
			  function() { eko.adjust(); });
	} else {
	    d.addEventListener('mousewheel',
			       function(e) { eko.wheel(e); },
			       true);
	    d.addEventListener('DOMMouseScroll',
			       function(e) { eko.wheel(e); },
			       true);
	    w.addEventListener('resize',
			       function(e) { eko.adjust(); },
			       true);
	}
    },

    wheel: function(e) {
	var wh = e.wheelDelta? e.wheelDelta > 0: e.detail < 0;
	gi('body').scrollTop += 25 - 50 * wh;
	if (gi('body').scrollTop > 50)
	    gi('header').style.marginTop = (50 - gi('body').scrollTop) + "px";
	else gi('header').style.marginTop = 0;
    }

}

w.onload = eko.init;