
function Ordinal(number)
{
	var tens = number%100
	var units = number%10

	if (tens == 11) return "th"
	if (tens == 12) return "th"
	if (tens == 13) return "th"
	if (units == 1) return "st"
	if (units == 2) return "nd"
	if (units == 3) return "rd"
	return "th"
}

function Month(number)
{
	if (number == 1) return "Jan"
	if (number == 2) return "Feb"
	if (number == 3) return "Mar"
	if (number == 4) return "Apr"
	if (number == 5) return "May"
	if (number == 6) return "Jun"
	if (number == 7) return "Jul"
	if (number == 8) return "Aug"
	if (number == 9) return "Sep"
	if (number == 10) return "Oct"
	if (number == 11) return "Nov"
	if (number == 12) return "Dec"
	return "???"
}

        var date = new Date();
        var latest = new Date();

        // Set date/time back 15 hours to allow maps to be processed by NOAA
        var max_secs = latest.getTime();
        var this_year = latest.getFullYear() 
        max_secs = max_secs - (15.0 * 3600.0 * 1000.0);
        latest.setTime(max_secs);

function show_date(now)
{
        year  = now.getFullYear();
        month = now.getMonth() + 1;
        day   = now.getDate();

        document.form.map_date.value =
            day + Ordinal(day) + " " + Month(month) + " " + year;
}

function get_map()
{ 
        area = "http://polar.ncep.noaa.gov/seaice/analysis/";

        year  = date.getFullYear();
        month = date.getMonth() + 1;
        day   = date.getDate();
        get_secs = date.getTime();

        // When setting date month is zero based!
        new_date = new Date(2004, 7, 27);
        new_secs = new_date.getTime();
//        if (get_secs < min_secs)
//        {
                pole = (name == "n0" || name == "n1" || name == "n2") ? "nh/nh."
                                                              : "sh/sh."
//        }
//        else
//        {
//                pole = (name == "n0" || name == "n1" || name == "n2") ? "nh/nh12."
//                                                             : "sh/sh12."
//        }

//      area = "ftp://polar.ncep.noaa.gov/pub/ice/";
//      pole = (name == "n0" || name == "n1" || name == "n2") ? "nh"
//                                                            : "sh"
//      folder = "/" + year + "/"

        if (month < 10) { month = "0" + month; }
        if (day   < 10) { day   = "0" + day;   }

//      if (year == this_year || year == this_year -1)
//      if (year == this_year)
//	    map = area + pole + "/" + pole + "." + year + month + day + ".gif";
//      else
//	    map = area + pole + folder + pole + "." + year + month + day + ".gif";

//        map.src = area + pole + year + month + day + ".gif";
        map = area + pole + year + month + day + ".gif";


        if (name == "n0") 
           parent.mapn0.location = map;
        if (name == "n1") 
           parent.mapn1.location = map;
        if (name == "n2") 
           parent.mapn2.location = map;
        if (name == "s0") 
           parent.maps0.location = map;
        if (name == "s1") 
           parent.maps1.location = map;
        if (name == "s2") 
           parent.maps2.location = map;
	
        show_date(date);
}

function check_date(new_date)
{

        secs = new_date.getTime();
        show_date(new_date);

        max_secs = latest.getTime();
        if (secs > max_secs)
        {
                alert("Map not available yet!")
                show_date(date);
                return false;
        }

        // When setting date month is zero based!
        min_date = new Date(1998, 6, 1);
        min_secs = min_date.getTime();
        if (secs < min_secs)
        {
                alert("Map not available then!")
                show_date(date);
                return false;
        }

        return true;
}

function update(x)
{
	
        date.setTime(latest.getTime());
        update_year(x)
}

function update_year(x)
{
	
        new_date = new Date();
        new_date.setTime(date.getTime());

        year = new_date.getFullYear();
        year += x;
        new_date.setFullYear(year);

        if (check_date(new_date) == true)
        {
                date = new_date;
                get_map();
        }
}

function update_month(x)
{

        new_date = new Date();
        new_date.setTime(date.getTime());

        months = new_date.getMonth();
        months += x + 12;
        month = months % 12;
        new_date.setMonth(month);

        years = new_date.getFullYear();
        months -= month;
        if (months/12 == 0) years -= 1;
        if (months/12 == 2) years += 1;
        new_date.setFullYear(years);

        if (check_date(new_date) == true)
        {
                date = new_date;
                get_map();
        }
}

function update_week(x)
{
	
        new_date = new Date();

        secs = date.getTime();
        secs += x * 7 * 24 * 3600000.0;
        new_date.setTime(secs);

        if (check_date(new_date) == true)
        {
                date = new_date;
                get_map();
        }
}

function update_day(x)
{

        new_date = new Date();

        secs = date.getTime();
        secs += x * 24 * 3600000.0;
        new_date.setTime(secs);

        if (check_date(new_date) == true)
        {
                date = new_date;
                get_map();
        }
}

function init()
{
    
    if (name == "n0" || name == "s0") update(0)
    else if (name == "n1" || name == "s1") update(-4)
    else update(-8)
}

