function emptyCall(){};
var ImageLinksSlider = new Class({
    loadAttributes: function(){
        if(! this.attributesLoaded)
        {
            this.element.getChildren().each(function(element){
                if(element.getTag() == 'a' && element.getProperty('href').contains('slide.'))
                {
                    var startAction = this.startMove.bind(this).pass(element.getProperty('href').substr(7));
                    var stopAction = this.stopMove.bind(this).pass(element.getProperty('href').substr(7));
                    element.addEvent('click', startAction);
                    element.addEvent('mouseover', startAction);
                    element.addEvent('mouseenter', startAction);
                    element.addEvent('mouseleave', stopAction);
                    element.setProperty('href','javascript:emptyCall();')
                }
                if(element.getTag() == 'div')
                {
                    this.size = element.getStyle('width').replace('px','').toInt();
                    this.slider = element.getFirst();
                    var moveAction = this.move.bind(this);
                    this.slider.addEvent('scroll',moveAction);
                    this.links = this.slider.getChildren();
                    this.calculateWidth();
                }
            }, this);
            this.slider.setStyle('opacity',0);
            this.scrollFX = new Fx.Style(this.slider, 'marginLeft', {duration:1000, wait:false, transition: Fx.Transitions.linear});
            this.startOpenTransactions();
        }
    },
    calculateWidth: function(){
        this.newWidth = 0;
        var buildSize = function(link){
            this.newWidth += link.getFirst().getSize().size.x + 3;
        }.bind(this)
        this.links.each(buildSize);
        if(! this.width)
        {
            this.width = this.newWidth;
        }
        if(this.width < this.newWidth)
        {
            this.width = this.newWidth;
            this.calculateWidth.bind(this).delay(500);
        }
    },
    startOpenTransactions: function(){
        this.attributesLoaded = true;
        var myTransition = new Fx.Transition(Fx.Transitions.Elastic, .6);
        var showLinks = function(){
            var enableButtons = function(){this.enableButton=true;}.bind(this);
            var transitionOptions = {'opacity': 1,'margin-left': (this.size-this.width)/2};
            this.slider.setStyle('marginLeft',-this.width);
            var slidingDuration = 4000
            if(this.width-this.size > 0)
            {
                slidingDuration = (this.width-this.size)*5
            }
            new Fx.Styles(this.slider, {duration:slidingDuration, wait:false, transition: myTransition.easeOut}).start(transitionOptions).chain(enableButtons);
        }.bind(this);
        var fx = new Fx.Style(this.element, 'opacity', {duration:300, wait:false, transition: Fx.Transitions.linear}).start(0,1).chain(showLinks);
    },
    initialize: function(elementToExtend){
        this.enableButton = false;
        this.attributesLoaded = false;
        this.element = elementToExtend;
        this.element.setStyle('opacity', 0);
        var onLoadAction = this.loadAttributes.bind(this);
        window.addEvent('load',onLoadAction);
        onLoadAction.delay(7000);
    },
    startMove: function(direction){
        if(this.enableButton && this.size < this.width)
        {
            this.direction = direction;
            this.slider.fireEvent('scroll');
        }
    },
    stopMove: function(){
        if(this.enableButton && this.size < this.width)
        {
            this.direction = '';
        }
    },
    move: function(event){
        var elementSize = this.size;
        var currentMargin = this.slider.getStyle('marginLeft').replace('px','').toInt();
        var refireEvent = function(){this.slider.fireEvent('scroll');}.bind(this);
        switch(this.direction)
        {
            case 'left':
                if(this.width + currentMargin > elementSize )
                {
                    var moveOffset = this.width/20 < this.width + currentMargin - elementSize ? this.width/20 : this.width + currentMargin - elementSize;
                    this.scrollFX.start(currentMargin,currentMargin-moveOffset).chain(refireEvent);
                }
            break;
            case 'right':
                if(currentMargin < 0)
                {
                    var moveOffset = this.width/20 < -(currentMargin) ? this.width/20 : -(currentMargin);
                    this.scrollFX.start(currentMargin,currentMargin+moveOffset).chain(refireEvent);
                }
            break;
        }
    }
});
ImageLinksSlider.extend(new Options, new Events);

var gallery;

window.addEvent('domready',function(){
    gallery = new ImageLinksSlider($('eventi-details-cont'));
});

