function Radar() {
    this.reload = 1000 * 60 * 10;
    this.delayms = 600;
    this.radarimages = new Array();
    this.numframes = 6;
    this.loaded = false;
    this.running = false;
    this.currentframe = 0;
}

Radar.prototype.load = function () {
    if (this.loaded) {
	return;
    }
    this.loaded = true;

    for(var i = 1; i <= this.numframes; i++) {
	var img = new Image();
	img.src = '/radar/image-' + i + '.jpg';
	this.radarimages.push(img);
    }
}

Radar.prototype.pause = function() {
    this.running = false;
    $('#pause').remove();
    clearInterval(this.timer);
}

Radar.prototype.go = function() {
    if (this.running) {
	return;
    }
    this.running = true;
    var thisobj = this;

    var pausebutton = $("<button id='pause'>Pausa</button>");
    pausebutton.click(function () { thisobj.pause(); });
    $('#controls').append(pausebutton);

    this.timer = setInterval(function() {
	thisobj.currentframe --;
	if (thisobj.currentframe == -1) {
	    thisobj.currentframe = thisobj.numframes - 1;
	    $('.seqframe').each(function(i) {
		$(this).css('background-color', '#cccccc');
	    });
	}
	$('#seq' + (thisobj.currentframe + 1)).css('background-color', '#999999');

	$('#radarimage').html(thisobj.radarimages[thisobj.currentframe]);
    }, this.delayms);
}

function toItalian(str) {
    switch(str) {
    case "lun" : return "Luned&igrave;";
    case "mar" : return "Martedi&igrave;";
    case "mer" : return "Mercoled&igrave;";
    case "gio" : return "Gioved&igrave;";
    case "ven" : return "Venerd&igrave;";
    case "sab" : return "Sabato";
    case "dom" : return "Domenica";
    case "Padua":
	return "Padova";
    case "Venice":
	return "Venezia";
    default:
	return str;
    }
}

function citySelect(city) {
    $('#citylabel').html("Meteo " + toItalian(city));

    $.ajax({
        type: "GET",
        url: "/weather/" + city.replace(/ /g, '+'),
        dataType: "xml",
        success: function(xml) {
	    var newdivstemplate = $('#weathercontents').clone().attr('id', null).addClass('forecast');

	    var current_conditions = $(xml).find('current_conditions');
	    $('.weathertxt').html(
		current_conditions.find('temp_c').attr('data') + '&deg;');
	    $('.weathertxt').append(
		"<br>" + current_conditions.find('condition').attr('data'));
	    $('.weathericon').attr('src', "http://www.google.com" +
				   current_conditions.find('icon').attr('data'));

	    var first = true;
	    $(xml).find('forecast_conditions').each(function () {
		var newdivs = newdivstemplate.clone();

		$('#weather').append(newdivs);
		var icon = $(newdivs).find('.weathericon');
		icon.attr('src', "http://www.google.com" +
			  $(this).find('icon').attr('data'));
		var weathertxt = $(newdivs).find('.weathertxt');
		var forecast = $(this).find('high').attr('data') + '&deg;' +
		    " / " +
		    $(this).find('low').attr('data') + '&deg;' + "<br>" +
		    $(this).find('condition').attr('data');
		weathertxt.html(forecast);

		var day = "Oggi";
		if (!first) {
		    day = toItalian($(this).find('day_of_week').attr('data'));
		}

		$(newdivs).find('.day h3').html(day);

		first = false;
	    });
        }
    });
}

function googleLoaded() {
    var city = "Padua";
    if (google.loader.ClientLocation) {
	var loc = google.loader.ClientLocation;
	if (loc.address.region == "Veneto") {
	    city = loc.address.city;
	}
    }
    citySelect(city);
}

google.load("search", "1", {"language" : "it"});
google.setOnLoadCallback(googleLoaded);

$(document).ready(function(){
    radar = new Radar();
    $('#animate').click(function() {
	radar.load();
	radar.go();
    });

    for(var i = 1; i <= radar.numframes; i++) {
	$('#sequence').prepend($('<td class="seqframe" style="background-color: #cccccc;" id="seq' + i + '">&nbsp;</td>'));
    }

    $('#cityselect').change(function () {
	$('.forecast').remove();
	citySelect($('#cityselect').val());
    });
});