$(document).ready(function() {
	
	var maskHeight = $(document).height();
	var galleryHeight = $('.l-container').height();
	var myHeight = (maskHeight - galleryHeight)/2-15;
	
	$('.l-header, .l-footer').height(myHeight);
});

SliderModel = {
    queue: [],
    size: 0,
    host: '',
    // Init
    init: function(queue, host, box) {
       if (queue.length >= 1) {
            // Formation queue
            for (var i = 0; i < queue.length; i++) {        
                SliderModel.queue.push({'id':queue[i][0],'src': queue[i][1], 'photo_w': queue[i][2]});
            }
            SliderModel.size = queue.length;
        }
        //������������ ������ ����� ��� ��������
        var w = 0;
        for(var i = 0; i < SliderModel.size; i++){
            w += parseInt(queue[i][2]);
            w += 10;
        }
        $("#"+box).css('width', w+ 'px');
        //������� ������ ����� ��� ���� � ������������� ����������
        for (var i = 0; i < SliderModel.size; i++){
            var li = $('<li id = "li' + queue[i][0] + '" style="width: ' + queue[i][2] + 'px" class = "loading"></li>');
            $("#"+box+" ul").append(li);
        }
        SliderModel.host = host;
        //����������� ��������
        $('.l-gallery-i .scroll-pane').jScrollPane(
            {
              showArrows: true,
              horizontalDragMinWidth: 60,
              horizontalDragMaxWidth: 60
             }
         );
        // ��������� ��������
        $(document).everyTime('25ms', 'slideshow-load', function() {
            SliderModel.load();
        });
    },
    load: function(){
        // If queue not empty
        if (SliderModel.queue.length > 0) {
            var el = SliderModel.queue.shift();
            var img = new Image();
            $(img).bind("load", function() {
                var lbox = $('#li'+el.id);
                lbox.removeClass('loading');
                var abox = $('<a href="#" onclick = "return false"></a>').append($(this));
                lbox.append(abox);
            }).attr('src', SliderModel.host+el.src);

        }
        else {
            $(document).stopTime('slideshow-load-queue');
        }
    }

}

BackgroundModel = {
    images: [],
    host: '',
    count: 0,
    limit: 0,
    box: '',
    action: false,
    current: 0,
    couter: 0,
    time: 500,
    timer: 5000,
    pause: false,
    init: function(images, host, count, limit, box) {
        BackgroundModel.images = images;
        BackgroundModel.host = host;
        BackgroundModel.count = count;
        BackgroundModel.limit = limit;
        BackgroundModel.box = box;
        if (BackgroundModel.count > 1) {
            if (BackgroundModel.count < BackgroundModel.limit) {
                BackgroundModel.limit = BackgroundModel.count;
            }
            BackgroundModel.counter = 0;
            BackgroundModel.loading = false;
            $(document).everyTime('25ms', 'background-loader', function() {BackgroundModel.loadImages()});
            $(document).everyTime('25ms', 'start', function(){
                if (BackgroundModel.action && BackgroundModel.pause){
                   BackgroundModel.slideImages();
                   $(document).everyTime(BackgroundModel.timer, 'slideshow', function() {BackgroundModel.slideImages()});
                   $(document).stopTime('start');
                }
            });
        }
        else {
            BackgroundModel.loadSingleImage();
        }
    },
    loadSingleImage: function() {
        var img = new Image();
        $(img).load(function() {
            $(this).css('display','none');
            $('#'+BackgroundModel.box).removeClass('loading').append($(this));
            $(this).fadeIn(BackgroundModel.time);
        }).attr('src', BackgroundModel.host+BackgroundModel.images[0]);
    },
    loadImages: function() {
        if (BackgroundModel.counter < BackgroundModel.images.length) {
            if (BackgroundModel.loading == false) {
                BackgroundModel.loading = true;
                var img = new Image();
                $(img).bind("load", function() {
                    $(this).css('display','none');
                    $('#'+BackgroundModel.box).append($(this));

                    if (BackgroundModel.counter == 0) {
                        $('#'+BackgroundModel.box).removeClass('loading');
                        $('#'+BackgroundModel.box+' img').eq(0).fadeIn(BackgroundModel.time);
                        $(document).oneTime(BackgroundModel.timer, function(){
                            BackgroundModel.pause = true;
                        });
                    }
                    if (BackgroundModel.counter == BackgroundModel.limit - 1){
                       BackgroundModel.action = true;
                    }
                    BackgroundModel.counter += 1;
                    BackgroundModel.loading = false;
                }).attr('src', BackgroundModel.host+BackgroundModel.images[BackgroundModel.counter]);
            }
        } else {
            $(document).stopTime('background-loader');
        }
    },
    slideImages: function() {
        var imgs = $('#'+BackgroundModel.box+' img');
        var size = imgs.size();
        if (BackgroundModel.action) {
            var prev = BackgroundModel.current;
            $(imgs[BackgroundModel.current]).css('z-index', '10');
            if (BackgroundModel.current < (size - 1)) {
                BackgroundModel.current = BackgroundModel.current + 1;
            } else {
                BackgroundModel.current = 0;
            }
            $(imgs[BackgroundModel.current]).fadeIn(1500, function(){
                $(imgs[prev]).fadeOut(1500);
                });
        }
    }
}

TextModel = {
    init: function(image, host, box) {
        $('.b-info-text .scroll-pane').jScrollPane(
            {
              showArrows: true,
              horizontalDragMinWidth: 60,
              horizontalDragMaxWidth: 60,
              verticalDragMinHeight: 60,
              verticalDragMaxHeight: 60

             }
         );
        var img = new Image();
        $(img).load(function() {
            $('#'+box).removeClass('loading').css('opacity','0').append('<img src = "' + $(this).attr('src') + '"/>');
            $('#'+box).animate({opacity: 1}, 700);
        }).attr('src',host+'storage/backgrounds/'+image);
    }
}

