/**
 * @preserve Light-White Theme
 * http://null-pointer.ru
 *
 * Copyright (c) 2011, NullPointer
 * Licensed under the MIT license.
 * 
 * @author ankostyuk
 */
 
/*global jQuery, Galleria */

(function($) {

Galleria.addTheme({
    name: 'light-white',
    author: 'NullPointer',
    version: 0.1,
    css: 'galleria.light-white.css',
    defaults: {
        transition: 'fade',
        thumbCrop:  'height'
    },
    init: function(options) {
        
        // add some elements
        this.addElement('fullscreen');
        this.append({
            'stage' : ['fullscreen']
        });
        
        // cache some stuff
        var galleria = this,
            fullscreen = this.$('fullscreen'),
            touch = Galleria.TOUCH,
            click = touch ? 'touchstart' : 'click';
        
        if ( !options.thumbnails ) {
            this.$('counter').hide();
        }
        
        // some stuff for non-touch browsers
        if (! touch ) {
            this.addIdleState( this.get('image-nav-left'), { left:-100 });
            this.addIdleState( this.get('image-nav-right'), { right:-100 });
            this.addIdleState( this.get('counter'), { right:-100, bottom:-100 });
            this.addIdleState( this.get('fullscreen'), { left:-100, top:-100 });
        }
        
        // toggle fullscreen
        toggleFullscreen(true);
        fullscreen.click(function(){
            toggleFullscreen();
        });
        function toggleFullscreen(init) {
            if (init || galleria._fullscreen.active) {
                fullscreen.removeClass("full");
                fullscreen.attr("title", "Перейти в полноэкранный режим");
            } else {
                fullscreen.addClass("full");
                fullscreen.attr("title", "Выйти из полноэкранного режима");
            }
            if (!init) {
                galleria.toggleFullscreen();
            }
        };
        
        // bind some stuff
        this.bind('thumbnail', function(e) {
            
            if (! touch ) {
                // fade thumbnails
                $(e.thumbTarget).css('opacity', 0.6).parent().hover(function() {
                    $(this).not('.active').children().stop().fadeTo(100, 1);
                }, function() {
                    $(this).not('.active').children().stop().fadeTo(400, 0.6);
                });
                
                if ( e.index === options.show ) {
                    $(e.thumbTarget).css('opacity',1);
                }
            }
        });
        
        this.bind('loadstart', function(e) {
            if (!e.cached) {
                this.$('loader').show().fadeTo(200, 0.4);
            }
            $(e.thumbTarget).css('opacity',1).parent().siblings().children().css('opacity', 0.6);
        });
        
        this.bind('loadfinish', function(e) {
            this.$('loader').fadeOut(200);
        });
    }
});

}(jQuery));

