var fullScreenState = false;
var noFullScreenIcon = null;
var fullScreenIcon = null;

window.onresize = handleScreenResize;

function initFullScreen(instance) {
	el("reklama_container").style.display = "inline";
	el("tv_container").style.display = "inline";

	createFullScreenIcon();
	createNormalScreenIcon();
	
	google.earth.addEventListener(ge.getWindow(), "click", handleMouseClick);

	makeNormalScreen();
}

function handleMouseClick(event) {
	var INSET_PIXELS_X = document.getElementById("map3d").offsetWidth
			- event.getClientX();
	var INSET_PIXELS_Y = event.getClientY();
	if (INSET_PIXELS_X < 32) {
		if (INSET_PIXELS_Y < 32) {
			toggleFullScreen();
		}
	}
}

function toggleFullScreen() {
	if (fullScreenState == true) {
		makeNormalScreen();
	} else {
		makeFullScreen();
	}
}

function makeFullScreen() {
	var fullscreencontainer = el('fullscreencontainer');
	var ge_container = el('ge_container');
	var tables_container = el('tables_container');
	var chat_container = el('chat_container');
	
	tables_container.style.top = '1px';
	tables_container.style.width = '0px';
	tables_container.style.left = (fullscreencontainer.offsetWidth - tables_container.offsetWidth)
			+ 'px';

	chat_container.style.width = 0 + 'px';
	el('cboxmain').width = "0";
	el('cboxform').width = "0";

	chat_container.style.top = (fullscreencontainer.offsetHeight - chat_container.offsetHeight)
			+ 'px';
	chat_container.style.left = (fullscreencontainer.offsetWidth - chat_container.offsetWidth)
			+ 'px';

	ge_container.style.left = '0px';
	ge_container.style.top = '0px';
	ge_container.style.width = fullscreencontainer.offsetWidth + 'px';
	ge_container.style.height = fullscreencontainer.offsetHeight + 'px';

	fullScreenState = true;
	noFullScreenIcon.setVisibility(fullScreenState);
	fullScreenIcon.setVisibility(!fullScreenState);
}

function makeNormalScreen() {
	var fullscreencontainer = el('fullscreencontainer');
	var ge_container = el('ge_container');
	var tables_container = el('tables_container');
	var chat_container = el('chat_container');

	tables_container.style.top = '1px';
	tables_container.style.width = tables_container_size.width + 'px';

	tables_container.style.left = (fullscreencontainer.offsetWidth - tables_container.offsetWidth)
			+ 'px';

	tables_container.style.height = (fullscreencontainer.offsetHeight - chat_container.offsetHeight)
			+ 'px'

	chat_container.style.width = tables_container_size.width + 'px';
	el('cboxmain').width = tables_container_size.width;
	el('cboxform').width = tables_container_size.width;

	// chat_container.style.top = (tables_container.offsetHeight + 2) + 'px';
	chat_container.style.top = (fullscreencontainer.offsetHeight
			- chat_container.offsetHeight - 2)
			+ 'px';
	chat_container.style.left = (fullscreencontainer.offsetWidth
			- chat_container.offsetWidth - 3)
			+ 'px';

	ge_container.style.left = 1 + 'px';
	ge_container.style.top = 1 + 'px';
	ge_container.style.width = (fullscreencontainer.offsetWidth
			- tables_container.offsetWidth - 3)
			+ 'px';
	ge_container.style.height = (fullscreencontainer.offsetHeight - 2) + 'px';

	fullScreenState = false;
	noFullScreenIcon.setVisibility(fullScreenState);
	fullScreenIcon.setVisibility(!fullScreenState);
}

function createFullScreenIcon() {
	// create an image for the screen overlay
	var icon = ge.createIcon('');
	icon.setHref(rootURL + 'image/tofull.png');
	// create the screen overlay
	fullScreenIcon = ge.createScreenOverlay('');
	fullScreenIcon.setDrawOrder(60);
	fullScreenIcon.setIcon(icon);
	// anchor point in top left of icon.
	fullScreenIcon.getScreenXY().setXUnits(ge.UNITS_FRACTION);
	fullScreenIcon.getScreenXY().setYUnits(ge.UNITS_FRACTION);
	fullScreenIcon.getScreenXY().setX(1);
	fullScreenIcon.getScreenXY().setY(1);
	// place icon in top left of screen.
	fullScreenIcon.getOverlayXY().setXUnits(ge.UNITS_INSET_PIXELS);
	fullScreenIcon.getOverlayXY().setYUnits(ge.UNITS_INSET_PIXELS);
	fullScreenIcon.getOverlayXY().setX(2);
	fullScreenIcon.getOverlayXY().setY(4);
	// Set icon size.
	fullScreenIcon.getSize().setXUnits(ge.UNITS_PIXELS);
	fullScreenIcon.getSize().setYUnits(ge.UNITS_PIXELS);
	fullScreenIcon.getSize().setY(32);
	fullScreenIcon.getSize().setX(32);
	// add the screen overlay to Earth
	ge.getFeatures().appendChild(fullScreenIcon);
}

function createNormalScreenIcon() {
	// create an image for the screen overlay
	var icon = ge.createIcon('');
	icon.setHref(rootURL + 'image/tonormal.png');
	// create the screen overlay
	noFullScreenIcon = ge.createScreenOverlay('');
	noFullScreenIcon.setDrawOrder(62);
	noFullScreenIcon.setIcon(icon);
	// anchor point in top left of icon.
	noFullScreenIcon.getScreenXY().setXUnits(ge.UNITS_FRACTION);
	noFullScreenIcon.getScreenXY().setYUnits(ge.UNITS_FRACTION);
	noFullScreenIcon.getScreenXY().setX(1);
	noFullScreenIcon.getScreenXY().setY(1);
	// place icon in top right of screen.
	noFullScreenIcon.getOverlayXY().setXUnits(ge.UNITS_INSET_PIXELS);
	noFullScreenIcon.getOverlayXY().setYUnits(ge.UNITS_INSET_PIXELS);
	noFullScreenIcon.getOverlayXY().setX(2);
	noFullScreenIcon.getOverlayXY().setY(4);
	// Set icon size.
	noFullScreenIcon.getSize().setXUnits(ge.UNITS_PIXELS);
	noFullScreenIcon.getSize().setYUnits(ge.UNITS_PIXELS);
	noFullScreenIcon.getSize().setY(32);
	noFullScreenIcon.getSize().setX(32);
	noFullScreenIcon.setVisibility(false);
	// add the screen overlay to Earth
	ge.getFeatures().appendChild(noFullScreenIcon);
}

function handleScreenResize() {
	if (fullScreenState == false)
		makeNormalScreen();
	else
		makeFullScreen();
}
