﻿// KIRJOITETAAN TÄHÄN DOKUMENTTIIN
// TÄMÄN PÄIVÄN TAPAHTUMIEN HAKEMINEN

$(document).ready(function() {

    $("#div_events").html('');
    $("#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);

        });
    }

    if ($("#div_current_image").length != 0) {
        //JOS LÖYTYY KUVAROTAATTORI :)
        $(".hf_rotator").attr("showing", "false");
        $(".hf_curr").attr("showing", "true");
$("#div_current_image").attr("link", "");

$("#div_current_image").click(function() {

	if($(this).attr("link") != "") {
		window.open($(this).attr("link"));
}

});

if($(".hf_curr").attr("title") != "") {
$("#div_current_image").css("cursor", "pointer");
$("#div_current_image").attr("link", $(".hf_curr").attr("title"));
}
        setTimeout(function() { showNextRotation(); }, 7500);

    }
});

var currentRotation = 0;
function showNextRotation() {
    //alert(currentRotation);
    var curr = 0;
    var found = false;
    $(".hf_rotator[showing=false]").each(function() {

        if (!found) {
            found = true;
            //alert($(this).val().replace("_thumb", ""));
var image =$(this).val().replace("_thmb", "");
var title = $(this).attr("title");

            $("#div_current_image").fadeOut(function() {
	$("#div_current_image").css("background-image", "url(" + image + ")");
$("#div_current_image").fadeIn();
$("#div_current_image").attr("link", title);
if( title != "") {
$("#div_current_image").css("cursor", "pointer");
} else {
$("#div_current_image").css("cursor", "default");
}

});
            $(this).attr("showing", "true");
            currentRotation = currentRotation + 1;
            setTimeout(function() { showNextRotation(); }, 7500);

        }

    });

    if (!found) {
        $(".hf_rotator").attr("showing", "false");
        showNextRotation();
    }

}

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; color: black;"><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; color: black;"><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" style="color: black;"><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);
    
}
