GameShop = Class.create({
    initialize: function(){
        this.initDescription();
        this.initUserReview();
        this.initGondole();
        this.initCarousel();
        this.UNWOUND_SLIDE_CLASS_NAME = 'slide_on';
    },
    initDescription: function(){
        $$('.slide_box').each(function(descBox, index){
            var show = descBox.down('.slide_box_more');
            var hide = descBox.down('.slide_box_less');
            var body = descBox.down('.slide_box_body');
            if (!show || !hide || !body) return;
            show.down('a').observe('click', this.showDescription.bind(this, show, hide, body, descBox));
            hide.down('a').observe('click', this.hideDescription.bind(this, show, hide, body, descBox));
        }.bind(this));
    },
    showDescription: function(show, hide, body, descBox){
    	this.hideAll();
        new Effect.BlindDown(body, effects.game);
        new Effect.Opacity(show, Object.extend({
            from: 1,
            to: 0,
            afterFinish: function(){
                show.hide();
                hide.setOpacity(0);
                hide.show();
                new Effect.Opacity(hide, {
                    from: 0,
                    to: 1
                });
            }.bind(this)
        }, {
            duration: effects.game.duration / 2
        }));
        descBox.addClassName(this.UNWOUND_SLIDE_CLASS_NAME);
    },
    hideAll: function() {
    	$$('.slide_box').each(function(descBox, index){
            if(descBox.hasClassName(this.UNWOUND_SLIDE_CLASS_NAME)) {
                var show = descBox.down('.slide_box_more');
                var hide = descBox.down('.slide_box_less');
                var body = descBox.down('.slide_box_body');
                if (!show || !hide || !body) return;
            	this.hideDescription(show, hide, body, descBox);
            }            
        }.bind(this));
    },
    hideDescription: function(show, hide, body, descBox){
        new Effect.BlindUp(body, effects.game);
        new Effect.Opacity(hide, Object.extend({
            from: 1,
            to: 0,
            afterFinish: function(){
                hide.hide();
                show.setOpacity(0);
                show.show();
                new Effect.Opacity(show, {
                    from: 0,
                    to: 1
                });
            }.bind(this)
        }, {
            duration: effects.game.duration / 2
        }));
        descBox.removeClassName(this.UNWOUND_SLIDE_CLASS_NAME);
    },
    initUserReview: function(){
        if (!$('scrollToUserReview') || !$('gameUserRating')) return;

        $('scrollToUserReview').observe('click', function(){
            new Effect.ScrollTo('userReviewsBox', effects.game);
        });
        $('gameUserRating').observe('click', function(){
            new Effect.ScrollTo('userReviewsBox', effects.game);
        });
        var userReviewsBox = $('userReviewsBox');
        if (! userReviewsBox.down('a.info')) {
            return;
        }
        userReviewsBox.down('a.info').observe('click', this.showDescription.bind(
            this,
            userReviewsBox.down('div.more_info_box_right'),
            userReviewsBox.down('div.more_info_box_right', 1),
            userReviewsBox.down('div.theme_t')
            ));
        userReviewsBox.down('a.info', 1).observe('click', this.hideDescription.bind(
            this,
            userReviewsBox.down('div.more_info_box_right'),
            userReviewsBox.down('div.more_info_box_right', 1),
            userReviewsBox.down('div.theme_t')
            ));
    },
    initGondole: function(){
        if (!$('scrollToGames')) return;
        $('scrollToGames').observe('click', function(){
            new Effect.ScrollTo('scrollToMe', effects.game);
        });
    },
    initCarousel: function() {
        if (!$('carousel-game')) return;
        var slidesCount = 3;
        if ($('carousel-game').hasClassName('isMM')) slidesCount=2;
        new Carousel($('carousel-game'), $$('.carousel-content .slide'), $$('a.carousel-control'), {
            duration: 1,
            transition: 'sinoidal',
            circular: false,
            step: slidesCount,
            visibleSlides: slidesCount,
            wheel: false
        });
    }
});

document.observe("dom:loaded", function() {
    new GameShop();
});

