MgppuUser = Ext.extend(Ext.util.Observable, {

	userUrl: "/inc/user.php",
	adminUrl: "/admin/admin.php",
	
	login:"",
	viewLogin:"",
	email:"",
	
	userIn:false,
	
	constructor: function(config){
        
        this.addEvents(
        	'login',
        	'logout'
        );

        MgppuUser.superclass.constructor.call(this, config);
        
    	Ext.Ajax.request({
    		method: 'post',
    		url: this.userUrl,
    		params:{d:'enter'},
    		success: this.userSuccess,
    		failure: this.userFail,
    		scope:this
    	});
        
    },
    
    /**
     * Обработка успешного запроса.
     * 
     * @param res Пришедшие данные.
     */
    userSuccess: function (res){
    	var userData = Ext.util.JSON.decode(res.responseText);
    	if (userData.type=='err'){
    		Ext.Msg.show({
    			title:'Ошибка!',
    			msg: userData.msg,
    			buttons: Ext.Msg.OK,
    			icon: Ext.MessageBox.ERROR
    		});
    	}
    	else{
    		Ext.get('loginform').dom.innerHTML=userData.msg;
    		if (userData.type=="info"){
        		this.login=userData.login;
        		this.viewLogin=userData.viewLogin;
        		this.email=userData.email;
    			Ext.get('loginform').addClass("userInfo");
    			Ext.get('loginform').removeClass("loginForm");
    			this.userIn=true;
    	    	this.fireEvent('login');
    	    	this.checkEmail();
    		}else{
        		this.login="";
        		this.viewLogin="";
        		this.email="";
    			Ext.get('loginform').addClass("loginForm");
    			Ext.get('loginform').removeClass("userInfo");
    			this.userIn=false;
    			this.fireEvent('logout');
    		}
    	}
    	buttonPressedView();
    },
    
    checkEmail:function(){
    	if (!this.email && !Ext.util.Cookies.get('noEmailNotify')){
    		Ext.Msg.show({
       			title:'Не указан Email!',
       			msg: 'У Вас не указан email!<br />Мы не сможем восстановить Ваш пароль, если Вы не укажете email.<br />Вы можете указать свой email и изменить другою информацию на странице <a href="/index.php?s=cabinet">личного кабинета</a>',
       			modal:true,
       			buttons: Ext.Msg.OK,
       			icon: Ext.MessageBox.WARNING
       		});
    		Ext.util.Cookies.set('noEmailNotify',true, new Date().add(Date.DAY, 2));
    	}
    },
    
    /**
     * Обработка ошибки при запросе
     */
    userFail: function(a){
		Ext.Msg.show({
   			title:'Ошибка!',
   			msg: 'Произошла ошибка при связи с сервером',
   			buttons: Ext.Msg.OK,
   			icon: Ext.MessageBox.ERROR
   		});
    },
    /**
     * Запрос авторизации.
     */
    userAuth: function (){
    	Ext.Ajax.request({
    		form: 'toploginform',
    		method: 'post',
    		url: this.userUrl,
    		success: this.userSuccess,
    		failure: this.userFail,
    		scope:this
    	});
    	return false;
    },

    /**
     * Запрос на выход.
     */
    userLogout: function() {
    	if (Ext.getCmp('adminTb')){
    		this.adminPanelOff();
    		for(var i=5000;i>0;i--);
    	}	
    	Ext.Ajax.request({
    		method: 'post',
    		url: this.userUrl,
    		params:{d:'logout'},
    		success: this.userSuccess,
    		failure: this.userFail,
    		scope:this
    	});
    	return false;
    },

    /**
     * Обработка успешного запроса на манипуляцию админ-панелью.
     * 	
     * @param res Пришедшие данные.
     */
    adminSuccess: function(res){
    	var userData = Ext.decode(res.responseText).data;
    	if (userData.type=='err'){
    		Ext.Msg.show({
    			title:'Ошибка!',
    			msg: userData.msg,
    			buttons: Ext.Msg.OK,
    			icon: Ext.MessageBox.ERROR
    		});
    	}
    	else{
    		Ext.get('loginHrefs').dom.innerHTML=userData.msg;
    	}
    },

    /**
     * Обработка успешного запроса на включение админ-панели.
     */
    adminPanelSuccess: function(res){
    	var data=Ext.decode(res.responseText);
    	if (!data.success){
    		this.userFail(data.data);
    		return;
    	}
    	addScript("/admin/js/admin.js");
    	setStyle('adminStyle',"/admin/css/admin.css");
    	this.adminSuccess(res);
    },

    /**
     * Запрос на отключение админ-панели.
     */
    adminPanelOff: function(){
    	Ext.getCmp('adminTb').destroy();
    	Ext.Ajax.request({
    		method: 'post',
    		url: this.adminUrl,
    		params:{d:'panelOff'},
    		success: this.adminSuccess,
    		failure: this.userFail,
    		scope:this
    	});
    	return false;
    },

    /**
     * Запрос на включение админ-панели.
     */
    adminPanelOn: function(){
    	Ext.Ajax.request({
    		method: 'post',
    		url: this.adminUrl,
    		params:{d:'panelOn'},
    		success: this.adminPanelSuccess,
    		failure: this.userFail,
    		scope:this
    	});
    	return false;
    }
});

Ext.onReady(function(){
	Ext.MgppuUser=new MgppuUser();
});
