$(document).ready( function() {

	// fancy headings
	$('#vnav li a').each( function() { fancyText(this, 11, {'background-position': '14px 9px'}); });
	$('#vnav-intra li a').each( function() { fancyText(this, 11, {'background-position': '14px 18px'}); });
	$('#heading-subnav h1').each( function() { fancyText(this, 22, {'background-position': '0 4px'}); });
	$('#heading-subnav li a').each( function() { fancyText(this, 10, {'padding-right': '10px'}); });

	if (typeof(window['nicEditors']) != 'undefined')
		nicEditors.allTextAreas({iconsPath : '/img/nicEditorIcons.gif', externalCSS: '/css/zab.css'});

	Date.format = 'dd/mm/yyyy';

	var $dateInputs = $('input.date');
	if ($dateInputs.length > 0)
		$dateInputs.datePicker({startDate:'01/01/1996', clickInput: true}).dpSetOffset(22, 0);
	
	setTimeout(hideUserMessage, 5000 );
});

function confirmDelete()
{
	if (confirm('Are you sure you want to delete this item?'))
		postCommand('delete');
}

function hideUserMessage()
{
	$('.user-message').fadeOut(2000);
}

function postCommand(command)
{
	var form = document.createElement('form');
	form.method = 'post';
	var input = document.createElement('input');
	input.setAttribute('type', 'hidden');
	input.setAttribute('name', 'command');
	input.setAttribute('value', command);
	form.appendChild(input);	
	document.body.appendChild(form);
	form.submit();
}

function postForm(formName)
{
	if (window['nicEditors'])
		for (var i in nicEditors.editors)
			nicEditors.editors[i].nicInstances[0].saveContent();
	document.forms[formName].submit();
}

function toFilename(s)
{
	s = s.toLowerCase();
	s = s.replace(/\s/g, '_');
	s = s.replace(/[^a-z_]/g, '');
	return s;
}

function fancyText(element, size, css, clearText)
{
	var $element = $(element);
	var fn = toFilename($element.html());
	var w = $element.width();

	$element.css({'background': 'transparent url(/img/fancy/' + fn + '_' + size + '.png) scroll 0 0 no-repeat'});
	$element.css({'text-indent': '-9001px'});
	$element.css({'width': (w + 6	) + 'px'});
	if (css)
		$element.css(css); 	
}

