FbLoginPanel = new (Class.create({
	
	bCountHide: false,
	timer: 0,
	iTimeout: 2,   //number of seconds till the select box will hide
	lastMovedListItem : null,
	beforeMoveListItem : null,
	
    initialize : function () {
    	Event.observe(document, 'dom:loaded', this.onLoad.bind(this));
	},
	
	reset: function(){
		this.bCountHide=false;
		this.timer=0;
	},

	onLoad : function () {
		this.fillDefault()
		var t=this;
		var fixedValues=[];

		var fieldLoginValue=$('formLogin').getValue();
		if(fieldLoginValue.empty()){
			var loginmsg=$('LoginMsg').innerHTML;
			var passwdmsg=$('PasswdMsg').innerHTML;
			$('formLogin').setValue(loginmsg.replace(":", ""));
			$('formPasswd').setValue(passwdmsg.replace(":", ""));
		}
		
		$('formLogin', 'formPasswd').each(function(item) {
			
			item.observe('click', function(){
				fixedValues[item.identify()]=item.getValue();
				t.focusOnPassword(item);
			});
			
			item.observe('focus', function() {
				fixedValues[item.identify()]=item.getValue();
				t.focusOnPassword(item);
			});
			
			item.observe('blur', function(event){
				this.removeClassName('text_focused');
				if(!item.present()){
					Form.Element.setValue(item,fixedValues[item.identify()]);
				}
			   });
			
		});

		$$('.inputline .serverbtnwrap').each(function(item) {
			
			item.observe('click', function(event){
				$$('.inputline .serverlistwrap').each(function(inneritem) {
					t.reset();
					inneritem.setStyle({display: 'block'});
				});
			   });
		   });
		
    	Event.observe(document.body, 'mousemove', function(event) {  
			if(Event.element(event).up('.inputline.server')==undefined){
				$$('.inputline .serverlistwrap').each(function(inneritem) {
					t.bCountHide=true;
						if(t.timer>t.iTimeout){
							t.timer=0;
						}
				});
			}else{
				t.bCountHide=false;
			}    		  
		});
    	
    	Event.observe(document.body, 'click', function(event) {  
			if(Event.element(event).up('.inputline.server')==undefined){
				t.reset();
				$$('.inputline .serverlistwrap').each(function(inneritem) {
					inneritem.setStyle({display: 'none'});
				});
			}    		  
		});
    	
    	var t = this;
    	$$('.inputline ul li a').each(function(item) {
			item.observe('click', function(event){
				t.changeAndClose(Event.element(event), true);
			   });
		   });

    	this.doHideSelect();
    },
    
    doHideSelect: function(){
    	var t = this;   	
		if(t.bCountHide==true && t.timer==t.iTimeout){
			t.reset();
			$$('.inputline .serverlistwrap').each(function(inneritem) {
				inneritem.setStyle({display: 'none'});
			});
		}
		
		t.timer+=1;
    	setTimeout(function(){
			t.doHideSelect();
		}, 1000);    	
    },
    
    fillDefault : function(){
    	var storedWorld=Cookie.getData('worldId');

    	if(storedWorld==undefined || $$('.inputline ul li.'+storedWorld).size()!=1 || storedWorld==null){
	    	var firstLi=$$('.inputline ul li').first();
    	}else{
    		var firstLi=$('list'+storedWorld);
    	}
	
	this.changeAndClose(firstLi.down());
    },

   focusOnPassword : function(item) {
    	item.addClassName('text_focused');
		Form.Element.setValue(item,'');
   },
    
   changeAndClose : function(element){
    	var t=this;
    	$$('.inputline .list').invoke('update',$(element).innerHTML); 
    	var className=$(element).up().className;
    	Form.Element.setValue("login-world",className.replace(" text_focused", ""));
    	
    	Cookie.setData('worldId', className.replace(" text_focused", ""), 9999);
    	
	    	$$('.inputline .serverlistwrap').each(function(inneritem) {	    		
	    			t.reset();	
	    			inneritem.setStyle({display: 'none'});
			});
	if (this.beforeMoveListItem) {
		$(this.beforeMoveListItem).insert({after: this.lastMovedListItem});
	}
	this.lastMovedListItem = $(element).up();
	this.beforeMoveListItem = $(element).up().previous();
	var firstLi=$$('.inputline ul');
	firstLi.invoke('insert',{top: this.lastMovedListItem});
    }
  
}))();
