
function slideshow ()
{
    this.images =  null;
    this._images = new Array();
    this._loaded = new Array();
    this._timeout = null;
    this.name = null;
    this.displayName = null;
    this.navName = null;

    this.current = 0;
    this.last    = null;

    this.auto = function ()
    {
        this.next();
        this._timeout = setTimeout(this.name +'.auto()',6000);
    }

    this.next = function ()
    {
        if (this.current < this.last) {
            var next = this.current + 1;
        } else {
            var next = 0;
        }
        this.seek(next);
    }

    this.seek = function (index)
    {
        if (this._timeout != null) {
            clearTimeout(this._timeout);
            this._timeout = null;
        }
        if (Object.isNumber(index)
            && Math.round(index) == index) {
            if (this.current != index) {
                this.show(index);
                this.hide(this.current);
                this.current = index;
            }
        }
    }

    this.show = function (index)
    {
        var lid = this.getLayerId(index);
        var nid = this.getNavId(index);
        if ($(lid)) {
        //    $(lid).show();
           jQuery('#'+ lid).fadeIn(800);
        //   $(lid).setOpacity(75);
        }
        if ($(nid)) {
            $(nid).addClassName('on');
        }

    }

    this.hide = function (index)
    {
        var lid = this.getLayerId(index);
        var nid = this.getNavId(index);
        if ($(lid)) {
       //    $(lid).hide();
       //    $(lid).setOpacity(25);
            jQuery('#'+ lid).fadeOut(800);
        }
        if ($(nid)) {
            $(nid).removeClassName('on');
        }
    }

    this.start = function ()
    {
        this.last = (this.images.length - 1);
        for (i=0;i<=this.last;i++) {
            var id = this.getLayerId(i);
            var elm = new Element('div',{'id':id,'class':'slide'});
            elm.hide();
            $(this.displayName).insert(elm);
         //   this.hide(i);
        }
        var id = this.getLayerId('Load');
        var elm = new Element('div',{'id':id,'class':'loading'});
        elm.update('loading...');
        $(this.displayName).insert(elm);
        this.loadImages();
        this.setupNav();
    }

    this.setupNav = function ()
    {
        $(this.navName).hide();
        for (i=0;i<=this.last;i++) {
            var id = this.getNavId(i);
            var elm = new Element('a',{'id':id,'href': 'javascript:'+ this.name +'.seek('+ i +')'});
            elm.update('<span>'+ (i + 1) +'</span>');
            $(this.navName).insert(elm);
        }
    }

    this.loadImages = function ()
    {
        this._images = new Array((this.images.length - 1));
        for (i=0;i<=this.last;i++) {
            var id = this.getLayerId(i);
            
            // load & observe
            var imgElm = new Image();
            imgElm.src = this.images[i].image;
            $(imgElm).writeAttribute('rel',i);
            $(imgElm).writeAttribute('name',this.name);
            $(imgElm).observe('load', function (event) {

                var elm   = Event.element(event);
                var name  = $(elm).readAttribute('name');
                var index = $(elm).readAttribute('rel');
                eval(name +'.loaded('+ index +')');
            //    top.alert(this.name);
            });
            // create ref.
            $(id).insert(new Element('img',{'src':this.images[i].image,'rel':i,'id':this.name +'Img'+ i}));
            // this._imags
            //$(id).setStyle({'background':"url("+ this.images[i].image +") top left no-repeat"});
            //$(id).update('<img src="'+ this.images[i].image +'">');
            this._images[i] = imgElm;
        }
    }

    this.loaded = function (loaded)
    {
        //elm = Event.element(event);
        // top.alert(elm.src);

       // top.alert(this._images[0].image);
       // loaded = -1;
       //  var loaded = this.findIndexByImage(elm.src);
        // var loaded = $(elm).getAttribute('rel');
        if (loaded > -1) {
            if (loaded == 0) {
                this.show(0);
                this.current = 0;
            }
            this._loaded[this._loaded.length] = loaded;
            if (this._loaded.length == this._images.length) {
                this.hide('Load');
                jQuery('#'+ this.navName).fadeIn(1000);
                this._timeout = setTimeout(this.name +'.auto()',10000);
            }
        }
    }

    this.findIndexByImage = function (url)
    {
        for (i=0;i<=this.last;i++) {
            if (this._images[i].image == url) {
                return i;
            }
        }
        return -1;
    }

    this.getLayerId = function (index)
    {
        return this.name +"Layer"+ index;
    }

    this.getNavId = function (index)
    {
        return this.name +"Nav"+ index;
    }

}

var slideWait = new Image();
slideWait.src = '/images/icons/wait.gif';
