/*  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", subtitle: "subtitle", 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, subtitle: items[i].subtitle, 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 );
                if( this.items.length > 1 ) {
	                p.addClass( "selected" );
		} else {
	                p.addClass( "selected_single" );
		}
                
                var top = p.position().top + $("#"+event.currentTarget.id).height();
                var left = p.position().left - 4;
                
                if( this.items.length > 1 )
                {
                    $("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 );
        	    if( self.items.length > 1 ) {
	                    p.removeClass( "selected" );
	            } else {
	                    p.removeClass( "selected_single" );
		    }
                    
                    $("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"
            } );
            $("div#"+ this.container_id ).addClass( this.options.container_class );
            
            for( var i=0; i<this.items.length; i++ )
            {
		var submenuitem = '<table border="0" style="width: 100%;" cellspacing="0" cellpadding="0"><tr><td class="submenu_border_left"></td><td><div class="item"><a href="'+ this.items[i].url +'" target="'+ this.items[i].target +'">'+ this.items[i].title +'</a>';
		if (this.items[i].subtitle != "") {
			submenuitem += '<div class="subitem">'+ this.items[i].subtitle +'</div>';
		}
		submenuitem += '</div></td><td class="submenu_border_right"></td></tr></table>'

                $("div#"+ this.container_id ).append( submenuitem );
            }
            $("div#"+ this.container_id ).append( '<table border="0" style="width: 100%;" cellspacing="0" cellpadding="0"><tr><td class="submenu_border_bottom_left"></td><td class="submenu_border_bottom">&nbsp;</td><td class="submenu_border_bottom_right"></td></tr></table>' );
            
            $("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) };
}

