var application = {
    langMas: {
        ru:{
            photo : "Фото",
            from : "из",
            close : "Закрыть"
        },
		en:{
            photo : "Photo",
            from : "from",
            close : "Сlose"
        }
	},
    lang:{},
    langFun: function(){
        curLang = $('#lang').text();
        switch(curLang){
            case'ru':
                this.lang = this.langMas.ru;
                break;
            case'en':
                this.lang = this.langMas.en;
                break;
        }
    },
    fancybox: function(){
        var self = this;
        $("a.fancybox").fancybox({
            'transitionIn'	:	'elastic',
            'transitionOut'	:	'elastic',
            'speedIn'		:	600, 
            'speedOut'		:	200,
            'padding'  	    :   20,
            'autoScale'     :   true,
            'titlePosition' :   'inside',
            'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
                return '<p class="fancybox-title">'+(title.length ? title : '')+'</p><p class="fancybox-sub-title">' + self.lang['photo'] +' '+ (currentIndex + 1) + ' ' + self.lang['from'] + ' ' + currentArray.length+'</p>';
            }
        });
        $("#fancybox-close").html(self.lang['close'])
    },
    gallery:{
        params: {
            target: null,
            slideLeft: null,
            slideRight: null,
            left: 0,
            step: 1,
            speed: 500,
            max: 0,
            min: 0
        },
        n: {
            target: '.slider',
            btnLeft:  '.gallery .arrows-left',
            btnRight: '.gallery .arrows-right'
        },
        o: {
            btn: {
                left: {
                    normal: 'arrows-left',
                    disabled:  'arrow-passive-left'
                },
                right: {
                    normal: 'arrows-right',
                    disabled:  'arrow-passive-right'
                }
            }
        },
        init: function()
        {
            var self = this;
            (function() {
                this.target = $(self.n.target);
                this.step = this.target.children().eq(0).width();
                this.target.css('width', (this.max = this.target.children().length * this.step) + 'px');
                this.max -= 5*this.step;
                this.left = 0;
                this.min -= Math.abs(this.left);
                this.slideLeft = $(self.n.btnLeft).addClass(self.o.btn.left.normal).addClass(self.o.btn.left.disabled);
                this.slideRight = $(self.n.btnRight).addClass(self.o.btn.right.normal);
                if(this.target.children().length < 6){
                    this.slideRight.addClass(self.o.btn.right.disabled);
                }
            }).call(self.params);
            
            self.params.slideRight.click(function(){
                self.slideRight(self, 1);
                return false;
            });
            self.params.slideLeft.click(function(){
                self.slideLeft(self, 1);
                return false;
            });
            self.customCurrent(self);
        },
        slideLeft: function(self, steps)
        {
            var step = (self.params.step*steps);
            (function() {
                if ((this.left - step) < this.min){
                    return;
                }
                if (this.slideRight.hasClass(self.o.btn.right.disabled)) { this.slideRight.removeClass(self.o.btn.right.disabled); }
                this.left -= step;
                if ((this.left - self.params.step) < this.min)
                    if (!this.slideLeft.hasClass(self.o.btn.left.disabled)) { this.slideLeft.addClass(self.o.btn.left.disabled); }
                this.target.animate({'left': '+=' + step + 'px'}, this.speed);
            }).call(self.params);
        },
        slideRight: function(self, steps)
        {
            var step = (self.params.step*steps);
            (function() {
                if ((this.left + step) >= this.max){
                    return;
                }
                this.left += step;
                if (this.slideLeft.hasClass(self.o.btn.left.disabled)) { this.slideLeft.removeClass(self.o.btn.left.disabled); }
                if ((this.left + self.params.step) >= this.max)
                    if (!this.slideRight.hasClass(self.o.btn.right.disabled)) { this.slideRight.addClass(self.o.btn.right.disabled); }
                this.target.animate({'left':  '-=' + step + 'px'}, this.speed);
            }).call(self.params);
        },
        customCurrent: function(self)
        {
            var hash = document.location.hash;
            if (!hash)
                return;
            self.params.target.children().each(function(key, value){
                if ($(value).attr('rel') == hash) {
                    self.slideRight(self, key);
                    return;
                }
            });
        }
    }
}

$(document).ready(function(){
    (function() {
        this.gallery.init();
        this.langFun();
        this.fancybox();
    }).call(application);
});

