var Photolenta = new Class({
    initialize: function(photos)
    {
        this.photos = photos;
        this.previews = $$("#b07-photoLine ul li a");
        this.bigPhoto = $('photoCell');
        this.showIndex = 0;
        this.selectedIndex = 0;
        this.left = this.previews[0];
        this.left.addEvent('click', function(e){e.stop();this.goLeft()}.bind(this));
        this.previews.splice(0,1);
        this.right = this.previews[this.previews.length-1]
        this.right.addEvent('click', function(e){e.stop();this.goRight()}.bind(this));
        this.previews.splice(this.previews.length-1, 1);
        this.previews.each(function(item, index){
            item.addEvent('click', function(e){
                e.stop();
                this.showPicture(item, index);
            }.bind(this));
        }.bind(this));
        this.loading = $('gloader');
        this.showPreviews();
    },
    goLeft: function(){
        this.showIndex = this.showIndex?this.showIndex-1:this.photos.length-1;
        this.showPreviews();
    },
    goRight: function(){
        this.showIndex = this.showIndex==this.photos.length-1?0:this.showIndex+1;
        this.showPreviews();
    },
    showPreviews: function(){
        this.previews.each(function(item, index){
            item.getParent().removeClass('active');
        });
        this.previews.each(function(item, index){
            var index = this.showIndex + index;
            if(index >= this.photos.length)
                index-= this.photos.length;
            item.getFirst().src = this.photos[index][0];
            item.photoIndex = index;
            if(index==this.selectedIndex){
                item.getParent().addClass('active');
            }
        }.bind(this));
    },
    showPicture: function(item, index){
        this.bigPhoto.setStyle('background','none');
        this.loading.setStyle('display','block');
        var myImage = new Asset.images([this.photos[item.photoIndex][1]],{
            onComplete:function(){
                this.loading.setStyle('display','none');
                this.selectedIndex = item.photoIndex;
                this.bigPhoto.setStyle('background','transparent url('+this.photos[item.photoIndex][1]+') no-repeat scroll left top');
                this.showPreviews();
            }.bind(this)
        });
    },
    selectPreview:function(index){
        this.previews.each(function(item, index){
            item.getParent().removeClass('active');
        });
        this.previews[index].getParent().addClass('active');
    }
})

var PhotoPreviews = new Class({
    initialize: function()
    {
        this.photos = $$(".previews a");
        this.showIndex = 0;
        this.left = $('arrowLeft');
        this.left.addEvent('click', function(e){e.stop();this.goLeft()}.bind(this));
        this.right = $('arrowRight');
        this.right.addEvent('click', function(e){e.stop();this.goRight()}.bind(this));
    },
    goLeft: function(){
        this.showIndex = this.showIndex-1;
        this.showPreviews();
    },
    goRight: function(){
        this.showIndex = this.showIndex+1;
        this.showPreviews();
    },
    showPreviews: function(){
        this.photos.each(function(item, index){
            if((index>this.showIndex+7)||(index<this.showIndex)){
                item.setStyle('display','none');
            }else{
                item.setStyle('display','inline');
            }
            if(!((index-this.showIndex)%4)){
                item.getFirst().removeClass('left5');
            }else{
                item.getFirst().addClass('left5');
            }
        }.bind(this));
        if(!this.showIndex){
            this.left.setStyle('display','none');
        }else{
            this.left.setStyle('display','block');
        }
        if((this.showIndex+8)>=(this.photos.length)){
            this.right.setStyle('display','none');
        }else{
            this.right.setStyle('display','block');
        }
    }
});

