function Gallery() {
    this.init = init
    this.next = next
    this.prev = prev
    this.go = go
    this.open = open
    this.showLoader = showLoader
    this.setPic= setPic
    this.checkloaded = checkloaded
    this.preloadImages = preloadImages
    this.findPicById = findPicById
    this.scroll = scroll
    this.cumulativeOffset = cumulativeOffset
    
    function init(images, imagesPath, curImg, leerImg, containerID, titleContainerID, js_id) {
        this.images = images;
        this.imagesPath = imagesPath;
        this.curImg = curImg;
        this.leerImg = leerImg;
        this.containerID = containerID;
        this.titleContainerID = titleContainerID;
        this.js_id = (typeof js_id == 'undefined') ? '' : js_id;
    }

    function go(step) {
        this.showLoader(1);
        my$(this.containerID).src           = this.leerImg;
        
        i = this.curImg + parseInt(step);
        if (i >= this.images.length) i=0; 
        if (i < 0) i=this.images.length-1;
        
        this.setPic(i);
    }

    function open(id) {
        i = this.findPicById(id);
        if (i>=0) {
            this.showLoader(1);
            my$(this.containerID).src           = this.leerImg;
            this.setPic(i);
        }
        return false;
    }

    function preloadImages() {
        var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments; if(!a) return; for(i=0; i<a.length; i++)
        if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
    }

    function checkloaded() {
        if (this.containerID && my$(this.containerID) && my$(this.containerID).complete) {
            // Preloading next and prev images
            i = this.curImg;
        
            var prevID = i-1;
            if (prevID >= this.images.length) prevID=0; 
            if (prevID < 0) prevID=this.images.length-1;
            
            var nextID = i+1;
            if (nextID >= this.images.length) nextID=0; 
            if (nextID < 0) nextID=this.images.length-1;
            
    //            this.showLoader(0);
            
            preloadImages(this.imagesPath+this.images[prevID].i, this.imagesPath+this.images[nextID].i);
        }
        else
            setTimeout("Gallery"+this.js_id+".checkloaded()",100);
    }

    function setPic(i,loading) {
        my$(this.titleContainerID).innerHTML = my$(this.containerID).alt = (i+1);
        my$(this.containerID).src           = this.imagesPath+this.images[i].i;
        this.curImg = i;
        
        setTimeout("Gallery"+this.js_id+".checkloaded()",1000);
    }

    function showLoader(show) {
        if (my$(this.containerID))
            my$(this.containerID).style.visibility = (!show) ? 'visible' : 'hidden';
    }

    function findPicById(i) {
        for(var j=0;j<this.images.length;j++)
            if (this.images[j].id == i) return j;
        return -1;
    }

    function scroll() {
        pos = this.cumulativeOffset(my$(this.containerID));
        window.scrollTo(0, pos[1]); 
    }

    function cumulativeOffset (element) {
        var valueT = 0, valueL = 0;
        do {
          valueT += element.offsetTop  || 0;
          valueL += element.offsetLeft || 0;
          element = element.offsetParent;
        } while (element);
        return [valueL, valueT];
    }

    function next() { this.go(1); }
    function prev() { this.go(-1); }

}
