Tauri.WorldsField = Class.create({
	
	box : null,
	list: null,
	current: null,
	field: null,
	currentUrl: null,
	loaded : false,
	reload : false,
	
	listBox: null,
	currentText: null,
	
	initialize : function (opts) {
		var t = this;
		$H(opts).each(function (p) {
			t[p.key] = p.value;
		});
        Event.observe(window, 'dom:loaded', this.init.bind(this));
        Event.observe(window, 'load', this.init.bind(this));
	},
	
	init : function () {
		if (this.loaded)
			return;
		this.loaded = true;
		var t = this, ul, selected = false;
		this.box = $(this.box);
		this.field = $(this.field);
		this.currentText = (new Element('span'));
		this.box.insert(this.currentText);
		if (!this.list.length) {
			this.currentText.update('---');
			return;
		}
		if (this.list.length > 1) {
			this.listBox = (new Element('div')).hide();
			if ((navigator.userAgent + '').toLowerCase().indexOf('iphone') > -1) {
				this.listBox.observe('click', function (ev) {
					Event.stop(ev);
					t.listBox.hide();
					return false;
				});
				this.box.observe('click', function (ev) {
					Event.stop(ev);
					t.listBox.show();
					return false;
				});
			} else {
				this.listBox.observe('mouseover', function (ev) {
					Event.stop(ev);
					t.listBox.show();
					return false;
				}).observe('mouseout', function (ev) {
					Event.stop(ev);
					t.listBox.hide();
					return false;
				})
				this.box.observe('mouseover', function (ev) {
					Event.stop(ev);
					t.listBox.show();
					return false;
				}).observe('mouseout', function (ev) {
					Event.stop(ev);
					t.listBox.hide();
					return false;
				});
			}
			$('pagecontainer').insert(this.listBox);
			var boxPos = this.box.cumulativeOffset();
			var diffPos = $('pagecontainer').cumulativeOffset();
			boxPos.left -= diffPos.left; boxPos.top -= diffPos.top; 
			this.listBox.addClassName(this.box.identify() + '-list');
			this.listBox.setStyle({left: boxPos.left+'px', top: boxPos.top+'px'});
			ul = new Element('ul');
			this.listBox.insert(ul);
			ul.insert((new Element('li')).addClassName('top'));
			this.list.each(function (item) {
				ul.insert(
					(new Element('li')).
						insert(item[1]).
						observe('click', function (ev) {
							Event.stop(ev);
							t.listBox.hide();
							t.select(item);
							return false;
						})
				);
				if (item[0] == t.current) {
					t.select(item);
					selected = true;
				}
			});
			ul.insert((new Element('li')).addClassName('bottom'));
		}
		if (!selected) {
			this.select(this.list[0]);
		}
		
	},
	
	select : function (item) {
		this.field.value = item[0];
		this.currentText.update(item[1]);
		this.currentUrl = item[2];
		if (item[3]){
			if ($('testServerNote')) {
				$('testServerNote').show();
			}
		} else {
			if ($('testServerNote')) {
				$('testServerNote').hide();
			}
		}
		if (this.reload) {
			TAPI.Forms.reload(this.currentUrl, this.field.value);
		}
		this.listBox.hide();
	},
	
	getSelectedUrl : function (){
		return this.currentUrl;
	}
	
	
});

Tauri.Worlds = Class.create({
	
	worlds : null,
	fields : null,
	worldsHash : null,

	initialize : function () {
		this.worlds = $A();
		this.fields = $H();
        this.worldsHash = $H();
	},

	addWorld : function (id, name, url, isTest, directActivation, regSort) {
		this.worlds.push([id, name, url, isTest]);
        this.worldsHash.set(id, $H({id : id, name : name, url : url, isTest : isTest, directActivation : directActivation, regSort: regSort}));
	},

	addField : function (field, outField, selected, doReload) {
		this.fields.set(field, new Tauri.WorldsField({
			box: field,
			list: this.worlds,
			current: selected,
			field: outField,
			reload: doReload
		}));
	},

    getWorlds : function() {
        return this.worldsHash;
    },
    getWorld    : function(wid) {
        return this.worldsHash.get(wid);
    },
    isWorldTest : function(wid) {
        return this.worldsHash.get(wid).get('isTest');
    },
    getWorldUrl : function(wid) {
        return this.worldsHash.get(wid).get('url');
    },
    getWorldName    : function(wid) {
        return this.worldsHash.get(wid).get('name');
    },
    compareRegistrationWorlds : function(w1, w2){
    	return w1.get('regSort') < w2.get('regSort') ? -1 : 1;
    }, 
	getUrl : function (field){
		var f = this.fields.get(field);
		if (f) {
			return f.getSelectedUrl();
		} else {
			return null;
		}
	}
});

Tauri.Worlds = new Tauri.Worlds();
