/**
 * @author dborie, NAVTEQ (DE) GmbH
 */

$(document).ready(function(){
	
	// check if this is ie6 or ie7...
	if($.browser.msie && $.browser.version < 8){
		// ...apply rounded-corners for the navigation-elements
		// without keeping the defined css-border 
		$('#tabs li').corner('7px top');
	}
	// ...in all other cases...
	else{
		// ...apply rounded-corners for the navigation-elements
		// keeping the defined css-border
		$('#tabs li').corner('keep 7px top');
	}
});


// Resize-event is located in a theme-specific-JS-file.
// window-resize event
// controls to always set content- and map-area to fit the available height.
$(window).resize(function(){
	//NVT.dump('onresize triggered');
	mr.skin.onResize();
});

mr.skin = {
	
	_values : {
		resizeRunning: false,
		resizeTimeout: null
	},
	
	onResize : function(){
		
		// if this is IE...
		if($.browser.msie){
			// ...go the normal way and exec. the 
			// resize-functionality
			this._resize();
		}
		// ...in any other browser...
		// and resize is not yet running...
		else if(this._values.resizeRunning === false){
			// ...trigger to run it in a timeout-process. 
			this._values.resizeRunning = true;
			this._values.resizeTimeout = window.setTimeout(this._onResizeTimeout, 500);
			//NVT.dump('resize: locked');
		}
		
	},
	
	_onResizeTimeout: function(){
		mr.skin._values.resizeRunning = false;
		mr.skin._values.resizeTimeout = null;
		mr.skin._resize();
		//NVT.dump('resize: unlocked');
	},
	
	_resize: function(){
		
		//NVT.dump('resize: exec.');
		
		var h = $(window).height();
		var pageId = $('body').attr('id');
		
		// 1. calculate heigths
		// FIXME: Replace hardcoded element-heights.
		var contentHeight = h 
			- $('#header').height() -15 // 'header-height' - header-margin
			- $('#navigation').height()	// 'nav.-height'
			- 45					// 'page-wrap' - margin - padding
			- 20;					// 'footer-height'
			
		
		var mapHeightReduction = 0;
		
		// Test if height of #map-message is larger then 2px.
		// It can't be tested on "height > 0" because of IE6.
		// This will return at least 2px (see iehacks.css for more info). 
		if($('#map-message').height() > 2){
			
			mapHeightReduction += 
				  $('#map-message').height() // 'map-message'-height
				+ 5 // 'map-message'-margin
		}
		
		var mapHeight = contentHeight - mapHeightReduction; 
		
		// 2. set the calculated heights for the map and the mapsize-control-bar.
		$('#map, #map-size-control').height( mapHeight );
		
		
		// workaround for the mapsize-control-bar:
		// set the margin of the containing div to half of the height of the mapsize-control-bar
		// to display the indicator-image in the vertical-center.
		$('#map-size-control div').css('margin-top', ($('#map-size-control').height() / 2 - 18) );
		
			
		// make sure the main-content-area has not defined
		// a fix height.
		$('#main-content, #additional-content').height('');	
		
		// 3. set the calculated heights for the main-content.
		// don't do it for certain page-ids anymore...
		/*if( pageId === 'report' || pageId === 'find' || pageId === 'labs'){
			// In the report-context not the whole page should be scrollable,
			// but only the main-content-area.
			// Therefor set the height of the #main-content.
			$('#main-content').height(contentHeight);
		}*/
		// .. but use it generally now
		$('#main-content').height(contentHeight);
		
		
		if( $('#main-content').height() <= $('#map-content').height() ){
			$('#main-content').height( $('#map-content').height() );
		}
		
		$('#additional-content').height($('#map-content').height());
	}
};



/**
 * Assign click-event to the mapsize-control-bar.
 * Clicking it toggles the map-display between
 * fullscreen-mode and normal-mode.
 */
$('#map-size-control').click(function(){
	
	$content = $('#content');
	if($content.hasClass('fullscreen-map')){
		mr.action.map.slideMap('normal');
		
		// check if the main-content has content already...
		if($('#main-content').html() == ''){
			// ...and if not, load the homepage
			mr.core.loadChunk('home', '#main-content', {
				registerHistory: 'stealth'
			});
		}
	}
	else{
		mr.action.map.slideMap('big');
	}
	
	
});
$('#map-size-control').append('<div>');
