jQuery(document).ready(function() {

	var clearMePrevious = '';
	var exampleClass = false;
	var commentControl = 0;

	// clear input on focus
	jQuery('.clear-on-focus').focus(function()
	{	
		if(jQuery(this).val()==jQuery(this).attr('title'))
		{
			clearMePrevious = jQuery(this).val();
			jQuery(this).val('');
		}
	});
	
	// if field is empty afterward, add text again
	// if field doesn't have a class of example, add it in
	jQuery('.clear-on-focus').blur(function()
	{
		if(jQuery(this).val()=='')
		{
			jQuery(this).val(clearMePrevious);

			if(!(jQuery(this).is('.example')) && exampleClass)
			{
				jQuery(this).addClass("example");
			}
		}
	});

	// confirmation box appears when button is clicked
	jQuery('.confirm').click(function()
	{
		message = jQuery(this).attr('title');
		var answer = confirm(message);
		return answer // answer is a boolean
	});

	// clear 
	jQuery('.example').focus(function()
	{	
		jQuery(this).removeClass("example");
		exampleClass = true;
	});

	// Character Limiting
	jQuery(function()
	{
		jQuery('.limit').keydown(function()
		{
			limitChars('wmd-input', 500, 'charlimitinfo');
		})
		jQuery('.limit').keyup(function()
		{
			limitChars('wmd-input', 500, 'charlimitinfo');
		})
	});

	// Toggle Comment Options 
	jQuery('#toggle-admin-controls').click(function()
	{	
		if(commentControl == 0)
		{
			jQuery('.comment-options').fadeIn();
			commentControl = 1;
			return false;
		}
		else
		{
			jQuery('.comment-options').fadeOut();
			commentControl = 0;
			return false;
		}
	});
});

function limitChars(textid, limit, infodiv)
{
	var text = jQuery('#'+textid).val();
	var textlength = text.length;
	if(textlength > limit)
	{
		jQuery('#' + infodiv).html('0');
		jQuery('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		jQuery('#' + infodiv).html(limit - textlength);
		return true;
	}
}
