Date.prototype.setISO8601 = function(dString){
    var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;

    if (dString.toString().match(new RegExp(regexp))) {
		var d = dString.match(new RegExp(regexp));
		var offset = 0;

		this.setUTCDate(1);
		this.setUTCFullYear(parseInt(d[1],10));
		this.setUTCMonth(parseInt(d[3],10) - 1);
		this.setUTCDate(parseInt(d[5],10));
		this.setUTCHours(parseInt(d[7],10));
		this.setUTCMinutes(parseInt(d[9],10));
		this.setUTCSeconds(parseInt(d[11],10));
		if (d[12])
			this.setUTCMilliseconds(parseFloat(d[12]) * 1000);
		else
			this.setUTCMilliseconds(0);
		if (d[13] != 'Z') {
			offset = (d[15] * 60) + parseInt(d[17],10);
			offset *= ((d[14] == '-') ? -1 : 1);
			this.setTime(this.getTime() - offset * 60 * 1000);
		}
    }
    else {
		this.setTime(Date.parse(dString));
    }
    return this;
};

function setSizes() {
    var w = $(window);
    var d = $(document);
    
    $("#tb_img").width(d.width());
    
    //$("#bg_img").height($(document).height());
    /*if($(window).width() < 1000) {
        alert("test");
    }*/
    if(w.width() > 1600) {
        $(".background-image").width(d.width());
        //$("#bg_img").width(w.width()).height(w.height());
    } else {
        $(".background-image").height(d.height());
    }
}

function toggleFullscreen() {
    if($("#menu").is(":visible")) {
        $("#menu").fadeOut('slow');
        $("#content").fadeOut('slow');
        $("#footer").fadeOut('slow');
        $("#resize img").attr("src", "/images/resize_in.png");
    } else {
        $("#footer").fadeIn('slow');
        $("#content").fadeIn('slow');
        $("#menu").fadeIn('slow');
        $("#resize img").attr("src", "/images/resize.png");
    }
}

var selectedBackground = 0;
var toggleInterval = 0;
var activeLayer = 0;
var default_zIndex = -10;

function getNextBackground() {
    if(backgrounds.length <= selectedBackground) {
        selectedBackground = 0; 
    }
    
    //console.log(backgrounds[selectedBackground]);
    return "/images/backgrounds/" + backgrounds[selectedBackground++];
}

function toggleLayer() {
    activeLayer = toggle(activeLayer);
    return activeLayer;
}

function toggle(s) {
    return (s?0:1);
}

function toggleBackground() {
    var next_layer = toggleLayer();
    
    var img1 = $($(".background-image").get(next_layer));
    var img2 = $($(".background-image").get(toggle(next_layer)));
    
    img1.css('z-index', default_zIndex);
    img2.css('z-index', default_zIndex-1);
    
    img2.attr("src", getNextBackground());
    img2.css('display', 'block');
    img1.fadeOut("slow")
    
    if(backgrounds.length > 1 && toggleInterval == 0) {
        toggleInterval = setInterval("toggleBackground()", 7000);
    }
}

$(document).ready(function() {
    $('a.lightbox').lightBox();
    
    toggleBackground();
    
    $('input[name="next"]').click(function() {
        //alert("click");
        $(this).parents("li").removeClass("active-page").next("li").addClass("active-page");
    });
    
    $('input[name="back"]').click(function() {
        //alert("click");
        $(this).parents("li").removeClass("active-page").prev("li").addClass("active-page");
    });
    
    $("#mycarousel").jcarousel({
        auto: 3, scroll: 1,
        wrap: "circular",
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
    
    var carousel = $('#mycarousel').data('jcarousel')
    
    $('.jcarousel-left-button').bind('click', function() {
        carousel.prev();
        return false;
    });

    $('.jcarousel-right-button').bind('click', function() {
        carousel.next();
        return false;
    });
    
    $(window).resize(function() {
        setSizes();
    });
    
    $('#logo').click(function() {
        window.location.href=home_link;
    });
    
    $(".weather_box").weather({city: "Obernzell"});
    
    if($("#content").height() > 400) {
        $("#bottom_box_3").hide();
        $("#footer-slogan").hide();
    } else if($("#content").height() > 340) {
        $("#footer-slogan").hide();
    }
    
    setSizes();
});
