(function($){
	$.fn.ajaxShoutbox = function(params){
		
		var params = $.extend({
			refresh:3,
			limit: 15,
			remove: true
		},params);
		
		var shoutbox = function (jElt) {
			//jElt is the jQuery object where the function starts
			var shoutbox=jElt.find('.shoutbox'); // this is div containing the messages
			var writeInput=jElt.find('.shoutboxWriteInput');
			
			// handle the submit message function
			var activateKeyboard = function(){
				writeInput.submit(function(){
				
					var input = $(this).find('textarea');
					var game_id = $(this).find(":hidden[name='game_id']").val();
					var comment = input.val();
					
					if ($.trim(comment).length > 0) { // need to have something to say !
						
						input.val('');
						input.attr("disabled", "disabled"); // we have to this so there'll be less spam messages
						
						var link = writeInput.attr("action");
						
						$.post(link, {comment: comment, game_id: game_id}, function(data){
							input.removeAttr("disabled");
							input.focus();
							if (data) {
								shoutbox.empty();
								$.each(data, function(i,msg){
									if(msg.comment) {
										//if(params.remove) shoutbox.find("div").eq(params.limit-1).remove();
										
										if(shoutbox.find("div:first").hasClass("grid1")) {
											new_class = "grid2";
										} else {
											new_class = "grid1";
										}
										shoutbox.prepend('<div class="'+new_class+'"><div class="head"><strong>'+ msg.username +'</strong> '+msg.time+'</div><div class="text">'+msg.comment+'</div></div>');
									}
								});
							}
							
						}, 'json');
					}
					
					return false;
				});
			}
				
			makeShoutboxHtml = function() {
				if (typeof shouts == "undefined") {
					setTimeout("makeShoutboxHtml()", 1000);
					return;
				}
				shoutbox.empty();
				$.each(shouts, function(i,msg){
					if(msg.comment) {
						//if(params.remove) shoutbox.find("div").eq(params.limit-1).remove();
								
						if(shoutbox.find("div:first").hasClass("grid1")) {
							new_class = "grid2";
						} else {
							new_class = "grid1";
						}
						shoutbox.prepend('<div class="'+new_class+'"><div class="head"><strong>'+ msg.username +'</strong> '+msg.time+'</div><div class="text">'+msg.comment+'</div></div>');
					}
				});
			};
			
			// handle the read messages function
			readMessages = function(){
			
				var link = jElt.find('.shoutboxCacheName').attr("value");
				
				var url = link;

				headElement = document.getElementsByTagName("head").item(0);
			
				var scriptTag = document.createElement("script");
				scriptTag.setAttribute("id", "factorialJSON");
				scriptTag.setAttribute("type", "text/javascript");
				scriptTag.setAttribute("src", url);
				headElement.appendChild(scriptTag);
				
				makeShoutboxHtml();
				setTimeout("readMessages()",params.refresh*1000);
				
			}
			
			//setTimeout(readMessages,params.refresh*1000);
			activateKeyboard();
			readMessages();
		}
		
		return this.each(function(){
			shoutbox($(this));
		});		
	};
})(jQuery)
