$(document).ready(function() {
	$(".spanPageNav").hover(function () {
		$(this).css("background-color","#B8C2DE").css("cursor","pointer");
	}, function () {
		$(this).css("background-color","");
	});
	
	$(".imgOpenMoveOption").toggle(function() {
		$("#moverow"+$(this).attr("alt")).fadeIn("fast");
	}, function() {
		$("#moverow"+$(this).attr("alt")).fadeOut("fast");
	});
	
	$(".imgDelete").click(function() {
		var topicNr = $(this).attr("topicnr");
		var sure = confirm("Delete topic with title: "+$(this).attr("title")+"?")
		if (sure) {
			$.post("/ajax/forummod.asp", { deletenr: topicNr, forumnr: $(this).attr("forumnr"), type: $(this).attr("type")}, function(data){
				if (data=="succes") {$("#tr"+topicNr).fadeOut("slow");}
			});
		}
	});

	$(".imgPostDelete").click(function() {
		var messageNr = $(this).attr("mesnr");
		var delCompOrForum = $(this).attr("type");
		var sureDeletePost = confirm("Delete post from manager: "+$(this).attr("title")+"?")
		if (sureDeletePost) {
			$.post("/ajax/forummod.asp", { postdelnr: messageNr, forumnr: $(this).attr("forumnr"), topicnr: $(this).attr("topicnr"), type: delCompOrForum}, function(data){
				if (data=="succes") {$(".tr"+messageNr).fadeOut("slow");}
			});
		}
	});
	
	$(".divPagination div").each(function() {
		if ($(this).hasClass("divPagClick")) {
			$(this).hover(function() {
				$(this).addClass("divHover");
				$("a[a='"+$(this).attr("a")+"']").css("color","white");
			},function() {
				$(this).removeClass("divHover");
				$("a[a='"+$(this).attr("a")+"']").css("color","");
			});
		}
	});
	
	$(".imgQuote").click(function() {
		var messageNr = $(this).attr("txt");
		var messageTxt = $("#divMsg"+messageNr).text();
		var messageManager = $("#aManager"+messageNr).text();
		var quoteTxt = "[quote="+messageManager+"]"+messageTxt+"[/quote]";
		quoteTxt = removeHTML(quoteTxt);
		quoteTxt.replace("\r","");
		quoteTxt.replace(new RegExp("[\\n]+", "g"),"\n");
		
		$("#txtText").val($("#txtText").val() + quoteTxt);
		document.location.href='#AddReply';
		console.log(quoteTxt);
	});
	
	$(".imgForumButton").mouseover(function() {
		$(this).css("background-image","url(/images/bbc/ofm_bbc_hoverbg.gif)")
	}).mouseout(function() {
		$(this).css("background-image","url(/images/bbc/ofm_bbc_bg.gif)")
	});
	
	$("#imgSetBold").click(function() {
		surroundText('[b]', '[/b]', document.forms['frmMsg'].elements['txtText']);
	});
	$("#imgSetItalic").click(function() {
		surroundText('[i]', '[/i]', document.forms['frmMsg'].elements['txtText']);
	});
	$("#imgSetUnderline").click(function() {
		surroundText('[u]', '[/u]', document.forms['frmMsg'].elements['txtText']);
	});
	$("#imgSetStrike").click(function() {
		surroundText('[s]', '[/s]', document.forms['frmMsg'].elements['txtText']);
	});
	$("#imgSetQuote").click(function() {
		surroundText('[quote]', '[/quote]', document.forms['frmMsg'].elements['txtText']);
	});
	$("#imgSetUrl").click(function() {
		surroundText('[url]', '[/url]', document.forms['frmMsg'].elements['txtText']);
	});
	$("#imgSetImg").click(function() {
		surroundText('[img]', '[/img]', document.forms['frmMsg'].elements['txtText']);
	});
	$("#imgSetMovie").click(function() {
		surroundText('[youtube]', '[/youtube]', document.forms['frmMsg'].elements['txtText']);
	});
	$("#imgSetHrule").click(function() {
		replaceText('[hr]', document.forms['frmMsg'].elements['txtText']);
	});
	$("#btnDeleteAll").click(function() {
		var allMessageNr = $("#spanMesNrSubStr").text();
		var sureDeleteAllPost = confirm("Are you sure you want to delete all posts on this page?")
		if (sureDeleteAllPost) {
			$.post("/ajax/forummod.asp", { postdelnr: allMessageNr, forumnr: $("#spanMesNrSubStr").attr("forumnr"), topicnr: $("#spanMesNrSubStr").attr("topicnr"), type: "1"}, function(data){
				window.location.href=''
			});
		}
	});
	$("#btnDeleteSelected").click(function() {
		var sureDeleteChkdTopics = confirm("Are you sure you want to delete all checked topics on this page?");
		if (sureDeleteChkdTopics) {
			var chkDelForum = $(this).attr("forum");
			var checkSubStr = "";
			$(".chkDelete").each(function() {
				if ($(this).attr("checked")==true) {
					checkSubStr = checkSubStr + $(this).attr("topic") + ","
				}
			});
			if (checkSubStr!="") {
				checkSubStr = checkSubStr.slice(0, -1);
				$.post("/ajax/forummod.asp", { deletenr: checkSubStr, forumnr: chkDelForum, type: $(this).attr("typeforum")}, function(data){
					if (data=="succes") {
						$(".chkDelete").each(function() {
							if ($(this).attr("checked")==true) {
								$("#tr"+$(this).attr("topic")).fadeOut("slow");
							}
						});
					}
				});
			}
		}
	});
	$("#btnDeleteChkdMsg").click(function() {
		var sureDeleteChkdPosts = confirm("Are you sure you want to delete all checked posts on this page?");
		var typeDeleteForum = $(this).attr("forumtype");
		if (sureDeleteChkdPosts) {
			var chkDelMsgForum = $(this).attr("forum");
			var chkDelMsgTopic = $(this).attr("topic");
			var chkMsgSubStr = "";
			$(".chkDelete").each(function() {
				if ($(this).attr("checked")==true) {
					chkMsgSubStr = chkMsgSubStr + $(this).attr("message") + ","
				}
			});
			if (chkMsgSubStr!="" && chkDelMsgForum!= "" && chkDelMsgTopic!="") {
				chkMsgSubStr = chkMsgSubStr.slice(0, -1);
				$.post("/ajax/forummod.asp", { postdelnr: chkMsgSubStr, forumnr: chkDelMsgForum, topicnr: chkDelMsgTopic, type: typeDeleteForum}, function(data){
					if (data=="succes") {
						window.location.href=''
					}
				});
			}
		}
	});
});

function removeHTML(txt) {
	txt = txt.replace(/<br>/gi,"\n");
	txt = txt.replace(/<[^>]+>/g,"");
	return txt;
}

function popWindow(page) {
	var tempdate = new Date();
	winbreedte = 570;
	winhoogte = 400;
	winposx = (screen.availWidth-winbreedte)/2; winposy = (screen.availHeight-winhoogte)/2-15;
	var winname = "osmcon" + tempdate.getTime();
	window.open(page,winname,"screenX="+winposx+",left="+winposx+",screenY="+winposy+",top="+winposy+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width="+winbreedte+",height="+winhoogte);
}

// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, textarea) {
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange){
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
		if (temp_length == 0){
			caretPos.moveStart("character", -text2.length);
			caretPos.moveEnd("character", -text2.length);
			caretPos.select();
		} else {
			textarea.focus(caretPos);
		}
	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined") {
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;
		if (textarea.setSelectionRange){
			if (selection.length == 0) {
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			} else {
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			}
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else {
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}

function replaceText(text, textarea){
	// Attempt to create a text range (IE).
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange){
		var caretPos = textarea.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		caretPos.select();
	}
	// Mozilla text range replace.
	else if (typeof(textarea.selectionStart) != "undefined") {
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text + end;

		if (textarea.setSelectionRange)	{
			textarea.focus();
			textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put it on the end.
	else {
		textarea.value += text;
		textarea.focus(textarea.value.length - 1);
	}
}
