$(function() {
    $("#loading")
        .ajaxStart(function(){
            $(this).show();
        })
        .ajaxComplete(function(){
            $(this).hide();
        });
    $.jGrowl.defaults.closerTemplate = '<div>закрыть все сообщения</div>';
});

Common = {
    message: function(msg) {
        if (msg.head == undefined) msg.head = '';
        if (msg.type == undefined) msg.type = '';
        $.jGrowl(msg.text, {
            theme: msg.type,
            header: msg.head,
            sticky: true,
            speed: 'slow'
        });
    },
    catalog:  {
        init: function() {
            $("#dialog").dialog({ autoOpen: false, modal: true });
            
            $("#catalog-status-dialog").dialog({
                autoOpen: false,
                height: 80,
                modal: true,
                resizable: false,
                title: 'Изменение статуса',
                buttons: {
                    'Сохранить': function() {
                        $('#catalog-status-dialog').dialog('close');
                        var el = $('#catalog-status-dialog');
                        $.post(
                            webSrvs.catalog.statusUrl,
                            {
                                status:$('select', el).val(),
                                id: el.data('item.id')
                            },
                            function(data){
                                $('#msg-box')
                                    .hide()
                                    .empty()
                                    .removeClass()
                                    .html(data.message);
                                if (data.result < 0) {
                                    $('#msg-box').addClass('alert');
                                } else {
                                    $('span', '#catalog-item-status-'+el.data('item.id')).html(data.status);
                                    $('#msg-box').addClass('msg');
                                }
                                $('#msg-box').show();
                                setTimeout("$('#msg-box').fadeOut(2000)",5000);
                            },
                            "json"
                        );
                    },
                    'Отмена': function() {
                        $(this).dialog('close');
                    }
                },
                close: function() {
                    
                }
            });
            $('.catalog-status-change').click(function() {
                var el = $('#catalog-status-dialog');
                el.data('item.id',$(this).parents('tr').attr('item_id'));
                var id = $(this).attr('status');
                $('select', el).val(id);
                el.dialog('open');
                return false;
            });

            Common.catalog.initphoto();
        },
        initphoto: function(){
            $('.catalog-foto-add').click(function(){
                $("#dialog ol").empty();
                $("#dialog").data('item.id',$(this).attr('id').replace('item-','')).dialog("open");
                return false;
            });
            $("a[rel^='prettyPhoto']", ".gallery").prettyPhoto({theme: 'light_rounded',modal:true});
            $('.catalog-gallery').click(function(){
                var el = $(this).next();
                $('a:first',el).click();
                return false;
            });

            $('#button1').click(function(){return false;});
            new AjaxUpload('#button1', {
                action: webSrvs.catalog.fotoUploadUrl,
                responseType: 'json',
                onSubmit : function(file , ext){
                    this.setData({
                        id: $('#dialog').data('item.id'),
                        format:'json'
                    });

                    $('#button1').text('Загрузка... ');
                    this.disable();
                },
                onComplete : function(file, response){
                    if (response.result < 0) {
                        $.jGrowl(response.message, {
                            theme: 'alert',
                            header: 'Ошибка при загрузке',
                            sticky: true,
                            speed: 'slow'
                        });
                    } else {
                        $('<li></li>').appendTo('#dialog .files').text(file);
                        var id = $('#dialog').data('item.id');
                        var el = $('#gallery-'+id);
                        el.removeClass('hidden');
                        el.next().append('<a href="'+response.path+'" rel="prettyPhoto[gallery'+id+']"></a>');
                        $("a[rel^='prettyPhoto']", el).prettyPhoto({theme: 'light_rounded',modal:true});
                    }
                    this.enable();
                    $('#button1').text('Добавить фотографии');
                }
            });
        }
    },
    loader: '<img src="/i/ajax-loader.gif" alt="">',
    board:  {
        init: function() {
            $("#dialog").dialog({ autoOpen: false, modal: true });
            
            $('.board-foto-add').click(function(){
                $("#dialog ol").empty();
                $("#dialog").data('item.id',$(this).attr('id').replace('item-','')).dialog("open");
                return false;
            });
            $("a[rel^='prettyPhoto']", ".gallery").prettyPhoto({theme: 'light_rounded',modal:true});
            $('.board-gallery').click(function(){
                var el = $(this).next();
                $('a:first',el).click();
                return false;
            });
            
            $('#button1').click(function(){return false;});
            new AjaxUpload('#button1', {
                action: webSrvs.board.fotoUploadUrl,
                responseType: 'json',
                onSubmit : function(file , ext){
                    this.setData({
                        id: $('#dialog').data('item.id'),
                        format:'json'
                    });
                    
                    $('#button1').text('Загрузка... ');
                    this.disable();
                },
                onComplete : function(file, response){
                    if (response.result < 0) {
                        $.jGrowl(response.message, {
                            theme: 'alert',
                            header: 'Ошибка при загрузке',
                            sticky: true,
                            speed: 'slow'
                        });
                    } else {
                        $('<li></li>').appendTo('#dialog .files').text(file);
                        var id = $('#dialog').data('item.id');
                        var el = $('#gallery-'+id);
                        el.removeClass('hidden');
                        el.next().append('<a href="'+response.path+'" rel="prettyPhoto[gallery'+id+']"></a>');
                        $("a[rel^='prettyPhoto']", el).prettyPhoto({theme: 'light_rounded',modal:true});
                    }
                    this.enable();
                    $('#button1').text('Добавить фотографии');
                }
            });
        }
    }
}
