var lastUser;
var lastNum;

function profileAwardsMenu (user, dialogTitle)
	{
	document.getElementById('profile-awards-menu').innerHTML = "<img src='"+loadingImage+"' alt='loading'/>";
	var url = 'ajaxdata.php?action=profile-awards_menu&u='+user;
	$('#profile-awards-menu').load(url, null, function() {profileActivateAwardsMenu();});
	$('#profile-awards-menu').dialog('open');
	}

function profileActivateAwardsMenu ()
	{
	$('#profile-awards-menu').dialog('option', 'title', langMes['profile-awards_title1'].replace(/\$user/g, document.forms['profile-asf']['list_usern_1'].value));
	$("#profile-awards-users-acc").accordion({
		event: "mouseover",
		autoHeight: false,
		changestart: function(event, ui) {profileHideAutoComplete();}
		});
	$('#comp_user_1, #comp_user_2').autocomplete("ajaxdata.php?action=users", {
		minChars: 0,
		max: 10,
		mustMatch: true,
		matchContains: false,
		scrollHeight: 220,
		formatItem: function(data) {
			return data[1];
		},
		formatResult: function(data) {
			return data[0];
		}

		}).result(function(){profileProcessAwardUserInput(document.forms['profile-asf']['cufn'].value);});
	}


function profileHideAutoComplete ()
	{
	if (document.activeElement.id == 'comp_user_1')
		{
		$('#comp_user_1').blur();
		}
	else if (document.activeElement.id == 'comp_user_2')
		{
		$('#comp_user_2').blur();
		}
	}


function profileSetAwardUserNum (num)
	{
	if (typeof(num) == 'undefined')
		{
		num = 1;
		}
	document.forms['profile-asf']['cufn'].value = num;
	}

function profileUpdateAwardsList (listNum, size)
	{
	var listID = 'profile-awards-list-'+listNum+'-'+size;
	if (document.getElementById(listID))
		{
		var userfid = 'list_user_' + listNum;
		var user = document.forms['profile-asf'][userfid].value;
		var url = 'ajaxdata.php?action=profile-awards_list&mode=xml&u='+user+'&size='+size;
		if (document.forms['profile-asf']['time_mode'])
			{
			for (var i=0; i < document.forms['profile-asf']['time_mode'].length; i++)
				{
				if (document.forms['profile-asf']['time_mode'][i].checked)
					{
					var timeMode = document.forms['profile-asf']['time_mode'][i].value;
					}
				}
			url += "&time_mode=" + timeMode;
			}
		if (document.forms['profile-asf']['time_num'].value != null)
			{
			url += "&time_num=" + document.forms['profile-asf']['time_num'].value;
			}
		if (document.forms['profile-asf']['time_what'].value != null)
			{
			url += "&time_what=" + document.forms['profile-asf']['time_what'].value;
			}
		if (document.forms['profile-asf']['ap_mol'].value != null)
			{
			url += "&ap_mol=" + document.forms['profile-asf']['ap_mol'].value;
			}
		if (document.forms['profile-asf']['ap_num'].value != null)
			{
			url += "&ap_num=" + document.forms['profile-asf']['ap_num'].value;
			}
		// if another user is being compared to, append that user to the URL
		if (document.forms['profile-asf']['list_usern_1'].value != 'undefined' && document.forms['profile-asf']['list_usern_2'].value != 'undefined')
			{
			if (listNum == 1)
				{
				var otherListNum = 2;
				}
			else
				{
				var otherListNum = 1;
				}
			url += "&user2=" + document.forms['profile-asf']['list_user_'+otherListNum].value;
			}
		/*loadURLIntoID(url, listID);*/
		$.get(
			url,
			{},
			function(xml)
				{
				var awardsCount = $('awardsCount', xml).text();
				var listData = $('listData', xml).text();
				$('#profile-awards-user'+listNum+'-total').html(awardsCount);
				$('#'+listID).html(listData);
				}
			);
		$('#profile-awards-user'+listNum+'-total').filter(':not(:animated)').effect('pulsate', {times:3}, 200);
		$('#'+listID).filter(':not(:animated)').effect('pulsate', {times:3}, 200);
		}
	else
		{
		//$('#profile-awards-list-1-'+size).effect('pulsate', {times:3}, 200);
		}
	}

function profileProcessAwardSettings ()
	{
	for (var i = 1; i <= 2; i++)
		{
		profileUpdateAwardsList(i, 'awf');
		}
	}

function profileCompareAwards ()
	{
	var user1 = document.forms['profile-asf']['list_user_1'].value;
	var user2 = document.forms['profile-asf']['list_user_2'].value;
	var user1p = document.getElementById('profile-awards-list-1-awf');
	var user2p = document.getElementById('profile-awards-list-2-awf');
	var user2exists = false;
	// the user2 pane did NOT exist, but there is now a user2 registered so CREATE IT
	if (!user2p && user2 != '')
		{
		user2exists = true;
		var user1head = langMes['profile-awards_title1'].replace(/\$user/, document.forms['profile-asf']['list_usern_1'].value);
		var user2head = langMes['profile-awards_title1'].replace(/\$user/, document.forms['profile-asf']['list_usern_2'].value);
		$("#profile-awards-list-1-awf").switchClass('profile-awards_menu_p1_half', 'profile-awards_menu_p1_full', 500);
	//	document.getElementById('profile-awards-left').innerHTML = "<div id='profile-awards-head-1-awf' class='profile-awards_menu_p1_head' style='display:none;'><div class='profile-awards_user_head'>" + user1head + "<span class='profile-awards_user_head_total' id='profile-awards-user1-total'></span></div></div>" + document.getElementById('profile-awards-left').innerHTML;
		document.getElementById('profile-awards-left').innerHTML += "<div id='profile-awards-head-2-awf' class='profile-awards_menu_p1_head' style='display:none;'><div class='profile-awards_user_head'><span class='profile-awards_user_head_name' id='profile-awards-head2-name'>" + user2head + "</span><span class='profile-awards_user_head_total' id='profile-awards-user2-total'></span></div></div><div id='profile-awards-list-2-awf' class='profile-awards_menu_p1_half' style='display:none;'></div>";
		var user2h = document.getElementById('profile-awards-head-2-awf');
		var user2p = document.getElementById('profile-awards-list-2-awf');
		$("#profile-awards-head-1-awf").fadeIn(600);
		$("#profile-awards-head-2-awf").fadeIn(600);
		$(user2p).fadeIn(600);
		}
	// the user2 pane DID exist, but there is no longer a use for user 2 so REMOVE IT
	else if (user2p && user2 == '')
		{
		user2exists = false;
		$('#profile-awards-list-2-awf').fadeOut(600);
		$('#profile-awards-head-1-awf').fadeOut(600);
		document.getElementById('profile-awards-left').removeChild(document.getElementById('profile-awards-head-2-awf'));
		document.getElementById('profile-awards-left').removeChild(document.getElementById('profile-awards-list-2-awf'));
		$('#profile-awards-list-1-awf').effect('pulsate', {times:2}, 200);//.fadeOut(100).fadeIn(400);
		$("#profile-awards-list-1-awf").switchClass('profile-awards_menu_p1_full', 'profile-awards_menu_p1_half', 600);//.effect('pulsate', {times:3}, 200);
		}
	// the user2 pane DID exist and we still need to use it
	else if (user2p)
		{
		user2exists = true;
		}
	// reset the dialog's title
	if (user2exists)
		{
		//alert(document.forms['profile-asf']['list_usern_1'].value);
		//alert(document.forms['profile-asf']['list_usern_2'].value);
		if (document.forms['profile-asf']['list_usern_1'].value != 'undefined' && document.forms['profile-asf']['list_usern_2'].value != 'undefined')
			{
			var newTitle = langMes['profile-awards_title2'].replace(/\$user1/g, document.forms['profile-asf']['list_usern_1'].value);
			newTitle = newTitle.replace(/\$user2/g, document.forms['profile-asf']['list_usern_2'].value);
			}
		//$('#profile-awards-menu').dialog('option', 'title', newTitle);
		}
	else
		{
		//alert(document.forms['profile-asf']['list_usern_1'].value);
		if (document.forms['profile-asf']['list_usern_1'].value != 'undefined')
			{
			var newTitle = langMes['profile-awards_title1'].replace(/\$user/g, document.forms['profile-asf']['list_usern_1'].value);
			}
		}
	//alert(newTitle);
	if (typeof(newTitle) != 'undefined')
		{
		$('#profile-awards-menu').dialog('option', 'title', newTitle);
		}
	}

function profileSetAwardsUser (num, userid, username)
	{
	document.forms['profile-asf']["list_user_" + num].value = userid;
	document.forms['profile-asf']["list_usern_" + num].value = username;
	}

function profileProcessAwardUserInput (num)
	{
	var lfid = 'list_user_' + num;
	var cfid = 'comp_user_' + num;
	if (document.forms['profile-asf'][cfid].value != lastUser || num != lastNum)
		{
		$.ajax({
			type: 'GET',
			url: 'ajaxdata.php?action=username&u=' + document.forms['profile-asf'][cfid].value,
			dataType: 'html',
			success: function(data, textStatus)
				{
				var u = data.split('|');
				if (data == 0)
					{
					profileSetAwardsUser(num, '');
					}
				else
					{
					profileSetAwardsUser(num, u[0], u[1]);
					}
				lastUser = document.forms['profile-asf'][cfid].value;
				lastNum = num;
				profileCompareAwards();
				if (document.forms['profile-asf']['list_usern_1'].value != 'undefined' && document.forms['profile-asf']['list_usern_2'].value != 'undefined')
					{
					if (num == 1)
						{
						$('#profile-awards-head1-name').html(langMes['profile-awards_title1'].replace(/\$user/g, document.forms['profile-asf']['list_usern_1'].value));
						}
					profileUpdateAwardsList(1, 'awf');
					profileUpdateAwardsList(2, 'awf');
					}
				else
					{
					profileUpdateAwardsList(num, 'awf');
					}
				}
			});
		}
	}



