//(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&amp;%_\./-~-]*)?
var editorInsertLinkEnabled =
	editorInsertPhotoEnabled =
	editorInsertObjectEnabled =
	editorInsertCiteEnabled = true

var urlProtocolCheck = /^([a-zA-Z0-9]{2,6}):\/\//

$(document).ready(
	function ()
	{
		// форма ввода ссылки для вставки
		var editorInsertLinkForm = $('#modal-editorInsertLink form')
		
		if ( editorInsertLinkForm.length == 1 )
		{
			editorInsertLinkForm.validateForm(
				{
					submitSelector: 'input[type=image]',

					passedHandler:
						function ( submit, helpText )
						{
							submit.removeAttr('disabled')
							submit.attr('src', '/img/buttons/insert/normal.gif')
						},
					failedHandler:
						function ( submit, helpText )
						{
							submit.attr('disabled', 'disabled')
							submit.attr('src', '/img/buttons/insert/disabled.gif')
						}
				}
			)
		}
		editorInsertLinkForm.submit(
			function ()
			{
				if( editorInsertLinkEnabled ){
					editorInsertLinkEnabled = false
					
					var link = $('#mfeil-link', editorInsertLinkForm).val()
					
					if( !urlProtocolCheck.test(link) )
					{
						link = 'http://' + link
					}
					
					insertLink(
						link,
						$('#mfeil-text', editorInsertLinkForm).val()
					)
				}
				tb_remove()
				
				return false
			}
		)

		// форма ввода ссылки на фотку для вставки
		var editorInsertPhotoForm = $('#modal-editorInsertPhoto form')
		
		if ( editorInsertPhotoForm.length == 1 )
		{
			editorInsertPhotoForm.validateForm(
				{
					submitSelector: 'input[type=image]',

					passedHandler:
						function ( submit, helpText )
						{
							submit.removeAttr('disabled')
							submit.attr('src', '/img/buttons/insert/normal.gif')
						},
					failedHandler:
						function ( submit, helpText )
						{
							submit.attr('disabled', 'disabled')
							submit.attr('src', '/img/buttons/insert/disabled.gif')
						}
				}
			)
		}
		editorInsertPhotoForm.submit(
			function ()
			{
				if( editorInsertPhotoEnabled ){
					editorInsertPhotoEnabled = false
					
					insertImage(
						$('#mfiep-link', editorInsertPhotoForm).val()
					)
				}
				tb_remove()

				return false
			}
		)

		// форма ввода для вставки объекта
		var editorInsertObjectForm = $('#modal-editorInsertObject form')
		
		if ( editorInsertObjectForm.length == 1 )
		{
			editorInsertObjectForm.validateForm(
				{
					submitSelector: 'input[type=image]',

					passedHandler:
						function ( submit, helpText )
						{
							submit.removeAttr('disabled')
							submit.attr('src', '/img/buttons/insert/normal.gif')
						},
					failedHandler:
						function ( submit, helpText )
						{
							submit.attr('disabled', 'disabled')
							submit.attr('src', '/img/buttons/insert/disabled.gif')
						}
				}
			)
		}
		editorInsertObjectForm.submit(
			function ()
			{
				if( editorInsertObjectEnabled ){
					editorInsertObjectEnabled = false
					
					insertCode(
						$('#mfieo-code', editorInsertObjectForm).val()
					)
				}
				tb_remove()

				return false
			}
		)
		
		$('textarea', editorInsertObjectForm).keypress(
			function( event )
			{
				submitOnCtrlEnter( $(this), event )
			}
		)

		// форма ввода для вставки цитаты
		var editorInsertCiteForm = $('#modal-editorInsertCite form')
		
		if ( editorInsertCiteForm.length == 1 )
		{
			editorInsertCiteForm.validateForm(
				{
					submitSelector: 'input[type=image]',

					passedHandler:
						function ( submit, helpText )
						{
							submit.removeAttr('disabled')
							submit.attr('src', '/img/buttons/insert/normal.gif')
						},
					failedHandler:
						function ( submit, helpText )
						{
							submit.attr('disabled', 'disabled')
							submit.attr('src', '/img/buttons/insert/disabled.gif')
						}
				}
			)
		}
		editorInsertCiteForm.submit(
			function ()
			{
				if( editorInsertCiteEnabled ){
					editorInsertCiteEnabled = false
				
					insertCite(
						$('#mfiec-cite', editorInsertCiteForm).val()
					)
				}
				tb_remove()

				return false
			}
		)
		
		$('textarea', editorInsertCiteForm).keypress(
			function( event )
			{
				submitOnCtrlEnter( $(this), event )
			}
		)

	}
)


function editorInsertLinkShow()
{
	editorInsertLinkEnabled = true
	
	setTimeout(
		function()
		{
			$('#mfeil-link').focus()
		},
		100
	)
}

function editorInsertPhotoShow()
{
	editorInsertPhotoEnabled = true
	
	setTimeout(
		function()
		{
			$('#mfiep-link').focus()
		},
		100
	)
}

function editorInsertObjectShow()
{
	editorInsertObjectEnabled = true
	
	setTimeout(
		function()
		{
			$('#mfieo-code').focus()
		},
		100
	)
}

function editorInsertCiteShow()
{
	editorInsertCiteEnabled = true
	
	setTimeout(
		function()
		{
			$('#mfiec-cite').focus()
		},
		100
	)
}

function submitOnCtrlEnter( textarea, event )
{
	if ( (event.keyCode == 13 || event.keyCode == 10) && event.ctrlKey )
	{
		textarea.parents('form').submit()
		return false
	}
}
