﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//& Ramil aka Radioact				
/***************************/

$(document).ready(function(){
	//global vars	
	var inputMessage = $("#shout_message");
	var messageList = $("#shout_content");
	
	//functions
	function updateShoutbox(){
		//just for the fade effect
		messageList.hide();		
		//send the post to shoutbox.php
		$.post(virpath+'index.php?hp=1&m=shoutbox', {},
			function(response) {
				var response = response.split("|");
				if ( response.length == 2 )
				{
					if ( response[0] == 'ok' && response[1] )
					{
						messageList.html(response[1]);
						messageList.fadeIn(2000);
						return true;
					}
				}
	
			}
		);		
	}
	
	//on submit event
	$("#shout_form").submit(function(){
		if(inputMessage.attr("value")){			
			var message = inputMessage.attr("value");					
			//we deactivate submit button while sending
			$("#shout_send").attr({ disabled:true});
			$("#shout_send").blur();
			//send the post to shoutbox.php
			$.post(virpath+'index.php?hp=1&m=shoutbox&post=1', {'val':message},
				function(response) {
					var response = response.split("|");
					if ( response.length == 2 )
					{
						if ( response[0] == 'ok' )
						{
							inputMessage.val('');
							updateShoutbox();
							//reactivate the send button
							$("#shout_send").attr({ disabled:false});
						}
						else if ( response[0] == 'err' && response[1] ) 
						{
							$("#shout_send").attr({ disabled:false});
							alert(response[1]);							
						}
					}
		
				}
			);			
		}
		else
		{ 
			alert("Please fill message!");
		}
		//we prevent the refresh of the page after submitting the form
		return false;
	});
});

// shout deletion
function delete_shout(question,member_id, shout_id)
{
	var is_confirmed = confirm(question);

	if ( is_confirmed )
	{
		$.post(virpath+'index.php?hp=1&m=shoutbox&delete=1', {'member_id':member_id, 'item_id':shout_id},
			function(response) {
				var response = response.split("|");
				if ( response.length > 0 )
				{
					if ( response[0] == 'ok' && response[1] )
					{
						$('#shout_delete_link_' + shout_id).html(response[1]);
						setTimeout(function(){
							$('#shout_text_' + shout_id).animate({ opacity: "hide" }, "fast");
						}, 1000);
						return true;
					}
					else if ( response[0] == 'err' && response[1] )
					{
						$('#shout_delete_link_' + shout_id).html('<span style="color:red">' + response[1] + '</span>');
						return true;
					}
				}
			}
		);
	}
}
