
/**
 *	@package Chat
 */

/****** Variables ******/

/** Path to Chat Script  
 *	@var String
 */
if ( ! c2t_scriptUrl) {
	var c2t_scriptUrl = 'http://localhost/renovierung/_script_chat.php'; 
}
//var c2t_scriptUrl = 'http://www.renovieren.biz/renovierung/_script_chat.php';

/** Actual Chat Item Pointer
 *	@var Integer
 */
var c2t_id_i2m_last = null;
/** ID of last User which added an Item
 *	@var Integer
 */
var c2t_id_u2r_last = null;

/** Chat Object
 *	@var HTMLElement
 */
var _c2t_chat = null;
/** User Object
 *	@var HTMLElement
 */
var _c2t_user = null;
/** User Picture Path
 *	@var String
 */
var c2t_path_user_picture = null;

/****** Functions ******/

/**** Chat Functions ****/

/**
 * Starts the Chat and its Updates
 *
 *	@param DivElement _chat
 *	@param Integer id_i2m_last
 *	@param Integer id_r2m
 *	@param String path_user_picture
 */
function c2t_initializeChat(_chat, id_i2m_last, id_r2m, path_user_picture) {    

	var code = '';

	// Styling Chat
	_chat.style.overflow = 'auto';
	_chat.width = 350;

	// Sending Initialization Request
	var params = '';
	params += '?mode=create';
	params += '&id_i2m_last='+id_i2m_last;

	_c2t_chat = _chat;
	c2t_path_user_picture = path_user_picture;

	//code += '<div id="'+_chat.id+'">';
	code += a2x_sendRequest(c2t_scriptUrl, params, 'GET');
	//code += '</div>';

	o1t_innerHTML(_chat, code);
	
	if (document.getElementById('c2t_id_i2m')) {
		document.getElementById('c2t_id_i2m').value = id_i2m_last;
	}
	c2t_changeRoom(_chat, id_r2m, true);
	
	window.setInterval('c2t_updateChat();', 1000);
}
/**
 * Reads out new Elements and appends these to Chat
 * Will automatically be called by Interval
 * 
 * @uses c2t_appendItem()
 */
function c2t_updateChat() {
		
	if (document.getElementById('c2t_id_i2m')) {
		var id_i2m_last = document.getElementById('c2t_id_i2m').value;
	}
	
	// Reading Chat Messages
	var params = '';
	params += '?mode=update';
	params += '&id_i2m_last='+id_i2m_last;
	
	var code = '';
	code += a2x_sendRequest(c2t_scriptUrl, params, 'GET');
	eval(code);

	//o1t_innerHTML(_chat, code);
	
	// Appeding Chat Messages
	for(var i=0; i<Item.length; i++) {
		c2t_appendItem(_c2t_chat, Item[i]);
		c2t_id_i2m_last = Item[i].id;
	}
	
	_c2t_chat.scrollTo(0, 9999999);
	
	// Setting Last Item
	if (Item.length > 0) {
		c2t_id_i2m_last++;
	}
	if (c2t_id_i2m_last > 0) {
		document.getElementById('c2t_id_i2m').value = c2t_id_i2m_last;
	}
}
/**
 * Changes the Room and creates a Message
 * @param _chat
 * @param id_r2m
 * @param first
 * @return
 */
function c2t_changeRoom(_chat, id_r2m, first) {  
	
	if (document.getElementById('c2t_id_r2m')) {
		document.getElementById('c2t_id_r2m').value = id_r2m;
	}

	if ( ! first) {
		var _item = new Object();
		_item.type = 'info';
		_item.text = 'Sie haben einen privaten Chatraum betreten';
		c2t_appendItem(_chat, _item);
	}
}
/**
 * Creates and changes to a private Chat Room 
 * @param _chat
 * @param id_u2r_2
 */
function c2t_createPrivateRoom(_chat, id_u2r_2) {
	
	var params = '';
	params += '?mode=private';
	params += '&id_u2r_2='+id_u2r_2;
	
	id_r2m = a2x_sendRequest(c2t_scriptUrl, params);
	
	c2t_changeRoom(_chat, id_r2m, false);
}

/**** Item Functions ****/

function c2t_submitItem(_input, id_u2r, event) {     

	// if key press was on 'enter' or 'return'
	if (event.keyCode == 13 && _input.value != '') {
		
		var code = '';
		
		if (document.getElementById('c2t_id_i2m')) {
			var c2t_id_i2m_last = document.getElementById('c2t_id_i2m').value;
		}
		if (document.getElementById('c2t_id_r2m')) {
			var c2t_id_r2m = document.getElementById('c2t_id_r2m').value;
		}
		
		var params = '';
		params += '?mode=add';
		params += '&text='+_input.value;
		if (c2t_id_i2m_last) {
			params += '&id_i2m_last='+c2t_id_i2m_last;
		}
		if (c2t_id_r2m) {
			params += '&id_r2m='+c2t_id_r2m;
		}
	
		code = a2x_sendRequest(c2t_scriptUrl, params, 'GET');
		eval(code);
		
		_input = document.getElementById(_input.id);
		_input.value = '';
		_input.focus();
		
		for(var i=0; i<Item.length; i++) {
			c2t_appendItem(_c2t_chat, Item[i]);
			c2t_id_i2m_last = Item[i].id;
		}
		c2t_id_i2m_last++;
	
		document.getElementById('c2t_id_i2m').value = c2t_id_i2m_last;	
	}
}
function c2t_appendItem(_chat, _item) {   
	
	var code = '';
	
	if ( ! _item.type || _item.type != 'info') {
		code += '<table cellspacing="0" cellpadding="0">';
		code += '<tr>';
		code += '<td style="font-weight: bold;">';
		code += _item.time;
		code += '</td>';
		code += '<td width="10" nowrap></td>';
		code += '<td style="color: #1c4774; font-weight: bold;">';
		code += _item.user;
		code += '</td>';
		code += '<td width="10" nowrap></td>';
		code += '<td>';
		code += _item.text;
		code += '</td>';
		code += '</tr>';
		code += '</table>';
	}
	else {
		code += _item.text;
	}
	
	_node = document.createElement('div');
	//_node.setAttribute('class', 'consoleContainerElement');
	_node.innerHTML = code;
	
	_chat.appendChild(_node);
	//_chat.style.top = _chat.style.top + 50;
}

/**** User Functions ****/

function c2t_setLastUser(id_u2r, nickName, picture, type, gender) { 

	var _user = new Object();
	_user.id = id_u2r;
	_user.nickName = nickName;

	var infoCode = '';
	infoCode += '<table cellspacing="0" cellpadding="0">';
	infoCode += '<tr>';
	infoCode += '<td style="color: #1c4774; font-weight: bold;">'+nickName+'</td>'; 
	infoCode += '</tr>';
	infoCode += '<tr>';
	infoCode += '<td>'+type+'</td>'; 
	infoCode += '</tr>';
	infoCode += '</table>';
	
	var code = '';
	code += '<table cellspacing="0" cellpadding="0">';
	code += '<tr>';
	code += '<td>';
	code += '<img src="'+c2t_path_user_picture+picture+'" width="32" height="32"/>';
	code += '</td>';
  	code += '<td width="5" nowrap></td>';
	code += '<td>'+infoCode+'</td>';
	code += '</tr>';
	code += '</table>';

	o1t_innerHTML(_c2t_user, code);
	
	c2t_id_u2r_last = id_u2r;
}
function c2t_getLastUser() {
	
	return c2t_id_u2r_last;	
}
