var navigationOpen = false;

window.addEvent('domready',function() {
	init();
});

function init() {
	registerResize();
	registerNavigation();
}

function registerResize() {
	resize();
	window.addEvent('resize',resize);
}

function registerNavigation() {
	$('navigation').addEvent('mouseover',expandNavigation);
	$('navigation').addEvent('mouseout',closeNavigation);
	$$('div.naviimage').addEvent('mouseover',function() {
		$(this.parentNode).addClass('selected');
	});
	$$('div.naviimage').addEvent('mouseout',function() {
		$(this.parentNode).removeClass('selected');
	});
	$$('div.naviimage').addEvent('click',function() {
		if (this.title == "Home") {
			var url = "index.php";
		} else {
			var url = this.title.toLowerCase() + ".php";
		}
		window.location = url;
	});
}

function expandNavigation() {
	$('navigation').tween('width','200px');
	$('header').tween('paddingLeft','210px');
	$('expand').tween('width','2px');
}

function closeNavigation() {
	$('navigation').tween('width','40px');
	$('header').tween('paddingLeft','50px');
	$('expand').tween('width','40px');
}

function resize() {
	var newHeight = Math.floor((getWindowHeight()-500)/2) + "px";
	$('top').setStyle('height',newHeight);
}

function displayLoaderBox() {
	loaderParams = positionCenter(100,100);
	var ajax = new Element('div', { 'id': 'ajaxLoader' });
	var loadingProgress = new Element('div', { 'id': 'loadProgress' });
	var loadingText = new Element('div', { 'id': 'loadText' });
	loadingText.set('html','loading');
	var loadingBox = new Element('div', {
		'id': 'loaderBox',
		'class': 'loaderBox',
		'styles': {
			'top': loaderParams.top+'px',
			'left': loaderParams.left+ 'px'
		}
	});
	ajax.inject(loadingBox);
	loadingText.inject(loadingBox);
	loadingProgress.inject(loadingBox);
	loadingBox.inject($('content'));
	window.addEvent('resize',resizeLoader);
}

function removeLoader() {
	window.removeEvent('resize',resizeLoader);
	if ($('loaderBox')) {
		var removalEffect = new Fx.Tween($('loaderBox'), {
			onComplete: function() {
				$('loaderBox').destroy();
			}
		});
		removalEffect.start('opacity',1,0);
	}
}


function resizeLoader() {
	newProps = positionCenter(100,100);
	$('loaderBox').setStyle('top',newProps.top+'px');
	$('loaderBox').setStyle('left',newProps.left+'px');
}




//This function returns an object with top and left properties, determinding the center position for an element
function positionCenter(eleHeight,eleWidth) {
	if (typeof(eleHeight) != "number" || typeof(eleWidth) != "number") return false;
	var height = getWindowHeight();
	var width = getWindowWidth();
	var newLeft = Math.floor((width-eleWidth)/2);
	var newTop = Math.floor((height-eleHeight)/2);
	return { top: newTop, left: newLeft};
}

function getWindowWidth() {
	var windowWidth;
	if (typeof (window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		windowWidth = document.documentElement.clientWidth;
	} else if (document.body && document.body.clientWidth) {
		windowWidth = document.body.clientWidth;
	}
	return windowWidth;
}

function getWindowHeight() {
	var windowHeight;
	if (typeof (window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		windowHeight = document.body.clientHeight;
	}
	return windowHeight;
}