
var day = 0;

var wind_active_cookie = readCookie('wind_active');
var wind_active =  wind_active_cookie == null ? true : ( wind_active_cookie == 'true' ? true : false );
createCookie('wind_active', wind_active?'true':'false', 365);

var prec_active_cookie = readCookie('prec_active');
var prec_active = prec_active_cookie == null ? true : ( prec_active_cookie == 'true' ? true : false );
createCookie('prec_active', prec_active?'true':'false', 365);

var temp_active_cookie = readCookie('temp_active');
var temp_active = temp_active_cookie == null ? true : ( temp_active_cookie == 'true' ? true : false );
createCookie('temp_active', temp_active?'true':'false', 365);

var cloud_active_cookie = readCookie('cloud_active');
var cloud_active = cloud_active_cookie == null ? false : ( cloud_active_cookie == 'true' ? true : false );
createCookie('cloud_active', cloud_active?'true':'false', 365);

var base_wind = "http://theyr.net/imgmaps/maps/wind_be_";
var base_cloud = "http://theyr.net/imgmaps/maps/cloud_be_";
var base_prec = "http://theyr.net/imgmaps/maps/prec_be_";
var base_temp = "http://theyr.net/imgmaps/maps/temp_be_";

var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

function switchPrec() {
	prec_active = !prec_active;
	createCookie('prec_active', prec_active?'true':'false', 365);
	refresh();
}
function switchTemp() {
	temp_active = !temp_active;
	createCookie('temp_active', temp_active?'true':'false', 365);
	refresh();
}
function switchClouds() {
	cloud_active = !cloud_active;
	createCookie('cloud_active', cloud_active?'true':'false', 365);
	refresh();
}
function switchWind() {
	wind_active = !wind_active;
	createCookie('wind_active', wind_active?'true':'false', 365);
	refresh();
}

function getDays() {
	var html = '<table><tr>';
	for (var i=0; i<=5; i++) {
		var d = new Date(today.getFullYear(), today.getMonth(), today.getDate() + i);
		html += '<td class="day_option day_' + (day == i ? 'active' : 'inactive') + '">';
		html += "<a href=\"javascript:setDay("+i+")\">" + weekday[d.getDay()]+"</a>";
		html+='</td>';
		html += (i == 2 ? '</tr><tr>' : '');
	}   
	html += "</tr></table>";
	return html;
}

function setDay(whichDay) {
	day = whichDay;
	refresh();
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function refresh() {
	var html = " "; //COOKIES : "+wind_active_cookie+prec_active_cookie+temp_active_cookie+cloud_active_cookie;
	var img="<img ";
	html += '<div class="topline"><table><tr>';
	html += '<td class="option ' + ( wind_active ? 'option_active' : 'option_inactive' ) + '"><a href="javascript:switchWind() "><img src="wind_'+( wind_active ? 'option_active' : 'option_inactive' )+'.png"/></a></span> ';
	html += '<td class="option ' + ( cloud_active ? 'option_active' : 'option_inactive' ) + '"><a href="javascript:switchClouds() "><img src="clouds_'+(  cloud_active ? 'option_active' : 'option_inactive' )+'.png"/></a></span> ';
	html += '<td class="option ' + ( prec_active ? 'option_active' : 'option_inactive' ) + '"><a href="javascript:switchPrec() "><img src="prec_'+( prec_active ? 'option_active' : 'option_inactive' )+'.png"/></a></span> ';
	html += '<td class="option ' + ( temp_active ? 'option_active' : 'option_inactive' ) + '"><a href="javascript:switchTemp() "><img src="temp_'+( temp_active ? 'option_active' : 'option_inactive' )+'.png"/></a></span> ';
	html += '<td class="option">' + getDays() +'</td>';
	html += '<td class="option"><a href="javascript:refresh()"><img src="refresh.png"/></a></td>';
	html += '</tr></table></div>';
	
	html += "<table>";
	if (wind_active) {
		html += "<td>Wind</td>\n";
	}
	if (cloud_active) {
		html += "<td>Clouds</td>\n";
	}
	if (prec_active) {
		html += "<td>Rain/snow</td>\n";
	}
	if (temp_active) {
		html += "<td>Temperatures</td>\n";
	}
	
	for (var hour=8; hour<=22; hour+=1) {
		var i = day * 24 + hour;

		html += "<tr>\n";
		if (wind_active) {
			html += "<td>"+img+"src=\""+base_wind+zeroPad(i,3)+".gif\" /></td>\n";
		}
		if (cloud_active) {
			html += "<td>"+img+"src=\""+base_cloud+zeroPad(i,3)+".gif\" /></td>\n";
		}
		if (prec_active) {
			html += "<td>"+img+"src=\""+base_prec+zeroPad(i,3)+".gif\" /></td>\n";
		}
		if (temp_active) {
			html += "<td>"+img+"src=\""+base_temp+zeroPad(i,3)+".gif\" /></td>\n";
		}
		html += "</tr>";
	}
	html += "</table>";
	document.getElementById("meteo").innerHTML = html;
}

function zeroPad(num,count)
{
    var numZeropad = num + '';
    while(numZeropad.length < count) {
    numZeropad = "0" + numZeropad;
    }
    return numZeropad;
}

var today = new Date();

