<?php
/*
 * TODO dizajn zobrazenia celeho bocneho panelu a dialogoveho okna 
*/
function send_message($db,$id,$komu, $predmet,$text){
	$sql= "INSERT INTO `spravy`
			( `id` , `odosielatel` , `predmet` , `text` , `precitane` , `primatel` , `datum_odoslania`)
			VALUES ( NULL , ?, ?, ?, -1, ?, ?); ";

	$db->queryWithBids($sql, array($id,$predmet,$text,$komu,date('Y-m-d H:i:s') ) );
}

/*
 * vrati pole ludi co boli online posledne 3 minuty
*/
function online_users($db, $id,$klub){
	$sql= 'SELECT * FROM `users` WHERE last_login>= DATE_SUB(NOW(), INTERVAL 3 MINUTE) AND id!='.$id.' AND klub_id='.$klub.'';
	return $db->query($sql)->fetchAll();
}

/*
 * vrati pole ludi co boli offline posledne 3 minuty
*/
function offline_users($db, $id){
	$sql= 'SELECT *
			FROM `users`
			WHERE last_login < DATE_SUB( NOW( ) , INTERVAL 3
			MINUTE ) AND id!='.$id.'  
			ORDER BY `last_login` DESC';
	return $db->query($sql)->fetchAll();
}



$str = '<div id="chat">';

$str .= '<script type="text/javascript" src="http://cms.badmintonovaskola.sk/core/tmpl/chat_ajax.js"></script>';

$pridajStr= '<div class="chatBox" > <div class="controls" > <a href="" onclick= "Close($(this)); return false;"><i class="icon-remove-sign"></i></a> odosielatel: <span>\'+spravy[i].odosielatel+\'</span> </div> <div class="textBox" > \'+spravy[i].text+\'</div> <input id="text" type="text" value="" maxlength="47" size="40">  </div> ';

$str .= '<script type="text/javascript">

		$(function() {

		/*
	 * kazde 2 sekundy spusti ajax
	 */

		setInterval(function() { spustiAjax('.$this->oSession->idUser.',function(results) {
				var spravy = jQuery.parseJSON(results);
				//kazdu spravu
				for (i = 0; i < spravy.length; ++i) {
				ok=true;
				//ak je otvorene okno s rovnakym clovekom napis to tam
				$(\'.chatBox .controls\').each(function( index ) {
				if ($(this).find(\'span\' ).text()==spravy[i].odosielatel) {
				$(this).parent().find(\'.textBox\' ).append(\'<br >\'+spravy[i].text);
				ok=false;
}
});
				//vypis spravu
				if (ok) $(\'#chatBoxContainer\').append(\''.$pridajStr.'\');
}

})}, 2000);
});

						$(\'.chatBox\').live(\'keydown\' ,function(e){
						if (e.keyCode==13) {
	    	if ($(this).find(\'input\').val() ){
						spustiAjaxOdosli( '.$this->oSession->idUser.', $(this).find(\'.controls span\').text(), $(this).find(\'input\').val() );
								//odoslal spravu
								$(this).find(\'.textBox\').append(\'<br > me: \'+$(this).find(\'input\').val());
								$(this).find(\'input\').val("");
}
}

});

								</script>';

$str.= "<h1>Chat</h1>";

//$str .= '<h1> Online uzivatelia:</h1>';

$uzivatelia=online_users($this->oDb,$this->oSession->idUser,$_SESSION['klubId']);
foreach ( $uzivatelia as $uzivatel ){
	$str .=  '<a href="#" onclick= "spustiAjax2( ' .$this->oSession->idUser.', ' .$uzivatel['id'].', 1 , function(results) {

			var spravy = jQuery.parseJSON(results);
			 
			var text;
			for (i = 0; i < spravy.length; ++i) {
			text += spravy[i].text+\'<br>\';
}
			OpenConverzationBox(' .$uzivatel['id'].', text);
}
					); return false;" ><div class="online">' .$uzivatel['priezvisko']. " </div></a>";
}

if (sizeof($uzivatelia)>0) $str.= '<div class="delimeter"></div>';

//$str.= '<h1> Offline uzivatelia:</h1>';

$uzivatelia=offline_users($this->oDb,$this->oSession->idUser);
foreach ( $uzivatelia as $uzivatel ){
	$str .=  '<a href="#" onclick= "spustiAjax2( ' .$this->oSession->idUser.', ' .$uzivatel['id'].', 1 , function(results) {

			var spravy = jQuery.parseJSON(results);

			var text;
			for (i = 0; i < spravy.length; ++i) {
			text += spravy[i].text+\'<br>\';
}
			OpenConverzationBox(' .$uzivatel['id'].', text);
}
					); return false;" ><div class="offline">' .$uzivatel['priezvisko']. "</div> </a>";
}

$str .= "";

/*
 * odoslanie spravy
*/
if (       isset($_POST['predmet'])
		&&  isset($_POST['text']) && isset($_POST['odosli'])
		&& isset($_POST['komu']))
{
	$text= addslashes(strip_tags(trim($_POST['text'])));
	send_message($this->oDb, $this->oSession->idUser, $_POST['komu'] , $_POST['predmet'], $_POST['text']);
}

/*
 * formular na odoslanie
*/

$form = "";
$form .= '<form name="send_message" id="send_message" action="'.$this->oUrl->href().'" method="post">';
$form .= '<label for="komu"><strong>primatel</strong> <br /> </label> <input
		name="komu" type="text" size="40" maxlength="47" id="komu"
		value="'.(isset($_POST["komu"])==1 ? $_POST["komu"] : "").'">';

$form .= '<label for="predmet"><strong>Predmet:</strong> <br /> </label> <input
		name="predmet" type="text" size="40" maxlength="47" id="predmet"
		value="'. (isset($_POST["predmet"])==1 ? $_POST["predmet"] : "").' ">';

$form .= '<label for="text"><strong>Zadajte text spravy:</strong> <br /> </label>
		<textarea rows="20" cols="47" id="text" name="text">'.(isset($_POST["text"])==1 ? $_POST["text"] : "").'
				</textarea>
				<br />';
$form .= '<input name="odosli" type="submit" id="submit" value="odosli!" />';

//$str .= $form;

$str .='</div>';

return $str;

?>
