function testImageAndStart(minutes) {
    if (document.radarpic.width != 422) {
	goTo(-600000);
	window.setTimeout("testImageAndStart("+minutes+")", 750);
	}  else  {
	start(minutes);
    }
}

function now() {
    var date = new Date();
    var secs = date.getTime();
    secs /= 600000;
    secs = Math.floor(secs);
    secs *= 600000;
    //    secs += 3600000;  // comment this line out in daylight saving time in the summer
    var date2 = new Date(secs);

    return date2;
}

function init() {
    setDate(now());
    document.control.isAnimating.value = 0;
    setImage(getImage(getDate()));
    window.setTimeout("testImageAndStart(60)", 750);
    
}

function getDate() {
    var date = new Date(document.control.year.value, (document.control.month.value -1), document.control.day.value, document.control.hour.value, document.control.minute.value);
    return date;
}

function setDate(date) {
    document.control.year.value = date.getFullYear();
    document.control.month.value = (date.getMonth() + 1);
    document.control.day.value = date.getDate();
    document.control.hour.value = date.getHours();
    document.control.minute.value = date.getMinutes();
    //    window.document.getElementById("timestamp").innerHTML = "as"; //date.getFullYear() + " " + day + "." + month + " " + hours + ;
}

function goTo(milliseconds) {
    var date = getDate();
    var secs = date.getTime();
    secs += milliseconds;
    date.setTime(secs);
    setDate(date);
    setImage(getImage(date));
}

function starterFunction(minutes) {
    setDate(now());
    start(minutes);
}

function start(minutes) {
    //    setDate(now());
    goTo(-(minutes*60000));
    if (document.control.isAnimating.value == 1) {
	document.control.isAnimating.value = 0;
	window.setTimeout("animate(("+minutes/10+"),600000)", 750);
    } else {
	animate((minutes/10),600000);
    }
}

function getImage(date) {
    var address = "http://img.landimeteo.ch/meteodata/radar/radar";
    var min = date.getMinutes() + "";
    var hours = date.getHours() + "";
    var month = date.getMonth() + 1;
    var day = date.getDate() + "";
    if (date.getMinutes() < 10) {
	min = "0"+ date.getMinutes();
    }
    if (date.getHours() < 10) {
	hours = "0" + date.getHours();
    }
    if (date.getDate() < 10) {
	day = "0" + date.getDate();
    }
    if (month < 10) {
	month = "0" + month;
    }
    month = month + "";
    var string = date.getFullYear() + month + day + hours + min;
    window.document.getElementById("timestamp").innerHTML = date.getFullYear() + " " + day + "." + month + " " + hours + ":" + min;
    return (address + string + ".gif");
}
function setImage(url) {
    window.document.radarpic.src = url; 
}

function animate(count,step) {
    if (document.control.isAnimating.value == 1) {
	// stop the animation
	// alert("stop the animation");
	document.control.isAnimating.value = 0;
    } else {
	// start the animation
	//	alert("start the animation");
	document.control.isAnimating.value = 1;
	animateRecursive(count, count, step);
    }
}

function animateRecursive(count, downcount, step) {
    if (document.control.isAnimating.value == 1) {
	// continue recursive animation
	if (downcount == 0) {
	    downcount = count;
	    goTo(step -(count*step));
	    window.setTimeout("animateRecursive("+ count +","+ (downcount -1) +","+ step +")", 750);
	} else if (downcount == 1) {
	    goTo(step);
	    window.setTimeout("animateRecursive("+ count +","+ (downcount -1) +","+ step +")", 1500);
	}
	else {
	    goTo(step);
	    window.setTimeout("animateRecursive("+ count +","+ (downcount -1) +","+ step +")", 750);
	}
	//	alert("downcount= "+ downcount + "  count = " + count);

    } else {
	// stop the recursive animation
	//	alert("animation stopped");
    }
}
