﻿	

function Photo(src,thumb) {
    this.src = src;
    this.thumb = thumb;
}
function PhotoGallery(images,maxWidth,maxHeight,index,container,element,interval,autoStart) {
    this.index = index;
    this.images = images;
    this.maxWidth = maxWidth;
    this.maxHeight = maxHeight;
    this.container = $(container);
    this.status = $('status');
	this.element = $(element);
    this.preLoad  = new Image();
	this.interval = interval;
	this.autoStart = autoStart;
	
	//this.fadeIn = new Fx.Style(element,'opacity',{duration: 200});
	//this.fadeOut = new Fx.Style(element,'opacity',{duration: 2000});
	
	var self = this;
	this.element.onclick = function() { self.next(); }
}
PhotoGallery.prototype.curImage = function() {
    return this.images[this.index];
}
PhotoGallery.prototype.startSlideshow = function(speed) {
    this.stopSlideshow();   
    var self = this;
    this.interval = setInterval(function() { self.nextSlide() }, speed);
}
PhotoGallery.prototype.stopSlideshow = function() {
    clearInterval(this.interval);
}
PhotoGallery.prototype.nextSlide = function() {
    this.index = this.index + 1 == this.images.length ? 0 : this.index + 1;
    this.load();
}
PhotoGallery.prototype.next = function() {
    this.stopSlideshow();
    this.index = this.index + 1 == this.images.length ? 0 : this.index + 1;
    this.load();
}
PhotoGallery.prototype.prev = function() {
    this.stopSlideshow();
    this.index = this.index - 1 <= -1 ? this.images.length - 1 : this.index - 1;
    this.load();
}
PhotoGallery.prototype.load = function() {
    var self = this;
    //this.fadeIn.stop();
    //this.fadeOut.start(.2);
    this.preLoad.onload = function() { self.display() };
    this.preLoad.src = this.curImage().src;
}
PhotoGallery.prototype.display = function() {
    if(this.element.src != this.preLoad.src) {
        this.element.src = this.preLoad.src;
        this.setSize();
    }
    //this.fadeOut.stop();
    //this.fadeIn.start(1);
    
    this.status.innerHTML = ((this.index + 1) + ' / ' + this.images.length);
}
PhotoGallery.prototype.setSize = function() {
    var w = this.preLoad.width;
    var h = this.preLoad.height;
    if(w > this.maxWidth) {
        h = Math.floor((h / w) * this.maxWidth);
        w = this.maxWidth;
    }
    if(h > this.maxHeight) {
        w = Math.floor((w / h) * this.maxHeight);
        h = this.maxHeight;
    }

    this.container.style.width  = w + 'px';
    //this.container.style.marginLeft =  -Math.round(w/2) + 'px';
    this.container.style.height = h + 'px';
    //this.container.style.marginTop  = -Math.round(h/2) + 'px';
    this.element.style.width  = w + 'px';
    this.element.style.height = h + 'px';
}

