﻿if (typeof (ZAK) == "undefined")
{ ZAK = { __namespace: true }; }



ZAK.Program = {

    data: undefined,

    initList: function ()
    {
        ZAK.Program.loadData(function ()
        {
            ZAK.Program.showEventsList();
        });
    },

    init: function ()
    {
        $("#eventContent").hide();

        $(document).keydown(function (e)
        {
            if (e.keyCode == 39)
            {
                location.hash = ZAK.Program.getNextEventId();
            }

            if (e.keyCode == 37)
            {
                location.hash = ZAK.Program.getPrevEventId();
            }

        });

        ZAK.Program.loadData(function ()
        {
            setInterval(ZAK.Program.showEvent, 250);
        });
    },



    bgNames: Array(),
    bgImages: Array(),

    preloadBackgrounds: function ()
    {
        for (i = 0; i < ZAK.Program.data.length; i++)
        {
            var bg = ZAK.Program.data[i].background;

            if (bg == "")
                continue;

            if ($.inArray(bg, ZAK.Program.bgNames) >= 0)
                continue;

            ZAK.Program.bgNames.push(bg);

            var img = new Image();           
            img.src = bg;

            ZAK.Program.bgImages.push(img);
        }
    },


    loadData: function (loadedCallback)
    {
        $.ajax({
            url: 'json.php',
            dataType: 'json',
            error: function (data, status, ex)
            {
                alert(status + ": " + ex);
            },
            success: function (data)
            {
                ZAK.Program.data = data;
                loadedCallback();
            }
        });
    },



    showEventsList: function ()
    {
        var html = "";

        for (i = 0; i < ZAK.Program.data.length; i++)
        {
            var event = ZAK.Program.data[i];
            var dateShort = event.eventDate.substring(8, 10) + "." + event.eventDate.substring(5, 7) + ".";

            var firstLast = "";

            if (i == 0)
                firstLast = " first";

            if (i == ZAK.Program.data.length - 1)
                firstLast = " last";

            html += "<div class=\"row eventRow\">";
            html += " <div class=\"left eventDate" + firstLast + "\">" + dateShort + "</div>";
            html += " <div class=\"right eventName" + firstLast + "\"><a href=\"program.html#" + i + "\">" + event.name + "</a></div>";
            html += "</div>";
        }


        $("#eventList").html(html);
    },

    loadedEvent: -1,
    showEvent: function ()
    {
        var eventId = ZAK.Program.getHashEventId();

        if (eventId == ZAK.Program.loadedEvent)
            return;

        if (eventId >= ZAK.Program.data.length)
            return;

        var event = ZAK.Program.data[eventId];
        var dateShort = event.eventDate.substring(8, 10) + "." + event.eventDate.substring(5, 7) + ".";

        $("#curEventDate").html(dateShort);

        var html = "<h1 id=\"eventTitle\">" + event.name + "</h1>";

        if (event.subtitle != "" && event.subtitle != null)
        {
            html += "<h2>" + event.subtitle + "</h2>";
        }

        if (event.text == null)
            event.text = "";

        if (event.text.length > 600)
        {
            var firstPartInitLength = Math.floor(event.text.length * 2 / 3);
            var text1 = event.text.substring(0, firstPartInitLength);

            text1 = text1.substring(0, text1.lastIndexOf(" "));

            var text2 = event.text.substring(text1.length, event.text.length);
        }
        else
        {
            var text1 = event.text;
            var text2 = "";
        }
        
        text1 = text1.replace(/\n/g, "<br />");
        text2 = text2.replace(/\n/g, "<br />");

        html += "<div class=\"col\"><p>" + text1 + "</p></div>";
        html += "<div class=\"col\">";

        if (text2 != "")
        {
            html += "<p>" + text2 + "</p>";
        }

        html += "<p>";

        if (event.doors.substring(0, 5) != "00:00")
            html += "T&uuml;r: " + event.doors.substring(0, 5);

        if (event.priceAK != "" && event.priceAK != null)
            html += " / AK: " + event.priceAK + ".-";

        if (event.priceVVK != "" && event.priceVVK != null)
            html += " / VVK: " + event.priceVVK + ".-";

        if (event.age != "" && event.age != null)
            html += " / Ab  " + event.age + " J.";

        html += "</p>";

        if (event.links != "" && event.links != null)
        {
            html += "<p>";

            var links = event.links.split("\r\n");

            for (i = 0; i < links.length; i++)
            {
                var l = links[i].split("|");
                html += "<a href=\"" + l[1] + "\" target=\"_blank\">" + l[0] + "</a><br />";
            }

            html += "</p>";
        }

		  if(event.vvkUrl != "" && event.vvkUrl != null)
		  {
		  		html += "<span id=\"vvk\">";
		  		html += "<a href=\"" + event.vvkUrl + "\" target=\"_blank\">";
		  		html += "Tickets: <img src=\"img/starticket_small.png\" />";
		  		html += "</a></span>";
		  }

		  if(event.facebookEvent != "" && event.facebookEvent != null)
		  {
		  		html += "<span id=\"facebookEvent\">";
		  		html += "<a href=\"http://www.facebook.com/event.php?eid=" + event.facebookEvent + "\" target=\"_blank\">";
		  		html += "Facebook Event: <img src=\"img/facebook_event.png\" />";
		  		html += "</a></span>";
		  }

        html += "</div>";

        if (event.youtube != "" && event.youtube != null)
        {
            html += "<p class=\"youtube\">";
            html += "<iframe width=\"610\" height=\"370\" src=\"http://www.youtube.com/embed/" + event.youtube + "\" frameborder=\"0\" allowfullscreen></iframe>";
            html += "</p>";
        }

        $("#eventContent").html(html);
        $("#nextEvent").attr("href", "#" + ZAK.Program.getNextEventId());
        
        var background = "default.jpg";

        if (event.background != "")
        {
            background = event.background;
        }

        $("body").css("background-image", "url('img/background/" + background + "')");

		  if(event.brightBackground == 1)
		  {
		  		$("#hint").css("color", "#555555");
		  		$("#menu a").css("color", "#555555");
		  		$("#hintCaption").css("color", "#555555");
		  		$("#nextEvent").css("color", "#555555");
		  		$("#allEventsCaption").css("color", "#555555");
		  		$("#curEventDate").css("color", "#555555");
		  		$("body").css("background-color", "white");
        		$("#eventContent").addClass("opaque");
        		$("#nextEvent").addClass("opaque");
		  }
		  else
		  {
		  		$("#hint").css("color", "");
		  		$("#menu a").css("color", "");
		  		$("#hintCaption").css("color", "");
		  		$("#nextEvent").css("color", "");
		  		$("#allEventsCaption").css("color", "");
		  		$("#curEventDate").css("color", "");
		  		$("body").css("background-color", "");		  
        		$("#eventContent").removeClass("opaque");
        		$("#nextEvent").removeClass("opaque");
		  }

        $("#loading").hide();
        $("#eventContent").show();

        ZAK.Program.preloadBackgrounds();
        ZAK.Program.loadedEvent = eventId;
    },

    getHashEventId: function ()
    {
        var eventId = 0;

        if (location.hash != undefined && location.hash != "")
        {
            var id = location.hash.replace(/#/g, "");

            if (!isNaN(id) && id != "")
                eventId = id;

        }

        return eventId;
    },

    getNextEventId: function ()
    {
        var nextEventId = parseInt(ZAK.Program.getHashEventId()) + 1;

        if (nextEventId >= ZAK.Program.data.length)
            nextEventId = 0;

        return nextEventId;
    },

    getPrevEventId: function ()
    {
        var prevEventId = parseInt(ZAK.Program.getHashEventId()) - 1;

        if (prevEventId < 0)
            prevEventId = ZAK.Program.data.length - 1;

        return prevEventId;
    },

    __namespace: true
}
