Ext.namespace('User');

User.cookie = new Ext.state.CookieProvider({
	 path: "/"
	,expires: new Date(new Date().getTime()+(1000*60*60*24*30)) //30 days
});

User.loginFormStore = new Ext.data.JsonStore({
	url: '/register/auth/',
	root: 'result',
	fields: ['name', 'id', 'msg']
});

User.loginForm = new Ext.FormPanel({
	labelWidth: 50,
	bodyStyle: 'padding: 10px;',
	defaults: {width: 120},
	frame: true,
	defaultType: 'textfield',
	monitorValid: true,
	items:[
		{
			fieldLabel: 'Логин',
			id: 'login-form-login-item',
			name: 'login',
			allowBlank: false,
			maxLength: 50
		},
		{
			fieldLabel: 'Пароль',
			name: 'password',
			maxLength: 50,
			allowBlank: false,
			inputType: 'password'
		}
	],
	listeners: {
		'clientvalidation': {
			fn: function(form, valid){
				if(valid)
					Ext.getCmp('login-window-login-btn').enable();
				else
					Ext.getCmp('login-window-login-btn').disable();
			}
		}
	}
});

User.loginWindow = new Ext.Window({
	bodyBorder: false,
	border: false,
	modal: true,
	buttonAlign: 'center',
	layout: 'fit',
	width: 240,
	height: 150,
	closable: false,
	//closeAction: 'hide',
	title: 'Введите ваши логин и пароль',
	plain: true,
	items: [
		User.loginForm
	],
	buttons: [
		{
			text: 'Войти',
			id: 'login-window-login-btn',
			disabled: true,
			handler: function(){
				//this.getEl().frame('0085f5');
				this.ownerCt.getEl().mask('Loading', 'x-mask-loading');
				//User.loginWindow.disable();
				Ext.getCmp('login-window-close-btn').disable();
				var data = [];
				User.loginForm.items.each(function(item, index, length){
					if(!Ext.isEmpty(item.name)){
						data.push({'name': item.name, 'val': item.getValue(), 'id': item.id});
					}
				});
				//alert(Ext.encode(data));
				if(data.length){
					User.loginFormStore.removeAll();
					User.loginFormStore.load({
						params:{data: Ext.encode(data)},
						callback: function(r){
							Ext.getCmp('login-window-close-btn').enable();
							this.ownerCt.getEl().unmask();

							Ext.each(r, function(item, index){
								if(item.data.name != 'register_complete')
									Ext.getCmp(item.data['id']).markInvalid(item.data.msg);
								else {
								}
							});
						},
						scope: this
					});
				}


			}
		},
		{
			text: 'Закрыть',
			id: 'login-window-close-btn',
			handler: function(){
				this.ownerCt.getEl().unmask();
				User.loginWindow.hide();
			}
		}
	],
	listeners: {
		'show': {
			fn: function(){
				Ext.getCmp('login-form-login-item').focus();
			},
			delay: 100
		}
	}
});




/*
User.loginMenuButton = new Ext.Toolbar.MenuButton({
	id: 'personal-bar-login-button',
	icon: '/i/16_icons/key.png',
	cls: 'x-btn-text-icon',
	disabled: true,
	text: 'Войти',
	handler: function(){
		User.loginWindow.show(this.getEl());
	},
	menu : {
		items: [
			{
				icon: '/i/16_icons/application_key.png',
				cls: 'x-btn-text-icon',
				//id: 'register-menu-item',
				text: 'Восстановить пароль'
			}
	]}
});

User.registerButton = {
	id: 'register-menu-item',
	text: 'Зарегистрироваться',
	handler: function(){
		this.disable();
		window.location.href = '/register/';
	}
};
*/


/*
Server.personalBarItems = [
	//(Server.logoutMenuButton?Server.logoutMenuButton:User.loginMenuButton),
	User.registerButton,
	'->'
	//User.searchField, User.searchButton
];
*/

/*
User.personalBar = new Ext.Toolbar({
	id: 'personal-bar',
	renderTo: 'personal-bar-place',
	items: Server.personalBarItems
});
*/

User.taskRunner = new Ext.util.TaskRunner();

User.browserAnnotation = function(){
	Ys.lightMsg.msg(
		 '<div style="text-align: right; padding-right: 30px;">С новым Firefox 3 сайты работают быстрее!</div>'
		,''
			+'<div style="cursor: pointer; display:block; height: 124px; width: 365px; background-image: url(http://www.mozilla-europe.org/img/tignish/firefox/download-button-primary.png); //background: none; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'http://www.mozilla-europe.org/img/tignish/firefox/download-button-primary.png\', sizingMethod=\'crop\');" onclick="window.open(\'http://www.mozilla-europe.org/ru/firefox/\', \'firefox\'); User.ieff(0); User.cookie.set(\'ie-disable-ff-download\', true);">'
				+'<span style="padding:55px 20px 20px 128px; display:block;">'
					+'<strong>Бесплатная загрузка Firefox 3</strong>'
				+'</span>'
			+'</div>'
			+'<div><a href="#" style="color: #a0a0a0; text-decoration: none;" onclick="User.ieff(1); User.cookie.set(\'ie-disable-ff-download\', true); Ext.fly(\'msg-div\').ghost(\'t\', {remove: true}); return false;">Не, мне больше нравится Internet Explorer.</a></div>'
		+''
		,1000
		,'400px'
	);
	//var el = Ext.getBody().insertHtml('beforeEnd', '<div class="annotation">'+alt+'</div>', false);
}

User.ieff = function(ni){
	Ext.Ajax.request({
		 url: '/ieff/?ni='+ni
	});

}


User.applyInfMsg = function(root) {
	var ExtInfMsg = Ext.DomQuery.jsSelect('*[class*=ext-information-message]', root);
	if(!Ext.isEmpty(ExtInfMsg) && ExtInfMsg.length){
		Ext.each(ExtInfMsg, function(item){
			var target = Ext.get(item).child('*[class*=ext-link]');
			var body = Ext.get(item).child('div[class*=ext-message-body]');
			var msgBox = new Ext.ToolTip({
				 anchor: 'top'
				,target: target
				,anchorOffset: -4
				,autoHide: true
				//,title: 'Скидка при регистрации'
				//,cls: 'yellow-hint'
				,contentEl: body
				,dismissDelay: 0
				,padding: '10px 20px'
				,shadowOffset: 6
				//,closable: true
			});
			target.on('click', function(e){
				e.preventDefault();
				msgBox.show();
			}, this);

		});
	}
}

Ext.onReady(function(){

	/*
	Ext.DomHelper.append(Ext.getBody(), {
		 tag: 'div'
		,id: 'loader'
		,html: 'Загрузка...'
	}, true).hide();
	*/

	Ext.Ajax.on('beforerequest',
		function(conn, opt){
			Ext.fly('loader').dom.innerHTML = 'Загрузка...';
			Ext.fly('loader').show();
		}, this
	);
	Ext.Ajax.on('requestcomplete',
		function(){
			Ext.fly('loader').hide();
		}, this
	);





	/*if(Ext.isIE && !User.cookie.get('ie-disable-ff-download', false))
		User.browserAnnotation();*/


	/* window.setTimeout(function(){
		if(User.loginMenuButton.isVisible())
			User.taskRunner.start({
				run: function(){
					Ext.fly('personal-bar-login-button').frame('#f5d436', 1, {duration: 2});
				},
				interval: 30000
			});
	}, 3000); */

	window.setTimeout(function(){
		try {
			Ext.get(loadingConsole).hide();
		}
		catch(e) {}
	}, 500);

	if(Ext.isEmpty(Ys.getCookie('reg-content-hide')) && !Ext.isEmpty(Ext.fly('reg-content'))){
		//Ext.get('reg-link');
		User.regDiscountMsg = new Ext.ToolTip({
			 anchor: 'right'
			,target: 'reg-link'
			,anchorOffset: -4
			,autoHide: false
			//,title: 'Скидка при регистрации'
			//,cls: 'yellow-hint'
			,contentEl: 'reg-content'
			,dismissDelay: 0
			,padding: '10px 20px'
			,shadowOffset: 6
			//,closable: true
		});

		User.regDiscountMsg.show();
	}

	// begin apply ext-information-message
	User.applyInfMsg();
	// end apply ext-information-message

	Ext.QuickTips.init();
});

