//onready
$(function(){
    //hide the right divs
    $("#right .inside > div").hide();

    //hide all but the first image in the boxanimations
    $(".boxanimation td").each(function(index) {
        $(this).find("a img").each(function(index) {
            if(index >= 1) {
                $(this).hide();
            }
        });
    });

    //hide all but the first image in the imageslide
    $("#imageslide .inside li").each(function(index) {
        if(index > 2) {
            $(this).hide();
        }
    });

    newsticker();

    //initialize bookmarks
    if($("#bookmark").length > 0) {
        $("#bookmark").jFav();
    }

    $(".kms-chat").click(function(e) {
        e.preventDefault();
        $("#fe_chat_container_start").fadeIn(1000);
    });

    if(jQuery.url.attr("anchor") == "contact") {
        $("#contact").fadeIn(1000);
        $(".close").fadeIn(1000);
    }

    $(".contact").click(function(e) {
        e.preventDefault();
        $("#contact").fadeIn(1000);
        $(".close").fadeIn(1000);
    });

    $(".close").click(function(e) {
        e.preventDefault();
        $("#contact").fadeOut(1000);
        $(".close").fadeOut(1000);
    });
});

// Onload
window.onload = function(){

    fadeOnce("#right .inside > div", 1000);

    if($("#imageslide .inside").length > 0) {
        //random Timeouts so the three different image columns fade at random times betweeen 2 and 7 seconds
        setTimeout(function() {
            fadeImages("#imageslide .inside .first", 4000);
        }, Math.ceil((Math.random() * (7000 - 2000) + 2000)));
        setTimeout(function() {
            fadeImages("#imageslide .inside .second", 4000);
        }, Math.ceil((Math.random() * (7000 - 2000) + 2000)));
        setTimeout(function() {
            fadeImages("#imageslide .inside .third", 4000);
        }, Math.ceil((Math.random() * (7000 - 2000) + 2000)));
    }

    $(".boxanimation td").each(function(index) {
        fadeImages($(this).find("a img"), Math.ceil((Math.random() * (6000 - 2000) + 2000)));
    });

    setTimeout(function() {
        $(".overlay").animate({
            opacity: 0.8,
            height: '50px'
        }, 2000);

        $(".boxanimation td h2, .boxanimation td p").fadeOut(1000, function() {
            $(".boxanimation td h2").css({
                "bottom": "28px",
                "margin": "0"
            });
            $(".boxanimation td p").css({
                "bottom": "8px",
                "margin": "0"
            });
            $(".boxanimation td h2, .boxanimation td p").fadeIn(1000);
        });

    }, 1000);
}

var fadeOnce = function(element, time) {
    var jQEls = $(element);

    var run = function(i) {
        $(jQEls[i]).fadeIn(time, function() {
            i++;
            if(jQEls.length > i+1) {
                run(i);
            }
        });
    }
    run(0);
}

var fadeImages = function(element, time) {
    var jQEls = $(element);
    fadeTime = time
    shortFadeTime = Math.ceil(time / 2);
    if(jQEls.length > 1) {
		var run = function(i) {
			$(jQEls[i]).fadeOut(fadeTime);
			i++;
			//fade in the next image
			$(jQEls[i]).fadeIn(fadeTime, function() {
				//wait for n seconds
				setTimeout(function() {
					//check if the end of the jQEls array is reached
					if(jQEls.length == i+1) {
						//if yes fade current image out and first array image in then
						//wait for n seconds and restart call run with i set to 0
						$(jQEls[i]).fadeOut(shortFadeTime);
						$(jQEls[0]).fadeIn(shortFadeTime, function(){
							setTimeout(function() {
								run(0);
							}, fadeTime);
						});
					} else {
						//else just call "run" with the current i
						run(i)
					}
				}, fadeTime);
			});
		}
                run(0);
    }
};

//newsticker function (captain obvious strikes again)
var newsticker = function() {
    var i = 0;
    var maxSize = 0;
    var whiteSpaces = 0;
    var elements = $("#newsticker .elem > *");
    var marginLeft = parseInt($(".anim-inner").css("margin-left"));

    //calculating the overall size
    while (i < elements.length) {
        maxSize += $(elements[i]).width();
        whiteSpaces += 40;
        i++;
    }

    //calculate the complete size and the animation duration
    var wholeSize = maxSize + whiteSpaces;
    var duration = (wholeSize * 5) * 5;
    //prevent line-breaks/ carriage returns
    $(".anim-inner").css("width", wholeSize + 1);

    //run the animation
    var run = function() {
        $(".anim-inner").animate({
            marginLeft: -wholeSize + 1
        }, duration, "linear", function() {
            //reset the position and run the animation again
            $(this).css("margin-left", marginLeft);
            run();
        });
    }
    run();
}
