/*
@author     Igor "SkAZi" Potapov <igor@potapoff.org>
@copyright  Plan-B Ltd.
@requires   MooTools 1.2
*/

function ImageScroller( promo_images, bindings ){
    
    this.img_width = $('promo').getSize().x;
    this.img_height = $('promo').getSize().y;
    this.promo_images = promo_images;
    this.loading = promo_images.length + 1;
    
    var that = this;
    this.onimagesready = function(){
        
        if( document.location.href.test(/#\d$/) ){
            var id = parseInt(document.location.href.match(/#(\d$)/)[1]);
            var pindex ;
            this.promo_images.each(function( item, index ){
                if(item[3]==id)
                    pindex = index;
            });
            if(pindex || pindex === 0)
                that.firstScrollTo(pindex);
        }
        
    }

    window.addEvent('domready', function(){

        $('page').setStyle('overflow', 'hidden');

        if(bindings && typeof(bindings) == 'object')
            that.bindTo( bindings );
            
        else if(bindings){
            that.promo_images.each(function( item, i ){

                if(i>0 && i<that.promo_images.length-1){
                    new Element('a', {
                            'href' : '#',
                            'html' : item[2]
                    }).inject( new Element('li').inject( $(bindings) ) );
                
                }
                
            });
            
            that.bindTo( $(bindings).getElements('a') );
        }

    });
    that.setupScroller();
    
}



ImageScroller.prototype.scrollTo = function( left, fast ){
        
    $("promo-text").set('tween', { 'fps': 12, 'duration': 150 })
        
    new Fx.Tween( $("promo-roller"),
        { 'fps': 24, 'duration': fast? 1: 700, 'transition': Fx.Transitions.Back.easeInOut } )
        .start('marginLeft', left)
        .chain( function() {
            
            if( left == 0)
                this.roller.style.marginLeft = ( -parseInt(this.roller.style.width) + this.img_width*2 )+"px";
                
            if( left <= -parseInt(this.roller.style.width) + this.img_width )
                this.roller.style.marginLeft = (-this.img_width)+'px';
            
            var pos = (-(left + this.img_width) / this.img_width) + 1;
            
            $('promo-text-content').set('html', this.promo_images[pos][1] || '')
                .getElements('a[href$=nospam]').each(function( item ){
                    mailize(item);
            });                   
                
            $("promo-text").tween('opacity', 1);
        }.bind( this ) );
        
    $("promo-text").tween('opacity', 0);

}



ImageScroller.prototype.slideTo = function( x, fast, doafter ){
    var left = parseInt(this.roller.style.marginLeft);

    var that = this;
    var scroll = function(){
        $('promo-loader').setStyle('visibility', 'hidden');
        
        if(!(left % that.img_width)){
            that.scrollTo( -x * that.img_width, fast );
            
            if( x == 0 )
                x = that.promo_images.length-2;
            if( x == that.promo_images.length-1 )
                x = 1;
            
            if(that.buttons)
                that.buttons.each(function( btn, i ){
                    if( i == x-1 )
                        btn.addClass( 'selected' );
                    else
                        btn.removeClass( 'selected' );
                });
        }
        
    }
    
    var images = this.roller.getElements('img');

    if(images[ x ].getStyle('visibility') == 'hidden'){
        images[ x ].onload = function(){
            scroll();
            if(doafter) doafter();
        }

        images[ x ].setStyle('visibility', 'visible').setAttribute( 'src', this.promo_images[x][0] );

        var go = false;
        switch(x){
            case images.length-1:
                go = 1; break;    
            case 1:
                go = images.length-1; break;
            case 0:
                go = images.length-2; break;
            case images.length-2:
                go = 0; break;
        }
        if(go !== false)
            images[ go ].setStyle('visibility', 'visible').setAttribute( 'src', this.promo_images[ x ][0] );

    } else
        scroll();
        
    return false;
}



ImageScroller.prototype.slideToLeft = function( ){
    $('promo-loader').setStyle('visibility', 'visible');
    var x = -parseInt(this.roller.style.marginLeft) / this.img_width;
    this.slideTo( x-1 ); 
    return false;
}



ImageScroller.prototype.slideToRight= function( ){
    $('promo-loader').setStyle('visibility', 'visible');
    var x = -parseInt(this.roller.style.marginLeft) / this.img_width;
    this.slideTo( x+1 ); 
    return false;
}



ImageScroller.prototype.setupScroller = function(){

    this.roller = $("promo-roller");
    this.roller.style.marginLeft = (-this.img_width) + 'px';

    this.promo_images.push( this.promo_images[ 0 ] );
    this.promo_images.unshift( this.promo_images[ this.promo_images.length-2 ] );
    
    this.roller.style.width = this.img_width * this.promo_images.length;
    $('promo-roller').setStyle('width', this.promo_images.length*this.img_width);

    var that = this;
    this.promo_images.each(function(item) {
        
        new Element('img', {
            'src': oldschool? item[0]: '/media/img/0.gif',
            'alt': item[1],
            'title': '',
            'width': that.img_width+'px',
            'height': that.img_height+'px',
            'styles': {
                'width': that.img_width+'px',
                'height': that.img_height+'px',
                'visibility': 'hidden'
            },
            'events': {
                'load' : function(){
                    that.loading--;
                    if(!that.loading)
                        that.onimagesready();
                }
            }

        }).inject( that.roller );
        
    });
    
    /*if (Browser.Engine.name == 'trident' && Browser.Engine.version < 7 ){
        $('promo-hover').addEvent('mouseover', function(){ this.addClass('hover') });
        $('promo-hover').addEvent('mouseout', function(){ this.removeClass('hover') });
    }*/

    $("promo-left").addEvent('click', this.slideToLeft.bind( this ) );
    $("promo-right").addEvent('click', this.slideToRight.bind( this ) );
    
}


ImageScroller.prototype.firstScrollTo = function( i ){
    
    this.slideTo( i, true, function(){
        $('page').addClass('hat-foot');
        $('promo').setStyles({'visibility': '', 'position': '', 'top': ''});
        $('page-sub-header').setStyle('display', '');
        $('persons-list-index').setStyle('display', 'none');
    } );
    return false;
}


ImageScroller.prototype.bindTo = function( buttons ){
    this.buttons = buttons;
    
    var that = this;
    this.buttons.each(function( button, i ){
        
        button.addEvent('click', function(){
            that.slideTo( i + 1 );
            hideDropDown.apply( this.getParent('ul').getParent('li') );
            return false;
        });
        
    });
    
    $$('#persons-list-index h4 a').each(function( item, i ){
        item.addEvent('click', function(){ that.firstScrollTo( i+1 ); return true; } );
    });    
    
}

