var url_hs = null;
var hs_hash = null;
var hs_try_limit = 12;
var hs_gameid = null;
function check_highscore(){
	if( !url_hs || !hs_hash || !hs_gameid ) return false;
			
	var ajax_data = {
		url: url_hs +"?nocache="+Math.random(),
		type: "POST",
		data: { hash:hs_hash, gameid:hs_gameid },
		dataType: 'xml',
		timeout: 30000,
		complete:function(){},
		success: function(data, status){
			var xml = $(data);
			var prompt_msg = xml.find('prompt').text();
			var error_no = parseInt( xml.find("error").text() );
			var status_no = parseInt( xml.find("status").text() );
			if( typeof(error_no) == 'undefined' ) return
		
			if( error_no ){
				if( prompt_msg )
					alert( prompt_msg );
			}
			xml.find("replacehtmlid").children("*").each(function(){
				if( this.tagName == 'script' ){
					$(this).children("*").each(function(){
						eval( $(this).text() );
					});
				}else
					$("#"+ this.tagName).html( $(this).text() );//.empty().remove();
			});
		},
		error: function(data, status, e){
			if( status == 'timeout' )
				alert( "Server was taking too long to respond.\nThe request may have been submitted but a response wasn't recieved.\nPlease try again later or refresh the page." );
			else if( status )
				alert( "["+ e.number +"] "+ e.message +"\n"+ status +"..");
		}
	}
	
	if( jQuery.active < 3 ){
		$.ajax( ajax_data );
	}else if( hs_try_limit-- ){
		setTimeout( "check_highscore();", 5000 );
	}else if( confirm("Too many request are being handled.\nKeep trying to load highscores data?") ){
		hs_try_limit = 12;
		setTimeout( "check_highscore();", 5000 );
	}
		
	
	return(true);
}

// you can override this functions
var hs_show_prompt = function(){
	if( ! $("#hs_prompt").length ){
		var hs_prompt = document.createElement("div");
		hs_prompt.setAttribute("id","hs_prompt");
		$(hs_prompt)
			.prependTo("body")
			.width(320)
			.height(0)
			.css("position", "fixed")
			.css("left", ($("body").width()-350)+"px")
			.css("bottom","-25px")
			.css("z-index", "150")
			.html('<a href="javascript:void(0)" onclick="hs_hide_prompt()" class="close alignright" title="close">[x]</a><div id="hs_content"></div>')
			.animate({height:0},1000)
			.animate({height:75},1000);
	}else
		$("#hs_prompt").animate({height:0},100).animate({height:75},1000);
}

var hs_hide_prompt = function(){
	$("#hs_prompt")
		.animate({height:0},500,null,function(){
			$(this).empty().remove();
		});
}
