$(document).ready(function() {
	initSteamProfiler();
});

function initSteamProfiler() {

	$(".SteamProfiler").each( function(i) {
		var SteamID = new String(""+this.title+"");
		$(this).attr("title", "");
		$(this).html('<div style="line-height:48px; text-align:center; color:#171717;"><img src="/images/ajax-loader.gif" /> Chargement en cours...</div>');
		SteamProfiler_LoadData(this, SteamID);	
	});
}

function SteamProfiler_LoadData(that, SteamID) {
	url = "/do/steam_profile/"+SteamID+".xml";

	$.ajax({
		type: "GET",
		url: url,
		cache: true,
		dataType: 'xml',
		complete: function(request, status) {
			SteamProfiler_SetData(that, $(request.responseXML) );
		}
	});
}
function SteamProfiler_SetData(that, XML) {
	var status = 'SteamProfilerOffline';
	
	if( XML.find('data > Status').text() != 'offline' ) {
		status = 'SteamProfilerOnline';
	}
	if( XML.find('data > SteamID').text() == "STEAM_0:0:-38280598980132864" ) {
		XML.find('data > SteamID').text("error");
	}

	var profile = '';
	if( XML.find('data > inGameInfo').text() ) {
		status = 'SteamProfilerInGame';
		profile = profile + '<img class="SteamProfilerGame" src="'+ XML.find('data > inGameInfo > gameLogoSmall').text() + '" />';
		profile = profile + '<div class="SteamProfilerGame"></div>';
	}
	profile = profile + '<div class="SteamProfilerAvatar '+status+'Avatar"><a href="http://steamcommunity.com/profiles/'+ XML.find('data > steamID64').text() +'" >';
		profile = profile + '<img src="'+ XML.find('data > AvatarMedium').text() +'" /></a></div>';

	profile = profile + '<div class="SteamProfilerName '+status+'" >'+ XML.find('data > Nom').text() +'</div>';
	profile = profile + '<div class="SeamProfilerStatus '+status+'">'+ XML.find('data > StatusFull').text()+'</div>';
	profile = profile + '<div class="SteamProfilerHandler"></div>';
	profile = profile + '<table class="SteamProfilerHandlerSub"><tr>';

		profile = profile + '<td><a href="steam://friends/add/'+ XML.find('data > steamID64').text() +'">';
		profile = profile + '<img src="/images/SteamProfiler/icons-add-to-friends.png" title="Ajouter &agrave; mes amis"/></a></td>';

		if( XML.find('data > inGameInfo > ip').text() ) {
			profile = profile + '<td><a href="steam://connect/'+ XML.find('data > inGameInfo > ip').text() +'">';
			profile = profile + '<img src="/images/SteamProfiler/icons-join-server.png" title="Rejoindre" /></a></td>';
		}
	profile = profile + '</tr></table>';
	$(that).html(profile);
	$(that).attr("title", XML.find('data > SteamID').text() );
	if( $("#EvluatingID").length != 0 && XML.find('data > SteamID').text() != "error" ) {
		$("#EvluatingID").val( XML.find('data > SteamID').text() );
	}
	$(that).find('.SteamProfilerHandler').click(function() {
		SteamProfiler_Handler(that);
	});
}
function SteamProfiler_Handler(that) {
	
	if( !$(that).hasClass("HandlerOpen") ) {
		var sizeof = $(that).find('.SteamProfilerHandlerSub').width()-1;

		$(that).find('.SteamProfilerHandler').animate({ left: ""+(240-sizeof)+"px"}, 200);
		$(that).find('.SteamProfilerHandlerSub').animate({ left: ""+(246-sizeof)+"px"}, 200);
		$(that).addClass("HandlerOpen");
	}
	else {
		var sizeof = 0;

		$(that).find('.SteamProfilerHandler').animate({ left: ""+(240-sizeof)+"px"}, 200);
		$(that).find('.SteamProfilerHandlerSub').animate({ left: ""+(246-sizeof)+"px"}, 200);
		$(that).removeClass("HandlerOpen");
	}
}

