//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function'){
        window.onload = func;
    } else {
        window.onload = function(){
        oldonload();
        func();
        }
    }

}

function AlbumImage (id, file, caption, orient)
{
    this.id = id;
    this.file = file;
    this.caption = caption;
    this.orientation = orient;
}

AlbumImage.prototype.getId = function () { return this.id; }
AlbumImage.prototype.getPath = function () { return this.file; }
AlbumImage.prototype.getCaption = function () { return this.caption; }
AlbumImage.prototype.getOrientation = function () { return this.orientation; }

function Album ()
{
    this.ready = false;
    this.list = new Array;
    this.current = -1;
    this.previous = -1;

    if (!document.getElementsByTagName){ return; }

    var e = document.getElementById('image_list');
    if (e != null)
    {
        var links = e.getElementsByTagName('a');
        for (var i = links.length - 1; i >= 0; i--)
        {
            var id = links[i].parentNode.id.split('_');
            var img = new AlbumImage(
                id[1],
                links[i].href,
                links[i].getAttribute('title'),
                links[i].parentNode.className
                );
            this.list.unshift(img);
            links[i].href = 'javascript: select_image('+i+');';
        };
    }

    if (this.list.length > 0) this.current = 0;
    this.preload(this.current);
    this.preload(this.nextIdx());
    this.preload(this.previousIdx());
    this.ready = true;
}

Album.prototype.isReady = function() { return this.ready; }

Album.prototype.start = function()
{
    this.setImage(this.current);
}

Album.prototype.setImage = function(idx)
{
    var e = document.getElementById('image_screen');
    e.innerHTML = '<img src="'+this.list[idx].getPath()+'" class="'+this.list[idx].getOrientation()+'" />';
    // e.className = this.list[idx].getOrientation();

    e = document.getElementById('caption');
    e.innerHTML = this.list[idx].getCaption();
    if (e.innerHTML == '') e.parentNode.style.visibility = 'hidden';
    else e.parentNode.style.visibility = 'visible';

    this.hightlightThumb();
}

Album.prototype.selectImage = function(idx)
{
    if (idx < 0 || idx >= this.list.length) return;

    this.previous = this.current;
    this.current = idx;
    this.setImage(this.current);

    this.preload(this.nextIdx());
    this.preload(this.previousIdx());
}

Album.prototype.nextImage = function()
{
    if (this.list.length < 0) return;

    this.previous = this.current;
    this.current = this.nextIdx();
    this.setImage(this.current);

    this.preload(this.nextIdx());
}

Album.prototype.nextIdx = function()
{
    if (this.list.length < 0) return;

    if (this.current + 1 < this.list.length)
        return this.current + 1;

    return 0;
}

Album.prototype.previousImage = function()
{
    if (this.list.length < 0) return;

    this.previous = this.current;
    this.current = this.previousIdx();
    this.setImage(this.current);

    this.preload(this.previousIdx());
}

Album.prototype.previousIdx = function()
{
    if (this.list.length < 0) return;

    if (this.current - 1 >= 0)
        return this.current - 1;

    return this.list.length - 1;
}

Album.prototype.preload = function(idx)
{
    var imgPreloader = new Image();
    imgPreloader.src = this.list[idx].getPath();
}

Album.prototype.hightlightThumb = function()
{
    var old_idx = this.previous;
    var new_idx = this.current;
    if (old_idx == null) new_idx = old_idx;
    if (old_idx == -1) old_idx = new_idx;

    e = document.getElementById('thumb_'+this.list[old_idx].getId());
    if (e != null)
    {
        var o = e.className.split(' ');
        var n = '';
        for (var i = o.length - 1; i >= 0; i--){
            if (o[i] != 'highlight')
                n += o[i]+' ';
        };
        e.className = n;
    }
    e = document.getElementById('thumb_'+this.list[new_idx].getId());
    e.className += ' highlight';
}

var album = null;
function init_album ()
{
    album = new Album;

    var e = document.getElementById('image_projector');
    e.style.display = 'block';

    album.start();
}
function next_image () { album.nextImage(); }
function prev_image () { album.previousImage(); }
function select_image (i)
{
    album.selectImage(i);
    var e = document.getElementById('image_screen');
    if (e) e.scrollIntoView();
}
function projector_size (size)
{
    e = document.getElementById('image_projector');
    e.className = 'image_projector_'+size;
}

addLoadEvent(init_album);

