var lbdPhotoGallery = new Class({
    Implements: [Options, Events],

    initialize: function() {
        this.current_image = parseInt($('photo-current-image').get('text'));
        this.images_count = parseInt($('photo-count').get('text'));

        var object = this;
        this.slider = new Fx.Tween('photo-previews-inner',{
            duration:'long',
            onStart: function(){
                object.slider_active = true;
            },
            onComplete: function() {
                object.slider_active = false;
            }
        });
        this.current_page = Math.ceil(this.current_image / 3);
        this.slideImagesContainer(this.current_page);

        this.initEvents();
    },

    initEvents : function(){
        var object = this;
        $$('.photo_img').addEvent('click',this.showImgPhoto.bind(this));
        $$('a.photo').addEvent('click',this.showAPhoto.bind(this));

        $$('.pager_item').addEvent('click',this.slideToPage.bind(this));

        $('photo-lister-prev').addEvent('click',this.showPrevPhoto.bind(this));
        $('photo-lister-next').addEvent('click',this.showNextPhoto.bind(this));

        $('photo-container').getElement('img').addEvent('click',this.showNextPhotoCycled.bind(this));
    },

    slideImagesContainer : function(num) {
        if (this.slider_active == true) {
            return false;
        }

        $$('.pager_item').each(function(item){
            item.removeClass('active');
        });
        if ($('pager-item-'+num)) $('pager-item-'+num).addClass('active');
        var ml = (num-1)*(-1)*(576);
        this.slider.start('margin-left',ml);
        this.current_page = parseInt(num);
    },

    slideToPage: function(evt){
        var num = evt.target.get('id').split('-').pop();
        this.slideImagesContainer(num);
    },

    showPhoto: function(num) {
        if (num > this.images_count || num <= 0 || this.slider_active == true) {
            return false
        }
        $('photo-container').getElement('img').destroy();
        var link = $('photo-'+num).get('href');
        var normlink = link.substr(1);
        $('send-link-input').value = baseUrl+normlink;
        $('embed-input').value = '<img src="'+baseUrl+normlink+'" alt="">';
        $('photo-comment').innerHTML = '';
        $('comment-wrapper').addClass('none');
        var img = new Element('img',{'src':link,'alt':''});
        img.fade('hide');
        var object = this;
        img.addEvents({
            'load':function(){
                img.fade('in');
            },
            'click':object.showNextPhotoCycled.bind(object)
        });
        $('photo-container').adopt(img);
        this.current_image = parseInt(num);
        $('photo-current-image').set('text',this.current_image);

        if ((this.current_image / 3) > this.current_page) {
            this.slideImagesContainer(Math.floor(this.current_image / 3) + 1);
        }
        if (Math.ceil(this.current_image / 3) < this.current_page) {
            this.slideImagesContainer(Math.ceil(this.current_image / 3));
        }
    },

    showImgPhoto: function(evt) {
        evt.stop();
        var num = evt.target.getParent('a').get('id').split('-').pop();
        this.showPhoto(num);
    },
    showAPhoto: function(evt) {
        evt.stop();
        var num = evt.target.get('id').split('-').pop();
        this.showPhoto(num);
    },
    showPrevPhoto: function() {
        this.showPhoto(this.current_image - 1)
    },
    showNextPhoto: function() {
        this.showPhoto(this.current_image + 1)
    },
    showNextPhotoCycled: function() {
        if (this.current_image + 1 > this.images_count) {
            this.showPhoto(1);
        } else {
            this.showPhoto(this.current_image + 1);
        }
    }
});

window.addEvent('domready',function(){
    window.sml = new lbdPhotoGallery();

    if (document.getElementById('share')) {
        $('share').slide('hide');
    }
    if (document.getElementById('embed')) {
        $('embed').slide('hide');
    }

    if (document.getElementById('link-share')){
        $('link-share').addEvent('click',function(evt){
            evt.stop();
            if (document.getElementById('embed')) {
                $('embed').slide('hide');
            }
            if (document.getElementById('share')) {
                $('share').slide('in');
            }
        });
    }
    if (document.getElementById('link-embed')){
        $('link-embed').addEvent('click',function(evt){
            evt.stop();
            if (document.getElementById('share')) {
                $('share').slide('hide');
            }
            if (document.getElementById('embed')) {
                $('embed').slide('in');
            }
        });
    }

    if (document.getElementById('post-photo-comment')){
        $('post-photo-comment').addEvent('click',function(evt){
            var num = window.sml.current_image;
            var link = $('photo-'+num).get('href');
            var file_name = link.substr(link.indexOf('image/')+6);
            var content = $('photo-comment-input').value;
            new Request.JSON({
                    'url' : '/photo/post_comment.json',
                    onComplete: function(res) {
                        if(res){
                            $('photo-comment').innerHTML = res.html;
                        }
                    }
                }).post({
                    'photo_name': file_name,
                    'content': content
                });
        });
    }

    if (document.getElementById('show-photo-comments')){
        $('show-photo-comments').addEvent('click',function(evt){
            $('comment-wrapper').removeClass('none');
            $('photo-comment').set('html', 'загружаем комментарии...<br/><br/>');
            var num = window.sml.current_image;
            var link = $('photo-'+num).get('href');
            var file_name = link.substr(link.indexOf('image/')+6);
            var content = '';
            new Request.JSON({
                    'url' : '/photo/post_comment.json',
                    onComplete: function(res) {
                        if(res){
                            $('photo-comment').innerHTML = res.html;
                        }
                    }
                }).post({
                    'photo_name': file_name,
                    'content': content
                });
        });
    }
});

