var Events = {};

Events.text = [
    [' Начнётся через ', '<font color="green">открыт, до начала </font>'],
    [' Появится через ', '<font color="red">торопись, осталось </font>'],
    [' До вторжения ', '<font color="red">торопись, осталось </font>']
];

var muLegend = {
        isfirst: false,
        events_time: [],
        eventTimeDiv: false,
        firstRun: 0,
        firstRun2 : true
}
Events.sked = [
    ['Blood Castle - ', 0, '00:30', '02:30', '04:30', '06:30', '08:30', '10:30', '12:30', '14:30', '16:30', '18:30', '20:30', '22:30'],
    ['Devil Square - ', 0, '01:30', '05:30', '09:30', '13:30', '17:30', '21:30'],
    ['Chaos Castle - ', 0, '01:55',  '05:55', '09:55', '13:55', '17:55', '21:55'],
    ['Golden Invasion - ', 2, '00:36', '04:36', '08:36', '12:36', '16:36', '20:36'],
    ['Skeleton King - ', 2, '16:00', '21:00'],
    ['Red Dragon - ', 2, '20:00'],
    ['Kundun - ', 1, '22:15'], 
    ['Lord Ferea - ', 1, '20:20'],    
    ['Spring Invasion - ', 2, '11:25', '17:25'],
    ['Summer Invasion - ', 2, '13:25', '19:25'],
    ['Winter Invasion - ', 2, '15:25', '21:25'],
    ['White Wizard - ', 2, '02:48', '06:48', '10:48', '14:48', '18:48', '22:48'],
    ['Cryptos Event - ', 2, '15:00', '20:00'],
    ['Gorgon Event - ', 2, '17:00'],
    ['Ice Giant Event - ', 2, '16:00', '21:00'],
    ['Balrog Event - ', 2, '13:00', '18:00'],
    ['Hydra Event - ', 2, '15:00', '17:00'],
    ['Captain Grizzly Event - ', 2, '14:00', '19:00'],
    ['Alpha Crust Event - ', 2, '13:00', '18:00'],
    ['Kentauros Event - ', 2, '14:00', '19:00'],
    ['Zaikan Event - ', 2, '19:55'],
    ['Premium RaidBoss - ', 1, '19:10'],
    ['Medusa - ', 1, '21:10'],

];

Events.init = function(e)
{
    if (typeof e == "string") var g = new Date(new Date().toString().replace(/\d+:\d+:\d+/g, e));

    var f = (typeof e == "number" ? e : (g.getHours() * 60 + g.getMinutes()) * 60 + g.getSeconds()),
        q = Events.sked,
        j = [];

    for (var a = 0; a < q.length; a++)
    {
        var n = q[a];
        for (var k = 2; k < q[a].length; k++)
        {
            var b = 0,
                p = q[a][k].split(":"),
                o = (p[0] * 60 + p[1] * 1) * 60,
                c = q[a][2].split(":");

            if (q[a].length - 1 == k && (o - f) < 0) b = 1;

            var r = b ? (1440 * 60 - f) + ((c[0] * 60 + c[1] * 1) * 60) : o - f;

            if (f <= o || b)
            {
                var l = Math.floor((r / 60) / 60),
                    l = l < 10 ? "0" + l : l,
                    d = Math.floor((r / 60) % 60),
                    d = d < 10 ? "0" + d : d,
                    u = r % 60,
                    u = u < 10 ? "0" + u : u;
                    //<li><i>White Wizard </i><span>[Normal Server]: </span><small id="event53">15m 50s</small></li>
                if (muLegend.firstRun < Events.sked.length ) {
                    j.push('<li class="event" id="event'+Events.sked.indexOf(n)+'">' + '<i>' + n[0] + '</i> <span>[' + (Events.text[q[a][1]][+(l == 0 && d < (q[a][1] ? 1 : 5))]) + '] : </span><small>' + (l + ":" + d + ":" + u) + "</small> " + q[a][b ? 2 : k] + "</li>");
                    muLegend.firstRun++;
                } else {
                    $("#event"+Events.sked.indexOf(n)+" small").html(l + ":" + d + ":" + u);
                } //j.push('<div class="event">' + '<b class="time" style="color: #FECF75;">' + n[0] + q[a][b ? 2 : k] + '</b><div class="small"><span>' + (Events.text[q[a][1]][+(l == 0 && d < (q[a][1] ? 1 : 5))]) + "</span>" + (l + ":" + d + ":" + u) + "</div></div>");
                break;
            };
        };
    };
    if (j.length != 0 && muLegend.firstRun2) {
        if (document.getElementById("eventsTime")) {
            document.getElementById("eventsTime").innerHTML = j.join("");
        }
        muLegend.firstRun2 = false;
    } else {
    }
    /*if (document.getElementById("events")) {
        document.getElementById("events").innerHTML = j.join("");
    }*/
    setTimeout(function()
    {
        Events.init(f == 86400 ? 1 : ++f);
    }, 1000);
};

jQuery(document).ready(function($)
{
    var d = new Date();
    hh = d.getUTCHours() + 3, mm = d.getMinutes(), ss = d.getSeconds();

    if (hh >= 24)
    {
        hh = hh - 24;
    }

    if (hh <= 9)
    {
        hh = "0" + hh;
    }

    if (mm <= 9)
    {
        mm = "0" + mm;
    }

    if (ss <= 9)
    {
        ss = "0" + ss;
    }

    Events.init(hh + ":" + mm + ":" + ss);

          jQuery(document).on("click", ".eventsButtonClick", function() {
              if(muLegend.eventTimeDiv) {
                jQuery('.sliderEvents').animate({"left": '-=340'});
                jQuery('.eventsButton').css('display','block');
                muLegend.eventTimeDiv = false;
              } else {
                jQuery('.sliderEvents').animate({"left": '+=340'});
                jQuery('.eventsButton').css('display','none');
                muLegend.eventTimeDiv = true;
              }

              console.log('eventsButton ' + muLegend.eventTimeDiv);
          });
});

