
var reset=false;

function openChatWindow(user){
	window.open(baseUrl+'ajax/im/start/'+user, 'chat'+user, 'width=430,height=360,menubar=no,toolbar=no,location=no,scrollbars=no,status=no,titlebar=no,toolbar=no,directories=no,resizable=0');
}

function cehckIMRequests(){
	$.ajax({
		type: 'GET',
		url: baseUrl+'ajax/im/checkrequests',
		dataType: 'json',
		complete:function(){
			setTimeout('cehckIMRequests()', 30000);
		},
		success:function(data){
			var opn=false;
			reset=false;
			$('#imlist').html('');
			$('#newim').dialog('close');
			if(data.length>0){
				$('#newim').show();
			}
			$.each(data, function(i, request){
				opn=true;
				$('#imlist').append('<div><a href="javascript:openChatWindow(\''+request.id+'\');" >'+request.nick+'</a></div>');
			});
			if(opn){
				reset=true;
				$('#newim').dialog('open');
			}
		}
	});
}

function addFriend(user){
	$.ajax({
		type: 'GET',
		url: baseUrl+'ajax/friends/add/'+user,
		success:function(data){
			switch(data){
				case '0':
					alert(text_friend_added);
					break;
				case '1':
					alert(text_friend_accepted);
					break;
				default:
					alert(text_err_unknown);
					break;
			}
		}
	});
	
}

function removeFriend(user){
	$.ajax({
		type: 'GET',
		url: baseUrl+'ajax/friends/remove/'+user,
		success:function(data){
			showNewFriends();
		}
	});
	
}

function showNewFriends(){
	$('#newfriends').load(baseUrl+'ajax/friends/pendingfriends', null, function(){
		$(this).dialog('open');
		$('#newfriends form').each(function(){
			if($(this).attr('action')==''){
				$(this).attr('action', baseUrl);
			}else{
				$(this).attr('action', baseUrl+'/'+$(this).attr('action'));
			}
			$(this).ajaxForm(
				{
				target:'#newfriends'
				}
			);
		});
	});
	return false;
}

var tolist;
function openPMWindow(userid){
	tolist=new Object();
	tolist[userid]=true;
	$('#newmessage').load(baseUrl+'ajax/mailbox/new/'+userid, null, function(){
		$(this).dialog('open');
		$('#newmessage .messagefriends').hover(
		function(){
			$(this).children('.addfriend').show();
			$(this).css('background', '#f1f1de');
			},
		function(){
			$(this).children('.addfriend').hide();
			$(this).css('background', 'white');
			}
		);
		$('#newmessage .messagefriends .addfriend').hover(
		function(){
			$(this).css('text-decoration', 'underline');
			},
		function(){
			$(this).css('text-decoration', 'none');
			}
		);
		$('#newmessage form').each(function(){
			if($(this).attr('action')==''){
				$(this).attr('action', baseUrl);
			}else{
				$(this).attr('action', baseUrl+'/ajax/'+$(this).attr('action'));
			}
			$(this).ajaxForm(
				{
				target:'#newmessage'
				}
			);
		});
	});
}

function messageAddUser(userid, nick){
	if(!tolist[userid]){
		tolist[userid]=true;
		$('#tolist').append('<div><input name="to[]" type="hidden" value="'+userid+'" />'+nick+' <span onClick="$(this).parent().remove();tolist['+userid+']=false;" style="color:red; cursor:pointer;">X</span></div>');
	}
}

$(document).ready(function(){
	$('#newim').dialog({
		autoOpen:false,
		draggable:true,
		resizable:true,
		history:{expires:10},
		close:function(event, ui){
			if(reset){
				$.ajax({
					type: 'GET',
					url: baseUrl+'ajax/im/reset'
				});
			}
		}
	});
	$('#attackoptions').dialog({
		autoOpen:false,
		draggable:true,
		resizable:true,
		history:{expires:10}
	});
	$('#newfriends').dialog({
		autoOpen:false,
		draggable:true,
		resizable:true,
		history:{expires:10}
	});
	$('#newmessage').dialog({
		autoOpen:false,
		draggable:true,
		resizable:true,
		width:'500px',
		history:{expires:10}
	});
	cehckIMRequests();
});