﻿// ******************EorzeaClock.js***********************
/*
 * Localisation.
 */
 var url = "http://www.finalfantasy14.eu/";
var L_MoonPhase = "Phase de la lune";
var L_NewMoon = "Nouvelle Lune";
var L_WaxingCrescent = "Premier Croissant de lune";
var L_FirstQuarter = "Premier trimestre";
var L_FullMoon = "Pleine Lune";
var L_WaxingGibbous = "Gibbeuse ascendante";
var L_WaningGibbous = "Gibbeuse décroissante";
var L_LastQuarter = "Dernier trimestre";
var L_WaningCrescent = "Dernier croissant de lune";
var L_DayElement = "Élément du jour";
var L_HourElement = "Élément de l'heure";
var L_Ice = "La glace";
var L_Water = "L'eau";
var L_Wind = "Le vent";
var L_Lightning = "La foudre";
var L_Fire = "Le feu";
var L_Earth = "La terre";
var L_MoonPolarity = "Polarité de la lune";
var L_Astral = "Astral";
var L_Umbral = "Umbral";
var L_1st = "1er";
var L_2nd = "2ème";
var L_3rd = "33";
var L_4th = "4ème";
var L_5th = "5e";
var L_6th = "6e";
var L_MoonPhase = "Phase de la lune:";
var L_MoonElement = "Élément de la lune:";
var L_HourElement = "Élément de l'heure:";
var L_MoonPolarity = "Polarité de la lune:";
function updateTimes()
{
    updateCurrentTimeToGMT();
    updateEorzeaTime();
    updateLeveReset();
}
function updateCurrentTimeToGMT()
{
    var curTime = new Date();
    var curHours = curTime.getHours();
    var curMins = curTime.getMinutes();
    var curSecs = curTime.getSeconds();
    var timeOfDay = (curHours < 12) ? "AM" : "PM";
    
    curHours = (curHours < 12) ? curHours - 12 : curHours;
    curHours = ((curHours < 10) ? "0" : "") + curHours;
    curMins = ((curMins < 10) ? "0" : "") + curMins;
    curSecs = ((curSecs < 10 )? "0" : "") + curSecs;
    
    // var timeString = curHours + ":" + curMins + ":" + curSecs + " " + timeOfDay;
    var timeString = curTime.toUTCString();
}
function updateLeveReset() {

    var now = new Date();
    var utc = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());

    var rtime = 129600 - ((utc - Date.UTC(2010, 9, 19)) / 1000) % 129600;
    var sec = (rtime % 60) | 0;
    rtime -= sec;
    rtime /= 60;
    var min = (rtime % 60) | 0;
    rtime -= min;
    rtime /= 60;
    var hr = (rtime % 24) | 0;
    rtime -= hr;
    hr = hr + rtime;

    rtime = 129600 - ((utc - Date.UTC(2010, 9, 19)) / 1000) % 129600;
    var leveDate = new Date();
    leveDate.setTime(leveDate.getTime() + (rtime * 1000));
    var cDay, cMon, cYear, cHours, cMins;
    cDay = leveDate.getDate();
    cMon = leveDate.getMonth();
    cYear = leveDate.getFullYear();
    cHours = leveDate.getHours();
    if (cHours < 10) cHours = "0" + cHours;
    cMins = leveDate.getMinutes();
    if (cMins < 10) cMins = "0" + cMins;
    var formattedDate = cDay + "/" + cMon + "/" + cYear + " " + cHours + ":" + cMins;

    document.getElementById("resetdate").firstChild.nodeValue = formattedDate;
    document.getElementById("countdown").firstChild.nodeValue = Math.floor(hr) + "h " + Math.floor(min) + "m " + Math.floor(sec) + "s";
}
function updateEorzeaTime() {
    // Code here compliments of Tomoe: http://ffxiv.zam.com/forum.html?forum=152;mid=1286934344191701521&page=1&howmany=100#msg1287573349246314878

    var now = new Date();
    var utc = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());

    var etime = ((utc - Date.UTC(2010, 6, 12, 16)) / 1000) * (1440 / 70);
    var second = (etime % 60) | 0;
    etime = (etime - second) / 60;
    var minute = (etime % 60) | 0;
    etime = (etime - minute) / 60;
    var hour = (etime % 24) | 0;
    etime = (etime - hour) / 24;
    var day = (etime % 32) | 0;
    etime = (etime - day) / 32;
    day++;
    var month = (etime % 12) | 0;
    etime = (etime - month) / 12;
    month++;
    var year = etime | 0;

    if (second < 10) second = "0" + second;
    if (minute < 10) minute = "0" + minute;
    
    // Display
    document.getElementById("eorzeatime").firstChild.nodeValue = month + "/" + day + "/" + year + " " + hour + ":" + minute;
}
function updateFlyout() {
    // Vars to be used below
    var ratioRealToGame = (1440 / 70);

    // Convert local time to GMT
    var curTime = new Date();

    // Check for DST, and counter if necessary
    var arr = DstDetect();
    var isDST = checkDST();
    if (isDST == 1) {
        if (curTime >= arr[0] && curTime <= arr[1])
            isDST = 0;
    }

    var localTime = curTime.getTime();
    var d = (curTime.getTimezoneOffset() + (isDST * 60));
    var localOffest = (curTime.getTimezoneOffset() + (isDST * 60)) * 60000;  // Convert to miliseconds
    var utc = localTime + localOffest;
    var offset = 9;  // JP Offset
    var japan = utc + (3600000 * offset);
    var jpTime = new Date(japan);

    // Get time span since epoch in millis
    var Epoch = new Date(2010, 6, 12, 0, 0, 0, 0);
    var curMillis = jpTime.getTime();
    var epochMillis = Epoch.getTime();
    var diffInMillis = (curMillis - epochMillis);
    var diffInSeconds = ((diffInMillis / 1000) - 90000);

    // Convert to game time
    var delta = (diffInSeconds * ratioRealToGame);

    var gameSecond = (delta % 60) | 0;
    delta -= gameSecond;
    delta /= 60.0;

    var gameMinute = (delta % 60) | 0;
    delta -= gameMinute;
    delta /= 60.0;

    var gameHour = (delta % 24) | 0;
    delta -= gameHour;
    delta /= 24.0;

    var gameDay = (delta % 32) | 0;
    delta -= gameDay;
    delta /= 32.0;

    var gameMonth = (delta % 12) | 0;
    delta -= gameMonth;
    delta /= 12.0;

    var gameYear = delta | 0;

    gameDay++;
    gameMonth++;

     switch (gameDay) {
        case 1:
        case 2:
        case 3:
        case 4:
            {
                document.getElementById("moonphase").src = "http://forum.finalfantasy14.eu/1_new_moon.png";
				document.getElementById("moonphase").alt = "Phase lunaire: Nouvelle Lune";
				document.getElementById("moonphase").title = "Phase lunaire: Nouvelle Lune";
                break;
            }
        case 5:
        case 6:
        case 7:
        case 8:
            {
			     document.getElementById("moonphase").src = "http://forum.finalfantasy14.eu/2_waxing_crescent.png";
				document.getElementById("moonphase").alt = "Phase lunaire: Premier Croissant";
				document.getElementById("moonphase").title = "Phase lunaire: Premier Croissant";
                break;
            }
        case 9:
        case 10:
        case 11:
        case 12:
            {
			    document.getElementById("moonphase").src = "http://forum.finalfantasy14.eu/3_first_quarter.png";
				document.getElementById("moonphase").alt = "Phase lunaire: Premier Quartier";
				document.getElementById("moonphase").title = "Phase lunaire: Premier Quartier";

                break;
            }
        case 13:
        case 14:
        case 15:
        case 16:
            {
			    document.getElementById("moonphase").src = "http://forum.finalfantasy14.eu/4_waxing_gibbous.png";
				document.getElementById("moonphase").alt = "Phase lunaire: Première Lune Gibbeuse";
				document.getElementById("moonphase").title = "Phase lunaire: Première Lune Gibbeuse";
                break;
            }
        case 17:
        case 18:
        case 19:
        case 20:
            {
			    document.getElementById("moonphase").src = "http://forum.finalfantasy14.eu/5_full_moon.png";
				document.getElementById("moonphase").alt = "Phase lunaire: Pleine Lune";
				document.getElementById("moonphase").title = "Phase lunaire: Pleine Lune";
                break;
            }
        case 21:
        case 22:
        case 23:
        case 24:
            {
			                
				document.getElementById("moonphase").src = "http://forum.finalfantasy14.eu/6_waning_gibbous.png";
				document.getElementById("moonphase").alt = "Phase lunaire: Deuxième Lune Gibbeuse";
				document.getElementById("moonphase").title = "Phase lunaire: Deuxième Lune Gibbeuse";
                break;
            }
        case 25:
        case 26:
        case 27:
        case 28:
            {
			     document.getElementById("moonphase").src = "http://forum.finalfantasy14.eu/7_last_quarter.png";
				document.getElementById("moonphase").alt = "Phase lunaire: Dernier Quartier";
				document.getElementById("moonphase").title = "Phase lunaire: Dernier Quartier";
                break;
            }
        case 29:
        case 30:
        case 31:
        case 32:
            {
			    document.getElementById("moonphase").src = "http://forum.finalfantasy14.eu/8_waning_crescent.png";
				document.getElementById("moonphase").alt = "Phase lunaire: Dernier Croissant";
				document.getElementById("moonphase").title = "Phase lunaire: Dernier Croissant";
                break;
            }
    }

    switch (gameMonth) {
        case 1:
        case 2:
            {
			    document.getElementById("moonelement").src = "http://forum.finalfantasy14.eu/ice.png";
				document.getElementById("moonelement").alt = "Jour de la Glace";
				document.getElementById("moonelement").title = "Jour de la Glace";
                break;
            }
        case 3:
        case 4:
            {
                document.getElementById("moonelement").alt = "Jour de l' Eau";			
                document.getElementById("moonelement").title = "Jour de l' Eau";
                document.getElementById("moonelement").src = "http://forum.finalfantasy14.eu/water.png";
                break;
            }
        case 5:
        case 6:
            {
                document.getElementById("moonelement").alt = "Jour du Vent";
                document.getElementById("moonelement").title = "Jour du Vent";
                document.getElementById("moonelement").src = "http://forum.finalfantasy14.eu/wind.png";
                break;
            }
        case 7:
        case 8:
            {
			     document.getElementById("moonelement").alt = "Jour de la Foudre";
                document.getElementById("moonelement").title = "Jour de la Foudre";
                document.getElementById("moonelement").src = "http://forum.finalfantasy14.eu/lightning.png";
                break;
            }
        case 9:
        case 10:
            {
                document.getElementById("moonelement").alt = "Jour du Feu";
                document.getElementById("moonelement").title = "Jour du Feu";
                document.getElementById("moonelement").src = "http://forum.finalfantasy14.eu/fire.png";
                break;
            }
        case 11:
        case 12:
            {
			    document.getElementById("moonelement").alt = "Jour de la Terre";
                document.getElementById("moonelement").title = "Jour de la Terre";
                document.getElementById("moonelement").src = "http://forum.finalfantasy14.eu/earth.png";
                break;
            }
    }

    switch (gameHour) {
        case 0:
        case 1:
        case 2:
        case 3:
            {
                document.getElementById("hourelement").alt = "Heure de la Glace";
                document.getElementById("hourelement").title = "Heure de la Glace";
                document.getElementById("hourelement").src = "http://forum.finalfantasy14.eu/ice.png";
                break;
            }
        case 4:
        case 5:
        case 6:
        case 7:
            {
                document.getElementById("hourelement").alt = "Heure de l'Eau";
                document.getElementById("hourelement").title = "Heure de l'Eau";
                document.getElementById("hourelement").src = "http://forum.finalfantasy14.eu/water.png";
                break;
            }
        case 8:
        case 9:
        case 10:
        case 11:
            {
                document.getElementById("hourelement").alt = "Heure du Vent";
                document.getElementById("hourelement").title = "Heure du Vent";
                document.getElementById("hourelement").src = "http://forum.finalfantasy14.eu/wind.png";
                break;
            }
        case 12:
        case 13:
        case 14:
        case 15:
            {
                document.getElementById("hourelement").alt = "Heure de la Foudre";			
                document.getElementById("hourelement").title = "Heure de la Foudre";
                document.getElementById("hourelement").src = "http://forum.finalfantasy14.eu/lightning.png";
                break;
            }
        case 16:
        case 17:
        case 18:
        case 19:
            {
                document.getElementById("hourelement").alt = "Heure du Feu";			
                document.getElementById("hourelement").title = "Heure du Feu";
                document.getElementById("hourelement").src = "http://forum.finalfantasy14.eu/fire.png";
                break;
            }
        case 20:
        case 21:
        case 22:
        case 23:
            {
                document.getElementById("hourelement").alt = "Heure de la Terre";			
                document.getElementById("hourelement").title = "Heure de la Terre";
                document.getElementById("hourelement").src = "http://forum.finalfantasy14.eu/earth.png";
                break;
            }
    }

    switch (gameMonth) {
        case 1:
            {
				document.getElementById("moonpolarity").alt = "Polarité lunaire: 1° Astral";			
                document.getElementById("moonpolarity").title = "Polarité lunaire: 1° Astral";
                break;
            }
        case 2:
            {
                document.getElementById("moonpolarity").alt = "Polarité lunaire: 1° Umbral";			
                document.getElementById("moonpolarity").title = "Polarité lunaire: 1° Umbral";
                break;
            }
        case 3:
            {
                document.getElementById("moonpolarity").alt = "Polarité lunaire: 2° Astral";			
                document.getElementById("moonpolarity").title = "Polarité lunaire: 2° Astral";
                break;
            }
        case 4:
            {
				document.getElementById("moonpolarity").alt = "Polarité lunaire: 2° Umbral";			
                document.getElementById("moonpolarity").title = "Polarité lunaire: 2° Umbral";
                break;
            }
        case 5:
            {
                document.getElementById("moonpolarity").alt = "Polarité lunaire: 3° Astral";			
                document.getElementById("moonpolarity").title = "Polarité lunaire: 3° Astral";
                break;
            }
        case 6:
            {
			    document.getElementById("moonpolarity").alt = "Polarité lunaire: 3° Umbral";
                document.getElementById("moonpolarity").title = "Polarité lunaire: 3° Umbral";
                break;
            }
        case 7:
            {
                document.getElementById("moonpolarity").alt = "Polarité lunaire: 4° Astral";			
                document.getElementById("moonpolarity").title = "Polarité lunaire: 4° Astral";
                break;
            }
        case 8:
            {
                document.getElementById("moonpolarity").alt = "Polarité lunaire: 4° Umbral";			
                document.getElementById("moonpolarity").title = "Polarité lunaire: 4° Umbral";
                break;
            }
        case 9:
            {
                document.getElementById("moonpolarity").alt = "Polarité lunaire: 5° Astral";			
                document.getElementById("moonpolarity").title = "Polarité lunaire: 5° Astral";
                break;
            }
        case 10:
            {
                document.getElementById("moonpolarity").alt = "Polarité lunaire: 5° Umbral";			
                document.getElementById("moonpolarity").title = "Polarité lunaire: 5° Umbral";
                break;
            }
        case 11:
            {
                document.getElementById("moonpolarity").alt = "Polarité lunaire: 6° Astral";			
                document.getElementById("moonpolarity").title = "Polarité lunaire: 6° Astral";
                break;
            }
        case 12:
            {
                document.getElementById("moonpolarity").alt = "Polarité lunaire: 6° Umbral";			
                document.getElementById("moonpolarity").title = "Polarité lunaire: 6° Umbral";
                break;
            }
    }

    var eoo = gameMonth % 2;
    if (eoo == 0) {
        document.getElementById("moonpolarity").src = "http://forum.finalfantasy14.eu/dark.png";
    }
    else {
        document.getElementById("moonpolarity").src = "http://forum.finalfantasy14.eu/light.png";
    }
}

function checkDST() {
    var rightNow = new Date();
    var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st 
    var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st 
    var temp = jan1.toGMTString();
    var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
    temp = june1.toGMTString();
    var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
    var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
    var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
    var dst;
    if (std_time_offset == daylight_time_offset) {
        dst = "0"; // daylight savings time is NOT observed 
    } else {
        // positive is southern, negative is northern hemisphere 
        var hemisphere = std_time_offset - daylight_time_offset;
        if (hemisphere >= 0)
            std_time_offset = daylight_time_offset;
        dst = "1"; // daylight savings time is observed 
    }

    return dst;
}

function TimezoneDetect()
{
    var dtDate = new Date('1/1/' + (new Date()).getUTCFullYear());
    var intOffset = 10000; //set initial offset high so it is adjusted on the first attempt
    var intMonth;
    var intHoursUtc;
    var intHours;
    var intDaysMultiplyBy;

    //go through each month to find the lowest offset to account for DST
    for (intMonth=0;intMonth < 12;intMonth++){
        //go to the next month
        dtDate.setUTCMonth(dtDate.getUTCMonth() + 1);

        //To ignore daylight saving time look for the lowest offset.
        //Since, during DST, the clock moves forward, it'll be a bigger number.
        if (intOffset > (dtDate.getTimezoneOffset() * (-1))){
            intOffset = (dtDate.getTimezoneOffset() * (-1));
        }
    }

    return intOffset;
}
//Find start and end of DST
function DstDetect()
{
    var dtDstDetect = new Date();
    var dtDstStart = '';
    var dtDstEnd = '';
    var dtDstStartHold = ''; //Temp date hold
    var intYearDayCount = 732; //366 (include leap year) * 2 (for two years)
    var intHourOfYear = 1;
    var intDayOfYear;
    var intOffset = TimezoneDetect(); //Custom function. Make sure you include it.

    //Start from a year ago to make sure we include any previously starting DST
    dtDstDetect = new Date()
    dtDstDetect.setUTCFullYear(dtDstDetect.getUTCFullYear() - 1);
    dtDstDetect.setUTCHours(0,0,0,0);

    //Going hour by hour through the year will detect DST with shorter code but that could result in 8760
    //FOR loops and several seconds of script execution time. Longer code narrows this down a little.
    //Go one day at a time and find out approx time of DST and if there even is DST on this computer.
    //Also need to make sure we catch the most current start and end cycle.
    for(intDayOfYear = 1; intDayOfYear <= intYearDayCount; intDayOfYear++){
        dtDstDetect.setUTCDate(dtDstDetect.getUTCDate() + 1);

        if ((dtDstDetect.getTimezoneOffset() * (-1)) != intOffset && dtDstStartHold == ''){
            dtDstStartHold = new Date(dtDstDetect);
        }
        if ((dtDstDetect.getTimezoneOffset() * (-1)) == intOffset && dtDstStartHold != ''){
            dtDstStart = new Date(dtDstStartHold);
            dtDstEnd = new Date(dtDstDetect);
            dtDstStartHold = '';

            //DST is being used in this timezone. Narrow the time down to the exact hour the change happens
            //Remove 48 hours (a few extra to be on safe side) from the start/end date and find the exact change point
            //Go hour by hour until a change in the timezone offset is detected.
            dtDstStart.setUTCHours(dtDstStart.getUTCHours() - 48);
            dtDstEnd.setUTCHours(dtDstEnd.getUTCHours() - 48);

            //First find when DST starts
            for(intHourOfYear=1; intHourOfYear <= 48; intHourOfYear++){
                dtDstStart.setUTCHours(dtDstStart.getUTCHours() + 1);

                //If we found it then exit the loop. dtDstStart will have the correct value left in it.
                if ((dtDstStart.getTimezoneOffset() * (-1)) != intOffset){
                    break;
                }
            }

            //Now find out when DST ends
            for(intHourOfYear=1; intHourOfYear <= 48; intHourOfYear++){
                dtDstEnd.setUTCHours(dtDstEnd.getUTCHours() + 1);

                //If we found it then exit the loop. dtDstEnd will have the correct value left in it.
                if ((dtDstEnd.getTimezoneOffset() * (-1)) != (intOffset + 60)){
                    break;
                }
            }

            //Check if DST is currently on for this time frame. If it is then return these values.
            //If not then keep going. The function will either return the last values collected
            //or another value that is currently in effect
            if ((new Date()).getTime() >= dtDstStart.getTime() && (new Date()).getTime() <= dtDstEnd.getTime()){
                return new Array(dtDstStart,dtDstEnd);
            }

        }
    }
    return new Array(dtDstStart,dtDstEnd);
}

