var slideOut;
var slideIn;
var caption_state = 'hidden';
var player = null; // video player id

function initSlideCaption(pos, width)
{
    slideOut = new YAHOO.util.Anim('caption', {
        left: { from: 240, to: pos },
        width: { from: width, to: 0 }
        }, 2, YAHOO.util.Easing.easeOut);
    slideOut.onComplete.subscribe(updateCaptionState);

    slideIn = new YAHOO.util.Anim('caption', {
        left: { from: pos, to: 240 },
        width: { from: 0, to: width }
        }, 2, YAHOO.util.Easing.easeOut);
    slideIn.onComplete.subscribe(updateCaptionState);
}

function slideCaption ()
{
    if (caption_state == 'visible')
    {
        caption_state = 'visible-animating';
        slideOut.animate();
    }
    else if (caption_state == 'hidden')
    {
        caption_state = 'hidden-animating';
        slideIn.animate();
    }
}

function updateCaptionState()
{
    if (caption_state == 'visible-animating')
    {
        caption_state = 'hidden';
    }
    else if (caption_state == 'hidden-animating')
    {
        caption_state = 'visible';
    }
}

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--)
        {
            if (links[i].className != 'img') continue;

            var id = links[i].parentNode.id.split('_');
            var img = new AlbumImage(
                // id[1],
                i,
                links[i].pathname,
                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()
{
    if (this.ready)
        this.setImage(this.current);
}

Album.prototype.setImage = function(idx)
{
    var path = this.list[idx].getPath();
    if (browser.isIE) path = '/'+path;

    // is this a video?
    if (player != null)
    {
        // kludge together an image path
        var image_path = path.substr(0, path.indexOf('zooms'))
            +'stills'+path.substr(path.lastIndexOf('/'), path.length - path.lastIndexOf('/') - 3)
            +'jpg';

        var obj = {
            file:path,
            image:image_path,
            type:'video',
            title:this.list[idx].getCaption()
            };
        player.sendEvent('PLAY', false);
        player.sendEvent('LOAD', obj);
    }
    else // just an image
    {
        // animate the transition
        var anim = new YAHOO.util.Anim('visible_image', {
             opacity: { to: 0.0 }
            }, 1, YAHOO.util.Easing.easeOut);
        anim.onComplete.subscribe(function(){
            var e = document.getElementById('visible_image');
            e.style.backgroundImage = 'url("'+path+'")';

            var x = new YAHOO.util.Anim('visible_image', {
                 opacity: { to: 1 }
                }, 1, YAHOO.util.Easing.easeOut);
            x.animate();
        });

        // hard swap
        var e = document.getElementById('visible_image');
        // e.style.backgroundImage = 'url("'+path+'")';
        var cls = e.className.split(' ');
        var ncls = '';
        for (var i = cls.length - 1; i >= 0; i--) {
            if (cls[i] != 'landscape' && cls[i] != 'portrait')
                ncls += ' '+cls[i];
        };
        e.className = ncls +' '+ this.list[idx].orientation;

        anim.animate();
    }

    // update the caption
    var cap = document.getElementById('caption_copy_text');
    cap.innerHTML = '';
    e = document.getElementById('thumb_caption_'+this.list[idx].getId());
    if (e != null)
    {
        // # of #
        //cap.innerHTML = '<span class="of">'+pad(String(idx+1), String(this.list.length).length, '0', STR_PAD_LEFT)+' of '+this.list.length+'</span>';
        cap.innerHTML += e.innerHTML;
    }

//    e.innerHTML = '<img src="'+this.list[idx].getPath()+'" />';
//    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)
{
    // no preload when video
    if (player != null) return;

    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;
    album.start();
}
function next_image () { album.nextImage(); }
function prev_image () { album.previousImage(); }
function select_image (i)
{
    album.selectImage(i);
    // var e = document.getElementById('visible_image');
    // if (e) e.scrollIntoView();
}

// ==================
// =  Video Control =
// ==================

// see player var above
function playerReady(thePlayer) { player = document.getElementById('video'); init_album(); }
