var JournalController = Class.create();
JournalController.prototype = {
    
    post_list_loaded: false,

    initialize: function() {
    },

    titleActionJournal: function(title, url) {
        var value = title.value.strip();
        new Ajax.Request(url,
            { 
                method: 'post',
                asynchronous: true,
                parameters: {
                    title: value
                },
                evalScripts:true,
                onSuccess: function(transport) {
                    var ret = eval("(" + transport.responseText + ")");
                    if (ret.error)
                        alert(ret.error)
                    else if (ret.redirect)
                        document.location = ret.redirect;
                    else
                        document.location.reload();
                }
            });
    },

    filterPopJournal: function(block_id, sel) {
        var block = $(block_id);
        this.filterRedraw(block_id, sel);
        new Ajax.VideoUpdater(block_id, '/dnevniki/ajax/pop/list/draw/',
            {
                method: 'get',
                asynchronous: true,                
                parameters: {
                    time: sel
                },
                evalScripts:true
        });
    },

    filterMostComment: function(block_id, sel) {
        var block = $(block_id);
        this.filterRedraw(block_id, sel);        
        new Ajax.VideoUpdater(block_id, '/dnevniki/ajax/most/comment/list/draw/',
            {
                method: 'get',
                asynchronous: true,                
                parameters: {
                    time: sel
                },
                evalScripts:true
        });
    },

    filterLastUpdate: function(block_id, sel) {
        var block = $(block_id);
        this.filterRedraw(block_id, sel);        
        new Ajax.VideoUpdater(block_id, '/dnevniki/ajax/last/update/list/draw/',
            {
                method: 'get',
                asynchronous: true, 
                parameters: {
                    state: sel
                },
                evalScripts:true
        });
    },

    filterRedraw: function(block_id, sel) {
        var filter_block = $('filter_' + block_id);
        filter_block.select('span.FilterEl').each(
            function(f_el) {
                if ($(f_el).readAttribute('filter') == sel)
                    $(f_el).show();
                else
                    $(f_el).hide();
            }
        );        
        filter_block.select('a').each(
            function(f_el) {
                if ($(f_el).readAttribute('filter') == sel)
                    $(f_el).hide();
                else
                    $(f_el).show();
            }
        );
    },

    calendarMSlide: function(block_id, el, add, id) {
        el = $(el);
        var val = Number(el.up('div.CalendarHeader').down('div.Month').readAttribute('month')) + add;
        if (val > 12 || val < 1)
            return;
        el.up('div.CalendarHeader').down('div.Month').writeAttribute('month', val)
        var y = el.up('div.CalendarHeader').down('div.Year').readAttribute('year');
        el.up('div.CalendarHeader').down('div.Month').update(str_month[val]);
        new Ajax.Updater(block_id, '/dnevniki/ajax/calendar/draw/',
            {
                method: 'get',
                asynchronous: true, 
                parameters: {
                    id: id,
                    month: val,
                    year: y
                },
                evalScripts:true
        });
        
    },

    calendarYSlide: function(block_id, el, add, id) {
        el = $(el);
        var val = Number(el.up('div.CalendarHeader').down('div.Year').readAttribute('year')) + add;
        el.up('div.CalendarHeader').down('div.Year').writeAttribute('year', val);
        el.up('div.CalendarHeader').down('div.Year').update(val);
        var m = el.up('div.CalendarHeader').down('div.Month').readAttribute('month');
        new Ajax.Updater(block_id, '/dnevniki/ajax/calendar/draw/',
            {
                method: 'get',
                asynchronous: true, 
                parameters: {
                    id: id,
                    year: val,
                    month: m
                },
                evalScripts:true
        });
        
    },

    hideListPostVideo: function() {
        $('journal_post_video_select').hide();
    },

    showAllReader: function(journal_id ) {
        $('journal_reader_list').update('<img src="' + image_path + '/window_loading.gif">');
        new Ajax.Updater('journal_reader_list', '/dnevniki/ajax/show/all/reader/',
        {
            method: 'get',
            asynchronous: true, 
            evalScripts:true,
            parameters: {
                journal_id: journal_id
            },
            onSuccess:  function() {
            }
        });

    },

    reader_load_scroll: function (journal_id, section_el)
    {
        // уже грузим
        if (section_el.readAttribute('loaded') == '1')
            return;
        section_el.writeAttribute('loaded', '1');
        var page = section_el.readAttribute('page');
        var scroll_top = section_el.scrollTop;
        page ++;
        // загрузка след. страницы
        new Ajax.Updater(section_el, '/dnevniki/ajax/show/all/reader/page/',
            {
                method: 'get',
                asynchronous: true,
                parameters: 'journal_id=' + escape(journal_id)  + ('&page=' + escape(page)),
                evalScripts:true,
                onSuccess: function() {
                    section_el.writeAttribute('page', page);
                    section_el.writeAttribute('loaded', '0');
                },
                insertion: Insertion.Bottom
        });
    },

    viewListPostVideo: function( ) {
        var self = this;
        if (self.post_list_loaded)
        {
            $('journal_post_video_select').show();
            return;
        }
        new Ajax.VideoUpdater('journal_post_video_select', '/dnevniki/ajax/post/video/page/',
        {
            method: 'get',
            asynchronous: true, 
            evalScripts:true,
            onSuccess:  function() {
                self.post_list_loaded = true;
                $('journal_post_video_select').show();
            }
        });
        
    },

    lastUserViewJournal: function (el, id)
    {
        pp_control.runPopup(el, 'main_video_user_view', {
            showTimeout: 0,
            noMouseEvent: true,
            onFirstShow: function() {
                new Ajax.VideoUpdater("main_video_user_view", "/dnevniki/ajax/last/user/view/journal/",
                    {
                        method: 'get',
                        asynchronous: true,
                        parameters: 'id=' + escape(id),
                        evalScripts:true
                });
            }
        });
    }
}

journal_control = new JournalController();
