var slideshowClass = Class.create({

    initialize: function(ids, slideshow_prefix, slideshow_items_prefix, slideshow_items_class, object_name, start_link, stop_link){
        this.ids = ids;
        this.s_pref = slideshow_prefix;
        this.si_pref = slideshow_items_prefix;
        this.si_class = slideshow_items_class;
        this.delay = 5000;
        this.object = object_name;
		this.timeout = window.setInterval(this.object+'.showNext()', this.delay);
        this.start_link = start_link;
        this.stop_link = stop_link;
        this.item_width = 62;
        this.items_div_width = 487;
    },

    showItem: function(id){
        if (this.timeout) {
            this.stop();
        }
        this.rotate(id);
        this.show(id);
    },

    show: function(id){
        var si_class = this.si_class;
        $$('#'+this.s_pref+' li').each(function(el){
            if (el.style.display != 'none') {
                hide_id = el.id;
            }
        });
        $$('#'+this.si_pref+' li').each(function(el){
            if (el.hasClassName(si_class)) {
                el.removeClassName(si_class);
            }
        });
        var show_id = this.s_pref+'_'+id;
        $(this.si_pref+'_'+id).addClassName(si_class);
        new Effect.Fade(hide_id, {duration: 1.0, queue: 'front'});
        new Effect.Appear(show_id, {duration: 1.0, queue: 'end'});
    },

    start: function(){
        $(this.start_link).hide();
        $(this.stop_link).show();
        this.timeout = window.setInterval(this.object+'.showNext()', this.delay);
    },

    stop: function(){
        $(this.stop_link).hide();
        $(this.start_link).show();
        window.clearInterval(this.timeout);
    },

    showNext: function(){
        var id = this.ids.shift();
        this.ids.push(id);
        if ($(this.s_pref+'_'+id).style.display != 'none') {
            id = this.ids.shift();
            this.ids.push(id);
        }
        this.show(id);
    },

    rotate: function(id){
        var ids = this.ids;
        for (var i = 0; i < ids.length; i++) {
            if (ids[i] == id) {
                var j = i;
            }
        }
        for (i = 0; i <= j; i++) {
            ids.push(ids[i]);
        }
        this.ids = ids.slice(j+1);
    },

    itemsNext: function(){
        if ((parseInt($(this.si_pref).style.width) + parseInt($(this.si_pref).style.left)) > this.items_div_width) {
            new Effect.Move($(this.si_pref), { x: -parseInt(this.item_width*3), mode: 'relative', duration: 0.6});
        }
    },

    itemsPrev: function(){
        if (parseInt($(this.si_pref).style.left) < 0) {
            new Effect.Move($(this.si_pref), { x: parseInt(this.item_width*3), mode: 'relative',  duration: 0.6});
        }
    },

    setHeight: function(){
        var heights = new Array();
        $$('#'+this.s_pref+' li').each(function(el, index){
            heights.push(el.getHeight());
            if (index > 0) {
                el.hide();
            }
        });
        $(this.s_pref).style.height = heights.max()+'px';
    }

});
