function prevPost(text) {
	new Ajax.Request('/ajax/ajax-forum.php', {
		method: 'post',
		parameters: {previewPostBody: text},
		onSuccess: function(transport){
			var response = transport.responseText || "There was some kind of error when trying to preview your post.";
			$('preview').innerHTML = response;
			$('previewPost').style.display = 'block';
		},
		onFailure: function(transport){
			var response = "There was some kind of error when trying to preview your post.";
			$('preview').innerHTML = response;
		}
	})
}
function topicQuote(id) {
	new Ajax.Request('/ajax/ajax-forum.php', {
		method: 'post',
		parameters: {getQuoteBody: id},
		onSuccess: function(transport){
			var response = transport.responseText || "An error occurred while trying to quote this response?";
			$('replyB').value += '[quote=' + $('post_user_' + id).innerHTML + ']' + response +  '[/quote]' + '\r\n';
			window.location = '#postReply';
		},
		onFailure: function(transport){
			var response = "There was an error while trying to use this function. Please contact an admin!";
			alert(response);
		}
	})
	
}
function startEdit(id) {
	new Ajax.Request('/ajax/ajax-forum.php', {
		method: 'post',
		parameters: {editing_id: id},
		onSuccess: function(transport){
			var response = transport.responseText || "An error has occurred.";
			$('edit_' + id).innerHTML = response;
			$('reply_' + id).style.display = 'none';
			$('edit_' + id).style.display = 'block';
		},
		onFailure: function(transport){
			var response = "An error has occurred.";
			alert(response);
		}
	})  
}
function cancelEdit(id) { 
	$('reply_' + id).style.display = 'block';
	$('edit_' + id).style.display = 'none';
}
function saveEdit(id) {
	new Ajax.Request('/ajax/ajax-forum.php', {
		method: 'post',
		parameters: {saving_id: id, saving_text: $('txtarea_' + id).value},
		onSuccess: function(transport){
			var response = transport.responseText || "An error has occurred.";
			if (response != 'failed') { 
				$('reply_' + id).innerHTML = response;
			}
			else { alert("There was an error"); } 
			$('reply_' + id).style.display = 'block';
			$('edit_' + id).style.display = 'none';
		},
		onFailure: function(transport){
			var response = "An error has occurred.";
			alert(response);
		}
	})  
}
function editTitle(id,currTitle) {
	var title = prompt("Enter a new title for this topic", currTitle)
	if (title) {
		new Ajax.Request('/ajax/ajax-forum.php', { 
			method: 'post',
			parameters: {editTopicID: id,title: title},
			onSuccess: function(transport){
				var response = transport.responseText || "There was some kind of error when trying to edit this.";
				if (response) { 
					window.location = "http://www.deadformat.net/topic.php?id=" + id;
				}
			}
		})
	}
}

function addCat(formName) {
	var Zurl = $(formName).getAttribute('action');
	var Zmethod = $(formName).getAttribute('method');
	var Zpars = Form.serialize(formName);
	var myAjax = new Ajax.Request(Zurl, {method: Zmethod, parameters: Zpars,
		onSuccess: function(transport){
			var response = transport.responseText || "An error occurred while trying to quote this response?";
			if (response == 1) {
				reloadCats();
			}
		},
		onFailure: function(transport){
			var response = "There was an error while trying to use this function. Please contact an admin!";
			alert(response);
		}
	})
	
}
function reloadCats() {
	new Ajax.Request('/ajax/ajax-cats.php', {
		method: 'post',
		parameters: {loadcats: 1},
		onSuccess: function(transport){
			var response = transport.responseText || "An error occurred while trying to quote this response?";
			$('catList').innerHTML = response;
		},
		onFailure: function(transport){
			var response = "There was an error while trying to use this function. Please contact an admin!";
			alert(response);
		}
	})
	
}
function delCat(category_id) {
	new Ajax.Request('/ajax/ajax-cats.php', {
		method: 'post',
		parameters: {delCat: category_id},
		onSuccess: function(transport){
			var response = transport.responseText || "An error occurred while trying to quote this response?";
			if (response == 1) { $('cat_' + category_id).style.display='none'; }
		},
		onFailure: function(transport){
			var response = "There was an error while trying to use this function. Please contact an admin!";
			alert(response);
		}
	})
	
}
function refreshCats() {
	new Ajax.Request('/ajax/ajax-cats.php', {
		method: 'post',
		parameters: {refreshCats: 1},
		onSuccess: function(transport){
			var response = transport.responseText || "An error occurred while trying to quote this response?";
			$('addToListCategories').innerHTML = response;
		},
		onFailure: function(transport){
			var response = "There was an error while trying to use this function. Please contact an admin!";
			alert(response);
		}
	})
	
}

/*** START TEXTAREA2 ***/

if(typeof(Control) == 'undefined')
	Control = {};
Control.TextArea = Class.create();
Object.extend(Control.TextArea.prototype,{
	onChangeTimeoutLength: 500,
	element: false,
	onChangeTimeout: false,
	initialize: function(textarea){
		this.element = $(textarea);
		$(this.element).observe('keyup',this.doOnChange.bindAsEventListener(this));
		$(this.element).observe('paste',this.doOnChange.bindAsEventListener(this));
		$(this.element).observe('input',this.doOnChange.bindAsEventListener(this));
		if(!!document.selection){
			$(this.element).observe('mouseup',this.saveRange.bindAsEventListener(this));  
			$(this.element).observe('keyup',this.saveRange.bindAsEventListener(this));
		}
	},
	doOnChange: function(event){
		if(this.onChangeTimeout)
			window.clearTimeout(this.onChangeTimeout);
		this.onChangeTimeout = window.setTimeout(function(){
			if(this.notify)
				this.notify('change',this.getValue());
		}.bind(this),this.onChangeTimeoutLength);
	},
	saveRange: function(){
		this.range = document.selection.createRange();  
	},
	getValue: function(){
		return this.element.value;
	},
	getSelection: function(){
		if(!!document.selection)
			return document.selection.createRange().text;
		else if(!!this.element.setSelectionRange)
			return this.element.value.substring(this.element.selectionStart,this.element.selectionEnd);
		else
			return false;
	},
	replaceSelection: function(text){
		var scroll_top = this.element.scrollTop;
		if(!!document.selection){
			this.element.focus();
			var range = (this.range) ? this.range : document.selection.createRange();
			range.text = text;
			range.select();
		}else if(!!this.element.setSelectionRange){
			var selection_start = this.element.selectionStart;
			this.element.value = this.element.value.substring(0,selection_start) + text + this.element.value.substring(this.element.selectionEnd);
			this.element.setSelectionRange(selection_start + text.length,selection_start + text.length);
		}
		this.doOnChange();
		this.element.focus();
		this.element.scrollTop = scroll_top;
	},
	wrapSelection: function(before,after){
		this.replaceSelection(before + this.getSelection() + after);
	},
	insertBeforeSelection: function(text){
		this.replaceSelection(text + this.getSelection());
	},
	insertAfterSelection: function(text){
		this.replaceSelection(this.getSelection() + text);
	},
	injectEachSelectedLine: function(callback,before,after){
		this.replaceSelection((before || '') + $A(this.getSelection().split("\n")).inject([],callback).join("\n") + (after || ''));
	},
	insertBeforeEachSelectedLine: function(text,before,after){
		this.injectEachSelectedLine(function(lines,line){
			lines.push(text + line);
			return lines;
		},before,after);
	}
});
if(typeof(Object.Event) != 'undefined')
	Object.Event.extend(Control.TextArea);

Control.TextArea.ToolBar = Class.create();
Object.extend(Control.TextArea.ToolBar.prototype,{
	textarea: false,
	container: false,
	initialize: function(textarea,toolbar){
		this.textarea = textarea;
		if(toolbar)
			this.container = $(toolbar);
		else{
			this.container = $(document.createElement('ul'));
			this.textarea.element.parentNode.insertBefore(this.container,this.textarea.element);
		}
	},
	attachButton: function(node,callback){
		node.onclick = function(){return false;}
		$(node).observe('click',callback.bindAsEventListener(this.textarea));
	},
	addButton: function(link_text,callback,attrs){
		var li = document.createElement('li');
		var a = document.createElement('a');
		a.href = '#';
		this.attachButton(a,callback);
		li.appendChild(a);
		Object.extend(a,attrs || {});
		if(link_text){
			var span = document.createElement('span');
			span.innerHTML = link_text;
			a.appendChild(span);
		}
		this.container.appendChild(li);
	}
});


Control.TextArea.ToolBar.Markdown = Class.create();
Object.extend(Control.TextArea.ToolBar.Markdown.prototype,{
	textarea: false,
	toolbar: false,
	options: {},
	initialize: function(textarea,options){
		this.textarea = new Control.TextArea(textarea);
		this.toolbar = new Control.TextArea.ToolBar(this.textarea);
		this.converter = (typeof(Showdown) != 'undefined') ? new Showdown.converter : false;
		this.options = {
			preview: false,
			afterPreview: Prototype.emptyFunction
		};
		Object.extend(this.options,options || {});
		if(this.options.preview){
			this.textarea.observe('change',function(textarea){
				if(this.converter){
					$(this.options.preview).update(this.converter.makeHtml(textarea.getValue()));
					this.options.afterPreview();
				}
			}.bind(this));
		}

		//buttons
		
		
		this.toolbar.addButton('Bold',function(){ this.wrapSelection('<b>','</b>'); },{ id: 'markdown_bold_button' });
		this.toolbar.addButton('Italics',function(){ this.wrapSelection('<i>','</i>'); },{ id: 'markdown_italics_button' });
		this.toolbar.addButton('Underline',function(){ this.wrapSelection('<u>','</u>'); },{ id: 'markdown_underline_button' });
		this.toolbar.addButton('Strikethrough',function(){ this.wrapSelection('<strike>','</strike>'); },{ id: 'markdown_strikethrough_button' });
		
		this.toolbar.addButton('Link',function(){
			var selection = this.getSelection();
			var response = prompt('Enter Link URL','');
			if(response == null)
				return;
			this.replaceSelection('<a href=\"' + (response == '' ? 'http://link_url/' : response).replace(/^(?!(f|ht)tps?:\/\/)/,'http://') + '\">' + (selection == '' ? 'Link Text' : selection) + '</a>');
		},{
			id: 'markdown_link_button'
		});
		
		this.toolbar.addButton('Image',function(){
			var selection = this.getSelection();
			var response = prompt('Enter Image URL','');
			if(response == null)
				return;
			this.replaceSelection('<img src=\"' + (response == '' ? 'http://image_url/' : response).replace(/^(?!(f|ht)tps?:\/\/)/,'http://') + '">');
		},{
			id: 'markdown_image_button'
		});
		this.toolbar.addButton('Help',function(){
			Element.toggle('tagHelp');
		},{
			id: 'markdown_help_button'
		});

	}
});
/** END TEXTAREA2 **/
