document.observe('dom:loaded', loadItems);

function loadItems() {
	Menu.init();
	if ($('register')) Register.init();
}

var Register = {
	top: 10,
	element: null,
	
	init: function() {
		var div = $('register').getElementsByClassName('buttons');
		Register.element = div[0];
		window.onscroll = Register.scroll;
		Register.scroll();
	},
	
	scroll: function() {
		Register.element.style.top = Register.top + Register.getScroolbarOffset() + 'px';
	},
	
	getScroolbarOffset: function() {
		var result = window.pageYOffset?window.pageYOffset:0;
		result = (document.documentElement && result == 0)?document.documentElement.scrollTop:result;
		result = (document.body && result == 0)?document.body.scrollTop:result;
		return result;
	}
}

var Menu = {
	timeout: null,
	
	init: function() {
		Event.observe($('languages'), "mouseover", Menu.show);
		Event.observe($('languages'), "mouseout", Menu.startCount);
	},
	
	startCount: function() {
		if (!Menu.timeout) {
			Menu.timeout=setTimeout('Menu.hide()', 250);
		}
	},
	
	stopCount: function() {
		if (Menu.timeout) {
			clearTimeout(Menu.timeout);
			Menu.timeout = null;
		}
	},
	
	show: function() {
		Menu.stopCount();
		$('other').style.display = 'block';
	},
	
	hide: function(name) {
		$('other').style.display = 'none';
	}
}

var AjaxFunctions = {
	loader: null,
	overlay: null,
	holder: null,
	lock: false,

/* user functions */

	playersPage: function(page) {
		if (!AjaxFunctions.lock) {
			AjaxFunctions.lock = true;
			new Ajax.Request(window.location, {
				method: 'post',
				parameters: {page: page},
				onCreate: function() {
					AjaxFunctions.showSplashScreen('players');
				},
				onComplete: function(transport) {
					AjaxFunctions.hideSplashScreen();
					$('players').innerHTML = transport.responseText;
				}
			})
		}
	},

	racePage: function(date) {
		if (!AjaxFunctions.lock) {
			AjaxFunctions.lock = true;
			new Ajax.Request(window.location, {
				method: 'post',
				parameters: {date: date},
				onCreate: function() {
					AjaxFunctions.showSplashScreen('players');
				},
				onComplete: function(transport) {
					AjaxFunctions.hideSplashScreen();
					$('players').innerHTML = transport.responseText;
				}
			})
		}
	},
		
/* core functions */

	showPopUp: function(data) {
		if (!AjaxFunctions.overlay) {
			AjaxFunctions.overlay = document.createElement('div');
			AjaxFunctions.overlay.setAttribute('id', 'ajaxoverlay');
			AjaxFunctions.overlay.style.display = 'none';
			document.body.appendChild(AjaxFunctions.overlay);
			$('ajaxoverlay').style.width = AjaxFunctions.windowWidth()+'px';
			$('ajaxoverlay').style.height = AjaxFunctions.windowHeight()+'px';
			$('ajaxoverlay').style.zIndex = 100;
			if (AjaxFunctions.version() < 7) {
				$('ajaxoverlay').style.position = 'absolute';
				$('ajaxoverlay').style.top = AjaxFunctions.scrollTop()+'px';
			} else {
				$('ajaxoverlay').style.position = 'fixed';
				$('ajaxoverlay').style.top = '0px';
			}
			$('ajaxoverlay').style.left = '0px';
			
			AjaxFunctions.holder = document.createElement('div');
			AjaxFunctions.holder.setAttribute('id', 'ajaxholder');
			AjaxFunctions.holder.style.display = 'none';
			document.body.appendChild(AjaxFunctions.holder);
			$('ajaxholder').style.position = 'absolute';
			$('ajaxholder').style.zIndex = 150;
		}
		$('ajaxoverlay').style.display = 'block';
		$('ajaxholder').innerHTML = data;
		$('ajaxholder').style.display = 'block';
		$('ajaxholder').style.left = Math.round((AjaxFunctions.windowWidth()-$('ajaxholder').offsetWidth)/2)+'px';
		$('ajaxholder').style.top = (AjaxFunctions.scrollTop()+Math.round((AjaxFunctions.windowHeight()-$('ajaxholder').offsetHeight)/2))+'px';
	},
	
	hidePopUp: function() {
		$('ajaxoverlay').style.display = 'none';
		$('ajaxholder').style.display = 'none';
	},
	
	showSplashScreen: function(element) {
		if (!AjaxFunctions.loader) {
			AjaxFunctions.loader = document.createElement('div');
			AjaxFunctions.loader.setAttribute('id', 'ajaxloader');
			AjaxFunctions.loader.style.display = 'none';
			document.body.appendChild(AjaxFunctions.loader);
			var ajaxbar = document.createElement('div');
			ajaxbar.setAttribute('id', 'ajaxbar');
			AjaxFunctions.loader.appendChild(ajaxbar);
		}
		if (!element) {
			AjaxFunctions.loader.width = AjaxFunctions.windowWidth();
			AjaxFunctions.loader.height = AjaxFunctions.windowHeight();
		} else {
			AjaxFunctions.loader.width = $(element).getWidth();
			AjaxFunctions.loader.height = $(element).getHeight();
		}
		$('ajaxloader').style.width = AjaxFunctions.loader.width+'px';
		$('ajaxloader').style.height = AjaxFunctions.loader.height+'px';
		swfobject.embedSWF("/preload.swf", "ajaxbar", AjaxFunctions.loader.width, AjaxFunctions.loader.height, "9.0.0", "expressInstall.swf", null, {quality:'high', scale:'noscale', wmode:'transparent', salign:'tl'});
		$('ajaxloader').style.display = 'block';
		$('ajaxloader').style.zIndex = 200;
		if (!element) {
			if (AjaxFunctions.version() < 7) {
				$('ajaxloader').style.position = 'absolute';
				$('ajaxloader').style.top = AjaxFunctions.scrollTop()+'px';
			} else {
				$('ajaxloader').style.position = 'fixed';
				$('ajaxloader').style.top = '0px';
			}
			$('ajaxloader').style.left = '0px';
		} else {
			$('ajaxloader').style.position = 'absolute';
			var poz = $(element).cumulativeOffset();
			$('ajaxloader').style.top = poz.top+'px';
			$('ajaxloader').style.left = poz.left+'px';
		}
	},
	
	hideSplashScreen: function() {
		$('ajaxloader').style.display = 'none';
		AjaxFunctions.lock = false;
	},

	scrollTop: function() {
		var result = window.pageYOffset?window.pageYOffset:0;
		result = (document.documentElement && result == 0)?document.documentElement.scrollTop:result;
		result = (document.body && result == 0)?document.body.scrollTop:result;
		return result;
	},

	windowWidth: function() {
		var result = window.innerWidth?window.innerWidth:0;
		result = (document.documentElement && result == 0)?document.documentElement.clientWidth:result;
		result = (document.body && result == 0)?document.body.clientWidth:result;
		return result;
	},

	windowHeight: function() {
		var result = window.innerHeight?window.innerHeight:0;
		result = (document.documentElement && result == 0)?document.documentElement.clientHeight:result;
		result = (document.body && result == 0)?document.body.clientHeight:result;
		return result;
	},

	version: function() {
		var version = 999;
		if (navigator.appVersion.indexOf("MSIE") != -1) version = parseFloat(navigator.appVersion.split("MSIE")[1]);
		return version;
	}
	
}

var Password = {
	showChange: function() {
		$('changespan').style.display = 'none';
		$('changeform').style.display = 'block';
	},
	
	hideChange: function() {
		$('changespan').style.display = 'block';
		$('changeform').style.display = 'none';
	}
}

var Splash = {
	
	hide: function() {
		$('splashholder').style.display = 'none';
	}
}
