var activeMainNavi;
var activeSubNavi;
var hoveredSubNavi;

$(document).ready(function() {

	activeMainNavi = $( '#navigation .active' );
	activeMainNavi = getActivePage();
	//console.log(activeMainNavi);
	activeSubNavi = $( '#subnavigation #sub_' + activeMainNavi.attr('id') );
	
	activateNaviImage( activeMainNavi.find( 'a img' ) ); 	
	
	if( activeSubNavi != undefined ) {
		activeSubNavi.show();
		
		activeSubNaviPage = getActiveSubPage();		
		if( activeSubNaviPage != undefined 
			&& activeSubNaviPage != null
			&& activeSubNaviPage != '' ) {
			
			activateNaviImage( activeSubNaviPage.find( 'a img' ) ); 
		}
	}

	$( '#navigation .item a img' ).hover(
		function () {
			activateNaviImage( $(this) );							
       	}, 
       	function () {
		
			var navItem = $( this ).parent().parent();
			
			if ( navItem.attr('id')  == activeMainNavi.attr('id')  ) {				
				return;
			}		
				
	    	href = $( this ).attr( 'src' );
			href = href.replace( 'active', 'inactive' );
			$( this ).attr( 'src', href );
			
			if( !navItem.parent().hasClass( 'subitems' ) 
					&& activeMainNavi != navItem ) {
				//$( '#subnavigation #sub_' + navItem.attr('id') ).hide();				
				//activateNaviImage( activeMainNavi.find( 'a img' ) );				
			} 
	    }	
	);	
	
	$( '#thumbs a' ).click(function () {   
	  $( 'div#middleColumn' ).css (  { "background-image": "url( ../"+ $(this).attr('href') + ")" } );	  
	  return false;
    });
	
	$( '#thumbs a' ).hover(function () {   
	  $( 'div#middleColumn' ).css (  { "background-image": "url( ../"+ $(this).attr('href') + ")" } );	  
	  return false;
    });
	
	var firstLink = $( '#thumbs a:first' );
	if( firstLink.length > 0 ) {
		firstLink.click();
	}
	
});

function activateNaviImage( naviImage ) {
	
	if( naviImage == undefined ) {
		return;
	}
	//console.log( naviImage );	
	href = naviImage.attr( 'src' );
	
	href = href.replace( 'inactive', 'active' );			
	naviImage.attr( 'src', href );
	var navItem = naviImage.parent().parent();
	
	if( !navItem.parent().hasClass( 'subitems' ) ) {
		activeSubNavi.hide();
		activeSubNavi = $( '#subnavigation #sub_' + navItem.attr('id') );
		activeSubNavi.show();		
	}

}

function getActivePage() {
	var activePageId = '';
	activePageId = $( '#navigation' ).attr('class');
	
	if( activePageId == undefined 
		|| activePageId == 'impressum' ) {
		activePageId = 'mode';
	}
	
	return $( '#navigation #' + activePageId );
}

function getActiveSubPage() {
	var activeSubPageId = '';
	activeSubPageId = $( '#subnavigation' ).attr('class');
	
	if( activeSubPageId != undefined
		&& activeSubPageId != '' ) {
		return $( '#subnavigation #' + activeSubPageId );
	}
	else {
		return null;
	}
}
