function page_home_divAa(index, length) {
	if(length > 0) {
		index = index % length;
		if($('#page div.aa img[title=' + (index + 1) + ']').length == 0) {
			
			var img = $('<img class="display_none border-radius" src="/images/image.php/page_home_div.aa_img-' + (index + 1) + '.jpg?image=/images/page/home/div.aa_img/' + (index + 1) + '.jpg&amp;width=600&amp;height=270&amp;cropratio=600:270&amp;quality=100" title="' + (index + 1) + '">');
			
			if(index == 0) {
				$('#page div.aa').prepend(img);
			} else {
				$('#page div.aa img').eq(index - 1).after(img);
			}
			
			css();
			
			img.load(function() {
				$(this)
				.unbind()
				.css('background-image', 'url(/images/image.php/page_home_div.aa_img-' + (index + 1) + '.jpg?image=/images/page/home/div.aa_img/' + (index + 1) + '.jpg&width=600&height=270&cropratio=600:270&quality=100)')
				.attr('src', '/images/transparent.gif')
				.fadeIn(1000, function() {
					setTimeout(function() {
						page_home_divAa(++index, length);
					}, 4000);
				});
			});
			
		} else {
			$('#page div.aa img[title=' + (index + 1) + ']').fadeOut(0);
			$('#page div.aa div').before($('#page div.aa img[title=' + (index + 1) + ']'));
			$('#page div.aa img').eq(length - 1).fadeIn(1000, function() {
				setTimeout(function() {
					page_home_divAa(++index, length);
				}, 4000);
			});
		}
	}
}

$(document).ready(function() {
	$('#page').each(function() {
		
		var element = $(this).find('div.ba ul');
		
		$.post(
			'/page/home/AJAX_Google-Calendar.php',
			{},
			function(d) {
				element.animate( {
					height: 0
				}, 'fast', function() {
					
					element
					.html(d)
					.css( {
						'background-color': 'transparent',
						'background-image': 'none'
					});
					
					var heightA = 0;
					var heightB = 0;
					
					for(var i = 0; i < element.find('li').length; i++) {
						if(i < 3) {
							heightA += element.find('li').eq(i).outerHeight();
						} else {
							heightB += element.find('li').eq(i).outerHeight();
						}
					}
					
					heightB += heightA;
					
					element
					.hover(function() {
						element.find('li:gt(2)').removeClass('display_none'); // Opera fix
						element
						.stop()
						.animate( {
							height: heightB + 'px'
						}, 'fast');
						if(heightA < heightB) {
							element.parent().next('div.bb').animate( {
								height: 0
							}, 'fast');
						}
					}, function() {
						element
						.stop()
						.animate( {
							height: heightA + 'px'
						}, 'fast', function() { // Opera fix
							element.find('li:gt(2)').addClass('display_none');
						});
						if(heightA < heightB) {
							
							element.parent().next('div.bb')
							.stop()
							.addClass('visibility_hidden')
							.css('height', 'auto');
							
							var height = element.parent().next('div.bb').height();
							
							element.parent().next('div.bb')
							.css('height', 0)
							.removeClass('visibility_hidden')
							.animate( {
								height: height + 'px'
							}, 'fast', function() {
								$(this).css('height', 'auto');
							});
						}
					})
					.animate( {
						height: heightA + 'px'
					}, 'fast', function() { // Opera fix
						element.find('li:gt(2)').addClass('display_none');
					});
					
				});
			}
		);
		
		$(this).find('div.bba ul')
		.hover(function() {
			
			$(this).find('li:gt(0)').removeClass('display_none'); // Opera fix
			
			var height = 0;
			
			$(this).find('li').not('.display_none').each(function() {
				height += $(this).height();
			});
			
			$(this)
			.stop()
			.animate( {
				height: height + 'px'
			}, 'fast');
			
		}, function() {
			$(this)
			.stop()
			.animate( {
				height: $(this).find('li').not('.display_none').eq(0).height() + 'px'
			}, 'fast', function() { // Opera fix
				$(this).find('li:gt(0)').addClass('display_none');
			});
		})
		.mouseout();
		
		$(this).find('div.bbaa')
		.hover(function() {
			$(this)
			.stop()
			.animate( {
				height: '108px'
			}, 'fast')
			.find('form').eq(0).removeClass('visibility_hidden');
		}, function() {
			$(this)
			.stop()
			.animate( {
				height: '18px'
			}, 'fast', function() {
				$(this).find('form').eq(0).addClass('visibility_hidden');
			});
		})
		.removeClass('display_none')
		.find('input.text').each(function() {
			
			$(this)
			.css('width', $(this).parent().width() - 10 + 'px')
			.removeClass('display_none');
			
		});
		
		$(this).find('div.bbaa form').submit(function() {
			
			var element = $(this);
			
			if(!element.find('input.submit').hasClass('submit-disabled')) {
				element.find('input.text')
				.attr('readonly', true)
				.addClass('display_none');
				element.find('input.submit')
				.val(element.find('input.submit').val().substring(0, element.find('input.submit').val().length - 1) + 'ing')
				.addClass('submit-disabled');
				element.find('input.text')
				.css('width', element.find('input.text').parent().width() - 10 + 'px')
				.removeClass('display_none');
				switch(element.find('input.text').attr('id')) {
					case 'page_home_divBbaa_inputSubscribe':
						$.post(
							'/AJAX_query.php',
							{
								query: 'insert',
								table: 'newsletterSubscribers',
								values: {
									'email': element.find('input.text').val()
								}
							},
							function() {
								element.find('input.text').addClass('display_none');
								element.find('input.submit')
								.val(element.find('input.submit').val().substring(0, element.find('input.submit').val().length - 3) + 'ed')
								.css('background-color', 'yellowgreen');
								element.find('input.text')
								.css('width', element.find('input.text').parent().width() - 10 + 'px')
								.removeClass('display_none');
							}
						);
						break;
					case 'page_home_divBbaa_inputUnsubscribe':
						$.post(
							'/AJAX_query.php',
							{
								query: 'delete',
								table: 'newsletterSubscribers',
								values: {
									'email': element.find('input.text').val()
								}
							},
							function() {
								element.find('input.text').addClass('display_none');
								element.find('input.submit')
								.val(element.find('input.submit').val().substring(0, element.find('input.submit').val().length - 3) + 'ed')
								.css('background-color', 'yellowgreen');
								element.find('input.text')
								.css('width', element.find('input.text').parent().width() - 10 + 'px')
								.removeClass('display_none');
							}
						);
						break;
				}
			}
			
			return false;
			
		});
		
		var AJAX_validateDomain = false;
		
		$(this).find('div.bbaa input.text').keyup(function(e) {
			if(e.keyCode != 13 && !$(this).attr('readonly')) {
				
				var element = $(this);
				var domain = element.val().substring(element.val().indexOf('@') + 1, element.val().length);
				
				if(element.val().match(/^[0-9a-z_]+(\.?[+\-0-9a-z_])*\@[0-9a-z]+((\.|-)[0-9a-z]+)*\.[a-z]{2,6}$/i)) {
					clearTimeout(AJAX_validateDomain);
					AJAX_validateDomain = setTimeout(function() {
						$.post(
							'/AJAX_validate-domain.php',
							{
								domain: domain
							},
							function(d) {
								if(d == 1) {
									element.parents('form').find('input.submit').removeClass('submit-disabled');
								} else {
									element.parents('form').find('input.submit').addClass('submit-disabled');
								}
							}
						)
					}, 1000);
				} else {
					$(this).parents('form').find('input.submit').addClass('submit-disabled');
				}
				
			}
		});
		
		$(this).find('input.submit').hover(function() {
			if(!$(this).hasClass('submit-disabled')) {
				$(this).addClass('submit-hover');
			}
		}, function() {
			$(this).removeClass('submit-hover');
		});
		
		page_home_divAa(0, $(this).find('div.aa input').val());
		
	});
});
