/**
 * Cascading menu
 * @author Jerald Lopez
 * @version 1.0
 */
typeof jQuery !== "undefined" && jQuery( function( )
{
	// collect all sub menus
	var	sub_menus =	$( "li.sub-menu" ),
		nth_menus =	$( "li.nth-menu" ),

		active_menu,
		quick_close =	false;

	sub_menus.each( function( )
	{
		var height = 0;

		var next_menu = $( this ).find( '> ul' ).css('display', 'block');
 
		// collect all the list element
		$( ">li", next_menu ).each(function( )
		{
			height += this.offsetHeight;
		} );

		next_menu.attr( 'style', 'display: none' );

		$( this ).data( 'max-height', height );
	} ) 
	.click( function( e )
	{

		if( this !== active_menu || quick_close == true )
			close_menu( );

		active_menu = this;

		return open_menu( );
		
	} )
	.find( '> a' )
	.click( function( e )
	{
		e.preventDefault( );
	} );


	function open_menu( )
	{
		var	ul = $( active_menu ).find( '> ul' ),
			height = ul.height( );

		quick_close = true; 

		ul.addClass( 'shown' );
 
		ul.attr( 'style', 'height:' + height ).stop( ).animate( {height: $( active_menu ).data( 'max-height' )}, 500, 'easeInSine', function( )
		{
			
		} );
	}

	function close_menu( )
	{
		$( active_menu ).find( '> ul' ).stop( ).animate( {height: 0}, 500, function( )
		{
			$( this ).attr( 'style', '' );

			$( this ).removeClass( 'shown' );
		} );

		quick_close = false;
		
	}

	$( document ).bind( 'click.menuClose', function( e )
	{
		var outside = true,
		element = e.target;

		sub_menus.each( function( )
		{
			if( $( 'a', this )[0] == element )
				outside = false;
		} );

		if( outside && quick_close )
			close_menu( );
	} );



	fix_menu( );

	function fix_menu( )
	{
		$( "#menu" );
	}
} );



				$( function( )
				{ 
					$( ".ajax-load" ).live( 'mouseover.ajax', function( )
					{
						var cache = this;
						var mode = "category";

						$( this ).removeClass( 'ajax-load' );
						if( $( this ).hasClass( 'ajax-location' ) )
							mode = "localization";

						$( this ).unbind( 'mouseover.ajax' );

						var nth_menu = $( '<ul>' );
						var loading = $( '<li></li>' ).addClass( 'loading' ).appendTo( nth_menu );

						nth_menu.appendTo( this );

						// load from the database
						$.get( mode == "category" ? 'ajax.categories.php' : 'ajax.localizations.php', mode == "category" ? {category: $( this ).attr( 'data-target' )} :  {localization: $( this ).attr( 'data-target' )}, function( data )
						{
							// append
							loading.remove( );
 
							$.each( data, function( x, y )
							{
								var next_level	= y.categoryid		|| y.localizationid,
								title		= y.categorytitle	|| y.label;

								var link = $( '<li>' ).html( '<a href="index.php?' +  mode + '=' + next_level + '">' + title + '</a>' );

								link.appendTo( nth_menu );
 
								if( 0 < y._sub_category )
								{
									// add a class to the link
									link.addClass( 'nth-menu ajax-load' ).attr( 'data-target', next_level );

									if( "localization" == mode )
										link.addClass( 'ajax-location' );
								}
							} );
						}, "json" );
					} );
				} );

