/**
 * @author Falko Zander, falko-zander.de
 */

/**
 * @param id String
 * @param anim Boolean
 * @param speed Number
 * @param anim_speed Number
 * @return Object
 */
function cImageSlider(id, anim, speed, anim_speed) {
    var e = $(id);
    if (e == null) {
        error("image_slider: Your ID does not exist: " + 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);
    }
    
    if (img.length < 2) {
        left.style.display = 'none';
        right.style.display = 'none';
    }
    
    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;
    
    this.label.innerHTML = this.alt_(this.images[0].alt);

    var THIS = this;
    left.onclick = function() { THIS.left_(); };
    right.onclick = function() { THIS.right_(); };
    this.show_(0);
}

/**
 * @return Object
 */
cImageSlider.prototype.destructor = function() {
    this.elem = null;
    this.images = null;
    this.label = null;
    this.anim_info = null;
    
    return null;
}

cImageSlider.prototype.alt_ = function(txt) {
    p_href = txt.indexOf('href');
    if (p_href != -1) {
        var p_a = txt.indexOf('<a');
        var sign = txt.substr(p_href + 5).substr(0, 1);
        var p_end_sign = txt.indexOf(sign, p_href + 6);
        href = txt.substring(p_href + 6, p_end_sign);
        txt = txt.replace(/href='.*'/, 'href="javascript:void(0);"');
        txt = txt.substring(0, p_a) + '<a onclick="window.open(\'' + href + '\', \'\');"' + txt.substr(p_a + 2); 
    }
    
    return txt;
}

cImageSlider.prototype.show_ = function(nbr) {
    if (this.anim == 0 || this.previous_img == -1) {
        if (this.previous_img != -1) {
            this.images[this.previous_img].style.display = "none";
        }
        this.images[nbr].style.display = "inline";
    } else {
        var i_o = this.images[this.previous_img];
        var i_n = this.images[nbr];
        var p_o = i_o.parentNode;
        var p_n = i_n.parentNode;
            
        p_o.style.width = i_o.width + "px";
        if (this.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.step = 0;
        this.anim_info.speed = this.speed;
        this.anim_info.div_o = p_o;
        this.anim_info.div_n = p_n;

        if (this.handler == null) {
            var THIS = this;
            var fn = function() { THIS.animate_(THIS.label); };
            this.handler = window.setInterval(fn, this.anim_speed);
        }
    }
    this.previous_img = nbr;

    var img = this.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, ''); };
}

cImageSlider.prototype.animate_ = function(label) {
    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";
        n.style.width = n.getElementsByTagName('img')[0].width + 'px';
        
        label.innerHTML = this.alt_(n.getElementsByTagName("img")[0].alt);

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

cImageSlider.prototype.left_ = function() {
    if (this.anim != 0 && this.handler != null) return;

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

cImageSlider.prototype.right_ = function() {
    if (this.anim != 0 && this.handler != null) return;

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

/*
   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.
*/

