jQuery(document).ready(function(){
	var scrollDiv = document.createElement('div');
	jQuery(scrollDiv).attr('id', 'toTop').appendTo('body');
	jQuery(window).scroll(function(){
		if(jQuery(this).scrollTop() != 0){
			jQuery('#toTop').fadeIn();
		}else{
			jQuery('#toTop').fadeOut();
		}
	});

	jQuery('#toTop').click(function(){
		jQuery('body,html').animate({
			scrollTop: 0
		},1000);
	});

	$('#servRefresh').live('click', function(){
		$('#servCount').fadeOut('fast', function(){
			$('#servCount').html('<img src="/images/ajax-loader.gif" alt="Loading">').fadeIn('fast');
			RefreshServers();
		});
		
		$('#servPlayers').fadeOut('fast', function(){
			$('#servPlayers').html('<img src="/images/ajax-loader.gif" alt="Loading">').fadeIn('fast');
		});
	});
	
	RefreshServers();
	
	$('.main li:has(ul) > a').addClass('more');
	$('a.more').append('&nbsp;&raquo;');
	$('.main li').hover(function(){
		$(this).find('ul:first').stop(true, true).animate({height: 'toggle'}, 200).addClass('active_list');
	}, function(){
		$(this).children('ul.active_list').stop(true, true).animate({height: 'toggle'}, 200).removeClass('active_list');
	});
});

window.onload = stopload;

function stopload(){
	$("#loading").fadeOut("slow");
}

function slidedown(arg1,arg2,arg3){
	$("#"+arg1).slideDown('slow', function(){
		$("#"+arg2).hide();
		$("#"+arg3).show();
	});
}

function slideup(arg1,arg2,arg3){
	$("#"+arg1).slideUp('slow', function(){
		$("#"+arg2).hide();
		$("#"+arg3).show();
	});
}

function showAlert(status,txt){
	if(status == 'ok') {	
		status = '<span class="ok">';
	}
	if(status == 'error') {	
		status = '<span class="error">';
	}
	$('#alert').html(status+txt+'<br />&nbsp;</span>');
	$('#alert').slideDown('slow');
	setTimeout('$(\'#alert\').slideUp(\'slow\')',5000);
}

function InsertEmote(a,c){
	var dElement=document.getElementById(a);
	dElement.value+=" "+c+" ";
}

function RefreshServers(){
	$.ajax({
		type: "GET",
		url: "Servers",
		data: "getStats=1",
		success: function(Result){
			Result = $.parseJSON(Result);
			$('#servCount' ).fadeOut('fast', function(){
				Result.servers += '<span id="servRefresh" style="cursor:pointer;vertical-align:baseline;"><img alt="" src="/images/refresh.png"></span>';
				$('#servCount').html(Result.servers).fadeIn('slow');
			});
			
			$('#servPlayers').fadeOut('fast', function(){
				$('#servPlayers').html(Result.players).fadeIn('slow');
			});
		}
	});
}

function ShowServer(Id){
	$.post("Servers",{getServer : Id},
	function(data){
		$("#Server" + Id).fadeTo(200,0.1,function(){
				$(this).html(data).fadeTo(900,1);
		});
	});
}
