$(document).ready(function()
{
	//
	// Skróty wiadomości
	//
	$(".qnews-side a").attr("href", "javascript:void(0)");
	$(".qnews-side a").click(function(){

		$("#qnews").html("<div style=\"width: 100%; text-align: center; margin-top: 25px; font-size: 14px; line-height: 25px\">Pobieranie informacji.<br />Proszę czekać ...<br /><br /><img src=\"styles/img/yellow_indicator.gif\" alt=\"\" /></div>");
		$.get("response.xml.php", {
			mode: "qnews",
			sn: $(this).attr("title")

		}, function(xml) {
			$("#qnews").html("");

			$(".qnews-prev-side").css("display", $("prev_display", xml).text());
			$(".qnews-prev-side a").attr("title", $("prev", xml).text());
			$(".qnews-next-side").css("display", $("next_display", xml).text());
			$(".qnews-next-side a").attr("title", $("next", xml).text());

			$("qnews", xml).each(function(id)
			{
				qid = $("qnews", xml).get(id);

				$("#qnews").append("<span class=\"subject-news\" style=\"margin-top: 6px\">" +
								"<span class=\"link-news\" style=\"width: 260px; font-size: 13px\"><a href=\"" + $("link", qid).text() + "\" title=\"" + $("title", qid).text() + "\" style=\"text-decoration: none\">" + $("title_lower", qid).text() + "</a></span>" +
								"<span class=\"date-news\" style=\"width: 38px; background: #FFFFFF; font-size: 10px; color: #CA250C\">" + $("date", qid).text() + "</span>" +
							"</span>");

			});
		});
	});














	$(".border-side-hover").mouseover(function()
	{
		$(this).removeClass("border-grow").addClass("border-yellow");
	});

	$(".border-side-hover").mouseout(function()
	{
		$(this).removeClass("border-yellow").addClass("border-grow");
	});

	if( getCookie("fontbold") == 1 )
	{
		$("#form #fontbold").val("1");
		$("#form #textb").css("background", "#CCCCCC");
	} else {
		$("#form #fontbold").val("0");
		$("#form #textb").css("background", "white");
	}

	if( getCookie("fontitalic") == 1 )
	{
		$("#form #fontitalic").val("1");
		$("#form #texti").css("background", "#CCCCCC");
	} else {
		$("#form #fonttitalic").val("0");
		$("#form #texti").css("background", "white");
	}

	if( getCookie("fontunderline") == 1 )
	{
		$("#form #fontunderline").val("1");
		$("#form #textu").css("background", "#CCCCCC");
	} else {
		$("#form #fontunderline").val("0");
		$("#form #textu").css("background", "white");
	}

	colordisplayset(getCookie("color"));

	if( getCookie("autoscroll") == "0" )
	{
		$("#autoscroll_icon").attr("src", "styles/css/images/scroll_stop.gif");
		$("#autoscroll_icon").attr("title", "Włącz automatyczne przewijanie okna rozmowy");
		$("#autoscroll").val("0");
	}
	else
	{
		$("#autoscroll_icon").attr("src", "styles/css/images/scroll.gif");
		$("#autoscroll_icon").attr("title", "Wyłącz automatyczne przewijanie okna rozmowy");
		$("#autoscroll").val("1");
	}

	$("#autoscroll_icon").click(function()
	{
		autoscroll_icon($("#autoscroll").val());
	});
	
	$("#form #message").keyup(function(k)
	{
		if( k.keyCode == 13 )
		{
			msg = $("#form #message").val();
			$("#form #message").val("");

			if( msg )
			{
				$.post("chat.xml.php", {
					sid: $("#form #session_id").val(),
					mid: $("#form #message_id").val(),
					color: $("#form #color").val(),
					bold: $("#form #fontbold").val(),
					italic: $("#form #fontitalic").val(),
					underline: $("#form #fontunderline").val(),
					message: msg

				}, function(xml) {
					message();
				});
			}
		}
	});

	if( $("#form #session_id").val() != "" )
	{
		timerw = window.setInterval(function(){
			message();
		}, 3000);
	}
});

function sendmsg()
{
	msg = $("#form #message").val();
	$("#form #message").val("");

	if( msg )
	{
		$.post("chat.xml.php", {
			sid: $("#form #session_id").val(),
			mid: $("#form #message_id").val(),
			color: $("#form #color").val(),
			bold: $("#form #fontbold").val(),
			italic: $("#form #fontitalic").val(),
			underline: $("#form #fontunderline").val(),
			message: msg

		}, function(xml) {
			message();
		});
	}
}
function getCookie(name)
{
	cookie = document.cookie.split("; ");

	for( a = 0; a < cookie.length; a++ )
	{
		elName = cookie[a].split("=");
        if(elName[0] == name)
			return unescape(elName[1]);
	}
    return "";
}

function setCookie(name, value)
{
    date = new Date();
    date.setTime(date.getTime() + (1000 * 60 * 60 * 24 * 30));
    document.cookie = name + "=" + escape(value) + "; expires=" + date.toGMTString();
}

function eraser()
{
	if( confirm("Czy na pewno chcesz wyczyścić okno rozmowy?"))
	{
		$("#form #messages").html(
				"<p style=\"margin-top: 5px; margin-bottom: 5px\"><b>Okno rozmowy zostało wyczyszczone</b></p>"
		);
	}

	return false;
}

function colordisplay()
{
	$("#form #colordisplaylayer").css("display", "block");
}

function fontbold()
{
	if( $("#form #fontbold").val() == 0 )
	{
		setCookie("fontbold", 1);
		$("#form #fontbold").val("1");
		$("#form #textb").css("background", "#CCCCCC");
	} else {
		setCookie("fontbold", 0);
		$("#form #fontbold").val("0");
		$("#form #textb").css("background", "white");
	}
}

function fontitalic()
{
	if( $("#form #fontitalic").val() == 0 )
	{
		setCookie("fontitalic", 1);
		$("#form #fontitalic").val("1");
		$("#form #texti").css("background", "#CCCCCC");
	} else {
		setCookie("fontitalic", 0);
		$("#form #fontitalic").val("0");
		$("#form #texti").css("background", "white");
	}
}

function fontunderline()
{
	if( $("#form #fontunderline").val() == 0 )
	{
		setCookie("fontunderline", 1);
		$("#form #fontunderline").val("1");
		$("#form #textu").css("background", "#CCCCCC");
	} else {
		setCookie("fontunderline", 0);
		$("#form #fontunderline").val("0");
		$("#form #textu").css("background", "white");
	}
}

function colordisplayset(color)
{
	$color = color;

	switch( $color )
	{
		case "000000": $color = "000000"; $border = "FFFFFF"; break;
		case "0099FF": $color = "0099FF"; $border = "FFFFFF"; break;
		case "000099": $color = "000099"; $border = "FFFFFF"; break;
		case "FF00FF": $color = "FF00FF"; $border = "FFFFFF"; break;
		case "666666": $color = "666666"; $border = "FFFFFF"; break;
		case "00CC33": $color = "00CC33"; $border = "FFFFFF"; break;
		case "009933": $color = "009933"; $border = "FFFFFF"; break;
		case "FF9900": $color = "FF9900"; $border = "FFFFFF"; break;
		case "FF6600": $color = "FF6600"; $border = "FFFFFF"; break;
		case "990000": $color = "990000"; $border = "FFFFFF"; break;
		case "0000FF": $color = "0000FF"; $border = "FFFFFF"; break;
		case "FF0000": $color = "FF0000"; $border = "FFFFFF"; break;
		default: $color = "000000"; $border = "FFFFFF"; break;
	}
	setCookie("color", $color);

	$("#form #color").val($color);
	$("#form #colordisplay").css("background", "#" + $color);
	$("#form #colordisplay").css("border-color", "#" + $border);

	$("#form #colordisplaylayer").css("display", "none");
}

function autoscroll_icon(value)
{
	if( value == "1" )
	{
		$("#autoscroll_icon").attr("src", "styles/css/images/scroll_stop.gif");
		$("#autoscroll_icon").attr("title", "Włącz automatyczne przewijanie okna rozmowy");
		$("#autoscroll").val("0");
		setCookie("autoscroll", 0);
	}
	else
	{
		$("#autoscroll_icon").attr("src", "styles/css/images/scroll.gif");
		$("#autoscroll_icon").attr("title", "Wyłącz automatyczne przewijanie okna rozmowy");
		$("#autoscroll").val("1");
		setCookie("autoscroll", 1);
	}
}

function htmlspecialchars(s)
{
	if( s == undefined || !s )
	{
		return "";
	}
	return s.replace('&','&amp;').replace('<','&lt;').replace('>','&gt;');
}

function insertusername(username)
{
	if( $("#form #message").val() != "" )
	{
		$("#form #message").val($("#form #message").val() + " " + username);
	}
	else
	{
		$("#form #message").val(username);
	}
	$("#form #message").focus();
}

function smiles(str)
{
	var smiles_code = new Array(
					":)",
					";)",
					":(",
					";(",
					":D",
					"??",
					"!!",
					":roll:",
					":P",
					":|",
					"8)",
					":o",
					":["
					);
	var smiles_image = new Array(
					"images/smiles/icon_smile.gif",
					"images/smiles/icon_wink.gif",
					"images/smiles/icon_sad.gif",
					"images/smiles/icon_cry.gif",
					"images/smiles/icon_biggrin.gif",
					"images/smiles/icon_question.gif",
					"images/smiles/icon_exclaim.gif",
					"images/smiles/icon_rolleyes.gif",
					"images/smiles/icon_razz.gif",
					"images/smiles/icon_neutral.gif",
					"images/smiles/icon_cool.gif",
					"images/smiles/icon_eek.gif",
					"images/smiles/icon_evil.gif"
				);

	for( var i=0; i<smiles_code.length; i++)
	{
		str = str.replace(smiles_code[i], "<img src=\"" + smiles_image[i] + "\" alt=\"" + smiles_code[i] + "\" />");
	}
	return str;
}

function message()
{
	$.post("chat.xml.php", {
		sid: $("#form #session_id").val(),
		mid: $("#form #message_id").val()

	}, function(xml) {

		$("#form #loading").remove();

		$("#form #message_id").val($("message_id", xml).text());
		
		if( $("subject", xml).text() != "" )
		{
			$("#form #subject").html('<h3><span class="dotted">' + $("subject", xml).text() + '</span></h3>');
		}

		$("messages", xml).each(function(id)
		{
			mid = $("messages", xml).get(id);
			$check = document.getElementById($("messageid", mid).text());
			$color = ( $("color", mid).text() != "" ) ? " color: #" + $("color", mid).text() : "";
			$format = ( $("format", mid).text() != "" ) ? $("format", mid).text() : "0|0|0";
			$format_array = $format.split("|");

			$bold = ( $format_array[0] == 1 ) ? "; font-weight: bold;" : "";
			$italic = ( $format_array[1] == 1 ) ? "; font-style: italic;" : "";
			$underline = ( $format_array[2] == 1 ) ? "; text-decoration: underline;" : "";
			$author = '<b style="color: #' + $("ucolor", mid).text() + '">' + htmlspecialchars($("author", mid).text()) + '</b>';
			$background = ( $("hl", mid).text() == 1 ) ? "background: #E0E0E0;" : "";

			if( $check == null )
			{
				if( $("type", mid).text() == "1" )
				{
					if( $("text", mid).text() == "/playsound" )
					{
						$('#player').flash({ 
							src: 'styles/small_player.swf',
							width: 17,
							height: 17,
							flashvars: { autoplay: 'true', song_url: 'http://wrzutube.pl/download/aUPVcPZKaXh' }
						},
						{ version: 8 });

						setTimeout("$('#player').html('')", 10000);
					}
					else
					{
						$("#form #messages").append(
								"<p style='margin-top: 5px; margin-bottom: 5px' id='" 
								+ $("messageid", mid).text() + "'><img src='styles/css/images/clock.gif' alt='' title='"
								+ $("date", mid).text() + "' style='margin-top: 5px; margin-right: 2px; cursor: pointer' onclick=\"alert('" + $("date", mid).text() + "')\" /> <b>" 
								+ $("text", mid).text() + "</b></p>"
						);
					}
				}
				else
				{
					$("#form #messages").append(
							"<p style='margin-top: 5px; margin-bottom: 5px; " + $background + "' id='"  
							+ $("messageid", mid).text() + "'><img src='styles/css/images/clock.gif' alt='' title='"
							+ $("date", mid).text() + "' style='margin-top: 5px; margin-right: 2px; cursor: pointer' onclick=\"alert('" + $("date", mid).text() + "')\" /> <b>" 
							+ $author + "</b>: <span style='" 
							+ $color + $bold + $italic + $underline + "'>"
							+ smiles(htmlspecialchars($("text", mid).text())) +
							"</span></p>"
					);
				}

			}

			if( $("#autoscroll").val() == "1" )
			{
				$("#form #messages")[0].scrollTop = $("#form #messages")[0].scrollHeight;
			}
		});
		var $users = "";

		$("users", xml).each(function(id)
		{
			uid = $("users", xml).get(id);
			$status = ( $("status", uid).text() != "" ) ? "<br /><span>" + $("status", uid).text() + "</span>" : "";
			$status_away = ( $("away", uid).text() == 1 ) ? " <span>(z/w)</span>" : "";
			$usrip = ( $("ip", uid).text() != "" ) ? " <span>IP: " + $("ip", uid).text() + "</span>" : "";

			$username = '<b style="color: #' + $("color", uid).text() + '">' + $("username", uid).text() + '</b>';
			$users = $users + "<p onclick=\"insertusername('" + htmlspecialchars($("username", uid).text()) + "')\" onmouseover=\"this.className='chatover'\" onmouseout=\"this.className='chatout'\" class=\"chatout\">" + $username + $status_away + $usrip + $status + "</p>";
		});

		if( $users != "" )
		{
			$("#users").html($users);
		}

		if( $("code", xml).text() == 3 )
		{
				clearInterval(timerw);

				$("#form #messages").append(
						"<p style=\"margin-top: 5px; margin-bottom: 5px\"><b>Z powodu braku aktywności, zostałeś wylogowany.</b></p>"
				);
				if( $("#autoscroll").val() == "1" )
				{
					$("#form #messages")[0].scrollTop = $("#form #messages")[0].scrollHeight;
				}
		}


	});
}