﻿// KIRJOITETAAN TÄHÄN DOKUMENTTIIN
// TÄMÄN PÄIVÄN TAPAHTUMIEN HAKEMINEN

$(document).ready(function() {

    $("#div_events").html('<h3>Tunnit tänään</h3>');
    $("#div_events").append('<div id="div_events_ev" style="padding-bottom: 20px;"></div>');

    $("#div_events").ajaxError(function(e, xhr, settings, exception) {
    //alert(xhr.responseText);
    });

    RefreshClasses();


});

function RefreshClasses() {

    var classcount = 0;

    $.get("tanaan.aspx", { tool: 'getClasses' }, function(xml) {
        //$("#div_events_ev").html('');
        //alert(xml);

        $(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: hotpink; text-decoration: none;" href="http://www.maijagrace.net/varaa_tunnittunnintiedot.aspx?id=' + $(this).attr("id") + '">Varaa paikka</a>';
                } else {
                    tila = "<span style='color: silver;'>Kysy paikkoja!!!</span>";
                }
                $("#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: hotpink;">' + $(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><div><span id="span__' + $(this).attr("id") + '" style="font-size: 10px; color: silver;">(' + tila + ')</span></div></div></td></tr></table>');

            } else {

                var tila = "";
                if ($(this).attr("space") != "0") {
                    tila = '<a style="color: hotpink; text-decoration: none;" href="http://www.maijagrace.net/varaa_tunnittunnintiedot.aspx?id=' + $(this).attr("id") + '">Varaa paikka</a>';
                } else {
                    tila = "Kysy paikkoja!";
                }
                $("#span__" + $(this).attr("id")).html(tunti);

            }

        });

        if (classcount == 0) {
            $("#div_events_ev").html('<div>Tänään ei ole tunteja. Katso tulevat tunnit lukujärjestyksestä!</div>');

        }

        var eventCount = 0;
        $(xml).find("event").each(function() {
            eventCount = eventCount + 1;
            if ($("#div__ev_" + $(this).attr("id")).length == 0) {
                $("#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: hotpink;"><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>');
                //$(this).attr("date") + ", " + $(this).attr("time") + '<br /><strong>' + $(this).attr("name") + '</strong><br />' + $(this).attr("location"));
            }
        });

        if (eventCount != 0) {
            $("#h3_lkj").html('Tapahtumat ja viikkolukujärjestys');
            $("#p_events").show();
        }


        $("#div_events").show();


    });
    
    setTimeout(function() { RefreshClasses() }, 30000);
    
}
