<?php
session_start ();

include_once 'includes/inter.php';

?><!DOCTYPE html><html><head>
		<?php include 'includes/meta.php'; ?>
</head><body>	<script type="text/javascript" src="he.js"></script>	<script type="text/javascript"		src="http://code.jquery.com/jquery-latest.min.js"></script>
		<?php include 'includes/header.php'; ?>
		<br />
		<?php
		
		$disc = getDiscutInfos ();
		
		$adminLevel = - 1;
		
		if (isConnected ())
			
			$adminLevel = getAdminLevel ();
		
		if (! ($disc ['autorized'] === 'p' or (preg_match ( "#^a[0-9]+$#", $disc ['autorized'] ) == 1) ? (intval ( substr ( $disc ['autorized'], 1 ) ) <= $adminLevel) : FALSE or preg_match ( '#^x([0-9];)*' . $_SESSION ['session_id'] . '(;[0-9])*$#', $disc ['autorized'] ) == 1 or $adminLevel >= 14)) {
			
			header ( 'Location:403.php' );
			
			goto end;
		} else if (! discutExists ()) {
			
			header ( 'Location:40A.php' );
			
			goto end;
		} 

		else if (isConnected ()) {
			
			?>
			<form id="postMessageForm"		action="posting_message.php?d=<?php echo $_GET['d'];?>" method="post">		<textarea row="5" placeholder="Votre message :" name="msg"			autofocus="autofocus" required="required"></textarea>		<input type="submit" value="Poster le message">	</form>	
	<!-- Entrée->envoyer le message (dégeulasse) -->	<script type="text/javascript">				$('#postMessageForm textarea').on('keypress',function(e){					if(e.which == 13){						$('#postMessageForm input').trigger('click');						e.stopPropagation();					}				});			</script>
		<?php }else{ ?>
			<div>Vous devez vous connecter pour envoyer des messages !</div>
		<?php } ?>
		
		<main> </main>	<script type="text/javascript">	var oldTimeout = setTimeout;	window.setTimeout = function(callback, timeout) {	  console.log("timeout started");	  return oldTimeout(function() {	    console.log('timeout finished');	    callback();	  }, timeout);	}		
		function reloadEditListeners(){
			$('.editLink').on('click',function(e){
				editingLink = e.target;
				e.target.innerHTML = 'Edition';

				$('#editMessageFullScreen').css('display','block');
				$('#editMessageFullScreen div div form').attr('action','editing_message.php?d='+$(e.target).data('messageID'));
				$('#editMessageFullScreen').animate({opacity:0.97},1000);
				e.stopPropagation();
			});
			$('.delLink').on('click',function(e){
				//console.log(e);				e.target.innerHTML = 'Supression';				window.clearTimeout(currentClock);
				$.post('deletingMessage.php?m='+$(e.target).data('messageID'),{},askData);
				e.stopPropagation();
			});

			$('#abortEdition').on('click',function(e){
				editingLink.innerHTML = 'Editer';
				$('#editMessageFullScreen div div form').attr('action','js_waiting');
				$('#editMessageFullScreen').animate({opacity:0.00},1000,function(e){
					$('#editMessageFullScreen').css('display','none');
				});
			});
		}
	</script>	<!-- Messages download -->	<script type="text/javascript">			var currentClock = null;
			
			var askData = function(){
				$.post('getDiscutsMessages.php?d=<?php echo testInt($_GET['d'],'40A.php');?>',{},dataGet);
			}
			var dataGet = function(data,status){
				var newData = eval(data);
				newData = newData.sort(function(a,b){return b.sendTimestamp-a.sendTimestamp;});
				var messageGlobal = document.createElement('main');
				for(messagePos in newData){
					var message = newData[messagePos];
					//console.log(message);
					var section = document.createElement('section');
					section.className = 'message';
					var messageText = document.createTextNode(he.decode(message.texte).replace(/\n/g, "<br />"));
					section.appendChild(messageText);
					section.appendChild(document.createElement('br'));
					var messageData = document.createElement('div');
					var pseudoText = document.createTextNode(message.pseudo);
					var pseudoTimeSeparator = document.createTextNode('    ');//Quatre espaces
					var time = document.createElement('time');
					var sendTimeText = document.createTextNode(message.sendTime);
					time.appendChild(sendTimeText);
					messageData.appendChild(pseudoText);
					messageData.appendChild(pseudoTimeSeparator);
					messageData.appendChild(time);
					section.appendChild(messageData);
					if(message.rights){
						var editPane = document.createElement('nav');
						editPane.className = 'editPaneMessages';
						var editLink = document.createElement('a');
						editLink.className = 'editLink';
						$(editLink).data('messageID',message.messageID);
						editLink.appendChild(document.createTextNode('Editer'));
						var delLink = document.createElement('a');
						delLink.className = 'delLink';
						$(delLink).data('messageID',message.messageID);
						delLink.appendChild(document.createTextNode('Supprimer'));
						editPane.appendChild(editLink);
						editPane.appendChild(document.createTextNode('    '));
						editPane.appendChild(delLink);
						section.appendChild(editPane);
					}
					messageGlobal.appendChild(section);
				}
				document.body.replaceChild(messageGlobal,document.getElementsByTagName('main')[0]);
				reloadEditListeners();				
				currentClock = setTimeout(askData,15000);
			}
			$(document).ready(askData);			
		</script>
			<div id="editMessageFullScreen" class="fullscreen absent">
			<div class="screen-vertical-centered">
				<div class="screen-horizontal-centered">
  					<form id="editMessageForm" action="js modified" method="post">
						<textarea row="5" placeholder="Le nouveau message :" name="msg" autofocus="autofocus" required="required"></textarea>
						<input value="Modifier le message" type="submit">
						<input id="abortEdition" value="Annuler" type="button"/>
					</form>
				</div>
			</div>
		</div>
		<?php include 'includes/footer.php'; ?>
	</body></html><?php end: ?>