var cWindow_Startpage = {

    initButtons: function()
    {
        var _this = this;

        this.dom_edit_buttons = [];
        for (var i in this.blocks_data) {
            try {
                this.dom_edit_buttons[i] = $(_(this.name + '_edit_button_' + i));
            } catch (e) {
                continue;
            }

            this.dom_edit_buttons[i]
                .unbind('click')
                .click(function(block_id) {
                    return function() {
                        windows[_this.name + '_popup_editblock'].open(_this.blocks_data[block_id], _this.name);
                        return false;
                    }
                }(i));
        }

        this.dom_remove_buttons = [];
        for (var i in this.blocks_data) {
            try {
                this.dom_remove_buttons[i] = $(_(this.name + '_remove_button_' + i));
            } catch (e) {
                continue;
            }

            this.dom_remove_buttons[i]
                .unbind('click')
                .click(function(block_id) {
                    return function() {
                        if (confirm('Are you sure you want delete this block?')) {
                            _this.removeBlock(block_id);
                        }
                        return false;
                    }
                }(i));
        }

        this.dom_add_block_button = $(_(this.name + '_add_block_button'))
            .unbind('click')
            .click(function(){
                _this.addBlock();
                //return false;
            });

    },

    addBlock: function()
    {
        try {
            var r = Request.create();

            if (r !== null) {
                r.send(URL_AJAX_HANDLER, {'rm': 'add_block'}, this);
                delete(r);
            }
        }
        catch (e) {
            this.handleJSException(e);
        }
    },

    removeBlock: function(block_id)
    {
        if (this.blocks_data[block_id].block.block_type != 'USER') {
            return;
        }

        try {
            var r = Request.create();

            if (r !== null) {
                r.send(URL_AJAX_HANDLER, {'rm': 'remove_block', 'block_id': block_id}, this);
                delete(r);
            }
        }
        catch (e) {
            this.handleJSException(e);
        }
    },

    reorderBlocks: function() {
        var parity = 1;

        var user_blocks_count = 0;

        for (var i in this.blocks_data) {
            var cd = this.blocks_data[i].block;
            if (cd.block_type == 'USER') {
                var $block = $(_(this.name + '_user_block_' + cd.block_id));
                if (parity == 2) {
                    $block.removeClass('first');
                    parity = 1;
                } else {
                    $block.addClass('first');
                    parity = 2;
                }
                user_blocks_count++;
            }

        }

        for (var i in this.blocks_data) {
            var cd = this.blocks_data[i].block;
            if (cd.block_type == 'USER') {
                this.dom_remove_buttons[i]
                    .css('display', (user_blocks_count <= 4) ? 'none' :'inline');
            }
        }

    },

    blockDrow: function(data)
    {
        var block_id = data.block.block_id;
        this.blocks_data[block_id] = data;

        try {
            $(_(this.name + '_block_title_' + block_id)).text(data.block.block_title);
        } catch (e) {}

        try {
            $(_(this.name + '_block_title_' + block_id + '_bottom')).text(data.block.block_title);
        } catch (e) {}

        try {
            for (var i = 1; i <= 10; i++) {
                var line = $(_(this.name + '_block_line_' + i + '_' + block_id))
                    .find('a')
                        .text(data.lines[i - 1].block_content_line_title)
                        .attr('href', data.lines[i - 1].block_content_line_link)
                        .end();
                if (data.lines[i - 1].block_content_line_title != '') {
                    line.find('span').show();
                } else {
                    line.find('span').hide();
                }

                if (data.block.block_content_type == 'LINKS' && data.lines[i - 1].block_content_favicon_link != '') {
                    line.find('span').css({'background-image': 'url(' + data.lines[i - 1].block_content_favicon_link + ')'});
                } else if (data.block.block_content_type == 'RSS') {
                    line.find('span').css({'background-image': 'url(resources/img/icon_rss.png)'});
                } else {
                    line.find('span').css({'background-image': 'none'});
                }
            }
        } catch(e) {
        }

    },

    requestCallback: function(result, request_params, exception)
    {
        if (request_params.rm == 'add_block') {

            // find pattern
            var $pattern_block = $(_(this.name + '_user_block_pattern'));

            for (var i = 0; i < result.new_blocks.length; i++) {
                var cd = result.new_blocks[i];
                var block_id = cd.block.block_id;


                var pattern_clone = $pattern_block
                    .clone()
                    .removeClass('display_none')
                    .attr('id', this.name + '_user_block_' + block_id)

                    .find('#' + this.name + '_block_title_pattern').attr('id', this.name + '_block_title_' + block_id).end()
                    .find('#' + this.name + '_block_title_pattern_bottom').attr('id', this.name + '_block_title_' + block_id + '_bottom').end()
                    .find('#' + this.name + '_edit_button_pattern').attr('id', this.name + '_edit_button_' + block_id).end()
                    .find('#' + this.name + '_remove_button_pattern').attr('id', this.name + '_remove_button_' + block_id).end()

                for (var j = 1; j <= 10; j++) {
                    pattern_clone
                        .find("#" +  this.name + '_block_line_' + j + '_pattern').attr('id', this.name + '_block_line_' + j + '_' + block_id).end()
                }
                pattern_clone
                    .insertBefore(_(this.name + '_main_boxes_container_cleaner'));

                this.blockDrow(cd);

            }
            this.initButtons();
            this.reorderBlocks();

        } else if (request_params.rm == 'remove_block') {
            try {
                $(_(this.name + '_user_block_' + request_params.block_id)).remove();
                delete(this.blocks_data[request_params.block_id]);
                this.reorderBlocks();
            } catch(e) {
            }
        } else {
            this.m = cWindow.requestCallback;
            return this.m(result, request_params);
        }
    }

}

function Window_Startpage(data)
{
    this.pc = Window;
    this.pc(data);

    for (var f in  cWindow_Startpage) {
        this[f] =  cWindow_Startpage[f];
    }

    this.blocks_data = data.blocks_data;

    if (data.mode == 'EDIT') {
        this.initButtons();
    }
}
