jQuery(
	function($) {
		var
			$body = $('body').addClass('js'),

		// retrieve language from body class
			regExpLanguage = /language_([a-z]{2})/,
			matchesLanguage = $body.attr('class').match(regExpLanguage),
			language = matchesLanguage[1],
			screenHeight = window.screen.height || 9999;

		// handle animated head closing
		(
			function() {
				var
					$head = $('#head'),
					$scene = $('#scene', $head),
					cookieName = 'zippels_head_closed',
					keepHidden = 'keep-hidden',
					headIsClosed = '1' == $.cookie(cookieName),
					openHeight = $('img', $scene).attr('height'),
					closeHeightThreshold = 845;

				$scene.data('open-height', openHeight);
				$scene.data('closed-height', 230);

				$('.features', $head)
					.bind(
						'click',
						function(e, data) {
							var
								$target = $(e.target),
								duration = (data ? data.duration : 'normal');

							if ($target.is('a')) {
								if ($target.hasClass('close')) {
									if ($head.hasClass('open')) {
										$scene
											.animate(
												{
													height: $scene.data('closed-height')
												},
												duration
											);
										$head.removeClass('open');
										$.cookie(cookieName, 1, { path: '/' });
									} else {
										$scene
											.animate(
												{
													height: $scene.data('open-height')
												},
												duration
											);
										$head.addClass('open');
										$.cookie(cookieName, 0, { path: '/' });
									}
								}
								return false;
							}
						}
					);
			}
		)();

		// handle image sliding
		(
			function()
			{
				var
					$container = $('#scene'),
					$navigation = $('.navigation', $container),
					$images = $('img', $container),
					length = $images.length,
					imagesLoading = length,
					currentlyVisibleIndex = (length-1),
					$currentImage,
					currentImageWidth,
					o =
						{
							duration: 'normal',
							pause: 5000
						},
					slideNext,
					queueNext,
					imageLoadedHandler;

				// if there's no image or only one image, don't add sliding animations
				if(2 > length)
				{
					return;
				}

				slideNext =
					function()
					{
						$currentImage = $images.eq(currentlyVisibleIndex);
						currentImageWidth = $currentImage.width();
						currentlyVisibleIndex = ++currentlyVisibleIndex % length;

						// get next image element, adjust its horizontal position,
						// move it to the end of the image container, then slide it in
						$images.eq(currentlyVisibleIndex)
							.css('left', currentImageWidth)
							.insertAfter($currentImage)
							.animate(
								{
									left: 0
								},
								{
									complete: queueNext,
									duration: o.duration
								}
							);
						// at the same time, slide the current image out to the left
						$currentImage
							.animate(
								{
									left: -currentImageWidth
								},
								{
									duration: o.duration
								}
							);
					};

				queueNext =
					function()
					{
						setTimeout(slideNext, o.pause);
					};

				imageLoadedHandler =
					function()
					{
						if(!--imagesLoading)
						{
							queueNext();
						}
					};

				$images
					.each(
						function()
						{
							var image = new Image();
							image.onload = imageLoadedHandler;
							image.src = $(this).attr('src');
						}
					);
			}
		)();

		// handle sliders
		(
			function() {
				$('.imageSlider')
					.each(
						function() {
							var
								$this = $(this),
								$items = $('.tray', this),
								current = 0,
								$next = $('.next', this),
								offsets = [],
								$listItems = $('.mask .tray li', this),
								visibleItemsCount = (!!$listItems.length ? Math.round($('.mask', this).width() / $listItems.width()) : 1),
								$previous = $('.last', this),
								slideTo =
									function(offset) {
										$items
											.animate(
												{
													left: offset
												},
												'normal'
											);
									};

							if ($items.length) {
								// cache sliding positions
								$listItems
									.each(
										function() {
											offsets.push(-$(this).position().left);
										}
									);

								if (0 == current) {
									$previous.addClass('inactive');
								}

								if (offsets.length == current + 1) {
									$next.addClass('inactive');
								}

								$this
									.click(
										function(e) {
											if ($previous.get(0) == e.target) {
												if (0 == current) {
													// we're already all the way to the left
													return false;
												} else {
													if (--current == 0) {
														// we have now reached the left side
														// => deactivate left-navigation
														$previous.addClass('inactive');
													}
													$next.removeClass('inactive');
												}
												slideTo(offsets[current]);
												return false;
											} else if ($next.get(0) == e.target) {
												if (offsets.length-visibleItemsCount == current) {
													// we're already all the way to the right
													return false;
												} else {
													if (offsets.length-visibleItemsCount == ++current) {
														// we have now reached the right side
														// => deactivate right-navigation
														$next.addClass('inactive');
													}
													$previous.removeClass('inactive');
												}
												slideTo(offsets[current]);
												return false;
											}
										}
									);
							}
						}
					);
			}
		)();
	}
);
