(function(){
	// document.write('<link rel="stylesheet" type="text/css" href="/global/css/forums/forums.css" media="screen"/>');
	// document.write('<link rel="stylesheet" type="text/css" href="/lib/css/forums.css" media="screen"/>');	
	document.write('<script type="text/javascript" src="/lib/global/scripts/rating/rating.js"></script>');
	document.write('<script language="javascript" type="text/javascript" src="/lib/global/scripts/text-edit/text-edit.js"></script>');
})();

var forums = {
	currentPostID: 0,
	promptLogin: function() {
		var mssg = {
			"header": 'There is an error',
			"bodyContent": "<p>You need to be logged in to use that function</p>\n" +
			"<p><a href=\"/registration/\">Register now!</a></p>"
		}
		util.talkbox($H(mssg).toJSON());
	},
	showEditBox: function() {
		$('post_edit').show();
		new Effect.ScrollTo('post_edit',{duration: .5});
	},
	hidePostButton: function() {
		$A(document.getElementsByClassName('post-reply-button')).each(function(buttonAt,index) {
			if (index === 1 || index === 2) {
				buttonAt.hide();
			}
		});
	},
	showPostButton: function() {
		$A(document.getElementsByClassName('post-reply-button')).each(function(buttonAt,index) {
			if (index === 1 || index === 2) {
				new Effect.Appear($(buttonAt),{duration: .5});
			}
		});
	},
	postReply: function (postID) {
		if  (postID === 'default') {
			forums.hidePostButton();
		}
		if (!$('post_edit')) {
			util.promptLogin();
			return;
		}
		forums.hidePostButton();
		forums.showEditBox();
		forums.currentPostID = postID;
		$('ExistingPostID').value = 0;
		$('UpdateType').value = 'reply';
		$('body').value = '';
		$('body').focus();
	},
	postQuote: function (postID, username) {
		if (!$('post_edit')) {
			util.promptLogin();
			return;
		}
		forums.currentPostID = postID;
		$('ExistingPostID').value = 0;
		$('UpdateType').value = 'reply';
		var target = $('post_body_' + postID);
		var postText = $(target).innerHTML
		.replace(/<br\/>/gi,'\n')
		.replace(/<br>/gi,'\n')
		.replace(/<p>/gi,'')
		.replace(/<\/p>/gi,'\n\n')
		.replace(/(\t)/gi,'')
		.strip();
		var quotedText = '<blockquote><strong>' + username + ' wrote:</strong>' + postText + '</blockquote>\n\n';
		forums.hidePostButton();
		forums.showEditBox();
		$('body').value = quotedText;
		forums.setSelectionRange($('body'),$F('body').length,$F('body').length);
		preview.previewTimer();
	},
	postEdit: function (postID) {
		forums.currentPostID = postID;
		$('ExistingPostID').value = forums.currentPostID;
		$('UpdateType').value = 'edit';
		var target = $('post_body_' + postID);
		var postText = $(target).innerHTML
		.replace(/<br\/>/gi,'\n')
		.replace(/<br>/gi,'\n')
		.replace(/<p>/gi,'')
		//.replace(/<\/p>/gi,'')
		.replace(/<\/p>/gi,'\n\n')
		.replace(/(\t)/gi,'')
		.strip();
		forums.hidePostButton();
		forums.showEditBox();
		$('body').value = postText;
		forums.setSelectionRange($('body'),0,$F('body').length);
		preview.previewTimer();
	},
	setSelectionRange: function (input, selectionStart, selectionEnd) {
		if (input.setSelectionRange) {
			input.focus();
			input.setSelectionRange(selectionStart, selectionEnd);
		} else if (input.createTextRange) {
		var range = input.createTextRange();
			range.collapse(true);
			range.moveEnd('character', selectionEnd);
			range.moveStart('character', selectionStart);
			range.select();
		}
	},
	failPoint: 0,
	errorMssg: '',
	validatePostForm: function() {
		this.failPoint = 0;
		this.errorMssg = '';
		if ($('TopicTitle') && $F('TopicTitle').blank()) {
			this.failPoint++;
			this.errorMssg += "<li>Please enter a value for your topic\'s title</li>"
		}
		if ($('body') && $F('body').blank()) {
			this.failPoint++;
			this.errorMssg += "<li>Cannot send empty message</li>"
		}
		if(($('body').value.search(preview.regex01) != -1 ||
			$('body').value.search(preview.regex02) != -1 ||
			$('body').value.search(preview.regex03) != -1) && !insert.urlsokay){
			this.failPoint++;
			this.errorMssg += "<li>We're sorry.  Brand new users are currently not allowed to post links.  You should be able to do so in a few days.</li>";
		}
		this.finalPostCheck();
	},
	validateEditTopicForm: function() {
		this.failPoint = 0;
		this.errorMssg = '';
		if ($('TopicTitle') && $F('TopicTitle').blank()) {
			this.failPoint++;
			this.errorMssg += "<li>Please enter a value for your topic\'s title</li>"
		}
		this.finalPostCheck();
	},
	finalPostCheck: function() {
		if (this.failPoint != 0) {
			var message =  {
				"header": "There is an error",
				"bodyContent": "<ol>" + this.errorMssg + "</ol>"
			}
			util.talkbox($H(message).toJSON());
		} else {
			$('forumPostForm').submit();
		}
	},
	
	// reporting functions
	postReportSpam: function (postID,userID) {
		if (!confirm('Are you sure that you want to report this user for spamming?')) {return;}
		util.setSaving();
		new Ajax.Request('/report/ajax/forum_post_spam/', {
			method: 'post',
			parameters: {
				userID: userID,
				objectID: postID,
				url: window.location.href
			},
			onSuccess: function(t) {
				util.talkbox(t.responseText);
			},
			onFailure: function(t){
				_tools.ajaxBug(t);
			}
		});

	},
	postReportTroll: function (postID,userID) {
		if (!confirm('Are you sure that you want to report this user for trolling?')) {return;}
		util.setSaving();
		new Ajax.Request('/report/ajax/forum_post_troll/', {
			method: 'post',
			parameters: {
				userID: userID,
				objectID: postID,
				url: window.location.href
			},
			onSuccess: function(t) {
				util.talkbox(t.responseText);
			},
			onFailure: function(t){
				_tools.ajaxBug(t);
			}
		});

	},
	postReportObscene: function (postID,userID) {
		if (!confirm('Are you sure that you want to report this user for being obscene?')) {return;}
		util.setSaving();
		new Ajax.Request('/report/ajax/forum_post_obscene/', {
			method: 'post',
			parameters: {
				userID: userID,
				objectID: postID,
				url: window.location.href
			},
			onSuccess: function(t) {
				util.talkbox(t.responseText);
			},
			onFailure: function(t){
				_tools.ajaxBug(t);
			}
		});

	},

	// editing functions
	postCancel: function () {
		$('ExistingPostID').value = 0;
		$('body').value = '';
		if($('temp_span')){
			$('temp_span').update(''); 
		}
		forums.showPostButton();
		new Effect.Fade('post_edit');
		if ($('post_' + forums.currentPostID)) {
			new Effect.ScrollTo($('post_' + forums.currentPostID));
		}
	},
	topicRemove: function () {
		if (confirm('Are you sure you want to remove the selected Topics?')) {
			$('modifyTopicForm').action = '/forums/topicremove/';
			$('modifyTopicForm').submit();
		} else {
			alert('Delete Aborted');	
		};
	},
	topicModify: function () {
		$('modifyTopicForm').submit();
	},
	sectionNew: function () {
		$('Section_Title').value = '';
		$('Section_Blurb').value = '';
		$('actiontype').value = 'new';
		$('Section_ID').value = '';
		$('sectionButton').innerHTML = 'Create New Section';
	},
	sectionEdit: function (sectionID) {
		$('Section_Title').value = $('Title_' + sectionID).innerHTML;
		$('Section_Blurb').value = $('BlurbText_' + sectionID).innerHTML;
		$('actiontype').value = 'edit';
		$('Section_ID').value = '' + sectionID;
		$('sectionButton').innerHTML = 'Apply Changes';
	},
	sectionRemove: function (sectionID) {
		$('Section_Title').value = '';
		$('Section_Blurb').value = '';
		$('sectionButton').innerHTML = 'Create New Section';
		if (confirm('Are you sure you want to remove Section ' + sectionID + '?')) {
			$('actiontype').value = 'remove';
			$('Section_ID').value = '' + sectionID;
			$('modifySectionForm').submit();
		}
	},		
	sectionModify: function () {
		$('modifySectionForm').submit();
	},

	// control panel functions
	showControlPanel: function() {
		new Effect.BlindDown('controlPanel');
		$('openCP').hide();
		setTimeout("$('closeCP').show('slow')",1000);

	},
	hideControlPanel: function() {
		new Effect.BlindUp('controlPanel');
		setTimeout("$('openCP').show('slow')",1000);
		$('closeCP').hide();
	},
	applyCPChanges: function() {
		new Ajax.Request('/forums/ajax/updateCP/', {
			method: 'post',
			parameters: $('controlPanelForm').serialize(),
			onSuccess: function() {
				$('controlPanelMsg').style.display = "inline";
				$('controlPanelMsg').innerHTML = "Changes will take effect the next time your browser refreshes";
				new Effect.Highlight('controlPanel');
				new Effect.BlindUp('controlPanel');
				$('closeCP').hide();
				setTimeout("$('openCP').show('slow')",1000);
			},
			onFailure: function(){
				alert('There was an Error - Data not saved');
			}
		});
	}
}