/**
 * Скрипты для сайта shownews.ru
 * @author Сачик Сергей serginho.ru
 **/
$(function(){

    var body = $('#body');
    var normalWidth = 750;

    var windowResize = function(){
        if(body.width() > normalWidth){
            body.removeClass('narrow');
        } else {
            body.addClass('narrow');
        }
    }

    $(window).resize(windowResize);
    windowResize();

    $('a.news_main_image_link').fancybox();

    var photos_list = $('#photos_list');
    if(photos_list.hasClass('fancybox')){
        photos_list.find('a').fancybox({
            overlayColor: '#000',
            overlayOpacity: 0.8,
            padding: 3
        });
    }

    if(photos_list.length > 0){
        photos_list.initImagesList(false,true);
    }

    $('.side_block.photos .content').initImagesList(true,false);


    if(photos_list.length > 0){

        if(photos_list.hasClass('fancybox')){
            photos_list.find('a').fancybox({
                overlayColor: '#000',
                overlayOpacity: 0.8,
                padding: 3
            });
        }

        /*var items;

        var load_items = function(){
            items = photos_list.find('.item');
            items.each(function(){
                var $this = $(this);
                var image = $this.find('img');
                $this.data('init_width', image.width() + 2);
                $this.height(image.height());
                image.css('position','absolute');
                $this.css('overflow','hidden');
            });
        }

        load_items();



        var resize_items = function(){

            var main_width = photos_list.width();
            var line_items = [];
            var now_width = 0;
            for(var i = 0; i < items.length; i++){
                var item = items.eq(i);
                line_items.push(item);
                //console.log(item.data('init_width'));
                now_width += item.data('init_width');
                if(now_width > main_width){              
                    var dw = main_width / now_width * 0.98;
                    //console.log('y', now_width, main_width, dw, line_items.length);
                    for(var j in line_items){
                        var li = line_items[j];
                        var new_width = li.data('init_width') * dw - 2;
                        li.width(new_width);
                        li.find('img').css('left',(new_width - li.data('init_width')) / 2);
                    }
                    line_items = [];
                    now_width = 0;
                }

                for(var j in line_items){
                    var l = line_items[j];
                    l.width(l.data('init_width'));
                    l.find('img').css('left',0);
                }
            }
        }

        $(window).resize(resize_items);
        resize_items();
        //setTimeout(resize_items, 50);

        var init_show_photo_button = function(){
            $('#show_more_photos').click(function(){

                var $this = $(this);
                $this.html('Загрузка...');
                $.get(this.href, null, function(data){
                    $this.replaceWith(data);
                    load_items();
                    resize_items();
                    init_show_photo_button();
                });

                return false;
            });
        }

        setTimeout(init_show_photo_button, 100);*/


    }


});



$.fn.initImagesList = function(hide_extra_images, use_ajax){

    var $this = this;
    var items;    

    var reload = function()
    {
        items = $this.find('.item');
        items.preloadImagesListItems();

        if(use_ajax){
            var init_show_photo_button = function(){
                $this.find('.show_more_photos').click(function(){
                    var $this = $(this);
                    $this.html('Загрузка...');
                    $.get(this.href, null, function(data){
                        $this.replaceWith(data);
                        reload();
                    });
                    return false;
                });
            }

            setTimeout(init_show_photo_button, 200);
        }
        resize_items();
    }    

    var resize_items = function(){
        var main_width = $this.width();
        var line_items = [];
        var now_width = 0;
        for(var i = 0; i < items.length; i++){
            var item = items.eq(i);
            line_items.push(item);
            now_width += item.data('init_width');
            if(now_width > main_width){
                var dw = main_width / now_width * 0.98;
                for(var j in line_items){
                    var li = line_items[j];
                    li.show();
                    var new_width = li.data('init_width') * dw - 2;
                    li.width(new_width);
                    li.find('img').css('left',(new_width - li.data('init_width')) / 2);
                }
                line_items = [];
                now_width = 0;
            }

            for(var j in line_items){
                var l = line_items[j];
                if(hide_extra_images){
                    l.hide();
                } else {
                    l.width(l.data('init_width'));
                    l.find('img').css('left',0);
                }

            }
        }
    }

    reload();
    $(window).resize(resize_items);

    $('#vk_comments').css({width:'100%'});
    $('#vkwidget1').css({width:'100%'});
    
}

$.fn.preloadImagesListItems = function(){
    this.each(function(){
        var $this = $(this);
        var image = $this.find('img');
        $this.data('init_width', image.width() + 2);
        $this.height(image.height());
        image.css('position','absolute');
        $this.css('overflow','hidden');
        $this.width(image.width());
    });
}

