// JavaScript Document
$(function(){

	// menu
	$('#nav > li:last').addClass('last');

});

jQuery(document).ready(function() {
	jQuery('#videoNavi').jcarousel();

	$('.footerTeaserBox:last, .teaserContainerBottom .teaser:last').addClass('last-child');

	$('.accordion').accordion();
	$('.accordion').accordion({
		header: 'h4',
		autoHeight: false,
		collapsible: true,
		active: false,
		icons: {
			'header': 'accordionDefault',
			'headerSelected': 'accordionSelected'
		},
		change: function(event, ui) {
			var childAccordions = ui.newContent.find('.accordion');

			var animated = childAccordions.accordion('option', 'animated');
			childAccordions.accordion('option', 'animated', false);
			childAccordions.accordion('activate', false);
			childAccordions.accordion('option', 'animated', animated);
		}
	});

	$('a.layer, a.layerLink').fancybox({
		scrolling: 'no',
		titleShow: false,
		autoScale: true,
		type: 'ajax'
	});

	$('.defaultValue').each(function() {
		$(this).data('defaultValue', $(this).val());
	});

	$('.defaultValue').focus(function() {
		var value = $(this).val();
		if (value == $(this).data('defaultValue')) {
			$(this).val('');
		}
		else if (value == '') {
			$(this).val($(this).data('defaultValue'));
		}
	});

	$('.defaultValue').blur(function() {
		if ($(this).val() == '') {
			$(this).val($(this).data('defaultValue'));
		}
	});

	$('.bookmark').click(function(event) {
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel($('head title').text(), window.location.href, "");
		}
		else if (window.opera && window.print) {
			// opera
			var elem = document.createElement('a');
			elem.setAttribute('href', url);
			elem.setAttribute('title', title);
			elem.setAttribute('rel', 'sidebar');
			elem.click();
		}
		else if (document.all) {
			// ie
			window.external.AddFavorite(window.location.href, $('head title').text());
		}

		event.preventDefault();
		return false;
	});

	$('#fancybox-wrap form').live('submit', function(event) {
		event.preventDefault();

		var form = $(this);

		// recommend form
		if (form.find('#recommendURL').length > 0) {
			form.find('#recommendURL').val(window.location.href);
		}

		$.ajax({
			type: 'POST',
			url: form.attr('action'),
			data: form.serialize(),
			success: function(formHTML) {
				form.parents('.layer').parent().html(formHTML);
			},
			dataType: 'html'
		});

		return false;
	});

	$('.modVideo').each(function() {
		openYouTubeVideo($(this).find('#videoNavi a:eq(0)'));
	});

	// cheats
	$('#mailformNr, #mailformOrt').each(function() {
		var parentContainer = $(this).parent().prev();
		parentContainer.append('&nbsp;');
		$(this).appendTo(parentContainer);
	})
});

$(window).load(function() {
	$('.ui-accordion-content').css('height', 'auto');
});

function openYouTubeVideo(aTag) {
	var youTubeID = aTag.attr('rel');
	var videoHTML = '<object style="height: 390px; width: 640px">\
			<param name="movie" value="http://www.youtube.com/v/' + youTubeID + '?version=3" />\
			<param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always" />\
			<embed src="http://www.youtube.com/v/' + youTubeID + '?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="500" height="405" />\
		</object>';
	aTag.parents('.modVideo').find('.playerContainer').html(videoHTML);
}

var Stage = {
	object: null,
	numberOfItems: null,
	currentTimer: null,
	currentIndex: null,

	init: function() {
		this.object = $('#buehneSlider');
		if (this.object.length > 0) {
			// clone first and last item
			this.object.prepend(this.object.find('li:last').clone());
			this.object.append(this.object.find('li:eq(1)').clone());

			this.numberOfItems = this.object.find('li').length;

			this.object.find('li').css({
				float: 'left'
			});
			this.object.css({
				position: 'relative',
				width: this.object.find('li:first').outerWidth() * this.numberOfItems
			});

			$('#sliderContainer').hover(function() {
				Stage.stopAnimation();
			},
			function() {
				Stage.startAnimation();
			});

			this.moveTo(1, false);
			this.startAnimation();

			$('#jcarousel-prev').click(function() {
				Stage.moveTo('previous');
			});

			$('#jcarousel-next').click(function() {
				Stage.moveTo('next');
			});
		}
	},

	startAnimation: function() {
		this.stopAnimation();
		this.currentTimer = window.setTimeout(function() {
			Stage.moveTo('next');
		}, 8000);
	},

	stopAnimation: function() {
		window.clearTimeout(this.currentTimer);
	},

	moveTo: function(index, animated) {
		if ('undefined' == typeof animated) {
			animated = true;
		}

		if (index == 'next') {
			this.moveTo(this.currentIndex + 1);
		}
		else if (index == 'previous') {
			if (this.currentIndex > 0) {
				this.moveTo(this.currentIndex - 1);
			}
			else {
				this.moveTo(this.numberOfItems - 1);
			}
		}
		else {
			var item = this.object.find('li:eq(' + index + ')');
			if (item.length > 0) {
				if (animated) {
					this.object.animate({
						left: item.position().left * -1
					}, 400, function() {
						Stage.adjustPosition(index);
					});
				}
				else {
					this.object.css({
						left: item.position().left * -1
					});
					this.adjustPosition(index);
				}
			}
			else {
				this.moveTo(1);
			}
		}
	},

	adjustPosition: function(index) {
		// first element jump to last
		if (index == 0) {
			this.object.css({
				left: this.object.find('li:eq(' + (this.numberOfItems - 2) + ')').position().left * -1
			});
			index = this.numberOfItems - 2;
		}
		// last element jump to first
		else if (index == this.numberOfItems - 1) {
			this.object.css({
				left: this.object.find('li:eq(1)').position().left * -1
			});
			index = 1;
		}

		$('.sliderNavigation a').removeClass('sliderControlActive');
		$('.sliderNavigation a:eq(' + (index - 1) + ')').addClass('sliderControlActive');

		this.currentIndex = index;
		this.startAnimation();
	}
}

$(function(){
	Stage.init();
});

