/**************************************************************
 * the applet resize and fullscreen stuff
 *************************************************************/

// Applet normal ratio
var ratio = 1024/613;

// InternetExplorer adds some pixels under the <applet>
var EXTRA_SPACE_UNDER_APPLET = ((navigator.appName.search("Microsoft") != -1) ? 2 : 0);

// Menu & Footer height, in pixels
// - Menu height : cf. images height
// - Footer height : cf. templates/footer.php
// - Add some pixels "just in case" ... IE. for instance adds some space at bottom of the page
var NON_APPLET_HEIGHT = 103 + 20 + 10 + EXTRA_SPACE_UNDER_APPLET;

// Gulli dimensions
var GULLI_WIDTH = 995;
var GULLI_HEIGHT = 696;

// Menu width
var MENU_WIDTH = 965;

// Is applet fullscreen ?
var isFullscreen = false;

// Element names
var headerName = 'header';
var footerName = 'footer';
var appletName = 'theApplet';

// Dimensions
var interface_width = 0;
var interface_height = 0;
var fullscreen_width = 0;
var fullscreen_height = 0;

function saveDimensions() {
	if (isGulliIntegrated()) {
		interface_width = GULLI_WIDTH;
		interface_height = GULLI_HEIGHT;
		fullscreen_width = GULLI_WIDTH;
		fullscreen_height = GULLI_HEIGHT;
	} else {
		interface_height = getAvailableHeight() - NON_APPLET_HEIGHT;
		interface_width = interface_height * ratio;
		fullscreen_width = getAvailableWidth();
		fullscreen_height = getAvailableHeight() - EXTRA_SPACE_UNDER_APPLET;
	}
	
	if (interface_width < MENU_WIDTH) {
		interface_width = MENU_WIDTH;
	}
	
	if (fullscreen_width < MENU_WIDTH) {
		fullscreen_width = MENU_WIDTH;
	}
}

function isGulliIntegrated() {
	return (window.top != window);
}

function switchDisplayMode(mode) {

	var menuHeader = document.getElementById(headerName);
	var menuFooter = document.getElementById(footerName);
	
	if (mode == 'fullscreen') {
		menuHeader.style.display = 'none';
		menuFooter.style.display = 'none';
		isFullscreen = true;
		document.body.scroll = 'no';
	} else if (mode == 'interface') {
		menuHeader.style.display = 'block';
		menuFooter.style.display = 'block';
		isFullscreen = false;
		document.body.scroll = 'yes';
	}
	
	resizeApplet();
}

function resizeApplet() {

	var newWidth = 0;
	var newHeight = 0;
	
	if (isFullscreen) {
		newWidth = fullscreen_width;
		newHeight = fullscreen_height;
	} else {
		newWidth = interface_width;
		newHeight = interface_height;
	}
	
	applet = document.getElementById(appletName);
	if ( applet === null ) {
		return;
	}

	applet.width = newWidth;
	applet.height = newHeight;
}

function getAvailableWidth() {
	// Gulli
	if ( isGulliIntegrated() ) {
		return GULLI_WIDTH;
	} 
	// Non-IE
	else if (typeof(window.innerWidth) == 'number') { 
		return window.innerWidth;
	}
	// IE 6+ in 'standards compliant mode'
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		return document.documentElement.clientWidth;
	}
	// IE 4 compatible
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		return document.body.clientWidth;
	}
}

function getAvailableHeight() {
	// Gulli
	if ( isGulliIntegrated() ) {
		return GULLI_HEIGHT;
	} 
	// Non-IE
	else if (typeof(window.innerWidth) == 'number') {
		return window.innerHeight;
	}
	// IE 6+ in 'standards compliant mode'
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		return document.documentElement.clientHeight;
	}
	// IE 4 compatible
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		return document.body.clientHeight;
	}
}