/*
    Horizontal Drop-Down Menus
    Jon Parrott, 2010, Eyesore, Inc
    Requires Prototype and Scriptaculous
*/
Event.observe( document, 'dom:loaded', function(){
    var make_menu = function( selector ){
        $$(selector).each( function( elem ){
            
            elem.hide_timer = 0;
            
            elem.up().observe( 'mouseover', function(){
                if( this.hide_timer != 0 ){ clearTimeout( this.hide_timer ); }
                
                var parent_layout = new Element.Layout(this.up());
                
                this.setStyle({ 'width': parent_layout.get('padding-box-width') + 'px' });
                var layout = new Element.Layout(this);
                
                var offset = ( parent_layout.get('padding-box-width')/2 ) - ( layout.get('padding-box-width')/2 ) // different of the two centers
                var position = parent_layout.get('left') + offset - 2; // center of parent
                this.setStyle({ 'left':  position + 'px' });
                
                position = parent_layout.get('top') + parent_layout.get('padding-box-height');
                this.setStyle({ 'top':  position +'px' });
                
                if( elem.getStyle('display') == 'none' ) this.appear({duration: 0.2});
            }.bind( elem ));
            
            elem.up().observe( 'mouseout', function(){
                this.hide_timer = window.setTimeout( function(){ 
                    this.fade({duration: 0.3});
                }.bind( this ), 300 );
            }.bind( elem ));
            
        });
    };
    
    make_menu( '.sub_menu' );
});
