var send = create_XMLHttpRequest();

// used so only one request is sent to change position
var global_count = 0;

function friend_move_position(direction, current_pos, member_id)
{
	member_to_move = document.getElementById('member_id_' + current_pos).innerHTML
	switch (direction)
	{
		case 'up':
			position_to_move = parseInt(current_pos) - 1
			break

		case 'down':
			position_to_move = parseInt(current_pos) + 1
			break

		default:
			position_to_move = direction;
	}

	if( send.readyState == 4 || send.readyState == 0 )
	{
		send.open("GET", "/ajax/friend_move/" + member_id + "/" + member_to_move + "/" + position_to_move + "/", true);
		send.onreadystatechange = friend_move_position_complete;
		send.send(null);
	}
}


//var send = create_XMLHttpRequest();

function friend_move_position_complete()
{
	if (send.readyState == 4)
	{
		// Response code was 200, OK!
		if( send.status == 200 )
		{
			// Get XML data
	 		var xmldoc = send.responseXML;
 			var success = xml_get_field(xmldoc, "success");

 			// Success
	 		if( success.toUpperCase() == "TRUE" )
	 		{

	 			var from_position 	= xml_get_field(xmldoc, "from_position");
	 			var to_position 	= xml_get_field(xmldoc, "to_position");
	 			var max_value 		= xml_get_field(xmldoc, "max_position");
	 			var min_value 		= xml_get_field(xmldoc, "min_position");
	 			var max_friends 	= xml_get_field(xmldoc, "max_friend");



	 			// move up
	 			if( from_position - to_position > 0)
	 			{
	 				for (var x = parseInt(from_position); x > parseInt(to_position); x--)
					{
						if( ( x <= min_value) && (x != 1) )
						{
							var next_cell = xml_get_field(xmldoc, "previous_cell");
							// get the value from the previou cell on the other page and change it
							document.getElementById('cellid_' + min_value).innerHTML = next_cell;
						}
						else
						{
							switch_friends(x, parseInt(x)-1)
						}
					}
	 			}

	 			// move down
	 			if( from_position - to_position < 0)
	 			{
					for (var x = parseInt(from_position); x < parseInt(to_position); x++)
					{
						if( x >= max_value )
						{
							var next_cell = xml_get_field(xmldoc, "next_cell");
							// get the value from the previou cell on the other page and change it
							document.getElementById('cellid_' + max_value).innerHTML = next_cell;
						}
						else
						{
							switch_friends(x, (parseInt(x) + 1), max_friends);
						}
					}
	 			}

		   	}
	   	}
	}
}


// switches to positions that are next to each other
function switch_friends(position1, position2)
{
	// only allow positions that are next to each other
	if((position1 - position2 == 1) || (position1 - position2 == -1))
	{
	 	pic1 = document.getElementById('pic_row_' + position1).innerHTML
		pic2 = document.getElementById('pic_row_' + position2).innerHTML

		document.getElementById('pic_row_' + position1).innerHTML = pic2
		document.getElementById('pic_row_' + position2).innerHTML = pic1

		name1 = document.getElementById('name_row_' + position1).innerHTML
		name2 = document.getElementById('name_row_' + position2).innerHTML

		document.getElementById('name_row_' + position1).innerHTML = name2
		document.getElementById('name_row_' + position2).innerHTML = name1

		id1 = document.getElementById('member_id_' + position1).innerHTML
		id2 = document.getElementById('member_id_' + position2).innerHTML

		document.getElementById('member_id_' + position1).innerHTML = id2
		document.getElementById('member_id_' + position2).innerHTML = id1

		remove1 = document.getElementById('remove_row_' + position1).innerHTML
		remove2 = document.getElementById('remove_row_' + position2).innerHTML

		document.getElementById('remove_row_' + position1).innerHTML = remove2
		document.getElementById('remove_row_' + position2).innerHTML = remove1
	}
}


function move_friend_input_changed(e, member_to_move, member_id, current_pos, max_pos)
{
	global_count++;

	setTimeout('wait_function(' + global_count + ',' + member_to_move + ',' + member_id + ',' + current_pos + ',' + max_pos + ')', 1500);
}


function wait_function(count, member_to_move, member_id, current_pos, max_pos)
{
	if( count == global_count )
	{
		position_to_move = document.getElementById('change_position_' + current_pos).value;
		if( IsNumeric(position_to_move) )
		{
			if( (position_to_move < 1) || (position_to_move > max_pos))
			{
				// set start change back to true so they can use the input box again
				alert('Friend postion must be between 1 and ' + max_pos);
				document.getElementById('change_position_' + current_pos).value = current_pos;
				allowChange = 'TRUE';
			}
			else
			{
				friend_move_position(position_to_move, current_pos, member_id);
				document.getElementById('change_position_' + current_pos).value = current_pos;
			}
		}
		else
		{
			document.getElementById('change_position_' + current_pos).value = current_pos;

			// set start change back to true so they can use the input box again
			allowChange = 'TRUE';
		}
	}
}