var in_editor = false;
var in_edit = false;
var timer = [];
var buffertimer = [];
var timeBuffer = 0;
var edit_post_id = 0;
var is_hidden = false;
var editArr = [];
var poster = null;
var required = [];
required['q'] = 'Search term(s)';

handleEditor = function(evt, adjustTop, adjustLeft) {
	if (is_hidden) {
		is_hidden = false;
		toggleSelects('hide');
		positionLayer('editor_box', evt, adjustTop, adjustLeft);
		$('editor_box').style.visibility = 'visible';
		$('editor_box').style.display = 'inline';
		return true;
	}
	else if (!in_editor) {
		if ($('editor_box')) {
			toggleSelects('hide');
			positionLayer('editor_box', evt, adjustTop, adjustLeft);
			$('editor_box').style.visibility = 'visible';
			$('editor_box').style.display = 'inline';
			$('visualeditor').focus();
			return true;
		}
	}
	else {
		alert("You are already adding a reply elsewhere.\nClose the editor first by clicking the 'Cancel' button.");
		$('editor_box').style.visibility = 'visible';
		$('editor_box').style.display = 'inline';
		$('visualeditor').focus();
	}
	
	return false;
}

//adding a topic methods
addTopic = function(fid, evt, adjustTop, adjustLeft) {
	if (handleEditor(evt, adjustTop, adjustLeft)) {
		in_editor = true;
		$('visualeditor').value = '';
		$('editor_title').innerHTML = 'Add a Topic to ' + forum_name + ' forum';
		$('topic').style.display = 'block';
		$('op').value = 'add_topic';
		$('subject').focus();
	}
}

addReply = function(pid, evt, adjustTop, adjustLeft) {
	if (handleEditor(evt, adjustTop, 0)) {
		$('editor_title').innerHTML = 'Add a Reply';
		$('editor_title').scrollIntoView(true);
		$('op').value = 'add_reply';
	}
};

addReplyQuote = function(pid, username, evt, adjustTop, adjustLeft) {
	if (handleEditor(evt, adjustTop, 0)) {
		$('editor_title').innerHTML = 'Add a Reply and Quote the author';
		$('op').value = 'add_reply';
		var txt = $('post_'+pid).innerHTML;
		txt = stripTags(txt);
		txt = '[quote:'+username+']'+txt+'[/quote]'+"\n";
		if ($('visualeditor').value == '')
			$('visualeditor').value = txt;
		else
			$('visualeditor').value += "\n"+txt;

		$('visualeditor').focus();
		$('visualeditor').scrollTop = $('visualeditor').scrollHeight;
	}
};

quoteParag = function(para_id, author, evt, adjustTop, adjustLeft) {
	if ($('para_'+para_id)) {
		$('op').value = 'add_reply';
		var orig_str = $('para_'+para_id).innerHTML;
		var pid = para_id.split('_');
		pid = pid[0];
		var str = '[quote:'+author+']'+stripTags(orig_str)+'[/quote]';
		if (handleEditor(evt, "center", adjustLeft, true)) {
			$('editor_title').innerHTML = 'Add a Reply by Quoting a paragraph';
			if ($('visualeditor').value == '')
				$('visualeditor').value = str + "\n\n";
			else
				$('visualeditor').value += "\n" + str + "\n\n";
	
			$('visualeditor').focus();
			$('visualeditor').scrollTop = $('visualeditor').scrollHeight;
		}
	}
};

stripTags = function(str) {
	if (_isIE_) {
		var re = /<BR[^>]*>/gi; 
		str = str.replace(re,"\n"); 
	}
	
	re = /<BLOCKQUOTE><CITE[^>]*>/gi;
	str = str.replace(re,"[quote:"); 
	re = / wrote:<\/CITE[^>]*>/gi;
	str = str.replace(re,"]"); 
	re = /<\/BLOCKQUOTE[^>]*>/gi;
	str = str.replace(re,"[/quote]"); 
	
	re = /<\S[^>]*>/g; 
	str = str.replace(re,""); 
	str = str.replace("&amp;", "&");
	return str;
};

editPost = function (post_id, evt, adjustTop, adjustLeft) {
	if (in_edit) return;
	if (handleEditor(evt, adjustTop, adjustLeft)) {
		in_edit = true;
		clearTimeout(timer[post_id]);
		$('editor_title').innerHTML = 'Edit reply';	
		$('visualeditor').value = replaceString("<br />", "\n", editArr[post_id]);
		$('op').value = 'edit_reply';
		$('id').value = post_id;
		edit_post_id = post_id;
		doBufferTimer($('timer_'+post_id).innerHTML);
	}
};

validate = function() {
	$('editor_form').action = '/forums/topic.php?tid='+$('tid').value+'&fpage='+$('fpage').value;
	$('editor_form').target = '_self';
	if ($('op').value == 'add_topic') {
		if (trim($('subject').value) == '') {
			alert("Enter a Topic.");
			$('subject').focus();
			return false;
		}
		
		if (trim($('visualeditor').value) == '') {
			alert("Enter the message for the topic you're adding.");
			$('visualeditor').focus();
			toggleEditorButtons(false);
			return false;
		}
		
		if ($('progress_bar')) $('progress_bar').style.visibility = 'visible';
		toggleEditorButtons(true);
		return true;
	}
	else {
		//add/edit reply
		if (trim($('visualeditor').value) != '') {
			toggleEditorButtons(true);
			return true;
		}	
	}
	return false;
};

cancelPost = function() {
	$('visualeditor').value = '';
	$('editor_box').style.visibility = 'hidden';
	$('editor_box').style.display = 'none';	
	$('topic').style.display = 'none';
	$('op').value = 'add_reply';
	in_editor = false;
	toggleSelects('show');
			
	if (in_edit) {
		if (timeBuffer > 0) {
			$('timer_'+edit_post_id).innerHTML = timeBuffer;
			doTimer(edit_post_id);
		}		
		in_edit = false;		
	}
};

hideEditor = function() {
	if (!in_edit && $('op').value != 'add_topic' && $('editor_box')) {
		$('editor_box').style.visibility = 'hidden';
		$('editor_box').style.display = 'none';	
		$('topic').style.display = 'none';
		is_hidden = true;		
	}
};

doTimer = function(post_id) {
	var x = parseInt($('timer_'+post_id).innerHTML);
	x--;
	$('timer_'+post_id).innerHTML = x;
	
    if (x > 0)	
		timer[post_id] = setTimeout("doTimer("+post_id+")",1000);
    else {
		clearTimeout(timer[post_id]);
		if (editArr[post_id] != undefined) editArr[post_id] = null;
		$('can_edit_'+post_id).innerHTML = '';
		$('can_edit_'+post_id).style.display = 'none';
	}
};

doBufferTimer = function(x) {
	x = parseInt(x);
	//x--;
    if (x-- > 0) {
		bufferTimer = setTimeout("doBufferTimer("+x+")",1000);
		timeBuffer = x;
	}	
    else {
		clearTimeout(bufferTimer);
		timeBuffer = 0;
	}
};

optionDD = function(opt, evt) {
	if ($(opt)) {
		if ($(opt).style.visibility == 'hidden') {
			$(opt).style.visibility == 'visible';
			positionLayer(opt, evt, -5, -10);
		}
		else
			$(opt).style.visibility = 'hidden'
	}
};


/*
//beta code for forums index page
var chkr_timer = null;
initForumChecker = function () {
	chkr_timer = setTimeout("forumChecker()", 2000);
};

var forum_chkr = null;
forumChecker = function() {
	if (forum_chkr == null) forum_chkr = new Ajax();
	if (forum_chkr && !forum_chkr.in_request) {
		forum_chkr.send('/forums/forum_checker.php', 'post', 'handleCheckerResponse', 'op=forums&dat='+forums);
	}
};

myfade = [];
handleCheckerResponse = function() {
 	if (forum_chkr.request.readyState == 4) {
        if (forum_chkr.request.status == 200) {
			forum_chkr.in_request = false;
	        var response = forum_chkr.request.responseText;
	        if (response != '') {			
				if (response != -1) {
					var parts = response.split("\n\n");
					for(var i=0; i < parts.length; i+=2) {
						if (parts[i] != '') {
							var items = parts[i].split("|");
							if ($('post_'+items[0])) {
								$('topics_'+items[0]).innerHTML = items[1];
								$('posts_'+items[0]).innerHTML = items[2];
								myfade[items[0]] = new Fade('post_'+items[0], 'myfade', i);
								myfade[items[0]].start();
								$('post_'+items[0]).innerHTML = parts[i+1];
								
								forums = replaceString(items[3],items[4],forums);
							}
						}
					}
				}

				//ajtimer = setTimeout("forumChecker()", 60000);
	        }
		}
		else
			forum_chkr.in_request = false;
    }    
};


doit = function() {
	i = 4;
	myfade = [];
	myfade[i] = new Fade('post_'+4, 'myfade', i);
	myfade[i].start();

}
*/
