﻿// KIRJOITETAAN TÄHÄN DOKUMENTTIIN
// TÄMÄN PÄIVÄN TAPAHTUMIEN HAKEMINEN

$(document).ready(function() {

    $("#div_events").html('<h1 style="padding-top: 30px;">Tunnit tänään</h1>');
    $("#div_events").append('<div id="div_events_ev" style="padding-bottom: 20px;"></div>');
    $("#div_events").append('<h1 style="padding-top: 10px; display: none;" id="h3_lkj"></h1>');
    $("#div_events").append('<div id="p_events" style="padding-bottom: 20px;"></div>');

    $("#div_events").show();
    RefreshClasses();

    if ($("#img_nettivaraus").length != 0) {

        $("#img_nettivaraus").css("position", "absolute").css("left", (($("#tunnit").position().left + 45) - ($("#img_nettivaraus").width() / 2)) + "px").css("top", ($("#tunnit").position().top - $("#img_nettivaraus").height() + 40) + "px");
        $("#img_nettivaraus").fadeIn(function() {

        setTimeout(function() {
            $("#img_nettivaraus").fadeOut();
                
            }, 5000);

        });
    }

});

function RefreshClasses() {

    var classcount = 0;

    $.get("tanaan.aspx", { tool: 'getClasses' }, function(xml) {
        //$("#div_events_ev").html('');

        $(xml).find("class").each(function() {

            classcount = classcount + 1;
            if ($("#div__" + $(this).attr("id")).length == 0) {

                var tila = "";
                if ($(this).attr("space") != "0") {
                    tila = '<a style="color: #5CC800; text-decoration: none;" href="/tunnittiedot.aspx?id=' + $(this).attr("id") + '">Tilaa on vielä, varaa paikka klikkaamalla tästä!</a>';
                } else {
                    tila = "<span style='color: silver;'>Tunti on täynnä</span>";
                }

                if ($(this).attr("maxusers") != "0") {
                    tila = '<div>' + tila + '</div>';
                } else { tila = ''; }

                $("#div_events_ev").append('<table id="div__' + $(this).attr("id") + '" cellspacing="0" cellpadding="0"><tr><td valign="top" style="padding: 4px; padding-left: 0px;"><span style="font-size: 10px; color: #5CC800;">' + $(this).attr("starttime") + '</span></td><td><div style="padding-top: 3px; padding-bottom: 5px;"><strong>' + $(this).attr("name") + "</strong><br /> " + $(this).attr("place") + '<br /><span style="font-size: 10px;">' + $(this).attr("instructor") + '</span>' + tila + '</div></td></tr></table>');

            } else {

                var tila = "";
                if ($(this).attr("space") != "0") {
                    tila = '<a style="color: #5CC800; text-decoration: none;" href="http://www.samulilindgren.net/varaapaikka.aspx?id=' + $(this).attr("id") + '">Varaa paikka</a>';
                } else {
                    tila = "tunti on täynnä";
                }
                //$("#span__" + $(this).attr("id")).html(tunti);

            }

        });

        if (classcount == 0) {
            $("#div_events_ev").html('<div>Tänään ei ole tunteja.</div>');

        }

        var eventCount = 0;
        $(xml).find("event").each(function() {
            eventCount = eventCount + 1;
            if ($("#div__ev_" + $(this).attr("id")).length == 0) {
                var link = $(this).attr("link");

                if (link == "") {
                    $("#p_events").append('<table id="div__ev_' + $(this).attr("id") + '" cellspacing="0" cellpadding="0"><tr><td valign="top" style="padding: 4px; padding-left: 0px;"><span style="font-size: 10px; color: #5CC800;"><span style="font-size: 12px;">' + $(this).attr("date") + "</span><br />" + $(this).attr("time") + '</span></td><td><div style="padding-top: 3px; padding-bottom: 5px;"><strong>' + $(this).attr("name") + "</strong><br /> " + $(this).attr("location") + '<br /><span style="font-size: 10px;">' + $(this).attr("description") + '</span><div></div></div></td></tr></table>');
                } else {
                $("#p_events").append('<table id="div__ev_' + $(this).attr("id") + '" cellspacing="0" cellpadding="0"><tr><td valign="top" style="padding: 4px; padding-left: 0px;"><span style="font-size: 10px; color: #5CC800;"><span style="font-size: 12px;">' + $(this).attr("date") + "</span><br />" + $(this).attr("time") + '</span></td><td><div style="padding-top: 3px; padding-bottom: 5px;"><a href="' + link + '" target="_blank"><strong>' + $(this).attr("name") + "</strong><br /> " + $(this).attr("location") + '<br /><span style="font-size: 10px;">' + $(this).attr("description") + '</span></a><div></div></div></td></tr></table>');
                }
                //$(this).attr("date") + ", " + $(this).attr("time") + '<br /><strong>' + $(this).attr("name") + '</strong><br />' + $(this).attr("location"));
            }
        });

        if (eventCount != 0) {
            $("#h3_lkj").html('Tapahtumat');
            $("#h3_lkj").show();
            $("#p_events").show();
        }

    });
    
    //setTimeout(function() { RefreshClasses() }, 30000);
    
}