
/* SOME CODE SLIGHTY ADAPTED FROM: http://parentnode.org/javascript/working-with-the-cursor-position/ */
var Forums = {
	voted:0,
	openText:function() {
		if(!$('post_topic')) { return false; }
		$('post_topic').show();
		$('post_hide').hide();
	},
	closeText:function() {
		if(!$('post_topic')) { return false; }
		$('post_topic').hide();
		$('post_hide').show();
	},
	addText:function(text,space) {
		if(space) { text = ' '+text+' '; }
		var obj = $('input_message');
		if(obj.selectionStart) {
			var start = obj.selectionStart;
			var end = obj.selectionEnd;
			obj.value = obj.value.substr(0,start)+text+obj.value.substr(end, obj.value.length);
		}
		else if(document.selection) {
			obj.focus();
			var orig = obj.value.replace(/\r\n/g, "\n");
			var range = document.selection.createRange();
			if(range.parentElement()!=obj) { return false; }
			range.text = text;
			var actual = tmp = obj.value.replace(/\r\n/g, "\n");
			for(var diff=0;diff<orig.length;diff++) { if(orig.charAt(diff) != actual.charAt(diff)) break; }
			for(var index=0,start=0;tmp.match(text)&&(tmp=tmp.replace(text,''))&&index<=diff;index=start+text.length) { start = actual.indexOf(text,index); }
		}
		else {
			obj.value = obj.value+text;
			return true;
		}
		if(start!=null) { this.setCaretTo(start+text.length); }
		else { obj.value += text; }
	},
	addQuote:function(post_id,username) {
		this.openText();
		var theSelection = '';
		var divarea = $('post_message_'+post_id);
		if(window.getSelection) { theSelection = window.getSelection().toString(); }
		else if(document.getSelection) { theSelection = document.getSelection(); }
		else if(document.selection) { theSelection = document.selection.createRange().text; }
		if(theSelection==''||typeof theSelection=='undefined'||theSelection==null) {
			if(divarea.innerHTML) {
				theSelection = divarea.innerHTML.split('______________________');
				theSelection = theSelection[0];
				theSelection = theSelection.replace(/<br>/ig, '\n').replace(/<br\/>/ig, '\n').stripTags().replace(/&lt\;/ig, '<').replace(/&gt\;/ig, '>').replace(/&amp\;/ig, '&');
			}
			else if(document.all) { theSelection = divarea.innerText; }
			else if(divarea.textContent) { theSelection = divarea.textContent; }
			else if(divarea.firstChild.nodeValue) { theSelection = divarea.firstChild.nodeValue; }
		}
		if(theSelection) { this.addText('[quote="' + username + '"]' + theSelection + '[/quote]'); }
		return;
	},
	postAdd:function() {
		if($('input_message').value.length==0) {
			window.alert('Please write a message.');
			return false;
		}
		$('input_message').disable();
		$('input_subject').disable();
		$('input_submit').disable();
		$('input_submit').value = 'Previewing...';
		$('post_throbber').show();
		if($('page_id')) { p_id = $('page_id').innerHTML; }
		else { p_id = 1; }
		new Ajax.Request(engineURL,{
			method:'post',parameters:{
				control:'forums',
				action:'add',
				title:$('input_subject').value,
				message:$('input_message').value,
				page_id:p_id,
				thread_id:$('thread_id').value,
				post_id:$('post_id').value,
				forum_id:$('forum_id').value,
				poll_allow:false,
				poll_title:false,
				poll_run:false,
				poll_options:false,
				allow_bbcode:$('allow_bbcode').checked,
				allow_smilies:$('allow_smilies').checked,
				allow_signature:$('allow_signature').checked
			},
			onSuccess:function(trans) {
				var result = trans.responseText.evalJSON();
				if(result['type']=='location') { window.location = result['content']; }
				else {
					$('post_topic').show();
					$('post_preview').hide();
					Effect.BlindUp('post_throbber',{duration:.7});
					$('post_added').show();
					$('input_message').enable();
					$('input_subject').enable();
					$('input_submit').enable();
					$('input_submit').value = 'Add Post';
					$('input_subject').value = '';
					$('input_message').value = '';
					if($('post_id').value>0) { window.location = window.location; }
					$('post_id').value = 0;
					Effect.Fade('post_added',{delay:4});
					posts = $$('#content_post > table')[0];
					row = posts.insertRow(posts.rows.length);
					row.className = 'menu';
					cell = row.insertCell(0);
					cell.colSpan = 2;
					cell.innerHTML = '<ul><li><div><span>Subject:</span> '+result['content']['title']+'</div></li><li><span>Posted:</span> '+result['content']['created']+'</li></ul>';
					row = posts.insertRow(posts.rows.length);
					cell = row.insertCell(0);
					cell.className = 'left';
					cell.innerHTML = '<a href="/'+result['content']['username']+'"><img alt="'+result['content']['username']+'" src="'+result['content']['icon']+'" /></a><ul><li><a href="/'+result['content']['username']+'" title="View Profile">'+result['content']['firstname']+' '+result['content']['lastname']+'</a> (<a href="/'+result['content']['username']+'" title="View Profile">'+result['content']['username']+'</a>)</li><li><span>Joined:</span> '+result['content']['joined']+'</li><li><span>Posts:</span> '+result['content']['count']+'</li></ul><ul><li>'+result['content']['user_functions']+'</li></ul>';
					cell = row.insertCell(1);
					cell.id = 'post_message_'+result['content']['post_id'];
					cell.className = 'post';
					cell.innerHTML = result['content']['content'];
					row = posts.insertRow(posts.rows.length);
					row.className = 'functions';
					cell = row.insertCell(0);
					cell.colSpan = 2;
					cell.innerHTML = '<a class="forum_icon_top" href="#top">Top</a><a class="forum_icon_flag" href="javascript:void(0);" id="flag_'+result['content']['post_id']+'" onclick="Forums.postFlag(\''+result['content']['post_id']+'\');">Flag</a><a class="forum_icon_reply" href="#reply">Reply</a>'+result['content']['functions'];
					row = posts.insertRow(posts.rows.length);
					row.className = 'spacer';
					cell = row.insertCell(0);
					cell.colSpan = 2;
				}
			}
		});
	},
	postPreview:function() {
		if($('input_message').value.length==0) {
			window.alert('Please write a message.');
			return false;
		}
		$('input_message').disable();
		$('input_subject').disable();
		$('input_submit').value = 'Previewing...';
		$('input_submit').disable();
		$('post_throbber').show();
		if($('page_id')) { p_id = $('page_id').innerHTML; }
		else { p_id = 1; }
		new Ajax.Request(engineURL,{
			method:'post',parameters:{
				control:'forums',
				action:'preview',
				title:$('input_subject').value,
				message:$('input_message').value,
				page_id:p_id,
				thread_id:$('thread_id').value,
				forum_id:$('forum_id').value,
				poll_allow:false,
				poll_title:false,
				poll_run:false,
				poll_options:false,
				allow_bbcode:$('allow_bbcode').checked,
				allow_smilies:$('allow_smilies').checked,
				allow_signature:$('allow_signature').checked
			},
			onSuccess:function(trans) {
				$('post_preview').show();
				$('post_topic').hide();
				$('post_throbber').hide();
				$('input_message').enable();
				$('input_subject').enable();
				$('input_submit').enable();
				if($('post_id').value>0) {
					$('input_preview_submit').value = 'Save Post';
					$('input_submit').value = 'Save Post';
				}
				else { $('input_submit').value = 'Add Post'; }
				var result = trans.responseText.evalJSON();
				$('post_preview').update(result['content']);
			}
		});
	},
	postEdit:function(post_id) {
		new Ajax.Request(engineURL,{
			method:'post',parameters:{
				control:'forums',
				action:'edit',
				post_id:post_id,
				allow_bbcode:$('allow_bbcode').checked,
				allow_smilies:$('allow_smilies').checked,
				allow_signature:$('allow_signature').checked
			},
			onSuccess:function(trans) {
				$('post_hide').hide();
				$('post_preview').hide();
				$('post_topic').show();
				var result = trans.responseText.evalJSON();
				$('input_submit').value = 'Save Post';
				$('input_message').value = result['message'];
				$('input_subject').value = result['title'];
				$('post_id').value = result['post_id'];
				$('post_title').update('Edit Post "'+result['title']+'"');
				if(result['allow_bbcode']==1) { $('allow_bbcode').checked = true; }
				else { $('allow_bbcode').checked = false; }
				if(result['allow_smilies']==1) { $('allow_smilies').checked = true; }
				else { $('allow_smilies').checked = false; }
				if(result['allow_signature']==1) { $('allow_signature').checked = true; }
				else { $('allow_signature').checked = false; }
			}
		});
	},
	postFlag:function(post_id) {
		new Ajax.Request(engineURL,{
			method:'get',parameters:{
				control:'forums',
				action:'flag',
				post_id:post_id
			},
			onSuccess:function() {
				$('flag_'+post_id).className = 'forum_icon_flag_off';
				$('flag_'+post_id).update('Flagged!');
			}
		});
	},
	postDelete:function(post_id) {
		confirm_delete = window.confirm('Are you sure you want to delete this post?');
		if(!confirm_delete) { return false; }
		new Ajax.Request(engineURL,{
			method:'get',parameters:{
				control:'forums',
				action:'delete',
				post_id:post_id
			},
			onSuccess:function(trans) {
				var result = trans.responseText.evalJSON();
				if(result[0]) { window.location = result['content']; }
			}
		});
	},
	threadAdd:function() {
		if($('input_subject').value.length==0) {
			window.alert('Please include a subject for your thread.');
			return false;
		}
		if($('input_message').value.length==0) {
			window.alert('Please write a message for your thread.');
			return false;
		}
		$('input_message').disable();
		$('input_subject').disable();
		$('input_submit').value = 'Creating...';
		$('input_submit').disable();
		$('post_throbber').show();
		poll_run = $('input_poll_run').options[$('input_poll_run').selectedIndex].value;
		new Ajax.Request(engineURL,{
			method:'post',parameters:{
				control:'forums',
				action:'add',
				title:$('input_subject').value,
				message:$('input_message').value,
				forum_id:$('forum_id').value,
				poll_allow:$('poll_1').value,
				poll_title:$('input_poll_title').value,
				poll_run:poll_run,
				poll_options:$('input_poll_options').value,
				allow_bbcode:$('allow_bbcode').checked,
				allow_smilies:$('allow_smilies').checked,
				allow_signature:$('allow_signature').checked
			},
			onSuccess:function(trans) {
				$('input_message').enable();
				$('input_subject').enable();
				$('input_submit').value = 'Create Thread';
				$('input_submit').enable();
				Effect.BlindUp('post_throbber',{duration:.7});
				$('post_topic').show();
				$('post_preview').hide();
				var result = trans.responseText.evalJSON();
				window.location = result['content'];
			}
		});
	},
	threadAnnounce:function(thread_id) {
		new Ajax.Request(engineURL,{
			method:'get',parameters:{
				control:'forums',
				action:'announce',
				thread_id:thread_id
			},
			onSuccess:function() {
				$('announce').className = 'forum_icon_announce_off';
				$('announce').update('Announced!');
			}
		});
	},
	threadLock:function(thread_id) {
		new Ajax.Request(engineURL,{
			method:'get',parameters:{
				control:'forums',
				action:'lock',
				thread_id:thread_id
			},
			onSuccess:function() {
				$('lock').className = 'forum_icon_lock_off';
				$('lock').update('Locked!');
			}
		});
	},
	threadRegular:function(thread_id) {
		new Ajax.Request(engineURL,{
			method:'get',parameters:{
				control:'forums',
				action:'regular',
				thread_id:thread_id
			},
			onSuccess:function() {
				$('regular').className = 'forum_icon_reply_off';
				$('regular').update('Destatusfied!');
			}
		});
	},
	threadSticky:function(thread_id) {
		new Ajax.Request(engineURL,{
			method:'get',parameters:{
				control:'forums',
				action:'sticky',
				thread_id:thread_id
			},
			onSuccess:function() {
				$('sticky').className = 'forum_icon_sticky_off';
				$('sticky').update('Stickied!');
			}
		});
	},
	threadDelete:function(thread_id) {
		new Ajax.Request(engineURL,{
			method:'get',parameters:{
				control:'forums',
				action:'delete_thread',
				thread_id:thread_id
			},
			onSuccess:function(trans) {
				var result = trans.responseText.evalJSON();
				window.location = result['content'];
			}
		});
	},
	threadPreview:function() {
		if($('input_subject').value.length==0) {
			window.alert('Please include a subject for your thread.');
			return false;
		}
		if($('input_message').value.length==0) {
			window.alert('Please write a message for your thread.');
			return false;
		}
		$('input_message').disable();
		$('input_subject').disable();
		$('input_submit').value = 'Previewing';
		$('input_submit').disable();
		$('input_preview').hide();
		$('post_throbber').show();
		poll_run = $('input_poll_run').options[$('input_poll_run').selectedIndex].value;
		new Ajax.Request(engineURL,{
			method:'post',parameters:{
				control:'forums',
				action:'preview',
				title:$('input_subject').value,
				message:$('input_message').value,
				forum_id:$('forum_id').value,
				poll_allow:$('poll_1').value,
				poll_title:$('input_poll_title').value,
				poll_run:poll_run,
				poll_options:$('input_poll_options').value,
				allow_bbcode:$('allow_bbcode').checked,
				allow_smilies:$('allow_smilies').checked,
				allow_signature:$('allow_signature').checked
			},
			onSuccess:function(trans) {
				$('input_message').enable();
				$('input_subject').enable();
				$('input_submit').enable();
				$('input_submit').value = 'Create Thread';
				$('input_preview').show();
				$('post_preview').show();
				$('post_topic').hide();
				Effect.BlindUp('post_throbber',{duration:.7});
				var result = trans.responseText.evalJSON();
				$('post_preview').update(result['content']);
			}
		});
	},
	vote:function(option_id) {
		if(this.voted) {
			$('option_status_'+option_id).setStyle({color:'#c00'}).update('Already Voted');
			return false;
		}
		this.voted=1;
		new Ajax.Request(engineURL,{
			method:'get',parameters:{
				control:'forums',
				action:'vote',
				option_id:option_id
			},
			onSuccess:function(trans) {
				if(trans.responseText>=1) {
					var count = parseInt($('option_count_'+option_id).innerHTML);
					count++;
					if(count==1) { var s = ''; }
					else { var s = 's'; }
					$('option_count_'+option_id).update(count+' vote'+s);
					if(trans.responseText==1) { $('option_status_'+option_id).setStyle({color:'#0c0'}).update('Thanks for voting!'); }
					else { $('option_status_'+option_id).setStyle({color:'#0c0'}).update('Vote has been changed and will be noticed by <a href="javascript:void(0);" onclick="window.location.reload();">refreshing</a> this page.'); }
				}
				else { $('option_status_'+option_id).setStyle({color:'#c00'}).update('Must be <a href="javascript:void(0);" onclick="Header.openLogin();" style="font-weight:bold;color:#c00;">logged in</a> to vote'); }
				var votes = $$('.vote');
				for(i=0;i<votes.length;i++) { votes[i].disable(); }
			}
		});
	},
	setCaretTo:function(pos) {
		var obj = $('input_message');
		if(obj.createTextRange) {
			var range = obj.createTextRange();
			range.move('character', pos);
			range.select();
		}
		else if(obj.selectionStart) {
			obj.focus();
			obj.setSelectionRange(pos, pos);
		}
	}
};