/**
 * Javascript file for the site management system
 *
 * - Includes javascript for the main menu
 * - Includes javascript for submitting forms
 *
 * @author Frank Dekker
 * @date 26-07-2007
 * @version 1.0
 */

function do_toggleMainmenu( id )
{
	x_toggleMainmenu(id, writeContent);
	var subOptions = document.getElementById(id);
	var mainImage = document.getElementById( 'img_'+id );

    var imgClose = new String( baseUrl+"services/images/arrow_close.gif" );
    var imgLog = new String( mainImage.src );

    var substrstr = imgLog.substring( imgLog.length - imgClose.length );

	if ( substrstr == baseUrl+"services/images/arrow_close.gif" )
		mainImage.src = baseUrl+"services/images/arrow_open.gif";
	else
		mainImage.src = baseUrl+"services/images/arrow_close.gif";
}

function writeContent ( content )
{
    // dont do anything yet.
}

function submitForm( formname )
{
    var form = document.getElementById( formname );
    if ( form == null )
        alert( "Couldn't find form: " + formname );
    form.submit();
}

/**
 * Add event when dom is ready
 */
window.addEvent('domready', function() {

    /*
     * Search form javascript
     */
    var searchform = document.getElementById('searchform');
    var searchlink = document.getElementById('link_zoeken');
    if ( searchform != null && searchlink != null )
    {
        var searchcontent = document.getElementById('searchcontent');
        var searchfield = document.getElementById('searchfield');

        // add fx animation style to the search form
        var searchfx = new Fx.Styles( 'searchform', {duration:500, wait:true});
        var searchcontentfx = new Fx.Styles( 'searchcontent', {duration:500, wait:true});

        // add event to the 'Zoeken' link
        $('link_zoeken').addEvent( 'click', function(e) {
            searchform.style.visibility = 'visible';
            e = new Event( e );
            searchfx.stop( );
            searchcontentfx.stop();
            searchfx.start(
            {
               'width' : '340',
               'height' : '270'
            }).chain(function()
            {
                searchcontent.style.opacity = 0;
                searchcontent.style.display = 'block';
                searchcontentfx.start( { 'opacity': 1 }).chain(
                function() { searchfield.focus(); });
            });
            e.stop( );
        });

        // add event to the 'Close' link
        $('link_closezoeken').addEvent( 'click', function(e) {
            e = new Event( e );
            searchcontent.style.opacity = 0;
            searchcontent.style.display = 'none';
            searchfx.stop( );
            searchfx.start(
            {
                'width' : '0',
                'height' : '0'
            }).chain(function()
            {
                searchform.style.visibility = 'hidden';
            });
            e.stop( );
        });
    }

    /*
     * Add fx fade to the tooltips
     */
    var tooltips = document.getElementsByTagName( 'SPAN' );
    var tooltipfx = new Array();
    for ( i = 0; i < tooltips.length; i++ )
    {
        // get only the spans that start with 'label_'
        var label = tooltips.item(i).getAttribute( 'id' );
        if ( label == null || label.substring( 0, 6 ) != 'label_' )
            continue;

        tooltipfx[label] = new Fx.Styles( label, {duration:1000, wait:true} );

        var link = document.getElementById( 'link_'+label );

        link.onmouseover = function(e) {
            e = new Event(e);
            var idx = e.target.getAttribute( 'id' ).substring( 5 );
            tooltipfx[idx].stop();
            tooltipfx[idx].set( {'opacity':1} );
            document.getElementById( idx ).style.display = 'inline';
            e.stop();
        }

        link.onmouseout = function(e) {
            e = new Event(e);
            var idx = e.target.getAttribute( 'id' ).substring( 5 );
            tooltipfx[idx].start.delay( 2000, tooltipfx[idx], {'opacity': 0 } );
            e.stop();
        }
    }

    /*
     * Add fx style to the main menu
     */
    var mainmenu = document.getElementById('mainmenu_cell');
    if ( mainmenu != null )
    {
        // get all main menu elements
        var menuElms = mainmenu.childNodes
        var items = menuElms.length;
        var fx = new Array( items );

        // loop through each element
        for( i = 0; i < items; i++ )
        {
            var elm = menuElms.item(i);
            if ( elm.nodeName != 'DIV' )
                continue;

            var elmId = elm.getAttribute( 'id' );
            if ( elmId == null )
                continue;

            var menuL = document.getElementById('toggle_'+elmId);
            menuL.setAttribute( 'index', i );

            if ( elm.style.display == 'none' )
                elm.style.display = 'block';

            // create new Fx slide on the menu item
            fx[i] = new Fx.Slide( elmId );

            if ( hideMenu[elmId] == 1 )
                fx[i].hide();

            // on click event, handle it with global variables
            $('toggle_'+elmId).addEvent( 'click', function(e)
            {
			    e = new Event(e);
			    var index = e.target.parentNode.getAttribute( 'index' );
			    var eid = e.target.parentNode.getAttribute( 'id' ).substring( 7 );
			    do_toggleMainmenu( eid );
			    fx[index].toggle();
			    e.stop();
			});
        }
    }

}); // end of window.addEvent( 'domready' );


