$(document).ready(function() {
	var activeMainMenuItem = $('#navigation a.active');
	var activeSubMenu = activeMainMenuItem.parent().find('ul');
	var intervalID = -1;
	activeSubMenu.css('display','block');
	//console.log("activeMenuItem: " + activeMainMenuItem);

	$('#navigation ul.nav>li').hover(
		function(){
			// 
			$(this).parent().find('ul').css('display','none');
			$(this).siblings().removeClass('open');
			$(this).find('ul').css('display','block');
			$(this).addClass('open');
			clearInterval(intervalID);
			intervalID = -1;
		},
		function(){
			var self = this;
			intervalID = setInterval(function(){
				$(self).parent().find('ul').css('display','none');
				activeSubMenu.show();
				$(self).removeClass('open');
				clearInterval(intervalID);
				intervalID = -1;
			},1000);
		}
	);
	
	if($('div#sidebar').length > 0)
	{
		//$('div#sidebar').addClass('closed');
		if($('div#tracksZoom').length > 0)
		{
			$.post("../php/ajaxNews.php",{news:'test'},function(data){
				$("div#sidebar div.block ul").html(data);
			});
		}else
		{
			$.post("php/ajaxNews.php",{news:'test'},function(data){
				$("div#sidebar div.block ul").html(data);
			});
		}
			
		$('div#sidebar div.block a').click(function(event){
			$('div#sidebar').toggleClass('open');
		});
	}
	
	
	if($("a.slideShow").length > 0)
	{
		$("a.slideShow").fancybox({
			'showCloseButton' : true,
			'titlePosition' : 'inside',
			'titleFormat' : formatTitle
		});
	}
	
	if($('#newsBlock').length > 0)
	{
		$.post("php/ajaxActualNews.php",{news:'test'},function(data){
			$("div#$newsBlock p").html(data);
		});
	}
	
	// Declare the array variable for page infrastructure
	if($("#infrastructureMenu").length > 0)
	{
		//console.log("has infrastructureMenu!");
		var imgSwap = [ 
				'img/infrastructure/rlcw_WebSite_TechnAnlagen_01_111024.jpg',
				'img/infrastructure/rlcw_WebSite_TechnAnlagen_02_111024.jpg',
				'img/infrastructure/rlcw_WebSite_TechnAnlagen_03_111024.jpg',
				'img/infrastructure/rlcw_WebSite_TechnAnlagen_04_111024.jpg',
				'img/infrastructure/rlcw_WebSite_TechnAnlagen_05_111024.jpg',
				'img/infrastructure/rlcw_WebSite_TechnAnlagen_06_111024.jpg',
				'img/infrastructure/rlcw_WebSite_TechnAnlagen_07_111024.jpg',
				'img/infrastructure/rlcw_WebSite_TechnAnlagen_08_111024.jpg'];
		// Pass the array to our preload function
		$(imgSwap).preload();
	
		$("#infrastructureMenu a").hover(function() {
			$('#map-img').attr('src', $(this).attr('rel'));
		});
	}
	
	// Declare the array variable for page tracks
	if($("#tracksMap").length > 0)
	{
		//console.log("has tracksMap!");
		var imgSwap = [ 
				'img/tracks/1_Einfahrgruppe_111026.jpg',
				'img/tracks/2_LokabstellgruppeSued_111026.jpg',
				'img/tracks/3_LokabstellgruppeNord_111026.jpg',
				'img/tracks/4_Werkstattgleise_111026.jpg',
				'img/tracks/5_RichtungsgruppeNord_111026.jpg',
				'img/tracks/6_RichtungsgruppeSued_111026.jpg'];
		// Pass the array to our preload function
		$(imgSwap).preload();
	}
	
	$('a.externalLink').bind('click', function (evt) {
	       $(this).attr('target', '_blank');
	});
});
