/*
   Filename: image_slider.js
   Author:   Falko Zander, falko@falko-zander.de
   Created:  2007-11-05
   Update:   2007-11-26

   Abstract: image slider control

   Notes:
*/

var image_slider_collection_ = [];
function image_slider(id, anim, speed, anim_speed) {
    if (this.registered_(id) != -1) return;

    var e = $(id);
    if (e == null) {
        error("image_slider: Your ID does not exists: " + id);
    }
    if (e.nodeName.toLowerCase() != "table") {
        error("image_slider: Image Slider has to be table: " + id);
    }
    var tds = e.getElementsByTagName("td");
    if (tds.length == 0) {
        error("image_slider: Your table may not be empty: " + id);
    }    
    var img = tds[0].getElementsByTagName("img");
    if (img.length == 0) if (img.length == 0) {
        error("image_slider: No images in gallery found: " + id);
    }
    for (var i = img.length - 1; i >= 0; i--) {
        if (img[i].parentNode.nodeName.toLowerCase() != "div") {
            error("image_slider: Each image must be encapsulated in a div container: " + id);
        }
    }
    if (img[0].parentNode.parentNode.nodeName.toLowerCase() != "div") {
        error("image_slider:\n" +
                        "All images must be encapsulated in a div container.\n" +
                        "All that div containers must be encapsulated in a new div container.\n" +
                        "This div container must be encapsulated in a td container");
    }
    var label = tds[2];
    if (typeof(label) == 'undefined') {
        error("image_slider: No label area found: " + id);
    }
    var left = tds[3].getElementsByTagName("img")[0];
    if (typeof(left) == 'undefined') {
        error("image_slider: No button area found for left button: " + id);
    }
    var right = tds[4].getElementsByTagName("img")[0];
    if (typeof(right) == 'undefined') {
        error("image_slider: No button area found for right button: " + id);
    }
    
    this.id = id;
    this.anim = anim || 0;
    this.speed = (typeof speed != 'undefined') ? (speed != 0 ? speed : 10) : 10;
    this.elem = e;
    this.images = img;
    this.label = label;
    this.previous_img = -1;
    this.active_img = 0;
    this.anim_info = {};
    this.handler = null;
    this.anim_speed = anim_speed || 100;
    image_slider_collection_.push(this);

    var idx = image_slider_collection_.length - 1;
    var THIS = this;
    left.onclick = function() { THIS.left_(idx); };
    right.onclick = function() { THIS.right_(idx); };
    this.show_(idx, 0);
}

/* end of public interface */

image_slider.prototype.registered_ = function(id) {
    for (var i = image_slider_collection_.length - 1; i >= 0; i--) {
        if (image_slider_collection_[i].id == id) return i;
    }
    return -1;
}

image_slider.prototype.show_ = function(idx, nbr) {
    var coll = image_slider_collection_[idx];
    if (typeof(coll) == 'undefined') error("image_slider: element undefined");

    if (coll.anim == 0 || coll.previous_img == -1) {
        if (coll.previous_img != -1) {
            coll.images[coll.previous_img].style.display = "none";
        }
        coll.images[nbr].style.display = "inline";
    } else {
        var i_o = coll.images[coll.previous_img];
        var i_n = coll.images[nbr];
        var p_o = i_o.parentNode;
        var p_n = i_n.parentNode;
            
        p_o.style.width = i_o.width + "px";
        if (coll.previous_img < nbr) {
            this.anim_info.direc = 1;
            p_n.style.width = "0px";
            this.anim_info.size = p_o.offsetWidth;
        } else {
            this.anim_info.direc = 0;
            p_n.style.width = i_o.width + "px";
            p_n.style.marginLeft = "-" + i_o.width + "px";
            this.anim_info.size = p_n.offsetWidth;
        }
        i_n.style.display = "inline";
        
        this.anim_info.slider_idx = idx;        
        this.anim_info.step = 0;
        this.anim_info.speed = coll.speed;
        this.anim_info.div_o = p_o;
        this.anim_info.div_n = p_n;

        if (this.handler == null) {
            var id = this.id;
            this.handler = window.setInterval("image_slider_animate_('" + id + "')", this.anim_speed);
        }
    }
    coll.previous_img = nbr;

    var img = coll.images[nbr];
    var s = img.src.reverse();
    var p = img.src.length - s.indexOf('/');
    s = img.src.substr(0, p) + "big/" + img.src.substr(p);
    img.onclick = function() { window.open(s, ''); };
    
    coll.label.innerHTML = img.alt;
}

image_slider.prototype.animate_ = function() {
    var s = this.anim_info.step;
    var o = this.anim_info.div_o;
    var n = this.anim_info.div_n;
    var d = this.anim_info.direc;
    var r = this.anim_info.size;

    if (s < r && d == 1 || s - r <= 0 && d == 0) {
        if (d == 1) {
            o.style.marginLeft = "-" + s + "px";
            n.style.width = s + "px";
            this.anim_info.step += this.anim_info.speed;
        } else {
            o.style.width = r - s + "px";
            n.style.marginLeft = s - r + "px";
            this.anim_info.step += this.anim_info.speed;
        }
    } else {
        o.getElementsByTagName("img")[0].style.display = "none";
        o.style.width = "0px";

        if (this.handler != null) {
            window.clearInterval(this.handler);
            this.handler = null;    
        }
    }
}

image_slider.prototype.left_ = function(idx) {
    var coll = image_slider_collection_[idx];
    if (typeof(coll) == 'undefined') error("image_slider: element undefined");

    if (coll.anim != 0 && this.handler != null) return;

    if (coll.active_img == 0) return;
    coll.active_img--;
    this.show_(idx, coll.active_img);
}

image_slider.prototype.right_ = function(idx) {
    var coll = image_slider_collection_[idx];
    if (typeof(coll) == 'undefined') error("image_slider: element undefined");

    if (coll.anim != 0 && this.handler != null) return;

    if (coll.active_img == coll.images.length - 1) return;
    coll.active_img++;
    this.show_(idx, coll.active_img);
}

/* end of private functions */

function image_slider_animate_(id) {
    for (var i = image_slider_collection_.length - 1; i >= 0; i--) {
        if (image_slider_collection_[i].id == id) {
            image_slider_collection_[i].animate_();
        }
    }
}

/* end of external functions */

/*
   Copyright Notice:

   Copyright 2007, Falko Zander
   ALL RIGHTS RESERVED

   UNPUBLISHED -- Use of a copyright notice is precautionary only and
   does not imply publication or disclosure.

   THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
   INFORMATION OF FALKO ZANDER. ANY DUPLICATION, MODIFICATION,
   DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS
   STRICTLY PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF
   FALKO ZANDER.
*/
