Ext.onReady(function(){
	Ext.EventManager.onWindowResize(centerPanel);
	
	var panel = Ext.get("qo-panel");
	var btn = Ext.get("submitBtn");
    btn.on({
      'click': { fn: onClick }
      , 'mouseover': { fn: function(){ btn.addClass('qo-submit-over'); } }
      , 'mouseout': { fn: function(){ btn.removeClass('qo-submit-over'); } }
    });
    
    var demoBtn = Ext.get("demoBtn");
    demoBtn.on({
      'click': { fn: onClick }
      , 'mouseover': { fn: function(){ btn.addClass('qo-submit-over'); } }
      , 'mouseout': { fn: function(){ btn.removeClass('qo-submit-over'); } }
    });
	
	Ext.get("field3-label").setDisplayed('none');
	Ext.get("field3").setDisplayed('none');

	centerPanel();
	
	function centerPanel(){
		var xy = panel.getAlignToXY(document, 'c-c');
		positionPanel(panel, xy[0], xy[1]);
	}
	
	function positionPanel(el, x, y){
      if(x && typeof x[1] == 'number'){
         y = x[1];
         x = x[0];
      }
      el.pageX = x;
      el.pageY = y;
         
      if(x === undefined || y === undefined){ // cannot translate undefined points
         return null;
      }
        
      if(y < 0){ y = 10; }
        
      var p = el.translatePoints(x, y);
      el.setLocation(p.left, p.top);
      return el;
   }
	
	function hideLoginFields(){
		Ext.get("field1-label").setDisplayed('none');
		Ext.get("field1").setDisplayed('none');
		Ext.get("field2-label").setDisplayed('none');
		Ext.get("field2").setDisplayed('none');
	}
	
	function loadGroupField(d){
		var combo = Ext.get("field3");
		var comboEl = combo.dom;
		
		while(comboEl.options.length){
			comboEl.options[0] = null;
		}
		
		for(var i = 0, len = d.length; i < len; i++){
			comboEl.options[i] = new Option(d[i][1], d[i][0]);
		}
	}
	
	function onClick(e, btn){	
		var guest = false;
		
		if(btn.id == "demoBtn"){
			guest = true;
		}
		
		if(!guest){
			var emailField = Ext.get("field1");
			var email = emailField.dom.value;
			var pwdField = Ext.get("field2");
			var pwd = pwdField.dom.value;
			var groupField = Ext.get("field3");
			var group = groupField.dom.value;
			
			if(validate(email) === false){
				alert("Your email address is required");
				return false;
			}
			
			if(validate(pwd) === false){
				alert("Your password is required");
				return false;
			}
			
		}
		
		panel.mask('Please wait...', 'x-mask-loading');
	
		//var key = "L0ck it up saf3";
		//pwd = Ext.ux.Crypto.AES.encrypt(pwd, key, 128);
		
		Ext.Ajax.request({
			url: 'desktop/services.php'
			, params: {
				service: 'login'
				, user: email
				, pass: pwd
				, group: group
				, guest: guest
			}
			, success: function(o){
				panel.unmask();
				
				if(typeof o == 'object'){
					var d = Ext.decode(o.responseText);
					
					if(typeof d == 'object'){
						if(d.success == true){
							var g = d.groups;
							
							if(g && g.length > 0){	
								hideLoginFields();
								showGroupField();
						
								var d = [];
								for(var i = 0, len = g.length; i < len; i++){
									d.push([g[i].id, g[i].name]);
								}
								
								loadGroupField(d);
								
							}else if(d.sessionId !== ""){
								
								panel.mask('Redirecting...', 'x-mask-loading');
								
								// get the path
								var path = window.location.pathname;
								path = path.substring(0, path.lastIndexOf('/') + 1);
									
								// set the cookie
								set_cookie('sessionId', d.sessionId, '', path, '', '' );
								
								// redirect the window
								window.location = path + 'desktop/';
								
							}
						}else{
							if(d.errors && d.errors[0].msg){
								alert(d.errors[0].msg);
							}else{
								alert('Errors encountered on the server.');
							}
						}
					}
				}
			}
			, failure: function(){
				panel.unmask();
				alert('Lost connection to server.');
			}
		});
	}
    
   function showGroupField(){
      Ext.get("field3-label").setDisplayed(true);
      Ext.get("field3").setDisplayed(true);
   }
    
    function validate(field){
		if(field === ""){
			return false;
		}
		return true;
	}
});