$(document).ready(function()
{
	$('#navigation li.top').each(function()
	{
		var sub  = $(this).find('ul');
		var link = $(this).find('a:first');

		if (sub.length > 0 && typeof sub._h == 'undefined')
		{
			var p_top = sub.css('padding-top');
			var p_bot = sub.css('padding-bottom');

			// Set the expanded height
			sub._h = sub.height() + parseInt(p_top) + parseInt(p_bot);

			// Add top padding to the first child
			sub.find('li:first').css('padding-top', p_top);

			// Remove top/bottom padding
			sub.css('padding-bottom', 0).css('padding-top', 0);

			// Prepare to animate
			sub.css({ height: 0 });
		}

		$(this).hover(function()
		{
			// Activate top level
			$(this).addClass('active');

			// Go to full height
			sub.stop().animate({ height: sub._h }, 300);
		},
		function()
		{
			// Go to 0 height
			sub.stop().animate({ height: 0 }, 300);

			// Deactivate top level
			$(this).removeClass('active');
		});
	});

	// sIFR text headers
	$('#mainBody h1, #mainBody h2, #mainBody h3, #mainBody h4, #leftSideBar h4').flash(
		{
			src: base_url + 'media/flash/text_bankgothic.swf',
			flashvars: {
				css: [
					'* { color: #222; }',
					'a { color: #800; text-decoration: underline; }',
					'a:hover { color: #e00; }'
				].join(' ')
			}
		},
		{ version: 8 },
		function(options) {
			var self = $(this);
			// Set flash options
			options.flashvars.txt = self.text().toUpperCase();
			options.height = self.height() * 1.25;
			options.width = self.width();
			options.wmode = 'transparent';
			// Transform the text
			self.html('<div class="text-replaced">'+ this.innerHTML +'</div>').prepend($.fn.flash.transform(options));
		}
	);

	if ($('#admin textarea.editor').length)
	{
		// Changed editor textareas into markItUp with Markdown settings
		$('#admin textarea.editor').markItUp(mySettings);
	}

	// Require confirmation
	$('a.confirm').click(function()
	{
		return confirm('This action cannot be undone, continue?');
	});

	// Start the preloader when everything else has been loaded
	$(window).load(function()
	{
		if ($.browser.msie && $.browser.version < 7.0)
			return true;

		// Create the preloader and find the animation container
		var preload   = $('<div></div>').css({ position: 'absolute', top: '-1000px', left: '-1000px' }).appendTo('body');
		var animation = $('<div></div>').attr('id', 'animation').css('opacity', 0).appendTo('body .container');

		// Append a new image to the preloader
		var image = $('<img>').appendTo(preload);

		image.attr('src', base_url +'media/img/bg_header.gif').load(function()
		{
			var self = $(this);

			// Make sure the animation is the same size as the image
			animation.height(self.height()).width(self.width());

			// Insert the image into the animation and fade it in
			animation.append(self).animate({ opacity: 1 }, 500);

			// Remove the preloader
			preload.remove();
		});
	});
});

