function reload(url)
{
    if (url)
    {
        window.location.href = url;
    }
}

function resize()
{
    width = $("body").width();
    height = $("body").height();

    container_left = (width - 820) / 2;
    container_top = (height - 620) / 2;

    menu_left = (width - $("#menu").width()) / 2;
    menu_top = (height - $("#menu").height()) / 2;

    if (container_top < 0) { container_top = 0; }
    if (menu_top < 20) { menu_top = 20; }

    $("#container").css("left", container_left).css("top", container_top);
    $("#menu").css("left", menu_left).css("top", menu_top - 20);

    $("#up2").css("left", (width/2) - 20);
    $("#down2").css("left", (width/2) - 20);
    $("#left2").css("top", (height/2) - 40);
    $("#right2").css("top", (height/2) - 40);
}

function container_show()
{
    if ($("body").hasClass("dir_bottom"))
    {
        $("#container").css("top", height+10).show().animate({top: container_top}, 500);
        $("#up2").fadeOut(500);
        $("#menu").fadeOut(500);
    }
    else if ($("body").hasClass("dir_top"))
    {
        $("#container").css("top", -630).show().animate({top: container_top}, 500);
        $("#down2").fadeOut(500);
        $("#menu").fadeOut(500);
    }
    else if ($("body").hasClass("dir_right"))
    {
        $("#container").css("left", width+10).show().animate({left: container_left}, 500);
        $("#left2").fadeOut(500);
        $("#menu").fadeOut(500);
    }
    else if ($("body").hasClass("dir_left"))
    {
        $("#container").css("left", -830).show().animate({left: container_left}, 500);
        $("#right2").fadeOut(500);
        $("#menu").fadeOut(500);
    }
}

function container_hide(url)
{
    if ($("body").hasClass("dir_bottom"))
    {
        $("#container").animate({top: height+10}, 500, function() { $(this).hide(); });
        $("#up2").fadeIn(500);
    }
    else if ($("body").hasClass("dir_top"))
    {
        $("#container").animate({top: -630}, 500, function() { $(this).hide(); });
        $("#down2").fadeIn(500);
    }
    else if ($("body").hasClass("dir_right"))
    {
        $("#container").animate({left: width+10}, 500, function() { $(this).hide(); });
        $("#left2").fadeIn(500);
    }
    else if ($("body").hasClass("dir_left"))
    {
        $("#container").animate({left: -830}, 500, function() { $(this).hide(); });
        $("#right2").fadeIn(500);
    }
    $("#menu").fadeIn(500);
    if (url)
    {
        window.setTimeout(function() {
            window.setTimeout(function() {
                container_show();
            }, 1000);
            window.location.href = url;
        }, 700);
    }
}

$(function() {

    $("body").removeClass("nojs");
    $("body").addClass("js");

    $("#container").hide();
    
    date = new Date();
    ms = date.getMilliseconds();
    directions = ["dir_left", "dir_top", "dir_right", "dir_bottom"];
    dirnum = (parseInt(Math.random() * 10000) * ms) % 4;
    $("body").addClass(directions[dirnum]);
    
    $("#container")
        .append("<div id='close'></div>")
        .append("<div id='up'></div>")
        .append("<div id='down'></div>");

    $("body")
        .prepend("<div id='up2'></div>")
        .prepend("<div id='down2'></div>")
        .prepend("<div id='left2'></div>")
        .prepend("<div id='right2'></div>");

    $("#up2").hide();
    $("#down2").hide();
    $("#left2").hide();
    $("#right2").hide();

    $("#menu ul li").after("<li class='space'>|</li>");
    $("#menu ul li.space").last().remove();

    $("#container .page").hide()
    $("#container .page").first().show().addClass("active");
    
    resize();
    $(window).resize(resize);

    $("#close").click(function() {
        container_hide();
    });

    $("#menu li.active a").click( function() {
        container_show();
        return false;
    });

    $("#up2").click(container_show);
    $("#down2").click(container_show);
    $("#left2").click(container_show);
    $("#right2").click(container_show);

    if ($("#container .page").last().hasClass("active"))
        $("#down").fadeTo(0, 0.5);

    if ($("#container .page").first().hasClass("active"))
        $("#up").fadeTo(0, 0.5);

    $("#up").click(function() {
        if (!$("#container .page").first().hasClass("active"))
        {
            var active = $(".page.active");
            $(active).removeClass("active").fadeOut(100);
            $(active).prev().addClass("active").delay(110).fadeIn(100);
            $("#down").fadeTo(0, 1.0);
            if ($("#container .page").first().hasClass("active"))
                $("#up").fadeTo(0, 0.5);
        }
    });

    $("#down").click(function() {
        if (!$("#container .page").last().hasClass("active"))
        {
            var active = $(".page.active");

            $(active).removeClass("active").fadeOut(100).next().addClass("active").delay(110).fadeIn(100);

            /*$(active).removeClass("active").hide().next().addClass("active").show();*/
            $("#up").fadeTo(0, 1.0);
            if ($("#container .page").last().hasClass("active"))
                $("#down").fadeTo(0, 0.5);
        }
    });

    $("#right2").hover(
        function() { $(this).animate({left: "13px" }, 200); },
        function() { $(this).animate({left: "10px" }, 200); } );

    $("#left2").hover(
        function() { $(this).animate({right: "13px" }, 200); },
        function() { $(this).animate({right: "10px" }, 200); } );

    $("#down2").hover(
        function() { $(this).animate({top: "13px" }, 200); },
        function() { $(this).animate({top: "10px" }, 200); } );

    $("#up2").hover(
        function() { $(this).animate({bottom: "13px" }, 200); },
        function() { $(this).animate({bottom: "10px" }, 200); } );

  
    if ($("#newstrick").is("div"))
    {
        var current_id = $("#newstrick").attr("rel");
        
        // older entry
        if ($("#newstrick a[rel="+current_id+"]").next("a").is("a"))
        {
            var href = $("#newstrick a[rel="+current_id+"]").next("a").attr("href");
            var title = "Vorherige Nachricht: " + $("#newstrick a[rel="+current_id+"]").next("a").html();
            $("#container div.page").append("<div id='news_prev'><strong>&laquo; <a class='local' href='"+href+"'>Vorherige Nachricht</a></div>");
            $("#news_prev a").attr("title", title);
        }

        // newer entry
        if ($("#newstrick a[rel="+current_id+"]").prev("a").is("a"))
        {
            var href = $("#newstrick a[rel="+current_id+"]").prev("a").attr("href");
            var title = "Nachfolgende Nachricht: " + $("#newstrick a[rel="+current_id+"]").prev("a").html();
            $("#container div.page").append("<div id='news_next'><strong><a class='local' href='"+href+"'>Nachfolgende Nachricht</a> &raquo;</div>");
            $("#news_next a").attr("title", title);
        }
        
    }
    
    $("a.local").click(function() {
        container_hide($(this).attr("href"));
        return false;
    });
    
    preload_images();

    result = false;

    if (document.cookie)
    {
        var cookie = document.cookie;
        var result = cookie.match(/firstvisit=0/);
    }

    if (result)
    {
        $("#container").delay(500);
        container_show();
    }
    else
    {
        $("#menu").hide().fadeIn(500);
        container_hide();
        document.cookie = "firstvisit=0; path=/";
    }
});

function preload_images()
{
    var ImgSrc = new Array();

    ImgSrc[0] = "/assets/templates/template01/left2b.png";
    ImgSrc[1] = "/assets/templates/template01/right2b.png";
    ImgSrc[2] = "/assets/templates/template01/up2b.png";
    ImgSrc[3] = "/assets/templates/template01/down2b.png";
    ImgSrc[4] = "/assets/images/sidebar/facebook2.png";
    ImgSrc[5] = "/assets/images/sidebar/impressum2.png";
    ImgSrc[6] = "/assets/images/sidebar/pkone2.png";
    ImgSrc[7] = "/assets/images/sidebar/ycmp2.png";
    ImgSrc[8] = "/assets/images/sidebar/youtube2.png";
    ImgSrc[9] = "/assets/images/sidebar/workshop2a.png";

    var dummyImg = new Array();

    for (i = 0; i < ImgSrc.length; i++)
    {
        dummyImg[i] = new Image();
        dummyImg[i].src = ImgSrc[i];
    }
}


