function updateSnippets(response) {
	if (response.update_snippets) {
		for (var i in response.update_snippets) {
			var id = response.update_snippets[i][0];
			var holder = $("#" + id);
			if (holder.length !== 0) {
				holder.html(response.update_snippets[i][1]);
			}
		}
		return true;
	}
	return false;
}

var kChat = function() {
	var kChat = {
		timer: false,
		onsubmit: false,

		update: function() {
			if ($("#chat-content").length != 0) {
				var oldscrollHeight = $("#chat-content").attr("scrollHeight") - 20;
				$.ajax({
					url: './index.php?update_log=1&ajax=true',
					data: {},
					dataType: "json",
					type: "POST",
					success: function (response) {
						updateSnippets(response);
						var newscrollHeight = $("#chat-content").attr("scrollHeight") - 20;
						if(newscrollHeight > oldscrollHeight){
							$("#chat-content").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
						}
					}
				});
			} else {
				kChat.timer = clearInterval(timer);
			}
			return true;
		},

		submit: function(){
			var url = kChat.form.attr('action');
			var input_name = $("#name");
			var send_values = {};
			// najprv login
			if (input_name.length != 0) {
				if ( input_name.val() != '' ) {
					send_values['name'] = input_name.val();
				} else {
					alert('You are paranoid for a reason');
					input_name.focus();
					return false;
				}
			} else {
				// len chat 
				var input_msg = $("#usermsg");
				if (input_msg.length == 0 || input_msg.val() == '') {
					// nenasiel ani login ani chat input a radsej vsetko ukoncime
					alert('Oops! Something went wrong. You may be able to try again.');
					return false;
				}
				send_values['usermsg'] = input_msg.val();
				input_msg.val('');
			}
			url += '?ajax=true';
			// odosleme cez ajax formular
			$.ajax({
				url: url,
				data: send_values,
				dataType: "json",
				type: "POST",
				error: function(response) { alert('Oops! Something went wrong. You may be able to try again.'); }, // ak dojde k chybe vyskoci okno
				success: function (response) {
					updateSnippets(response);
					if (response.logged == 1) {
						kChat.initMenu();
						$("#usermsg").focus();
					}

					if ( $("#chat-content").length != 0 ) {
						if (typeof(kChat.timer) == 'undefined' || !kChat.timer) {
							kChat.timer = setInterval(kChat.update, 100013);
						}
						$("#chat-content").animate({ scrollTop: $("#chat-content").attr("scrollHeight")}, 'normal');
					}
				}
			});
			return false;
		},

		destroyMenu: function() {
			// TODO
		},

		initMenu: function() {
			var anchor_logout = $("#exit");
			if (anchor_logout.length != 0) {
				anchor_logout.click( function(e) {
					kChat.timer = clearInterval(kChat.timer);
					var url = anchor_logout.attr('href');
					url += '&ajax=true';
					$.ajax({
						url: url,
						data: {},
						dataType: "json",
						type: "POST",
						error: function(response) { alert('Oops! Something went wrong. You may be able to try again.'); }, // ak dojde k chybe vyskoci okno
						success: function (response) {
							kChat.destroyMenu();
							updateSnippets(response);
						}
					});
					return false;
				});
			}

			var anchor_show = $("#show-all");
			if (anchor_show.length != 0) {
				anchor_show.click( function(e) {
					var url = anchor_show.attr('href');
					url += '&ajax=true';
					$.ajax({
						url: url,
						data: {},
						dataType: "json",
						type: "POST",
						error: function(response) { alert('Oops! Something went wrong. You may be able to try again.'); }, // ak dojde k chybe vyskoci okno
						success: function (response) {
							updateSnippets(response);
						}
					});
					return false;
				});
			}

			var anchor_online = $("#online");
			if (anchor_online.length != 0) {
				anchor_online.click( function(e) {
					var url = anchor_online.attr('href');

					url += '&ajax=true';

					if ( $("#online-box").is(':visible') ) {
						url += '&online=0';
					} else {
						url += '&online=1';
					}

					$.ajax({
						url: url,
						data: {},
						dataType: "json",
						type: "POST",
						error: function(response) { alert('Oops! Something went wrong. You may be able to try again.'); }, // ak dojde k chybe vyskoci okno
						success: function (response) {
							kChat.destroyMenu();
							updateSnippets(response);
							if ( $("#online-box").is(':visible') ) {
								$("#online-box").hide();
								$("#chat-content").show();
							} else {
								$("#chat-content").hide();
								$("#online-box").show();
							}
						}
					});

					return false;
				});
			}
		},

		initForm: function() {
			this.form = $("#form-holder form");
			if (this.form && this.form.length > 0) {
				this.form.submit ( function (e) {
					kChat.submit();
					return false;
				});
			}
		},



		init: function() {
			kChat.initMenu();
			kChat.initForm();
			if ( $("#chat-content").length != 0 ) {
				$("#chat-content").animate({ scrollTop: $("#chat-content").attr("scrollHeight") }, 'normal');
				if (typeof(kChat.timer) == 'undefined' || !kChat.timer) {
					kChat.timer = setInterval(kChat.update, 15000);
				}
			}
		}
	}
	return kChat.init();
}

$(function() {
	$("#myTabs1").tabs();

	var myChat = new kChat();
});
