$(document).ready(function() {

    $("#li_teams").css("cursor", "pointer").attr("showing", "false");
    var waiter = 0;

    $("#li_teams > a").attr("href", "javascript:;");
    $("#li_teams").click(function() {
        waiter = 0
        if ($(this).attr("showing") != "true") {
            $(this).attr("showing", "true");
            $(".NotActive").each(function() {
                waiter = waiter + 20;
                var thiss = this;
                setTimeout(function() { $(thiss).show(); }, waiter);

            });

        } else {
            $(this).attr("showing", "false");
            $(".NotActive").each(function() {
                waiter = waiter + 20;
                var thiss = this;
                setTimeout(function() { $(thiss).hide(); }, waiter);

            });

        }

    });

    InitPage();

});

function InitPage() {

    $(".event").each(function() {
        var ohr = "" + $(this).attr("href");
        $(this).removeClass("event");
        $(this).attr("hrefi", ohr);
        $(this).attr("href", "javascript:;");
        $(this).attr("target", "");
        $(this).click(function() {
            var eventID = $(this).attr("hrefi").replace("/tapahtumat/kaikkitapahtumat/", "").replace(".aspx", "").replace("http://", "").replace(document.domain, "").replace("/", "");
            showEvent(eventID);

        });

    });

    if (document.domain != "admin.latomo.fi") {

        $("*[title!='']").mouseenter(function() {

            if ($(this).attr("title") != "") {
                $("#div__helper").html($(this).attr("title"));
                $("#div__helper").css("width", "");
                var heightFix = -15; //$("#div__helper__outer").height();
                $("#div__helper").css("top", ($(this).position().top - heightFix) + "px").css("left", ($(this).position().left - 0) + "px").css("display", "block");
                $(this).attr("utitle", $(this).attr("title").replace(/\/\/\//g, "<br />"));
                $(this).attr("title", "");

                if ($("#div__helper").width() > 240) {
                    $("#div__helper").css("width", "240px");
                }

            } else if ($(this).attr("utitle") != "") {
                $("#div__helper").html($(this).attr("utitle").replace(/\/\/\//g, "<br />"));
                var heightFix = -15; //$("#div__helper__outer").height();
                $("#div__helper").css("top", ($(this).position().top - heightFix) + "px").css("left", ($(this).position().left - 0) + "px").css("display", "block");

            }

        });
        $("*[title!='']").mouseleave(function() {

            $("#div__helper").html('');
            $("#div__helper").css("display", "none");


        });

    }

}

function showEvent(id) {

    if ($("#div_image").length == 0) {

        $("body").append("<div id='div_image' style='padding: 20px; z-index: 9999; position: absolute; background-color: white; border: 1px solid gray; display: none;'></div>");
        $("body").append("<div id='div_image_back' style='z-index: 9998; position: absolute; background-color: #143242; top: -20px; left: -20px;' onclick='hideImage();'>&nbsp;</div>");

    }

    $("#div_image").css("top", "-5000px");
    $("#div_image").css("display", "");

    $("#div_image").html('<img src="/images/ajax-loader.gif" />');
    
    resizeDiv();

    $.get("/usercontrols/ajax.aspx", { tool: 'getEvent', id: id }, function(xml) {

        $(xml).find("error").each(function() {

            //alert($(this).text()); 
            location.href = '/tapahtumat/kaikkitapahtumat/' + id + '.aspx';
            hideImage();

        });

        $(xml).find("event").each(function() {

            $("#div_image").html('<div id="div_ev" style="display: none;" />');
            var pleft = parseInt(($(window).width() - 500 - 20) / 2) + "px";
            var time = $(this).attr("startdate") + " " + $(this).attr("starttime");

            if ($(this).attr("enddate") != undefined) {
                time += " - ";

                if ($(this).attr("enddate") != $(this).attr("startdate")) {
                    time += " " + $(this).attr("enddate");
                }

                if ($(this).attr("endtime") != undefined) {
                    if ($(this).attr("endtime") != $(this).attr("starttime")) {
                        time += " " + $(this).attr("endtime");
                    }
                }

            }


            $("#div_ev").html('<h3>' + time + '</h3>');


            $("#div_ev").append('<h1>' + $(this).attr("name") + '</h1>');
            $("#div_ev").append('<h4>Paikka: ' + $(this).attr("place") + '</h4>');
            try {
                $("#div_ev").append('<div style="padding-top: 10px;">' + $(this).text() + '</div>');
            } catch (e) {

            }

            $("#div_ev").append('<div id="ev_tools_' + $(this).attr("id") + '" style="border-top: 1px solid silver; margin-top: 50px;"></div>');

            try {
                if ($(this).attr("allowregisteration") == "true") {
                    $("#ev_tools_" + $(this).attr("id")).append('<div style="padding-top: 10px;"><a id="a_register" href="/usercontrols/ilmoittaudu.aspx?id=' + $(this).attr("id") + '" class="register" target="_blank">Ilmoittaudu tapahtumaan</a></div>');

                    $("#a_register").attr("hrefi", $("#a_register").attr("href"));
                    $("#a_register").attr("href", "javascript:;");
                    $("#a_register").attr("target", "");

                    $("#a_register").click(function() {

                        window.open($(this).attr("hrefi"), "ilmoittaudu", "width=500,height=700", false);

                    });

                }

            } catch (e) {

            }
            try {
                $("#ev_tools_" + $(this).attr("id")).append('<div style="padding-top: 10px;"><a href="/usercontrols/ics.aspx?event=' + $(this).attr("id") + '" class="loadevent" target="_blank">Lataa tapahtuma tiedostona</a></div>');
            } catch (e) {

            }
            $("#ev_tools_" + $(this).attr("id")).append('<div style="padding-top: 10px;"><a class="closeevent" href="javascript:hideImage();">Sulje</a></div>');
            


            $("#div_image").animate({ width: '500px', left: pleft }, 'fast', function() {
                $("#div_ev").fadeIn();
            });


        });

    });

}

function resizeDiv() {

    $("#div_image_back").fadeTo("fast", 0.01, function() {
        $("#div_image_back").css("left", "0px");
        $("#div_image_back").css("width", $(window).width() + "px");
        $("#div_image_back").css("top", "0px");

        var maxHeight = 0;
        maxHeight = $(window).height();

        if ($(document).height() > maxHeight) maxHeight = $(document).height();

        $("#div_image_back").css("height", maxHeight + "px");
        $("#div_image_back").fadeTo("fast", 0.80, function() {

            var pleft = ($(window).width() - 500 - 20) / 2;
            $("#div_image").css("left", pleft + "px");
            $("#div_image").hide();
            $("#div_image").css("top", $(window).scrollTop() + 50 + "px");
            $("#div_image").fadeIn();

        });
    });


}

function hideImage() {

    $("#div_image").fadeOut();
    $("#div_image_back").fadeTo("fast", 0.01, function() {

        $("#div_image_back").css("left", "-10px");
        $("#div_image_back").css("width", 1 + "px");
        $("#div_image_back").css("height", 1 + "px");
        $("#div_image_back").css("top", "-10px");
        $("#div_image").remove();
        $("#div_image_back").remove();
        
    });

}

