/*  Submenu, version 1.2.1
 *  (c) 2010 Andrey Nebogin anebogin@gmail.com
 *
 * structure of the html code is: <div class="submenu"><div class="item"><a href=""></a></div>...</div>
 *
 *--------------------------------------------------------------------------*/

function ASubmenu( parentNodeId ) {
    this.parent = document.getElementById( parentNodeId );
    this.created = false;
    this.canBeHided = true;
    
    this.container_id = "";
    this.container = null;
    
    this.items = []; //{ title: "title", url: "url", target: "" }
    this.options = {
        container_class: "submenu"
    };
    
    if( this.parent )
    {
        $("#"+this.parent.id).bind( "mouseover", { submenu: this }, function( event )
        {
            event.data.submenu.canBeHided = false;
            event.data.submenu.show( event );
        } );
        $("#"+this.parent.id).bind( "mouseout", { submenu: this }, function( event )
        {
            if( event.relatedTarget != event.data.submenu.container && event.relatedTarget.parentNode != event.data.submenu.container )
            {
                event.data.submenu.canBeHided = true;
                event.data.submenu.hide( event );
            }
        } );
    }
    
    this.addItems = function( items )
    {
        if( items.length > 0 )
        {
            for( var i=0; i<items.length; i++ )
            {
                this.items.push( { title: items[i].title, url: items[i].url, target: items[i].target } );
            }
        }
    };
    
    this.show = function( event )
    {
        this.create();
        if( this.created )
        {
            if( event.currentTarget.id != "" )
            {
                var p = $( "#"+ this.parent.id );
                p.addClass( "selected" );
                
                var top = p.position().top + $("#"+event.currentTarget.id).height();
                var left = p.position().left;
                
                if( this.items.length > 0 )
                {
                    $("div#"+ this.container_id ).css( {
                        "left": left +"px",
                        "top": top +"px",
                        "visibility": "visible"
                    } );
                }
            }
        }
    };
    
    this.hide = function( event )
    {
        if( this.created )
        {
            var self = this;
            var t = setTimeout( function()
            {
                if( self.canBeHided )
                {
                    var p = $( "#"+ self.parent.id );
                    p.removeClass( "selected" );
                    
                    $("div#"+ self.container_id ).css( "visibility", "hidden" );
                }
            }, 100 );
            
        }
    };
    
    this.create = function()
    {
        if( !this.created )
        {
            var date = new Date();
            this.container_id = date.getTime();
            this.container = document.createElement("div");
            this.container.id = this.container_id;
            document.body.appendChild( this.container );
            $("div#"+ this.container_id ).css( {
                'position': "absolute",
                'top': "-200px",
                'left': "0",
                'z-index': "100",
                'backgroundColor': "#000"
            } );
            $("div#"+ this.container_id ).addClass( this.options.container_class );
            
            for( var i=0; i<this.items.length; i++ )
            {
                if( this.items[i].title == "&nbsp;" ) $("div#"+ this.container_id ).append( '<div class="item_separator">&nbsp;</div>' );
                else $("div#"+ this.container_id ).append( '<div class="item"><a href="'+ this.items[i].url +'" target="'+ this.items[i].target +'">'+ this.items[i].title +'</a></div>' );
            }
            
            $("div#"+ this.container_id ).bind( "mouseover", { submenu: this }, function( event )
            {
                event.data.submenu.canBeHided = false;
            } );
            
            $("div#"+ this.container_id ).bind( "mouseout", { submenu: this }, function( event )
            {
                if( event.relatedTarget != event.data.submenu.parent && event.relatedTarget.parentNode != event.data.submenu.parent )
                {
                    event.data.submenu.canBeHided = true;
                    event.data.submenu.hide( event );
                }
            } );
            
            this.created = true;
        }
    };
    
    this.getContainer = function() { return(this.container) };
    this.getContainerId = function() { return(this.container_id) };
}

