/* add hover effect to the main-menu items */
$(document).ready( function() {$("#nav ul.level1 > li .content > a").each(
	function(i) {
		$(this).hover(function() {
				$(this).parents('li')
					.find('.submenu').stop(true, true).slideDown('fast')
					.show()
					.trigger('submenuVisible')
					.hover( function() { $(this).stop(true, true) },
						function() { $(this).delay(200).slideUp('fast', 
							function() {
								$(this).trigger('submenuHidden');
								$(this).parent('li').removeClass('active'); 
								$(this).find('ul li').removeClass('active');
							}); 
						}
					);
				$(this).parents('li').addClass('active');
			},
			function() {
				var $submenu = $(this).parents('li').find('.submenu');
				if ($submenu.length > 0) {
					$(this).parents('li').find('.submenu').delay(200).slideUp('fast', 
						function() {
							$(this).trigger('submenuHidden');							
							$(this).parents('li').removeClass('active');
							$(this).find('ul li').removeClass('active');
						});
				} else {
					$(this).parents('li').removeClass('active');
				}
			})
	} ) 
} )

/* add hover effect to the submenu-items in the main-menu */
$(document).ready( function() {$("#nav .submenu.hovermenu a").each(
	function(i) {
		$(this).parent('li').hover( function() { 
				$(this).parent('ul').find('li').removeClass('active');
				$(this).addClass('active'); }, function() { });
	} )
} )

/* hotel browser in 'planen' menu */

var hotelBrowserTimer = null;
var visibleHotelEntity = 0;
var numHotelEntries = 0;
var hotelBrowserDelay = 5; // seconds

$(document).ready( function() { 
	initHotelBrowser();
})

function showHotelEntity($active) {
	var $contentNodes = $('#nav .submenu-content .content-entity');
	var $contentMarker = $('#nav .submenu-content .paginator span.entity');

	// $($contentNodes).hide();
	// $($contentNodes[$active]).show();
	$('#nav .submenu-content .wrapper').animate( {left: $active * -120 }, 500);
	$($contentMarker).removeClass('active');
	$($contentMarker[$contentMarker.length - $active - 1]).addClass('active');	
	visibleHotelEntity = $active;
} 

function hotelBrowserPlayPause(tag) {
	if ($(tag).hasClass('play')) {
		$(tag).removeClass('play').addClass('pause');
		stopHotelBrowserTimer();
	} else {
		$(tag).removeClass('pause').addClass('play');
		startHotelBrowserTimer();		
	}
}


function startHotelBrowserTimer() {
	if ($('#nav .paginator span.action').hasClass('play') && hotelBrowserTimer == null) {
		hotelBrowserTimer = setInterval( 'triggerTimer()', hotelBrowserDelay * 1000);
	}
}
function stopHotelBrowserTimer() {
	if (hotelBrowserTimer != null) {
		clearInterval(hotelBrowserTimer);
		hotelBrowserTimer = null;
	}
}
function triggerTimer() {
	showHotelEntity(visibleHotelEntity);
	visibleHotelEntity = (visibleHotelEntity + 1) % numHotelEntries;
}

function randomInitialHotel() { showHotelEntity( Math.floor(Math.random() * numHotelEntries) ); }

function initHotelBrowser() {
	var $contentNodes = $('#nav .content-entity');
	numHotelEntries = $contentNodes.length;
	var $contentMarker = $('#nav .paginator span.entity');
	$('#nav .paginator').append('<span class="action play"></span>');
	$('#nav .submenu-content .paginator span.action').click( function() { hotelBrowserPlayPause($(this)) } );
	for ($i = $contentNodes.length - $contentMarker.length; $i > 0; $i--) {
	  $('#nav .paginator').append('<span class="entity"></span>');
	}
	$('#nav .paginator span.entity').each( function(i) { 
	  $(this).click( function() { showHotelEntity($contentNodes.length - i - 1); } );
	})
	$('#nav .submenu-content .wrapper').css('width', 120 * numHotelEntries);
	
	$('#nav .submenu.planen').bind('submenuVisible', randomInitialHotel);
	$('#nav .submenu.planen').bind('submenuVisible', startHotelBrowserTimer).bind('submenuHidden', stopHotelBrowserTimer);
	showHotelEntity(visibleHotelEntity);
}


