var slideShow;

if(slideShow == undefined) {
    slideShow = function(id, options) {
        this.init(id, options);
    };
}

slideShow.prototype.init = function (id, options) {
    this.container = id;
    this.settings = {
        'pages':            null,
        'timeout':          2000,
        'containerheight':  'auto',
        'runningclass':     'innerfade'
    };
    this.timer = null;
    this.last = 0;
    this.elements = null;
        
    if(options)
        for(var property in options)
            this.settings[property] = options[property]
};

slideShow.prototype.start = function () {
    var container = $(this.container)
    this.elements = container.childElements();
    if(this.elements && this.elements.length > 1) {
        container.setStyle({
            'position' : 'relative',
            'height' :   this.settings.containerheight
        });
        container.addClassName(this.settings.runningclass);
        
        for(var i = 0; i < this.elements.length; i++) {
            $(this.elements[i]).setStyle({
                'z-index' :  String(this.elements.length-i),
                'position' : 'absolute'
            });
            $(this.elements[i]).hide();
        };
        
        if(this.settings.pages)
            this.pages(this.elements);
        
        var slideshow = this;
        this.timer = setTimeout(function() {
            slideshow.next(slideshow.elements, 1);
        }, this.settings.timeout);
        
        $(this.elements[0]).show();
    }
};

slideShow.prototype.pages = function(elements) {
    var html = "<div style='width:100%;overflow:hidden;'>";
    var len = elements.length < 5 ? elements.length : 4;
    
    for(var i=0; i<len; i++) {
        html += "<a href='#' onclick='slideshow.slide(this); return false;' " +(i == 0 ? "class='on'" : "class='off'")+ ">" + (i + 1) + "</a>";
    }
    
    html += "</div><div class='leftCorner'><img src='/img/greybg_lbc.gif' /></div><div class='rightCorner'><img src='/img/greybg_rbc.gif' /></div>";
    $(this.settings.pages).insert(html);
}

slideShow.prototype.slide = function(node) {
    var current = 0;
    var pages = $(this.settings.pages).getElementsBySelector('a');

    for(var i in pages) {
        if(pages[i] == node)
            current = i;
    }
    
    clearTimeout(this.timer);
    
    this.next(this.elements, parseInt(current));
};

slideShow.prototype.next = function(elements, current) {
    this.setPagerClass($(this.settings.pages).getElementsBySelector('a'), current);
    
    $(elements[this.last]).hide();
    $(elements[current]).show();
    this.removeFilter(elements[current]);
    
    if((current + 1) < elements.length) {
        this.last = current;
        current = current + 1;
    } else {
        this.last = elements.length - 1;
        current = 0;
    }
    
    var slideshow = this;
    this.timer = setTimeout((function() {
        slideshow.next(slideshow.elements, current);
    }), this.settings.timeout);
};

slideShow.prototype.setPagerClass = function (node, current) {
    node[current].className="on";
    node[this.last].className="off";
}

slideShow.prototype.removeFilter = function (element) {
    if(element.style.removeAttribute){
        element.style.removeAttribute('filter');
    }
}
