//http://upload.wikimedia.org/wikipedia/commons/3/3d/Timezones_optimized.png
//http://www.worldtimezone.com/daylight.htm
var tzinfo = new Array();
// chaque ville contient 4 données séparées par des ¤
// le premier : - si le décalage est avant greenwich, + s'il est sur ou après greenwich
// le deuxième : le nombre d'heures de décalage (sans les minutes)
// le troisième : le nombre de minutes de décalage si applicable, sinon 0
// le quatrième : 1 si le day saving time est pris en compte, sinon 0
tzinfo['Afrique'] = new Array();
tzinfo['Afrique']['Afrique du Sud'] = new Array();
tzinfo['Afrique']['Afrique du Sud']['Le Cap'] = '+¤2¤0¤0';
tzinfo['Afrique']['Afrique du Sud']['Johannesburg'] = '+¤2¤0¤0';
tzinfo['Afrique']['Algerie'] = new Array();
tzinfo['Afrique']['Algerie']['Alger'] = '+¤1¤0¤1';
tzinfo['Afrique']['Egypte'] = new Array();
tzinfo['Afrique']['Egypte']['Le Caire'] = '+¤2¤0¤1';
tzinfo['Afrique']['Maroc'] = new Array();
tzinfo['Afrique']['Maroc']['Casablanca'] = '+¤0¤0¤1';
tzinfo['Afrique']['Senegal'] = new Array();
tzinfo['Afrique']['Senegal']['Dakar'] = '+¤0¤0¤1';

tzinfo['Amérique'] = new Array();
tzinfo['Amérique']['Argentine'] = new Array();
tzinfo['Amérique']['Argentine']['Buenos Aires'] = '-¤3¤0¤0';
tzinfo['Amérique']['Canada'] = new Array();
tzinfo['Amérique']['Canada']['Calgary'] = '-¤7¤0¤1';
tzinfo['Amérique']['Canada']['Edmonton'] = '-¤7¤0¤1';
tzinfo['Amérique']['Canada']['Montréal'] = '-¤5¤0¤1';
tzinfo['Amérique']['Canada']['Québec'] = '-¤5¤0¤1';
tzinfo['Amérique']['Canada']['Toronto'] = '-¤5¤0¤1';
tzinfo['Amérique']['Canada']['Vancouver'] = '-¤8¤0¤1';
tzinfo['Amérique']['Canada']['Winnipeg'] = '-¤6¤0¤1';
tzinfo['Amérique']['États-Unis'] = new Array();
tzinfo['Amérique']['États-Unis']['Chicago'] = '-¤6¤0¤1';
tzinfo['Amérique']['États-Unis']['Houston'] = '-¤6¤0¤1';
tzinfo['Amérique']['États-Unis']['Las Vegas'] = '-¤8¤0¤1';
tzinfo['Amérique']['États-Unis']['Los Angeles'] = '-¤8¤0¤1';
tzinfo['Amérique']['États-Unis']['New York'] = '-¤5¤0¤1';
tzinfo['Amérique']['Mexique'] = new Array();
tzinfo['Amérique']['Mexique']['Mexico'] = '-¤6¤0¤1';

tzinfo['Asie'] = new Array();
tzinfo['Asie']['Afghanistan'] = new Array();
tzinfo['Asie']['Afghanistan']['Kaboul'] = '+¤4¤30¤0';
tzinfo['Asie']['Chine'] = new Array();
tzinfo['Asie']['Chine']['Pekin'] = '+¤8¤0¤0';
tzinfo['Asie']['Coree du Sud'] = new Array();
tzinfo['Asie']['Coree du Sud']['Seoul'] = '+¤9¤0¤0';
tzinfo['Asie']['Chine']['Shanghai'] = '+¤8¤0¤0';
tzinfo['Asie']['Inde'] = new Array();
tzinfo['Asie']['Inde']['New Delhi'] = '+¤5¤30¤0';
tzinfo['Asie']['Iran'] = new Array();
tzinfo['Asie']['Iran']['Teheran'] = '+¤3¤30¤0';
tzinfo['Asie']['Indonesie'] = new Array();
tzinfo['Asie']['Indonesie']['Djakarta'] = '+¤9¤0¤0';
tzinfo['Asie']['Japon'] = new Array();
tzinfo['Asie']['Japon']['Tokyo'] = '+¤9¤0¤0';
tzinfo['Asie']['Russie'] = new Array();
tzinfo['Asie']['Russie']['Moscou'] = '+¤3¤0¤1';
tzinfo['Asie']['Turquie'] = new Array();
tzinfo['Asie']['Turquie']['Ankara'] = '+¤2¤0¤1';


tzinfo['Europe'] = new Array();
tzinfo['Europe']['Allemagne'] = new Array();
tzinfo['Europe']['Allemagne']['Berlin'] = '+¤1¤0¤1';
tzinfo['Europe']['Angleterre'] = new Array();
tzinfo['Europe']['Angleterre']['Londres'] = '+¤0¤0¤1';
tzinfo['Europe']['Belgique'] = new Array();
tzinfo['Europe']['Belgique']['Bruxelles'] = '+¤1¤0¤1';
tzinfo['Europe']['Espagne'] = new Array();
tzinfo['Europe']['Espagne']['Madrid'] = '+¤0¤0¤1';
tzinfo['Europe']['France'] = new Array();
tzinfo['Europe']['France']['Paris'] = '+¤1¤0¤1';
tzinfo['Europe']['Grece'] = new Array();
tzinfo['Europe']['Grece']['Athenes'] = '+¤2¤0¤1';
tzinfo['Europe']['Italie'] = new Array();
tzinfo['Europe']['Italie']['Rome'] = '+¤0¤0¤1';


tzinfo['Océanie'] = new Array();
tzinfo['Océanie']['Australie'] = new Array();
tzinfo['Océanie']['Australie']['Sydney'] = '+¤10¤0¤1';

var time_digit = new Array();
time_digit[0] = 'Europe¤France¤Paris';
time_digit[1] = 'Amérique¤États-Unis¤New York';
time_digit[2] = 'Europe¤Russie¤Moscou';
time_digit[3] = 'Océanie¤Australie¤Sydney';
time_digit[4] = 'Asie¤Inde¤Bombay';
time_digit[5] = 'Afrique¤Afrique du Sud¤Le Cap';
time_digit[6] = 'Amérique¤Mexique¤Mexico';

var labels_fuseau = new Array();
//continent¤pays¤ville¤left¤top
labels_fuseau[0] = 'Amérique¤États-Unis¤Los Angeles¤55¤90';
labels_fuseau[1] = 'Amérique¤Mexique¤Mexico¤79¤129';
labels_fuseau[2] = 'Amérique¤États-Unis¤New York¤142¤80';
labels_fuseau[3] = 'Europe¤France¤Paris¤278¤69';
labels_fuseau[4] = 'Europe¤Russie¤Moscou¤330¤51';
labels_fuseau[5] = 'Afrique¤Afrique du Sud¤Le Cap¤307¤255';
labels_fuseau[6] = 'Asie¤Japon¤Tokyo¤528¤87';
//'Djakarta 490 200
//'Le Caire 334 116
//'Melbourne 534 282

function getDecalageForLocation(tz)
{
    var info = tz.split('¤');
    if(info.length != 4) return 0;

    // decalage de l'heure par rapport a l'heure GMT
    var coef = (info[0] == '-') ? -1 : 1;
    var decalage = coef * parseInt(info[1]) * 60 + coef * parseInt(info[2]);

    // heure d'été si applicable
    if(is_dst == 1 && info[3] == '1')
    {
        decalage += 60;
    }

    return decalage;
}

function getTimeForLocation(tz)
{
    var info = tz.split('¤');
    if(info.length != 4) return '00h00';

    // decalage de l'heure par rapport a l'heure GMT
    var coef = (info[0] == '-') ? -1 : 1;
    var h = gmt_time[0] + coef * parseInt(info[1]);
    var m = gmt_time[1] + coef * parseInt(info[2]);

    // heure d'été si applicable
    if(is_dst == 1 && info[3] == '1')
    {
        h++;
    }

    // ajustement aux minutes
    if(m < 0)
    {
        m += 60;
        h--;
    }
    else if(m > 59)
    {
        m -= 60;
        h++;
    }

    // ajustement aux heures
    if(h < 0)
    {
        h += 24;
    }
    else if(h > 23)
    {
        h -= 24;
    }

    var new_time = '';
    if(h < 10) new_time += '0';
    new_time += h.toString()+'h';
    if(m < 10) new_time += '0';
    new_time += m.toString();
    return new_time;
}

function updateClocks()
{
    var loc, t, img, pos;
    for(key in time_digit)
    {
        loc = time_digit[key].split('¤');
        if(loc.length == 3 && tzinfo[loc[0]][loc[1]][loc[2]])
        {
            t = getTimeForLocation(tzinfo[loc[0]][loc[1]][loc[2]]);
            for(var i = 0; i < t.length; ++i)
            {
                if(t.charAt(i) != 'h')
                {
                    img = document.getElementById('digit_'+key+'_'+i);
                    if(img && img.src.indexOf(t.charAt(i)+'.gif') == -1)
                    {
                        pos = img.src.indexOf('.gif');
                        img.src = img.src.substr(0,pos-1)+t.charAt(i)+'.gif';
                    }
                }
            }
        }
    }
}

function updateLabelsFuseau()
{
    var parts,label;
    for(i in labels_fuseau)
    {
        label = document.getElementById('label_fuseau'+i);
        if(label)
        {
            parts = labels_fuseau[i].split('¤');
            if(parts.length == 5 && tzinfo[parts[0]][parts[1]][parts[2]])
            {
                label.innerHTML = '&nbsp;'+getTimeForLocation(tzinfo[parts[0]][parts[1]][parts[2]])+'&nbsp;';
            }
        }
    }
}

function updateGreenwich()
{
    var g = document.getElementById('greenwich');
    var hg = document.getElementById('heuregmt');
    if(!g || !hg) return;
    g.innerHTML = getTimeForLocation('+¤0¤0¤0');
    hg.innerHTML = getTimeForLocation('+¤0¤0¤0');
}

function updateLocalTime()
{
    var l = document.getElementById('heurelocale');
    if(!l) return;
    var d = new Date();
    var decalage = d.getTimezoneOffset();
    var str = (decalage > 0) ? '-¤' : '+¤';
    l.innerHTML = getTimeForLocation(str + Math.floor(decalage/60) + '¤' + (decalage % 60) + '¤0');
}

function updateOrigine()
{
    var f = document.f_diff;
    var str = '';
    if(f.c_origine.value != '' && f.p_origine.value != '' && f.v_origine.value != '')
    {
        str = '<font color="7F7F7F">Il est actuellement <b>'+
            getTimeForLocation(tzinfo[f.c_origine.value][f.p_origine.value][f.v_origine.value])+
            '</b> à '+f.v_origine.value;
    }
    document.getElementById('time_origine').innerHTML = str;
    updateDifference();
}

function updateDestination()
{
    var f = document.f_diff;
    var str = '';
    if(f.c_destination.value != '' && f.p_destination.value != '' && f.v_destination.value != '')
    {
        str = '<font color="7F7F7F">Il est actuellement <b>'+
            getTimeForLocation(tzinfo[f.c_destination.value][f.p_destination.value][f.v_destination.value])+
            '</b> à '+f.v_destination.value;
    }
    document.getElementById('time_destination').innerHTML = str;
    updateDifference();
}

function updateDifference()
{
    var f = document.f_diff;
    var str = '';
    if(f.c_origine.value != '' && f.p_origine.value != '' && f.v_origine.value != '' &&
       f.c_destination.value != '' && f.p_destination.value != '' && f.v_destination.value != '')
    {
        var difference =
            getDecalageForLocation(tzinfo[f.c_destination.value][f.p_destination.value][f.v_destination.value]) -
            getDecalageForLocation(tzinfo[f.c_origine.value][f.p_origine.value][f.v_origine.value]);
        var h = Math.floor(difference / 60);
        var m;
        difference -= h * 60;
        if(difference < 0)
        {
            difference *= -1;

            if(h == 0)
            {
                h = '-0';
            }
        }

        if(difference >= 0 && difference < 10)
        {
            m = '0'+difference.toString();
        }
        else
        {
            m = difference;
        }

        str = '<b><font color="7F7F7F">Il y a donc <font size="3" color="000000"> '+h+'h'+m+'</font> de décalage horaire entre '+f.v_origine.value+' & '+f.v_destination.value+'</font></b>';
    }
    document.getElementById('time_difference').innerHTML = str;
}

function updateFuseau()
{
    var f = document.getElementById('imgfuseau');
    if(f)
    {
        var debut = f.src.indexOf('images-temps/');
        if(debut == -1) return;
        f.src = f.src.substring(0,debut+13)+gmt_time[0].toString()+'.gif';
    }
}

function updateTimes()
{
    gmt_time[2]++;
    if(gmt_time[2]==60)
    {
        gmt_time[1]++;
        gmt_time[2]=0;
    }
    else
        return;// pas besoin de continuer, ya que les secondes qui ont bougées

    if(gmt_time[1]==60)
    {
        gmt_time[0]++;
        gmt_time[1]=0;
        updateFuseau();
    }
    if(gmt_time[0]==24)
    {
        gmt_time[0]=0;
    }

    updateClocks();
    updateGreenwich();
    updateLocalTime();
    updateOrigine();
    updateDestination();
    updateLabelsFuseau()
}

function updateSelects(obj)
{
    var f = document.f_diff;
    if(obj.name == 'c_origine')
    {
        f.v_origine.selectedIndex = 0;
        f.v_origine.options.length = 1;
        f.p_origine.selectedIndex = 0;
        f.p_origine.options.length = 1;
        if(obj.value != '')
        {
            var opt;
            for(pays in tzinfo[obj.value])
            {
                opt = document.createElement('OPTION');
                opt.value = pays;
                opt.text = pays;
                f.p_origine.options[f.p_origine.options.length] = opt;
            }
        }
        updateOrigine();
    }
    else if(obj.name == 'p_origine')
    {
        f.v_origine.selectedIndex = 0;
        f.v_origine.options.length = 1;
        if(obj.value != '')
        {
            var opt;
            for(ville in tzinfo[f.c_origine.value][obj.value])
            {
                opt = document.createElement('OPTION');
                opt.value = ville;
                opt.text = ville;
                f.v_origine.options[f.v_origine.options.length] = opt;
            }
        }
        updateOrigine();
    }
    else if(obj.name == 'c_destination')
    {
        f.v_destination.selectedIndex = 0;
        f.v_destination.options.length = 1;
        f.p_destination.selectedIndex = 0;
        f.p_destination.options.length = 1;
        if(obj.value != '')
        {
            var opt;
            for(pays in tzinfo[obj.value])
            {
                opt = document.createElement('OPTION');
                opt.value = pays;
                opt.text = pays;
                f.p_destination.options[f.p_destination.options.length] = opt;
            }
        }
        updateDestination();
    }
    else if(obj.name == 'p_destination')
    {
        f.v_destination.selectedIndex = 0;
        f.v_destination.options.length = 1;
        if(obj.value != '')
        {
            var opt;
            for(ville in tzinfo[f.c_destination.value][obj.value])
            {
                opt = document.createElement('OPTION');
                opt.value = ville;
                opt.text = ville;
                f.v_destination.options[f.v_destination.options.length] = opt;
            }
        }
        updateDestination();
    }
}

function loadLabelsFuseau()
{
    var cadre = document.getElementById('cadre_fuseau');
    for(i in labels_fuseau)
    {
        var parts = labels_fuseau[i].split('¤');
        cadre.innerHTML += '<div class="arial-9-b" style="position:absolute;top:'+parts[4]+'px;left:'+parts[3]+'px;">* '+parts[2]+'<br><span id="label_fuseau'+i+'" class="label_fuseau">&nbsp;00h00&nbsp;</span></div>';
    }
}

function start_timer()
{
    loadLabelsFuseau();
    updateClocks();
    updateGreenwich();
    updateLocalTime();
    updateFuseau();
    updateLabelsFuseau()
    setInterval('updateTimes()', 1000);
}