var ModalWindow;
var ModalWindowMask;
function goajax(toEl,queryString,opt)
{
    var callback;
    defaultURL=DOMAIN+"submit.php?";
    tourl=defaultURL+queryString+"&rrr="+Math.random();

    if(!opt){
        opt = new Object;
    }
    if(opt['callback']){
        callback = opt['callback'];
    }
    Ext.get(toEl).load({
        url: defaultURL,
        scripts: true,
        params: queryString,
        text: "Loading...",
        callback:callback
    });
}

function AddToAjaxHistory(type,obj)
{
    if(isNaN(GLOBALS.historyId) || typeof(GLOBALS.historyId)=='undefined'){
        GLOBALS.historyId = 0;
    }else{
        GLOBALS.historyId++;
    }

    // check in already in stack
    if(!isObject(GLOBALS.historyAlreadyInStack)){
        GLOBALS.historyAlreadyInStack = new Object();
    }

    for(var n in GLOBALS.historyAlreadyInStack)
    {
        if(objectEqual(GLOBALS.historyAlreadyInStack[n],obj)){
            return ;
        }
    }
    GLOBALS.historyAlreadyInStack[type+':'+GLOBALS.historyId] = obj;


    if(!isObject(GLOBALS.historyStack)){
        GLOBALS.historyStack = new Object();
    }
    if(!isObject(GLOBALS.historyStack[type])){
        GLOBALS.historyStack[type] = new Object();
    }
    var tmp = GLOBALS.historyStack[type];
    tmp[GLOBALS.historyId] = obj;
    GLOBALS.historyStack[type] = tmp;


    GLOBALS.historyNowActive = GLOBALS.historyId;
    Ext.History.add(type+':'+GLOBALS.historyId);
}
//-------------------pl()--------------------
function pl(id,toEl,opt)
{
    
    if(typeof(opt)!='object'){
        opt = new Object();
    }

   if(CONFIG.isVK){opt.history = false;}
   // add to history
   if(opt.history)
    {
        AddToAjaxHistory('pl',{
            url:id,
            toEl:toEl
        });
    }

    if(window.location.search || CONFIG.query_string)
    {
        //var was=window.location.search.substring(1,window.location.search.length)+"&";
        var was = CONFIG.query_string+"&";
    }else
    {
        var was = '';
    }
    var defaultURL = DOMAIN+"ajax.php?";

    // clear datas
    if(toEl=="data1" && document.getElementById("data2"))
    {
        document.getElementById("data2").innerHTML="";
        if(document.getElementById("data3")){
            document.getElementById("data3").innerHTML="";
        }
    }
    if(toEl=="data2" && document.getElementById("data3")){
        document.getElementById("data3").innerHTML="";
    }

    var tourl = defaultURL+was+id;

    // if load in popup
    if(opt.target=="popup")
    {
        ModalWindow = new Ext.Window({
            title    : opt.popupTitle,
            autoLoad : {
                url:tourl+'&reloadModeOff=1',
                scripts: true,
                callback: popupLoadCb
            },
            resizable : false,
            //collapsible : true,
            autoScroll :true,
            width    : 640,
            autoHeight: true,
            modal: true
        });

        if(isObject(opt.popupSettings))
        {
            for(var i in opt.popupSettings)
            {
                ModalWindow[i] = opt.popupSettings[i];
            }
        }

        //Ext.get('body').mask('hh');
        ModalWindowMask = new Ext.LoadMask(Ext.getBody(), {
            msg: "Загружаем.."
        });
        ModalWindowMask.show();
        ModalWindow.setVisible(false);
        ModalWindow.show();
        ModalWindow.setVisible(false);

        return ;
    }

if(!IE){
        Ext.get(toEl).setOpacity(0.2,true);
        Ext.get(toEl).load
        ({
            url: tourl,
            method: 'GET',
            scripts: true,
            text: gd(toEl) ,
            disableCaching:true,
            nocache:true,
            callback: function()
            {
                Ext.get(toEl).setOpacity(1,true);
            }
        });
    }else{
        Ext.get(toEl).load
        ({
            url: tourl,
            scripts: true,
            nocache:true,
            disableCaching:true,
            //params: variable,
            text: "<div class=\"loading\"><div class=\"loadingTxt\">Loading...</div>"+gd(toEl)+"</div>"
        });
    }

}


Ext.onReady(function() {
    Ext.History.init();
    Ext.History.on('change', function(token){
        if(token){
            var parts = token.split(':');
            var type = parts[0];
            var historyId = parts[1];
            if(GLOBALS.historyNowActive==historyId){
                return ;
            }
            switch(type)
            {
                case 'pl':
                    if(!isObject(GLOBALS.historyStack.pl[historyId])){
                        break ;
                    }
                    pl(GLOBALS.historyStack.pl[historyId].url,GLOBALS.historyStack.pl[historyId].toEl);
                    break ;
                default:
                    return ;
            }
        }else{
            history.go(-1);
        // This is the initial default state.  Necessary if you navigate starting from the
        // page without any existing history token params and go back to the start state.
        }
    });
});


function popupLoadCb()
{
    if(ModalWindow.getInnerHeight()>480)
    {
        ModalWindow['autoHeight'] = false;
        //ModalWindow.setAutoHeight(false);
        ModalWindow['height'] = 480;
        ModalWindow.setHeight(480);
    //ModalWindow.doLayout();
    }

    ModalWindow.syncSize();
    ModalWindow.center();
    ModalWindow.show({
        duration:10
    })
    ModalWindowMask.hide();
}