/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright (C) 2002 - 2008 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: jquery_shoutbox.js
| Author: bartek124
| E-Mail: bartek124@php-fusion.pl
| Web: http://bartek124.php-fusion.pl
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/


var autorefresh;
var flood_control;
var refresh_control;

$(function() {
	$("textarea[name='shout_message']").keyup(function() {
		var count_chars = $(this).val().length;
		if (count_chars > 200) {
			var new_value = $(this).val().substring(0, 200);
			$(this).val(new_value)
		}
		$("div#shout_count").html(""+(200 - $(this).val().length)+"");
	});

	$("input[name=\'refresh_shout\']").css("display", "inline");
	prepare_shoutbox();
	autorefresh = setInterval("update_messages();", 10000);
	$("div#shout_preloader").css({"height": $("form[name='shout_form']").height()});
	$("img#sshout_preloader").css("margin-top", (($("form[name='shout_form']").height()/2)-32));
	$("input[name='edit_shout']").click(function() { 
		if($("textarea[name='shout_message']").val() == "") { alert(locale['jsp_003']); return false; }
		$("form[name='shout_form']").fadeOut("fast", function() {
			$("div#shout_preloader").fadeIn("fast", function() {
				$.post(basedir+"infusions/jquery_shoutbox_panel/jquery_shoutbox.php", {
					get_shouts: 1,
					action: "sedit",
					shoutid: $("input[name='edited_id']").val(),
					message: $("textarea[name='shout_message']").val() 
				}, function(data) {
					$("div#shout_preloader").fadeOut("slow", function() {
						$("div#shout_count").html("200");
						$("textarea[name='shout_message']").val("");
						$("input[name='post_shout']").css("display", "inline");
						$("input[name='edit_shout']").css("display", "none");
						$("form[name='shout_form']").fadeIn("slow", function() {
							$("div#shout_messages").empty();
							prepare_messages(data);
							parsesmileys();
							delete data;
						});
					});			
				}, "xml");
			});
		});
	return false;
	});
	
	$("input[name='post_shout']").click( function() {
		if($("input[name='shout_name']").val() == "") { alert(locale['jsp_002']); return false; }
		if($("textarea[name='shout_message']").val() == "") { alert(locale['jsp_003']); return false; }
		clearInterval(flood_control);
		$.post(basedir+"infusions/jquery_shoutbox_panel/jquery_shoutbox.php",{
			action: "captcha",
			sb_captcha: $("input[name='sb_captcha_code']").val()
		}, function(data) {
			if(imember == true || data == "ok") {
				$("input[name='post_shout']").attr("disabled", "disabled");
				$("form[name='shout_form']").fadeOut("fast", function() {
					$("div#shout_preloader").fadeIn("fast", function() {
						$.post(basedir+"infusions/jquery_shoutbox_panel/jquery_shoutbox.php", {
							get_shouts: 1,
							action: "add",
							name: $("input[name='shout_name']").val(), 
							message: $("textarea[name='shout_message']").val()
						}, function(data) {
							$("div#shout_preloader").fadeOut("slow", function() {
								$("div#shout_count").html("200");
								$("textarea[name='shout_message']").val("");
								if($("input[name='shout_name']").attr("type") == "text") {
									$("input[name='shout_name']").val("");
								}
								$("form[name='shout_form']").fadeIn("slow", function() {
									flood_control = setInterval("unlock_button('post_shout', '"+flood_control+"');", floodinterval+"000");
									$("div#shout_messages").empty();
									prepare_messages(data);
									parsesmileys();
									delete data;
									if(imember == false) {
									document.getElementById('sb_captcha').src = 'includes/securimage/securimage_show.php?sid=' + Math.random();
									}
								});
							});
						});
					});
				});
			} else {
				alert(locale['jsp_004']);
				document.getElementById('sb_captcha').src = 'includes/securimage/securimage_show.php?sid=' + Math.random(); 
				return false;
			}
		});
	return false;
	});
	
	$("input[name='refresh_shout']").click( function() {
		clearInterval(refresh_control);
		$("input[name='refresh_shout']").attr("disabled", "disabled");
		$("div#shout_messages").fadeTo("fast", 0.3, function() {
			$.post(basedir+"infusions/jquery_shoutbox_panel/jquery_shoutbox.php", {
				get_shouts: 1
			}, function(data) {
				refresh_control = setInterval("unlock_button('refresh_shout', '"+refresh_control+"');", 2500);
				$("div#shout_messages").empty();
				prepare_messages(data);
				parsesmileys();
				delete data;
				$("div#shout_messages").fadeTo("fast", 1.0);
			}, "xml");
		});
	});
	
	
	
});

function unlock_button(button, intervalid) {
	$("input[name='"+button+"']").removeAttr("disabled");
	clearInterval(intervalid);
}

function prepare_shoutbox() {
	var delete_links = $("a.delete_shoutbox_message");
	delete_links.each(function(i) {
		$(delete_links[i]).attr({href: "javascript:delete_message("+$(delete_links[i]).attr("id")+");"});
	});
	delete delete_links;
	
	var edit_links = $("a.edit_shoutbox_message");
	edit_links.each(function(i) {
		$(edit_links[i]).attr({href: "javascript:edit_message("+$(edit_links[i]).attr("id")+");"});
	});
	delete edit_links;
}

function update_messages() {
	$.post(basedir+"infusions/jquery_shoutbox_panel/jquery_shoutbox.php", {
		get_shouts: 1
	}, function(data) {
		$("div#shout_messages").empty();
		prepare_messages(data);
		parsesmileys();
		delete data;
	}, "xml");
}

function prepare_messages(data) {
	var i = 0;
	$("shout",data).each(function(id) {
		var shout = $("shout",data).get(id);
		if($("authoruname",shout).text() == "") {
			var author = $("authorname",shout).text();
		} else {
			var author = "<a href='"+basedir+"profile.php?lookup="+$("authorname",shout).text()+"' class='side'>"+$("authoruname",shout).text()+"</a>";
		}
		if($("access",shout).text() == 1) {
			var actions = "[<a href='javascript:edit_message("+$("shoutid",shout).text()+");' class='side'>"+locale['global_076']+"</a>] [<a href='javascript:delete_message("+$("shoutid",shout).text()+");' class='side'>"+locale['global_157']+"</a>]<br />";
		} else {
			var actions = "";
		}
		if(i == 0) { var br = ""; var br2 = "<br />"; } else if(i == 1) { var br = ""; var br2 = "";} else { var br = "<br />"; var br2 = ""; }
		$("#shout_messages").append("<div id='shout_"+$("shoutid",shout).text()+"'>"+br+"<div class='shoutboxname'>"+author+"</div>"
			+"<div class='shoutboxdate'>"+$("datestamp",shout).text()+"</div>"
			+"<div class='shoutbox'>"+$("message",shout).text()+"</div>"+actions+br2+"</div>");
		i++;
	});
	delete data;
}

function parsesmileys() {
	var smileys = $("img.smiley");
	$("img.smiley").each(function(i) {
		var img = $(smileys[i]).attr("alt");
		$(smileys[i]).attr({ "src": basedir+"images/smiley/"+img });
		$(smileys[i]).removeClass("smiley");
	});
	delete smileys;
}

function delete_message(shout_id) {
	$("div#shout_"+shout_id).fadeTo("fast", 0.3, function() {
		$.post(basedir+"infusions/jquery_shoutbox_panel/jquery_shoutbox.php", {
			get_shouts: 1,
			action: "delete", 
			shoutid: shout_id
		}, function(data) {
			$("div#shout_"+shout_id).slideUp("slow", function() {
				$("div#shout_messages").empty();
				prepare_messages(data);
				parsesmileys();
				delete data;
			});
		}, "xml");
	});
}

function edit_message(shout_id) {
	$("form[name='shout_form']").fadeOut("fast", function() {
		$("div#shout_preloader").fadeIn("fast", function() {
			$.post(basedir+"infusions/jquery_shoutbox_panel/jquery_shoutbox.php", {
				action: "ledit",
				shoutid: shout_id
			}, function(shout) {
				$("input[name='edited_id']").val(shout_id);
				$("div#shout_preloader").fadeOut("slow", function() {
					$("textarea[name='shout_message']").val(shout);
					$("div#shout_count").html(""+(200 - $("textarea[name='shout_message']").val().length)+"");
					$("input[name='post_shout']").css("display", "none");
					$("input[name='edit_shout']").css("display", "inline");
					$("form[name='shout_form']").fadeIn("slow");
				});
			});
		});
	});
}
