Ext.BLANK_IMAGE_URL = 'extjs/resources/images/default/s.gif';// reference local blank image
Ext.namespace('Tasks', 'Tasks.app');


function tooltipInit(){  // tooltips instantiate
    var tooltips=Ext.getBody().query(".EXTtooltip");
    Ext.each(tooltips,function(o){
        var tar=Ext.get(Ext.id(o))
        new Ext.ToolTip ({ html:o.title, title:o.name, frame:true, target:tar, trackMouse:true, mouseOffset:[-40,18],
				hideDelay: 0, dismissDelay:0, showDelay:200
			});
        o.title='';
    },this);
}




function checkLogin(){ Ext.Ajax.request({ url:'requests/login.php', params:{type:'check'}, callback:function(opt,suc,resp){if(suc)logOn(resp)} })	}
function checkRestricted(){
	var rp=['funds2']; // restricted pages
	var gets=document.location.search.substr(1).split('&'), get, restricted=false, i, j;
	for (i=0; i<gets.length; i++){ get=gets[i].split('='); if (get[0]=='p') for (j=0; j<rp.length; j++) if (get[1]==rp[j]) { restricted=true; break; }	}
	if (restricted) history.go(0);
}

function login(){ Ext.Ajax.request({ url:'requests/login.php', callback:function(opt,suc,resp){if(suc)logOn(resp,true)}, params:{ type:'bar', login:Ext.get('logbar_login').getValue(), password:Ext.get('logbar_pass').getValue()} }); }
function logout(){ Ext.Ajax.request({ url:'requests/logout.php', callback:function(opt,suc,resp){if(suc)logOn(0,true)}, params:{ type:'bar'} }); }

function logOn(resp,anim){ // handles logging in and loginbar animations
	var text='', btntext='', cls='', res;
	try {	res=resp.responseText.split(':'); } catch(e){ res=false }
	if (res!=false){
		if (res[0]=='error'){ text=res[1]; btntext='RETRY'; cls='form-logout form-logout-error'; } 
		else { text='Hello '+res[1]+'!'; btntext='LOGOUT'; cls='form-logout'; }

		Ext.getCmp('logoutForm').show(); 
			var form=document.getElementById('form_logout');
			form.innerHTML='<b>'+text+'</b> <button onclick="logout()">'+btntext+'</button>';	
			form.className=cls;
		if (anim==true){
			Ext.get('loginBar').shift({ y: -30, duration: .2 });
			Ext.get('loginForm').fadeOut({ duration: .5, useDisplay:true });
			Ext.get('loginBar').shift({ y: 0, duration: .5 });
			Ext.get('logoutForm').fadeIn({ duration: .2 }); 
			setTimeout(checkRestricted,500); // in this block, to run it only on login/out button click
		}
		else { Ext.get('loginForm').dom.style.display='none'; Ext.get('logoutForm').dom.style.display=''; }
	}
	else{
		Ext.getCmp('loginForm').show(); 
		if (anim==true){
			Ext.get('loginBar').shift({ y: -30, duration: .2 });	
			Ext.get('logoutForm').fadeOut({ duration: .5, useDisplay:true });
			Ext.get('loginForm').fadeIn({ duration: .2 });	
			Ext.get('loginBar').shift({ y: 0, duration: .5 });
			setTimeout(checkRestricted,500); // in this block, to run it only on login/out button click
		}
	}
}



function loginWindow(lnk,fundID){ // handles login window
	Ext.Ajax.request({ url:'requests/login.php', params:{type:'check', fundID:fundID}, callback:function(opt,suc,resp){ if(suc){
		var showin=0;
		try {	res=resp.responseText.split(':'); if (res[0]!='ok') showin=1; if (res[2]=='false') showin=2; } catch(e){ showin=1 }
		if (showin==2) Ext.Msg.show({ title:'Warning', msg: 'You currently don\'t have access to this section. If you would like further information about this fund, please <a href="index.php?p=contact" class="defaultLink">contact Alpro Funds</a>.', width:350, buttons: Ext.Msg.OK, icon: Ext.MessageBox.WARNING });
		if (showin==1) renderLoginWindow(lnk);	
		if (showin==0) location.href=lnk;
	}}});
}





//*** render and show ***********************************************************************

function loginBar(){ // renders loginbar and checks if the user is logged in
	Ext.app.LoginPanel=Ext.extend(Ext.Panel, { frame:false, hidden:true, border:false, layout:'table', width: 336, height:30, bodyStyle:'background:url(img/bg_login_panel.gif) center top no-repeat; position:relative; padding-left:30px; padding-top:4px;' });
	var logoutForm=new Ext.app.LoginPanel({ id:'logoutForm', html:'<div class="form-logout" id="form_logout"><b>Hello!</b> <button onclick="logout()">LOGOUT</button></div>' }); 
	var loginForm=new Ext.app.LoginPanel({ id:'loginForm', html:'<div class="form-login">'+'<form><div><input type="text" id="logbar_login" class="login" onfocus="this.className=\'clr\'; this.select()" onblur="if (this.value==\'\') this.className=\'login\'"/></div><div class="separator">&nbsp;</div>'+'<div><input type="password" id="logbar_pass" class="pass" onfocus="this.className=\'clr\'; this.select()" onblur="if (this.value==\'\') this.className=\'pass\'"/></div>'+'<div class="separator">&nbsp;</div><button onclick="login()">LOGIN</button></form></div>' });
	var loginBar = new Ext.Panel({ frame:false, border:false, renderTo:'login_panel', width: 336, height:30, id:'loginBar', layout:'fit', bodyStyle:'background:transparent',	items:[loginForm, logoutForm] });
	checkLogin();
}

function formToggle(no,noanim){	//1 = login; 2 = forgot pass
	if (no==1){
		Ext.getCmp('loginFormWin').getForm().reset();
		if (!noanim) {
			Ext.get('loginFormWin').fadeOut({ duration:.3, useDisplay: true });
			setTimeout(function(){ Ext.getCmp('loginWindow').setHeight(120); Ext.getCmp('btnLogIn').setText('Send'); },400);
			setTimeout(function(){ Ext.getCmp('forgotForm').show(); Ext.get('forgotForm').fadeIn({ duration:.4, useDisplay: true}); },500);
		}
		else {
			Ext.getCmp('loginWindow').setHeight(120); Ext.getCmp('btnLogIn').setText('Send');
			Ext.getCmp('loginFormWin').hide(); Ext.getCmp('forgotForm').show(); Ext.get('forgotForm').dom.style.display=''; 
		}
		setTimeout(function(){ Ext.getCmp('email').focus(true); }, 500);
	}
	if (no==2){
		Ext.getCmp('forgotForm').getForm().reset();
		if (!noanim){
			Ext.get('forgotForm').fadeOut({ duration:.5, useDisplay: true }); 
			setTimeout(function(){ 
				Ext.getCmp('loginWindow').setHeight(175);	Ext.getCmp('btnLogIn').setText('Log In');
				Ext.getCmp('loginFormWin').show(); Ext.get('loginFormWin').fadeIn({ duration:.5, useDisplay: true }); 
			},500);
		}
		else {
			Ext.getCmp('loginWindow').setHeight(175);	Ext.getCmp('btnLogIn').setText('Log In');
			Ext.getCmp('forgotForm').hide(); Ext.getCmp('loginFormWin').show(); Ext.get('loginFormWin').dom.style.display=''; 
		}
		setTimeout(function(){ Ext.getCmp('login').focus(true); }, 500);
	}
}

function renderLoginWindow(lnk){ // shows login window and renders it if necesarry
	if (Ext.getCmp('loginWindow')) Ext.getCmp('loginWindow').show();
	else {
		var loginFormWin = new Ext.FormPanel({ id:'loginFormWin', frame:false, bodyBorder:false, border:false, bodyStyle:'padding:5px 0 0 15px; background:transparent', labelWidth: 65, width: 300, defaultType: 'textfield', waitMsgTarget: true, 
			url:'requests/login.php', defaults:{ width: 180, msgTarget:'side' }, 
			items: [ 
				{ fieldLabel: 'Login', name: 'login', id:'login', allowBlank:false, validateOnBlur:false }, 
				{ fieldLabel: 'Password', name: 'password', id: 'password', allowBlank:false, inputType: 'password', 
					listeners:{ specialkey:function(f,e){ if(e.getKey()==13) loginFormSubmit(lnk); }  }  }, 
				{ labelSeparator:'', name: 'rememberme', xtype:'checkbox', boxLabel:'Remember Me' }, 
				{ xtype:'box', autoEl:{ tag:'div', html:'<a href="#" onclick="formToggle(1)" style="float:left; white-space:nowrap; margin-left:70px; font-weight:bold; font-size:9pt; color:#15428B;">Forgotten your password &raquo;</a>' }}
			]
		});
		var forgotForm = new Ext.FormPanel({ id:'forgotForm', frame:false, bodyBorder:false, border:false, bodyStyle:'padding:5px 0 0 15px; background:transparent', labelWidth: 65, width: 300, defaultType: 'textfield', waitMsgTarget: true, 
			url:'requests/forgot.php', defaults:{ width: 180, msgTarget:'side' }, 
			items: [ 
				{ fieldLabel: 'E-mail', name: 'email', id:'email', allowBlank:false, vtype:'email', validateOnBlur:false, listeners:{ specialkey:function(f,e){ if(e.getKey()==13) forgotFormSubmit(); } }   }, 
				{ xtype:'box', autoEl:{ tag:'div', html:'<a href="#" onclick="formToggle(2)" style="float:left; white-space:nowrap; margin-left:70px; font-weight:bold; font-size:9pt; color:#15428B;">&laquo; Back</a>' }}
			]
		});
		var login_win = new Ext.Window({ id:'loginWindow', width: 300, height:175, layout: 'fit', modal:true, resizable:false, plain:true, buttonAlign:'center',	title: 'Login', closable: false, 
			items: [loginFormWin, forgotForm],
			buttons: [ 
				{ text: 'Log In', id:'btnLogIn', handler:function(){ if (this.text=='Log In') loginFormSubmit(lnk); else forgotFormSubmit(); } }, 
				{ text: 'Cancel', handler:function(){ login_win.hide(); }}
			]
		});
		loginFormWin.on('actioncomplete', function(){ checkLogin(); });
		login_win.show();
		Ext.getCmp('forgotForm').hide();
	}
	formToggle(2,true);
}


function loginFormSubmit(lnk){ Ext.getCmp('loginFormWin').getForm().submit({ waitMsg:'Logging...', params:{type:'win'}, success: function(){ Ext.getCmp('loginWindow').hide(); location.href=lnk; } });}
function forgotFormSubmit(){ Ext.getCmp('forgotForm').getForm().submit({ waitMsg:'Sending...', success: function(){ Ext.getCmp('loginWindow').hide(); infobar('Your password has been send on a given email address.'); }});}


function infobar(text,delay){
	var anim=true, ib=document.getElementById('infobar');	ib.innerHTML=text; delay=!delay?5:delay;
	setTimeout(function(){ if (anim==true) Ext.get('infobar').fadeIn({ useDisplay:true }); else ib.style.display=''; }, 500);
	setTimeout(function(){ if (anim==true) Ext.get('infobar').fadeOut({ useDisplay:true }); else ib.style.display='none'; }, delay*1000);
}

