jQuery.iMax = {
    allowerrors: false,  
    
    serializePHP: function(obj)
    {        
        if (obj == null) return null;
        
        switch (typeof(obj))
        {
            case 'number':
                            return 'd:'+obj;
                            
            case 'string':
                            return obj === '' ? 's:4:"null"' : 's:'+obj.length+':"'+obj+'"';
                            
            case 'object':
                            var s = '', k = 0, i, ss = '';
                            for (i in obj)                            
                                if (i != 'indexOf')
                                {
                                    k++;
                                    ss = (ss = this.serializePHP(obj[i])) ? ss : 'N';                                    
                                    ss = ''+ss;
                                    s += this.serializePHP(i)+';'+ss;
                                    if (!(ss.charAt(ss.length-1) == '}')) s += ';';
                                }
                                                          
                            return 'a:'+k+':{'+s+'}';
        
            default:
                        s = ''+obj; 
                        return 's:'+s.length+':"'+s+'"';
        }   
    },
        

    //Любой запрос AJAX
    //mode - че хотим
    //pars - параметры
    req : function(mode, pars, onSuccess, SuccVars, SuccFunc)
    {
        function ajaxsucc(data)
        {
            removeShade();
            if (onSuccess != null) onSuccess(data, SuccVars);
            if (SuccFunc != null) SuccFunc(data, SuccVars);
        }
        
        pars['ajax_mode'] = mode;        
        $.post('?', pars, ajaxsucc);
        
        $('body').ajaxError(
                                function(request, settings)
                                {
                                    if ($.iMax.allowerrors && !(settings.statusText == 'OK')) return;
                                    $.iMax.allowerrors = true;
                                    
                                    if (!settings.statusText && !settings.responseText) return;
                                    
                                    alert('Произошла ошибка выполнения ajax запроса:\n\n'+
                                            settings.statusText+':\n'+settings.responseText);
                                    shade = 0;
                                    $('#shadeInner').remove();
                                    $('#shadeDocument').remove();
                                    
                                    setTimeout('$.iMax.allowerrors = false;', 100);
                                }
                            );
        if (cpanel) shadeDocument();
    },

    //Запрос с обработкой самим модулем
    //link_id - идентификатор связи на модуль
    //input - переменные для передачи как input
    //data - переменные для передачи
    toItemReq : function(link_id, input, data, onSuccess, SuccVars, SuccFunc)
    {
        var pars = {};
        pars['ajax_link_id'] = link_id;
        pars['ajax_input'] = this.serializePHP(input);                
        pars['ajax_data'] = this.serializePHP(data);        
        this.req(1, pars, onSuccess, SuccVars, SuccFunc);
    },    
        
    
    //Запрос на получение построенного модуля
    //block_link_id - идентификатор связи на модуль
    GetItemReq : function(block_link_id, onSuccess, SuccVars, SuccFunc)
    {
        this.req(2, {'block_link_id': block_link_id}, onSuccess, SuccVars, SuccFunc);
    },
    
    //Запрос на получение спецификации модуля
    //block_link_id - идентификатор связи на модуль
    GetItemSpecReq : function(block_link_id, onSuccess, SuccVars, SuccFunc)
    {
        this.req(3,  {'block_link_id': block_link_id}, onSuccess, SuccVars, SuccFunc);
    },
    
    //Запросы для редактирования страниц
    //action - че хотим (разбор в Ajax::EditPages)
    //pars - параметры
    EPreq : function(action, pars, onSuccess, SuccVars, SuccFunc)
    {
        pars['action'] = action;
        this.req(10,  pars, onSuccess, SuccVars, SuccFunc);
    },
    
    //Запросы для установки переменных
    //action - че хотим (разбор в Ajax::SetVars)
    //pars - параметры
    VarReq : function(this_block_id, action, pars, onSuccess, SuccVars, SuccFunc)
    {
        pars['action'] = action;
        this.req(11,  pars, onSuccess, SuccVars, SuccFunc);
    },
    
    //Запросы для редактирования menu
    //action - че хотим (разбор в Ajax::Menu)
    //pars - параметры
    Mreq : function(action, pars, onSuccess, SuccVars, SuccFunc)
    {
        pars['action'] = action;
        this.req(12,  pars, onSuccess, SuccVars, SuccFunc);
    },       
   
   
    //Использование Sortable для модулей   
    //Коллекция Sortable блоков используемые модулями
    itemsSortCollection: new Object(),
    addItemSort: function(id) {
        if (this.itemsSortCollection[id] == undefined)
            this.itemsSortCollection[id] = true;    
    },   
   
   
   
   
    //Загрузка внешних файлов      
    _extFiles: new Object(),
   
    /*
        Load css files
        param css_list
            strig - delimeter separated css files  or
            array - array of css files
    */
    loadCSS : function(c) {
        var t = this;
        if (!c) return;
        if ( t.is(c, 'array') ) {
            for (var i = 0; i < c.length; i++) {
                t.loadCSS(c)
            }
        }
        else {
            if (!t._extFiles[c]) 
                t._extFiles[c] = true; 
            else 
                return;            
            var node = document.createElement('link');
            $(node).attr({rel: 'stylesheet', type: 'text/css', href: c});
            $('head').append(node);
        }
    }, 
    
        /*
    loadScript : function(s_list) {
        if (!s_list) return;
        if ( this.is(s_list, 'array') ) {
            var head = $('head');
            for (var i = 0; i < s_list.length; i++)
                if (!this._extFiles[s_list[i]]) {
                    this._extFiles[s_list[i]] = true;
                    head.append( '<'+'script type="text/javascript" src="'+s_list[i]+'" ></'+'script'+'>' );
                }
        }
        else {
            var t = this;
            var head = $('head');
            jQuery.each(s_list.split(','), function(i, f) {
                if (t._extFiles[f]) return;
                t._extFiles[f] = true;
                head.append( '<'+'script type="text/javascript" src="'+f+'" ></'+'script'+'>' );
            });    
        
        }
    		            
    },            */
    
      
    /*
        check type object
        param object obj - object
        param string type - type    
    */
    is : function(obj, type) {
        var t = typeof(obj);
        if (!type) return t != 'undefined';
        if (type == 'array' && (obj instanceof Array)) return true;
        return t == type;
    }      
                   
	   
		       
};