var Slider = new Class({
    initialize: function(o) {
    	this.oContainer = o.oContainer;
        this.sSliderItemCN = o.sSliderItemCN || 'slider-item';
        this.iw = o.iSliderItemRealHight;
        this.ih = o.iHiddenSliderItemRealHight;
        this.iContainerHight = o.iContainerHight;
        
        this.time = o.iTime || 600;
        this.iImageMove = o.iImageMove || 0;
        this.imgTime = this.time;
        
        this.aItems = [];
        this.oLastOpened = {};
    },
    
    init: function() {
        this.getItems();
        this.oLastOpened = this.aItems[0];
        this.addEvents();
    },
    
    addEvents: function() {
        var This = this, it;
        this.aItems.each(function(item, i) {
        	item.obj.addEvent('mouseenter', function(e) {
        		This.handleEvent(e, item, i);
        	});
        }, this);
    },
    
    handleEvent: function(e, oItem, it) {
        if (oItem == this.oLastOpened) return;
        var aItemsToAnimate = [], direction;
        
    	if (oItem.moved == 1) { 
            direction = 'top';
            for (var i=it+1; i<this.aItems.length; i++) {
                if (!this.aItems[i].moved) continue;
                aItemsToAnimate.push(this.aItems[i])
            }
        } else {
            direction = 'right';
        	for (var i=0; i<it+1; i++) {
                if (this.aItems[i].moved) continue;
                aItemsToAnimate.push(this.aItems[i]);
            }
        }
        
        this.animateImg(oItem);
        this.animateItem(aItemsToAnimate, direction);
        this.oLastOpened = oItem;
    },
    
    animateItem: function(a, dir) {
        var start, end;
        for (var i=0; i<a.length; i++) {
            if (dir == 'top') {
                end = a[i].startPosition;
            } else {
            	end = a[i].endPosition;
            }
            
            a[i].moved = (a[i].moved == 1) ? 0 : 1;
            this.move(a[i], end);
        }
    },
    
    animateImg: function(oItem) {
        this.moveImg(oItem, 0);
        this.moveImg(this.oLastOpened, this.iImageMove);
    },

    move: function(item, end) {
    	if (item.anim) {
            item.anim.cancel();
    		item.anim = null;
    	}
        item.anim = new Fx.Tween(item.obj, {duration: this.time, transition: Fx.Transitions.Sine.easeOut});
        item.anim.start('top', end);
    },  
    
    moveImg: function(item, end) {
    	if (item.imgAnim) {
            item.imgAnim.cancel();
    		item.imgAnim = null;
    	}
        item.imgAnim = new Fx.Tween(item.img, {duration: this.time, transition: Fx.Transitions.Sine.easeOut});
        item.imgAnim.start('top', end);
    },
      
    getItems: function() {
        var p = 0, q = 0, ile = 0, img, aSliders = [], divs = this.oContainer.getElements('div');
        divs.each(function(div, i) {
        	if (div.hasClass(this.sSliderItemCN)) {
        		ile++;
                aSliders.push(div);
        	}
        }, this);
                
        var marg = this.iContainerHight - this.iw;
        this.ih = marg / (ile - 1);
        
        for (var i=0; i<ile; i++) {
    		if (aSliders[i].hasClass(this.sSliderItemCN)) {
                img = aSliders[i].getElement('img');
                this.aItems.push({
                    obj: aSliders[i],
                    startPosition: p,
                    endPosition: q,
                    moved: (p == 0) ? 1 : 0,
                    img: img
                });
                
                if (p == 0) {
                    p = this.iw;
                    q = this.ih;
                } else {
                    p = p + this.ih;
                    q = q + this.ih;
                }
    		}
    	}
        
        // uklada w kontenerze na stronie rowniutko slider-items
        for (var i=0; i<this.aItems.length; i++) {
        	this.aItems[i].obj.style.top = this.aItems[i].startPosition + 'px';
        }
    }
});
