/* constants */
var FN_MIN_WIDTH = 160;		// 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*/"/akademie/global/images/layout_images/ico_datepicker.gif",
			buttonImageOnly: true
		});
	}
}

    function closeDialog(event) {
        window.frames['content'].$(this).dialog("close");
        document.body.removeChild(document.getElementById("webcode"));
    }
    



    function viewWebcode(obj) {
        var dialogId = "webcode";
        var pos = findPos(obj);
        var dialog   = createDialog(dialogId,pos);
        var message = "Bitte geben Sie hier den Webcode ein<form class =\"yform\" method=\"post\" action=\"/akademie/inhalte/Anwendungen/webcode.jsp\"><input type=\"text\"  name=\"code\" id=\"code\"/>&nbsp;<input type=\"submit\" id=\"submit\" value=\"Gehe zu\"></form>";
        dialog.html(message);
        dialog.dialog("open");
    }


    function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;

		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
    }


   function createDialog(id,pos){
        var dialogDiv = document.getElementById( id );
        if (dialogDiv == null){
            $("body").append('<div id="' + id + '">&nbsp;</div>');
	}
        dialogDiv = document.getElementById( id );
    	// create dialog
	$( dialogDiv ).dialog({
		autoOpen:   false
		,close: function(){
		    $(this).dialog('destroy').remove();
		}
		,height:    100
		,title:     "Webcode"
		,position: [pos[0]-80,pos[1]]
		,width:     265
	 });
        return $( dialogDiv );
     }



