$(document).ready(pageLoaded);

function pageLoaded()
{
	if($('#title.title-complete').length)
	{
		$("#title.title-complete").keyup(function(){updateUrl(this);});
		$("#url.title-complete").after('<p id="url-example"></p>');
		
		$("#url.title-complete").keyup(function()
		{
			$("#url-example").html('e.g. http://www.expertopinions.org.uk/article/<strong>'+$("#url.title-complete").val()+'</strong>/');
		});
		$("#url.title-complete").keyup();
	}
	
	if($('#article-submit').length)
	{
		$("#article-content").after('<p>[<a id="correct-paragraph-spacing" href="#">Correct Paragraph Spacing</a>]</p>');
		$('#correct-paragraph-spacing').click(function(){paragraphing();return false;});
		$("#article-submit").before('<h2>Preview</h2><div id="article-preview"></div>');
		converter = new Showdown.converter();
		$("#article-content").keyup(function()
		{
			$('#article-preview').html('<div style="font-style:italic;">'+converter.makeHtml($("#article-intro").val())+'</div>'+converter.makeHtml($("#article-content").val()));
		});
		$("#article-content").keyup();
	}
	
	if($(".elastic").length)
	{
		$(".elastic").elastic();
		$(".elastic").keyup();
	}
	
	$(".top-level.nav a").mouseenter(function(){$(this).animate({opacity:0.75}, 200); return false;});
	$(".top-level.nav a").mouseleave(function(){$(this).animate({opacity:1}, 200); return false;});
}

function paragraphing()
{
	var content = $("#article-content").val();
	
	content = content.replace(/(.)\n(.)/g, '$1\n\n$2');
	
	$("#article-content").val(content);
	$("#article-content").keyup();
}

function updateUrl(that)
{
	var text = $(that).val();
	
	text = text.replace(/ /g, '-');
	text = text.replace(/[^a-zA-Z0-9-]/g, '');
	text = text.replace(/(-){2,}/g, '-');
	text = text.toLowerCase();
	
	$("#url.title-complete").val(text);
	$("#url.title-complete").keyup();
}
