
(function($){
	$.fn.bottom = function(options) {

		var defaults = {
			// how close to the scrollbar is to the bottom before triggering the event
			proximity: 0
		};

		var options = $.extend(defaults, options);

		return this.each(function() {
			var obj = this;
			$(obj).bind("scroll", function() {
				if (obj == window) {
					scrollHeight = $(document).height();
				}
				else {
					scrollHeight = $(obj)[0].scrollHeight;
				}
				scrollPosition = $(obj).height() + $(obj).scrollTop();
				if (((scrollHeight - scrollPosition) / scrollHeight <= options.proximity) ||
                    (scrollHeight - (scrollPosition + 1)) / scrollHeight <= options.proximity) {
					$(obj).trigger("bottom");
				}
			});

			return false;
		});
	};
})(jQuery);

$(document).ready(function() {
    // Header animation
    $('.header_m').cycle({
        fx: 'fade',
        timeout: 6300,
        speed: 1000
    });
    $('.header-logo').hover(function() {
        $('.header_m').stop(true, true).fadeTo(300, 1);
    }, function() {
        $('.header_m').stop(true, true).fadeTo(300, 0.5);
    });
    $('.header_m').fadeTo(1, 0.5);
    
    // Fancybox
    $('a.scalable').attr('rel', 'gallery').fancybox({
        loop: false,
        helpers: {
            title: {
                type: 'inside'
            }
        }
    });
    
    // Sidebar img-link fade
    $('.sidebar a img').hover(function() {
        $(this).stop().fadeTo(400, 1);
    }, function() {
        $(this).stop().fadeTo(400, 0.5);
    }).fadeTo(1, 0.5);
    
    // Title-overlay animation
    $('.gallery-elem, ul.events li').hover(function() {
        $('.title-overlay', this).stop(true, true).slideDown('fast');
    }, function() {
        $('.title-overlay', this).stop(true, true).slideUp('fast');
    });
    $('.title-overlay').hide();
    
    // Submenu animation
    $('.navi > ul > li').removeClass('submenu');
    $('.navi > ul > li').hover(function() {
        $('ul', this).stop(true, true).show(350);
    }, function() {
        $('ul', this).stop(true, true).delay(100).hide(350);
    });
    $('.pagination').hide();
        
    $('#bookform-link').click(function(e) {
        e.preventDefault();
        var $this = $(this);
        $.get('ajax.php?action=getBookForm', function(xhr) {
            var formDiv = $('<div id="bookform"></div>').css('display', 'none');
            formDiv.append(xhr);
            $this.after(formDiv).hide(300);
            formDiv.show(400);
        });
    });
    
    $('#bookform form').live('submit', function(e) {
        e.preventDefault();
        var $form = $(this);
        $.post('ajax.php?action=postBook', $form.serialize(), function(xhr) {
            if(xhr.success != 1) {
                var msg = '<h2>Fehler</h2><ul class="error">';
                $.each(xhr.msg, function(i, v) {
                    msg += '<li>' + v + "</li>";
                });
                msg += '</ul>';
                $.fancybox(msg);
            } else {
                $('.book-entry:first').before(xhr.html);
                $form[0].reset();
                $form.fadeOut(function() {
                    $('.book-entry:first').fadeIn();
                    $('#bookform-link').fadeIn();
                });
            }
        }, 'json');
    });
        
    $('#ajax-loading').ajaxStart(function() {
        $(this).fadeTo(300, 0.7);
    }).ajaxStop(function() {
        $(this).hide();
    });
    
    $('#nextTerm').click(function(e) {
        e.preventDefault();
        var  url = $(this).attr('href'),
            load = new reload();
        load.url = 'ajax.php?action=nextTerm&date=' + url;
        load.nextTerm($(this));
    });
    
});

function reload() {}
reload.prototype = {
    url:    'ajax.php',
    loading: false,
    bookEntries: function() {
        if (!this.loading) {
            this.loading = true;
            $.get(this.url, function(xhr) {
                if (xhr != "") {
                    $(".book-entry:last").after(xhr);
                    $('.book-entry').fadeIn();
                } else {
                    $(window).unbind('bottom');
                }
                this.loading = false;
            }, 'html');
        }
    },
    nextTerm: function($this) {
        $.get(this.url, function(xhr) {
            if(xhr != false) {
                $item = $('<li></li>');
                var term  = '<strong>' + xhr.datum + '</strong><br>';
                    term += xhr.text;
                $item.html(term)
                .appendTo('ul.calender')
                .fadeTo(0, 0)
                .css('margin-bottom', '-' + $item.outerHeight() + 'px')
                .animate({
                    'margin-bottom': 0
                }, 400, function() {
                    $item.fadeTo(400, 1);
                });
                $this.attr('href', xhr.date);
            } else {
                $this.fadeOut(function() {
                    $this.remove();
                });
            }
        }, 'json');
    }
}

