/* constants */
var FN_MIN_WIDTH = 210;		// the minimal width of the foldnav

/* global variables */
var activeFN = null;		// holds the last openened foldnav


$(document).ready(function()
{
	// set offest left and width for each foldnav entry found
	$('.foldnav').each(function()
	{
		naventry = $('#main_' + this.id);

		// workaround so we can use visibilty: hidden instead of display: none in CSS
		$(this).css('display', "none");
		$(this).css('visibility', "visible");


		if (!naventry.position()) return;

		left = naventry.position().left;
		width = naventry.width();

		seekdir = "right";
		rightstep = 0;
		leftstep = 0;
		totalsteps = 0;

		while (width < FN_MIN_WIDTH && totalsteps < naventry.siblings().length)
		{

			if (seekdir == "right")
			{
				checkentry = naventry.next();
				for (i=0; i<rightstep; i++) checkentry = checkentry.next();

				if (checkentry.width() > 0)
				{
					width += checkentry.width();
					rightstep++;
				}
				else
				{
					seekdir = "left";
				}
			}

			if (seekdir == "left")
			{
				checkentry = naventry.prev();
				for (i=0; i<leftstep; i++) checkentry = checkentry.prev();

				if (checkentry.width() > 0)
				{
					width += checkentry.width();
					left = checkentry.position().left;
					leftstep++;
				}
				else
				{
					seekdir = "right";
				}
			}

			totalsteps++;
		}

		$(this).css('left', left-1);
		$(this).width(width+1);

		//$(this).show();
	});


	// attach onmouseover events to the mainnav elements
	$('#nav .hlist li').each(function()
	{
		idsubnav = this.id.substring(this.id.indexOf("_") + 1);
		if ($('#' + idsubnav).width() > 0 || true)
		{
			$(this).mouseover(function()
			{
				if (activeFN) activeFN.hide();

				idsubnav = this.id.substring(this.id.indexOf("_") + 1);
				$('#' + idsubnav).show();
				activeFN = $('#' + idsubnav);
			});
		}
	});


	// attach events for hiding foldnav
	$('#main').mouseover(function()
	{
		if (activeFN) activeFN.hide();
		$('#directjump ul').hide();
	});

	$('#header').mouseover(function()
	{
		if (activeFN) activeFN.hide();
		//$('#directjump ul').hide();
	});

	$(this).click(function()
	{
		if (activeFN) activeFN.hide();
		$('#directjump ul').hide();
	});

	initDatePickers();
	
	
	// Directjump
	
	$('#directjump a.trigger').click(function()
	{
		$('#directjump ul').toggle();
		return false;
	});
	
	

});


function initDatePickers()
{
	if ($('input.datebox').datepicker)
	{
		$('input.datebox').datepicker({
			showOn: 'button',
			buttonImage: /*CMSURL*/"/abz/global/images/layout_images/ico_datepicker.gif",
			buttonImageOnly: true
		});
	}
}



