var WeatherBlock = new Class({
	
	Implements: Options,
	
	options: {
		active_city_id:			'weather-city-active',
		select_id:				'weather-select',
		
		city_pfx:				'weather-city-',
		day_pfx:				'weather-day-',
		forecast_pfx:			'weather-forecast-',
		
		active_city:			0,
		active_day:				0
	},
	
	initialize: function(options) {
		this.setOptions(options);
		this.hideCityList();

		this.loadCity();
		this.loadDay();
		
		this.start();
		
		var i = 0;
		for(i = 0; i < 6; i++) {
            try {
                $(this.options.city_pfx + i).addEvent('click', this.toggleCity.bind(this, i));
			} catch(e) {}
		}
		$(this.options.select_id).addEvent('click', this.hideCityList.bind(this));
		
		$(this.options.active_city_id).addEvent('mouseover', 	this.showCityList.bind(this));
		$(this.options.active_city_id).addEvent('mouseout', 	this.hideCityList.bind(this));
		$(this.options.active_city_id).addEvent('click', 		this.hideCityList.bind(this));
		
		for(i = 0; i < 3; i++) {
			$(this.options.day_pfx + i).addEvent('mouseover', this.toggleDay.bind(this, i));
		}
	},
	
	toggleCity: function(id) {
		this.setActiveCity(id);
		this.hideCityList();
	},
	
	toggleDay: function(id) {
		this.setActiveDay(id);
	},
	
	start: function() {
		this.setActiveCity(this.options.active_city);
		this.setActiveDay(this.options.active_day);
	},
	
	setActiveCity: function(index) {
		if(index > 6) {
			return;
		}
		
		var city = null;
		try {
            city = $(this.options.city_pfx + index).getText();
		} catch(e) { }
		try {
            $(this.options.active_city_id).setText(city);
		} catch(e) { }
		
		this.hideForecast(this.options.active_city, this.options.active_day);
		this.showForecast(index, this.options.active_day);
		
		this.options.active_city = index;
		
		this.saveCity();
	},
	
	setActiveDay: function(index){
		if(index > 2) {
			return;
		}
		
		this.hideForecast(this.options.active_city, this.options.active_day);
		this.showForecast(this.options.active_city, index);
		
		this.options.active_day = index;
		
		this.saveDay();
	},
	
	hideForecast: function(city, day) {
        try {
            var el = $(this.options.forecast_pfx + city + '-' + day);
            el.style.display = "none";
            el.style.visibility = "hidden";
		} catch(e) {}
		
		$(this.options.day_pfx + day).className = "day";
	},
	
	showForecast: function(city, day) {
        try {
            var el = $(this.options.forecast_pfx + city + '-' + day);
            el.style.display = "block";
            el.style.visibility = "visible";
		} catch(e) {}
		
		$(this.options.day_pfx + day).className = "day_act";
	},
	
	saveCity: function() {
		createCookie("weather_city", this.options.active_city, 30);
	},
	
	saveDay: function() {
		createCookie("weather_day", this.options.active_day, 1);
	},
	
	loadCity: function() {
		var id = readCookie("weather_city");
		if(id != null) {
			this.options.active_city = id;
		}
	},
	
	loadDay: function() {
		var today = new Date();
		today = today.getDay();

		if(today >= 1 && today <= 5) {
			this.options.active_day = 0;
		} else if(today == 6) {
			this.options.active_day = 1;
		} else {
			this.options.active_day = 2;
		}
		
		
        var day = readCookie("weather_day");
        if(day != null && this.options.active_day == 0) {
			this.options.active_day = day;
		}
	},
	
	showCityList: function() {
	},
	
	hideCityList: function() {
	}
});


window.addEvent('load', function() {
    var weather_options = {
        active_city: 0
    };
    var weather = new WeatherBlock(weather_options);
    new DropDownS("nav_1", "weather_menu_container");
});
