FourBalance = {
	

	gallery: function() {
		jQuery('.gallery-paragraph a')
		.click(function() {
			return false;
		})
		.each(function() {

			// Preload the images.
			var $this = jQuery(this);

			if ($this.css('display') == 'block') return;

			var img = new Image();
			img.src = $this.attr('href');

			var $viewer = $this.parents('.gallery-paragraph').children('.gallery-viewer');
			if ($viewer.length == 0) {
				$viewer = jQuery('<div class="gallery-viewer"><\/div>');
				$this.parents('.gallery-paragraph').append($viewer);
			}

			$this.hover(
				function() { // enter
					$viewer.append(img);
				},
				function() { // leave
					$viewer.empty();
				}
			);

		});
	},
	
	contact: function() {

		
		var $container 	= jQuery('<div id="ajax-footer"><\/div>');
		var $footer 	= jQuery('#footer')
		
		$container.insertBefore($footer);
		
				
		jQuery("a[href*='/termin.html']").each(function() {
			var $this = jQuery(this);
			
			$this.click(function() {
				
				if ($this.hasClass('active')) {
					$this.removeClass('active');
					
					$container.animate({height: 0},250,function() {
						$container.empty();
					});
				}
				else {
					var $elem = null;
					$elem = jQuery('.buttons a.active').removeClass('active');
					
					$elem = jQuery('.buttons a[href='+$this.attr('href')+']').addClass('active');
				
					jQuery.get($this.attr('href'), function(html) {
						var $doc = jQuery(html);
						var $html = $doc.find('.main');
						$container.html('<div class="ajax-footer-content">'+$html.html()+'<\/div>');
					
						var h = $container.children('.ajax-footer-content').height();
					
						$container.animate({top: 0, height:(h+40)+'px'},250);
						jQuery('html,body').animate({scrollTop: '9999px'});
						
						
						jQuery('#ajax-footer form').submit(function() {
							var $form = jQuery(this);
							
							if ($form.data('sent')) return false;
							$form.data('sent',true);
							$form.children().css({opacity:0.5});
							
							jQuery.post($form.attr("action"), $form.serialize(), function(ret) {
								var $doc = jQuery(ret);
								$form.parents('.form-paragraph').html($doc.find('.main').html());
								
								var h = $container.children('.ajax-footer-content').height();
								$container.animate({top: 0, height:(h+40)+'px'},250);
								
							});
							return false;
						});
					});
				}
				return false;
			});
			
		});
		
	},
	
	cycle: function() {
		
		var $images = jQuery('.image-paragraph');
		
		if ($images.length > 1) {
			
			$images.each(function() {
				
				var $para 	= jQuery(this);
				var $src	= $para.children('.img');
				
				if ($src.length) {

					var src		= $src.attr('href');
					var alt		= $src.attr('title');
					
					var img		= new Image();
					var $img	= jQuery(img);
					$img.load(function() {
						$para.prepend($img);
					});
					img.src = src;
					$img.attr('alt',alt);

				}
				
			});
			
			
		
		
			var $current = $images.eq(0);
		
			var $container = $current.parent();
		
			$container.css({height: $current.height() + "px"});
		
			setInterval(function() {
			
				var $next = $current.next('.image-paragraph');
			
				if ($next.length == 0) {
					$next = $current.parent().children('.image-paragraph').eq(0);
				}
			
				var heightBefore	= $current.height();
				var heightAfter		= $next.height();
			
			
				var resizeTimeout	= 500;
				var fadeTimeout		= 10;
			
				if (heightAfter < heightBefore) {
					fadeTimeout		= 300;
					resizeTimeout	= 10;
				}
			
			
				// Fade
				setTimeout(function() {

					$current.css({top: 0, position: 'absolute', zIndex: 0});
					$next.css({position: 'relative', zIndex: 1}).fadeIn(500, function() {
						$current.hide();
						$current = $next;
					});
				
				}, fadeTimeout);
			
				// Resize
				setTimeout(function() {
					$container.animate({
						height: heightAfter+'px'
					},300);
				}, resizeTimeout);
			
			
			}, 3000);
		
		}
		
	}
	
		
}




jQuery(function() {
	
	
	if (jQuery.browser.msie) {
		jQuery(document.body).addClass('ie');
	}
	else if (jQuery.browser.mozilla) {
		jQuery(document.body).addClass('mozilla');
	}
	else if (jQuery.browser.safari) {
		jQuery(document.body).addClass('webkit');
	}

	FourBalance.gallery();   
	FourBalance.contact();
	FourBalance.cycle();
	
	jQuery('.buttons a').focus(function() { jQuery(this).blur() })
	
	
});
