// Copyright by Morgoth
$(function() {
	$('#page').hide();
	$('#pageload').fadeIn('fast');
	$('#status').click(function() {
		status();
    });
	$('.headlink').hover(
		function() { $(this).children('ul').fadeIn('fast'); },
		function() {
			if ($.browser.msie)
				$(this).children('ul').fadeOut('slow');
			else
				$(this).children('ul').fadeOut('normal');
	});
	$("#load").progressbar({
			value: 100
		});
	$('a[rel=#over2]').overlay({
			expose: { 
				color: '#000', 
				loadSpeed: 200, 
				opacity: 0.7 
			},
			oneInstance: false,
			onBeforeLoad: function() { 
				$('a[rel=#over1]').each(function() { 
					$(this).overlay().close(); 
				}); 
			}
	});
	window.onload=function() {
		$('#pageload').fadeOut('fast');
		$('#page').fadeIn('normal');
		status();
		loadContent('sites/infos.html');
	};
});
function loadContent(toLoad, keep) {
    $('#content').fadeOut('slow', function() {
        $('#load').fadeIn('fast', function() {
			$('#content').html('');
			if(keep != true)
				$('#choose').fadeOut('slow', function() {
					$('#choose').html('');
					$('#choose').fadeIn('slow');
				});
            $('#content').load(toLoad + '?' + new Date().getTime(), '', function() {
                $('#load').fadeOut('fast', function() {
					$('#content').jScrollPane({reinitialiseOnImageLoad: true});
                    $('#content').fadeIn('slow');
                });
            });
        });
    });
}
function loadChoose(toLoad) {
    $('#choose').fadeOut('fast', function() {
		$('#content').fadeOut('slow', function() {
			$('#content').html('');
			$('#content').jScrollPane({reinitialiseOnImageLoad: true});
			$('#content').fadeIn('slow');
		});
		if(toLoad == 'playermap')
		{
		 	$('#choose').html('<a href="Silverrain/'+toLoad+'.html" rel="#over1"><img src="image/silverrain.png" /></a>'
				+'<a href="Sunrise/'+toLoad+'.html" rel="#over1"><img src="image/sunrise.png" /></a>'
				+'<a href="Dracor/'+toLoad+'.html" rel="#over1"><img src="image/dracor.png" /></a>');
			$('a[rel=#over1]').overlay({
				expose: { 
					color: '#000', 
					loadSpeed: 200, 
					opacity: 0.7 
				},
				absolute: true,
				top: 100,
				oneInstance: false,
				onBeforeLoad: function() { 
					$('a[rel=#over2]').each(function() { 
						$(this).overlay().close(); 
					}); 
					$('#contentWrap').load(this.getTrigger().attr("href") + '?' + new Date().getTime()); 
				},
				onClose: function() { 
					$('#contentWrap').html(''); 
				}
			});
		}
		else
		{
			$('#choose').html('<a href="javascript:loadContent(\'Silverrain/'+toLoad+'.html\', true)"><img src="image/silverrain.png" /></a>'
				+'<a href="javascript:loadContent(\'Sunrise/'+toLoad+'.html\', true)"><img src="image/sunrise.png" /></a>'
				+'<a href="javascript:loadContent(\'Dracor/'+toLoad+'.html\', true)"><img src="image/dracor.png" /></a>');
			loadContent('Silverrain/'+toLoad+'.html', true);
		}
        $('#choose').fadeIn('fast');
    });
}
function status() {
	var content = '';
	$.get('onlinecount.php', function(request) {
		content = content+'<li>Online Players: <font color="#0fe00a">'+request+'</font></li>';
		$.get('status.php', function(request2) {
			$('#statusContent').fadeOut('normal');
			$(request2).find('server').each(function() {
				var name = $(this).attr('name');
				var online = $(this).attr('online');
				if(name=='Silverrain')
					name = name + ' 500x';
				if(name=='Dracor')
					name = name + ' 25x';
				if(name=='Sunrise')
					name = name + ' 4x';
				if(online==1) {
					content = content+'<li>'+name+': <font color="#0fe00a">Online</font></li>';
				} else {
					content = content+'<li>'+name+': <font color="#e00a0f">Offline</font></li>';
				}
			});
			$('#statusContent').html('<ul>'+content+'</ul>');
			$('#statusContent').fadeIn('normal');
		});
	});
}