var getUrl = window.location;
var baseUrl = getUrl.protocol + "//" + getUrl.host;
var chatbox = baseUrl + '/guestbook/chat.php';
var loadcontent = '<div class="gmenu"><p align="center"><button class="aan-black"> <i class="icon-spinner icon-spin icon-large"></i>  Loading messages... </button></p></div>';
$(document).ready(function() {
    $("#idChat").html(loadcontent);

    $.get(chatbox, function (html) {
        $("#idChat").html(html).hide().slideDown("slow");
    });

	reload_chat = setInterval(function() {
		$.get(chatbox, function (html) {
			$("#idChat").html(html);
		});
	}, 4000);
	var form = $('#form'); // contact form
	var submit = $('#submit');	// submit button
	var alert = $('#alert'); // alert div for show alert message
	var text = $('#postText');

	// form submit event
	form.on('submit', function(e) {
		e.preventDefault(); // prevent default form submit
		// sending ajax request through jQuery
		if (text == '') {
			alert.show();
			alert.text('You have not entered content !!!');
			$('#postText').focus();
			return false;
		}
		$.ajax({
			url: '../guestbook/chat.php', // form action url
			type: 'POST', // form submit method get/post
			timeout: 5000,
			dataType: 'html', // request type html/json/xml
			data: form.serialize(), // serialize form data 
			beforeSend: function() {
				alert.fadeOut();
				submit.html(' <i class="icon-spinner icon-spin"></i> Sending...'); // change submit button text
			},
			success: function(data) {
				$.get(chatbox, function (html) {
					$("#idChat").html(html).hide().slideDown("slow");
				});
				form.trigger('reset'); // reset form
				$('#postText').focus();
				$('#postText').val('');
				submit.html('Send'); // reset submit button text
			},
			error: function(e) {
				console.log(e)
			}
		});
	});
});