var WhereAndWhenBlock = new Class({
	
	Implements: Options,
	
	options: {
        cookie_name:        'whereandwhenblock',
		toggle_id:			'whereandwhen-toggle',
		tab_pfx:			'whereandwhen-category-',
		container_pfx:		'whereandwhen-container-',
		active_city:		-1,
		active_category:	-1,
		category_count:		5
	},	
	
	initialize: function(options) {
		this.setOptions(options);
		
		$(this.options.toggle_id).addEvent('change', this.toggleCity.bind(this));
		
		for (var i = 0; i < this.options.category_count; i++) {
			if($(this.options.tab_pfx + i))
				$(this.options.tab_pfx + i).addEvent('click', this.setActiveCategory.bind(this, [i, true]));
		}
		
        this.setStartupCity();
	},
	
	toggleCity: function() {
		this.setActiveCity($(this.options.toggle_id).selectedIndex);
	},	

    setStartupCity: function() {
        var cookie = Cookie.read(this.options.cookie_name);
        if (cookie !== null) {
            this.activateCity(cookie);
            return;
        }
        this.setActiveCity(0);
    },

    activateCity: function(city_id) {
        $(this.options.toggle_id).value = city_id;
        this.toggleCity();
    },

	setActiveCity: function(index) {
		if($(this.options.toggle_id) == null) {
			return;
		}
		if(this.options.active_city == index) {
			this.options.active_city = index;
			return;
		}

        Cookie.write(this.options.cookie_name, $(this.options.toggle_id).value);

		this.hideObjectContainer(this.options.active_city);
		this.options.active_city = index;
		var first = this.hideEmptyCategories();
		this.setActiveCategory(first, false);
		this.showObjectContainer(index);
	},
	
	setActiveCategory: function(index, hide) {
		if($(this.options.tab_pfx + index) == null) {
			return;
		}
		
		var el_name = (this.options.tab_pfx + this.options.active_category.toString());
		if ($(el_name) && hide) {
			$(el_name).className = "";
		}
		
		if(this.options.active_category == index) {
			this.options.active_category = index;
		}
		
		this.hideObjectContainer(index);
		this.options.active_category = index;
		
		$(this.options.tab_pfx + this.options.active_category).className = "act";
		
		this.showObjectContainer(index);
		
		return false;
	},
	
	hideEmptyCategories: function() {
		var firstActive = -1;
		for(var i = 0; i < this.options.category_count; i++) {
			if($(this.options.container_pfx + this.options.active_city + "-" + i) == null) {
				if($(this.options.tab_pfx + i) != null) {
					$(this.options.tab_pfx + i).className = "hide";
				}
			} else {
				$(this.options.tab_pfx + i).className = "";
				if(firstActive < 0) {
					firstActive = i;
				}
			}
		}
		return firstActive == -1 ? 0 : firstActive;
	},

	hideObjectContainer: function(index) {
		if($(this.options.container_pfx + this.options.active_city + "-" + this.options.active_category) == null) {
			return;
		}
		
		//$(this.options.container_pfx + index).className = "hide";
		$(this.options.container_pfx + this.options.active_city + "-" + this.options.active_category).className = "hide";
	},
	
	showObjectContainer: function(index) {
		if($(this.options.container_pfx + this.options.active_city + "-" + this.options.active_category) == null) {
			return;
		}
		
		//$(this.options.container_pfx + index).className = "show";
		$(this.options.container_pfx + this.options.active_city + "-" + this.options.active_category).className = "show";
	}
	
});

window.addEvent('domready', function() {
    if (typeof whereandwhen_options == 'undefined') {
        var whereandwhen_options = {
            category_count: 5
        };
    }
	var whereandwhenblock = new WhereAndWhenBlock(whereandwhen_options);
});