//	Gallery v1.0 by Bjørn Rosell
//	Find more scripts here: www.prototypeDHTML.net

/* The path formula must contain the special placeholders: "[name]" and "[state]".
   Example: "images/eform_vlampe_[name]_[state].jpg"
*/
function Gallery(pathFormula) {
    this.pathFormula = pathFormula;
	this.id = Gallery.count;
	Gallery.instances[Gallery.count] = this
	Gallery.count++
}
Gallery.instances = []
Gallery.count = 0
Gallery.getInstance = function(id) {
	return Gallery.instances[id]
}
Gallery.prototype.setImageStateNames = function(sLarge, sThumb, sThumbOver) {
	this.stateNames = [sLarge, sThumb, sThumbOver];
}
Gallery.prototype.setImages = function(aImages) {
	this.aImages = aImages;
    
    // preload thumb over
    for(i=0; i<aImages.length; i++) {
        var img = new Image();
        img.src = this.getImagePath2(i, 2);
    }
}
Gallery.prototype.getImagePath = function(name, stateName) {
	return this.pathFormula.split('[name]').join(name).split('[state]').join(stateName);
}
Gallery.prototype.getImagePath2 = function(iImage, iState) {
    var name = this.aImages[iImage];
    var stateName = this.stateNames[iState];
	return this.getImagePath(name, stateName);
}
Gallery.prototype.getDivElm = function() {
	return API.getElmById('galleryElm'+this.id)
}
Gallery.mouseOverThumb = function(elm, iGallery, iImage) {
    var gal = Gallery.getInstance(iGallery);
    elm.src = gal.getImagePath2(iImage, 2);
}
Gallery.mouseOutThumb = function(elm, iGallery, iImage) {
    var gal = Gallery.getInstance(iGallery);
    elm.src = gal.getImagePath2(iImage, 1);
}
Gallery.mouseClickThumb = function(iGallery, iImage) {
    var gal = Gallery.getInstance(iGallery);
    var elm = API.getElmById('galleryLarge' + iGallery)
    elm.src = gal.getImagePath2(iImage, 0);
}
/* The path formula must contain the special placeholders: "[direction]" and "[state]".
   [direction]: left | right
   [state]: over | normal
   Example: "images/scrollbutton_[direction]_[state].gif"
*/
Gallery.prototype.setScrollButtonImagePaths = function(formula) {
    this.scrollButtonsFormula = formula;
}
Gallery.prototype.setScrollButtonDelta = function(delta) {
    this.scrollButtonDelta = delta;
}
Gallery.prototype.getScrollButtonImagePath = function(direction, stateName) {
    return this.scrollButtonsFormula.split('[direction]').join(direction).split('[state]').join(stateName);
}
Gallery.mouseOverScrollButton = function(elm, iGallery, isLeft) {
    var gal = Gallery.getInstance(iGallery);
    elm.src = gal.getScrollButtonImagePath((isLeft ? 'left' : 'right'), 'over');
}
Gallery.mouseOutScrollButton = function(elm, iGallery, isLeft) {
    var gal = Gallery.getInstance(iGallery);
    elm.src = gal.getScrollButtonImagePath((isLeft ? 'left' : 'right'), 'normal');
}
/*
Gallery.mouseDownScrollButton = function(bLeft, iGallery) {
    var gal = Gallery.getInstance(iGallery);
    var id = window.setInterval(function() {
        var elm = API.getElmById('galleryThumbs' + iGallery)
        elm.scrollLeft += 5;
    }, 50)
}
*/
Gallery.mouseClickScrollButton = function(bLeft, iGallery) {
    var gal = Gallery.getInstance(iGallery);
    var elm = API.getElmById('galleryThumbs' + iGallery)
    if (bLeft) {
        Gallery.scrollThumbs(elm, -(gal.scrollButtonDelta))
    }
    else {
        Gallery.scrollThumbs(elm, gal.scrollButtonDelta);
    }   
}

Gallery.scrollThumbs = function(elm, delta) {
    var numberOfTimes = 10;
    var miniDelta = delta / numberOfTimes;
    function doScroll() {
        elm.scrollLeft += miniDelta;
        numberOfTimes--
        if (numberOfTimes != 0) {
            window.setTimeout(doScroll, 20);
        }
    }
    doScroll();
}


Gallery.prototype.getHTML = function(offsetMonths) {
	var sb = [];
    sb.push('<div class="thumbsbar">');
    if (this.scrollButtonsFormula) {
        sb.push('<img src="' + this.getScrollButtonImagePath('left', 'normal') + '"');
        sb.push(' id="galleryScrollLeft' + this.id + '"');
        sb.push(' class="galleryScrollButtonLeft"');
        sb.push(' onmouseover="Gallery.mouseOverScrollButton(this, ' + this.id + ', true)"');
        sb.push(' onmouseout="Gallery.mouseOutScrollButton(this, ' + this.id + ', true)"');
        sb.push(' onclick="Gallery.mouseClickScrollButton(true, ' + this.id + ')"');
        sb.push(' />');
    }
    sb.push('<div id="galleryThumbs' + this.id + '" class="thumbs">');
    for (var i=0; i<this.aImages.length; i++) {
        var id = "galleryThumb" + this.id + "_" + i;
        
        sb.push('<img src="' + this.getImagePath2(i, 1) + '"');
        sb.push(' id="' + id + '"');
        sb.push(' onmouseover="Gallery.mouseOverThumb(this, ' + this.id + ',' + i + ')"');
        sb.push(' onmouseout="Gallery.mouseOutThumb(this, ' + this.id + ',' + i + ')"');
        sb.push(' onclick="Gallery.mouseClickThumb(' + this.id + ',' + i + ')"');
        sb.push(' />');
    }
    sb.push('</div>');
    if (this.scrollButtonsFormula) {
        sb.push('<img src="' + this.getScrollButtonImagePath('right', 'normal') + '"');
        sb.push(' id="galleryScrollRight' + this.id + '"');
        sb.push(' class="galleryScrollButtonRight"');
        sb.push(' onmouseover="Gallery.mouseOverScrollButton(this, ' + this.id + ', false)"');
        sb.push(' onmouseout="Gallery.mouseOutScrollButton(this, ' + this.id + ', false)"');
        sb.push(' onclick="Gallery.mouseClickScrollButton(false, ' + this.id + ')"');
        sb.push(' onmousedown="Gallery.mouseDownScrollButton(false, ' + this.id + ')"');
        sb.push(' />');
    }
    sb.push('</div>');
    sb.push('<div class="large">');
    var id = "galleryLarge" + this.id;
    sb.push('<img id="' + id + '"' + '" src="');
    sb.push(this.getImagePath2(0, 0));
    sb.push('"/>');
    sb.push('</div>');
	return sb.join("");
}
Gallery.prototype.writeHTML = function() {
	document.write('<div class="gallery" id="galleryElm'+this.id+'">'+this.getHTML()+'</div>')
}

