	//TODO
// OPTIMERA JQUERY KEDJNINGEN
// FADE IN NYA INLÄGG

var ajax_handler = function(form, loader, list) {
	var data_handler = function() {
		var value, interval,
		length = 0,
		current = 0,
		error_interval,
		updates = 0,
		update_seconds = 20;
		this.add = function(item) {
			clearInterval(interval);
			var item_values = {namn: item[0].value, djur: item[2].value};
			value.push(item_values);
			current = length;
			length++;
			list.find("ul.error").fadeOut("slow", function() { $(this).remove()})
			this.handler("new");
			current++	
			interval = setInterval(this.handler, 4000);
		};
		this.initiate = function(json) {
			value = parse(json).data;
			length = value.length
			current = 0;
			interval = setInterval(this.handler, 4000);
			
			var limit = (length < 4) ? length : 4 ;
				
			list.find("ul").empty();
				
			while(limit--) {
				list.find("ul").append("<li><strong>" + value[limit].namn + "</strong> is a "+ value[limit].djur +"</li>");
			}
		};
		this.handler = function(type) {

			var data_name = (value[current].namn != undefined) ? value[current].namn : value[current][0],
			data_type = (value[current].djur != undefined) ? value[current].djur : value[current][1];
			
			current++;
			if(current == length) current = 0;
			
			list.find("ul[class!=error] li:last").fadeOut("fast", function() { $(this).remove()});
			list.find("ul[class!=error] li:first").before("<li><strong>" + data_name + "</strong> is a "+ data_type +"</li>");
			list.find("ul[class!=error] li:first").hide().slideDown(1000);
		};
		this.error = function(type, error) {
			if(type == "get") {
				clearInterval(interval);
				error_interval = setInterval(this.errorUpdate, 1000);
				list.find("ul").empty();
				list.find("ul").append("<li class=\"error\"><strong>It didn't work! Trying again in " + update_seconds + " seconds.</strong></li>");
			}
			else {
				list.find("ul.error").remove()
				list.prepend("<ul class=\"error\"><li class=\"error\"><strong>It didn't work! Try again later.</strong></li></ul>");
				list.find("ul li:first").hide().fadeIn("slow");
			}
		};
		this.errorUpdate = function() {
			updates++;
			list.find("ul li.error").html("<strong>It didn't work! Trying again in "  + (update_seconds - updates) + " seconds.</strong>");
			if(updates == update_seconds) {
				clearInterval(error_interval);
				get();
			}
		}
	};
	
	var loader = $(loader),
	form = $(form),
	list = $(list),
	post_size = 10,
	handler = new data_handler();
	
	this.post = function() {
		loader.show();
		form_data = form.serialize();
	    form_data_json = form.serializeArray();
		$.post("insert.php?mode=ajax", form_data, 
			function(data) {
				loader.hide();
				form.clearForm();
			  	if(parse(data).result == "true")
			  		handler.add(form_data_json);
			  	else
			  		handler.error("post");
		});
	};
	
	this.get = function() {
		get();
	}

	function parse(src) {
	    if (typeof(JSON) == 'object' && JSON.parse)
			return JSON.parse(src);
		return eval("(" + src + ")");
	}
	
	function get() {
		$.getJSON("get.php", {size: post_size}, function(data, error) {
			if(error == "success") {
				if(parse(data).result == "true") {
					handler.initiate(data);
				}
				else {
					handler.error("get", error);
				}
			}
			else {
				handler.error("get", error);
			}
		});
	}
	
};
