var iLastId = 0;
var iLastIdChat = 0;
var iLastIdLog = 0;
var iChatOtherUserId = 0;

function refreshShoutbox (iUserIdChat, iUserIdShout)
{
	if (iUserIdChat)
	{
		this.iChatOtherUserId = iUserIdChat;
	}
	if (iUserIdShout)
	{
		this.iShoutOtherUserId = iUserIdShout;
	}
	
	var callback = function (aData) 
	{
		iLastId = parseInt(aData['last_id']);
		
		
		for (iUserId in aData['new_messages']) 
		{
			var iNewMessages = aData['new_messages'][iUserId];
		}
		
		
		//if (iNewMessages > 0)
		//{
			newItem('shout',iNewMessages);
		//}
		
		$('#shoutbox').html(aData['html']);
	}
	
	var callbackChat = function (aData) 
	{
		var iNewMessagesTotal = 0;
		
		$('#chatSelector .comboItem a').removeClass('online');
		
		for (iUserId in aData['online_users']) 
		{
			$('a.chatlist_' + iUserId).addClass('online');
		}
		
		$('#chatSelector .newMessages').html('');
		
		for (iUserId in aData['new_messages']) 
		{
			var iNewMessages = aData['new_messages'][iUserId];
			
			
			//console.debug('chat ',iNewMessages,iUserId);
			if (iNewMessages > 0)
			{
				$('.chat_msgcount_' + iUserId).html('(' + iNewMessages + ')');
			}
			
			iNewMessagesTotal += iNewMessages;
		}
		
		newItem('chat',iNewMessagesTotal);
		
		
		$('#chatbox').html(aData['html']);
		$('#chatSelector').html(aData['html_selector']);
	}
	
	//console.debug('Refresh shoutbox');
	$.getJSON ('/shoutbox/list/?actiontype=ajax&last_id=' + iLastId + '&user_id=' + this.iShoutOtherUserId, callback);
	$.getJSON ('/shoutbox/list/?actiontype=ajax&last_id=' + iLastIdChat + '&type=chat&user_id=' + this.iChatOtherUserId, callbackChat);
}

function poller ()
{
	refreshShoutbox();
	refreshLog();
	setTimeout('poller()',25 * 1000);
}



function removeShout (iId)
{
	var callback = function () 
	{
		refreshShoutbox();
	}
	$.getJSON ('/shoutbox/remove/?actiontype=ajax&id=' + iId, callback);
	return false;	// To prevent the page to follow the a href link
}

function readedShout (iId)
{
	$('#shout_' + iId).remove();
	
	var callback = function () 
	{
		refreshShoutbox(false);
	}
	$.getJSON ('/shoutbox/readed/?actiontype=ajax&id=' + iId, callback);
	
	return false;	// To prevent the page to follow the a href link
}

function addShout (oText, sType)
{
	if (oText.val() != '')
	{
		var callback = function () 
		{
			refreshShoutbox();
			oText.val('');
		}
		if (sType == 'chat')
		{
			var iOtherId = iChatOtherUserId;
		}
		else
		{
			var iOtherId = iShoutOtherUserId;
		}
		
		
		$.post ('/shoutbox/add/?actiontype=ajax&user_id=' + iOtherId, {type: sType, text : oText.val()},callback);
	}
	return false;	// To prevent the page to follow the a href link
}



function refreshLog (bNoLoader)
{
	var iHeight = $('#log').height();
	
	if (!bNoLoader)
	{
		$('#log').html('');
		$('#log').css('height',iHeight);
		$('#log').css({'background':'url(/library/icon/CSloader.gif) no-repeat center center'});
	}
	
	var callback = function (aData) 
	{
		iLastIdLog = parseInt(aData['last_id']);
		$('#log').css('height','');
		
		$('#log').html(aData['html']);
		$('#log').css({'height':'','background':''});
		//if (aData['item_count'] != 0)
		//{
			newItem('log',aData['item_count_total']);
			setLogTitle(aData['item_count']);
		//}
	}
	//console.debug('Refresh sb');
	$.getJSON ('/log/list/?actiontype=ajax&last_id=' + iLastIdLog, callback);
	return false;	// To prevent the page to follow the a href link
}

function pollLog ()
{
	//refreshLog();
	//setTimeout('pollLog()',10000);
}

function addLogItem (oType, oText, sDefault)
{
	$('#logError').css('display','none');
	$('#addLog textarea').removeClass('error');
	if (oText.val() != '' && oText.val() != sDefault)
	{
		var callback = function () 
		{
			refreshLog();
			oText.val('');
		}
		$.post ('/log/add/?actiontype=ajax', {type : oType.val(), text : oText.val()},callback);
	}
	else
	{
		$('#logError').css('display','block');
		$('#addLog textarea').addClass('error');
	}
	return false;	// To prevent the page to follow the a href link
}

function readedLog (iId)
{
	$('#log_' + iId).remove();
	
	var callback = function () 
	{
		refreshLog(true);
	}
	$.getJSON ('/log/readed/?actiontype=ajax&id=' + iId, callback);
	
	return false;	// To prevent the page to follow the a href link
}

function showDelete (oNode)
{
	var aNodes = oNode.getElementsByTagName('img');
	for (var i=0; i<aNodes.length; i++)
	{
	
		if (aNodes[i].className == 'deleteImage')
		{
			oNode.getElementsByTagName('img')[i].style.display = '';;		
		}
	}
	
}

function hideDelete (oNode)
{
	var aNodes = oNode.getElementsByTagName('img');
	for (var i=0; i<aNodes.length; i++)
	{
		if (aNodes[i].className == 'deleteImage')
		{
			oNode.getElementsByTagName('img')[i].style.display = 'none';	
		}
	}
}

function selectChatUser (iUserId)
{
	userSetting('last_chat_id',iUserId);
	$('#chatSelector .comboItem a').removeClass('active');
	$('.chatlist_' + iUserId).addClass('active');
	$('#cmbChat .title').html($('.chatlist_' + iUserId).html());
	$('#chatSelector').hide();
	refreshShoutbox(iUserId);
}

