var DepartureModel = new (function($){
	$ = jQuery;

	//!!! These values are constants !!!
	var NAME2INT = {
	    tokyo:1, nagoya:2, osaka:3
	};
	var INT2NAME = [];
	INT2NAME[1] = "tokyo";
	INT2NAME[2] = "nagoya";
	INT2NAME[3] = "osaka";

	var current = 1;
	var previous = 0;
	var observers = {
	    changed: [],
	    initialized: []
	};

	this.current = function() {
	    return current;
	}
	this.currentName = function() {
	    return INT2NAME[current];
	}

	function _asFunc(obj) {
	    return (typeof obj == 'function') ? obj : function(){ obj.update(); };
	}

	this.addObserver = function(event, obj) {
	    observers[event].push(_asFunc(obj));
	}

	function _notify(event) {
	    var lst = observers[event];
	    for(var i = 0; i<lst.length; ++i) {
		setTimeout(lst[i], 1);
	    }
	}

	this.init = function() {
	    var deptId;
	    if($.cookie && (deptId = $.cookie('departure_id'))) {
		current = deptId;
	    }
	    _notify('initialized');
	}

	this.set = function(val) {
	    if(current != val) {
		previous = current;
		current = val;
		$.cookie && $.cookie('departure_id', current, { path: '/'});
		_notify('changed');
	    }
	}

	this.setByName = function(name) {
	    if(NAME2INT[name]) {
		this.set(NAME2INT[name]);
	    }
	}
    });

jQuery(function($){
	DepartureModel.init();
    });
