// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var TopicForm = {
	editNewTitle: function(txtField)
	{
		$('new_topic').innerHTML = (txtField.value.length > 5) ? txtField.value : 'New Topic';
	}
}

var EditForm = {
// show the form
	init: function(postId)
	{
		$('edit-post-' + postId + '_spinner').show();
		this.clearReplyId();
	},

// sets the current post id we're editing
	setReplyId: function(postId)
	{
		$('edit').setAttribute('post_id', postId.toString());
		$('posts-' + postId + '-row').addClassName('editing');
		if ($('reply')) $('reply').hide();
	},

// clears the current post id
	clearReplyId: function()
	{
		var currentId = this.currentReplyId()
		if (!currentId || currentId == '') return;

		var row = $('posts-' + currentId + '-row');
		if (row) row.removeClassName('editing');
		$('edit').setAttribute('post_id', '');
	},

// gets the current post id we're editing
	currentReplyId: function()
	{
		return $('edit').getAttribute('post_id');
	},

// checks whether we're editing this post already
	isEditing: function(postId)
	{
		if (this.currentReplyId() == postId.toString())
		{
			$('edit').show();
			$('edit_post_body').focus();
			return true;
		}
		return false;
	},

// close reply, clear current reply id
	cancel: function()
	{
		this.clearReplyId();
		$('edit').hide()
	}
}

var ReplyForm = {
// yes, i use setTimeout for a reason
	init: function()
	{
		EditForm.cancel();
		$('reply').toggle();
		$('post_body').focus();
		// for Safari which is sometime weird
		//    setTimeout('$(\"post_body\").focus();',50);
	}
}
/*
Event.addBehavior({
	'#search,#monitor_submit': function()
	{
		this.hide();
	}
})
*/
function insertimage()
{
	// our textfield
	var textarea = document.getElementById("reply_form");

	// our image
	var url = prompt("Пожалуйста , введите адрес", "http://");
	var image = "[img]" + url + "[/img]";

	if (!textarea.setSelectionRange)
	{
		// get selected text
		var selected = document.selection.createRange().text;

		if (selected.length <= 0)
		{
			// no text was selected so add the image to the end
			textarea.value += image;
		}
		else
		{
			// replace the selection with the image
			document.selection.createRange().text = image;
		}
	}
	else
	{
		// the text before the selection
		var pretext = textarea.value.substring(0, textarea.selectionStart);

		// the text after the selection
		var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length)

		// update the text field
		textarea.value = pretext + image + posttext;
	}

	// set the focus on the text field
	textarea.focus();
}


function insertlink()
{
	// our textfield
	var textarea = document.getElementById("reply_form");

	// our link
	var url = prompt("Введите адрес", "http://");
	var desc = prompt("Введите название ссылки");
	var link = "[url=" + url + "]" + desc + "[/url]";

	if (!textarea.setSelectionRange)
	{
		// get selected text
		var selected = document.selection.createRange().text;

		if (selected.length <= 0)
		{
			// no text was selected so add the link to the end
			textarea.value += link;
		}
		else
		{
			// replace the selection with the link
			document.selection.createRange().text = link;
		}
	}
	else
	{
		// the text before the selection
		var pretext = textarea.value.substring(0, textarea.selectionStart);

		// the text after the selection
		var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length)

		// update the text field
		textarea.value = pretext + link + posttext;
	}

	// set the focus on the text field
	textarea.focus();
}


function insertcode(tag, desc)
{
	// our textfield
	var textarea = document.getElementById("reply_form");

	// our open tag
	var open = "[" + tag + "]";

	// our close tag
	var close = "[/" + tag + "]";

	if (!textarea.setSelectionRange)
	{
		var selected = document.selection.createRange().text;
		if (selected.length <= 0)
		{
			// no text was selected so prompt the user for some text
			textarea.value += open + prompt("Please enter the text you'd like to " + desc, "") + close;
		}
		else
		{
			// put the code around the selected text
			document.selection.createRange().text = open + selected + close;
		}

	}
	else
	{
		// the text before the selection
		var pretext = textarea.value.substring(0, textarea.selectionStart);

		// the selected text with tags before and after
		var codetext = open + textarea.value.substring(textarea.selectionStart, textarea.selectionEnd) + close;

		// the text after the selection
		var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length)

		// check if there was a selection
		if (codetext == open + close)
		{
			//prompt the user
			codetext = open + prompt("Please enter the text you'd like to " + desc, "") + close;
		}

		// update the text field
		textarea.value = pretext + codetext + posttext;
	}

	// set the focus on the text field
	textarea.focus();
}

