
Type.registerNamespace('AjaxControlToolkit');
Type.registerNamespace('BlueBridge.SharePointExtensions.Menu.Publishing');

BlueBridge.SharePointExtensions.Menu.Publishing.BreadcrumbHoverMenuBehavior = function(element) {

    BlueBridge.SharePointExtensions.Menu.Publishing.BreadcrumbHoverMenuBehavior.initializeBase(this, [element]);
    
    // Properties
    this._contextKey = null;
    
    // Variables
    this._popupVisible = false;
    this._populated = null;
    this._timer = null;
}

BlueBridge.SharePointExtensions.Menu.Publishing.BreadcrumbHoverMenuBehavior.prototype = 
{

    initialize : function() 
    {
        //jslog.debug ("BreadcrumbHoverMenuBehavior.initialize ...");
        BlueBridge.SharePointExtensions.Menu.Publishing.BreadcrumbHoverMenuBehavior.callBaseMethod(this, 'initialize');
        
        this._tickHandler = Function.createDelegate(this, this._onTimerTick);
        
        this._timer = new Sys.Timer();
        this.initializeTimer(this._timer);
    },

    dispose : function() 
    {
        if (this._timer) 
        {        
            this._timer.dispose();
            this._timer = null;
        }
        
        BlueBridge.SharePointExtensions.Menu.Publishing.BreadcrumbHoverMenuBehavior.callBaseMethod(this, 'dispose');
        
    },
        
    get_contextKey : function() 
    {
        return this._contextKey;
    },
    set_contextKey : function(key) 
    {
        this._contextKey = key;
        this.raisePropertyChanged('contextKey');
    },
        
    initializeTimer : function(timer) 
    {
        timer.set_interval(100);
        timer.add_tick(this._tickHandler);
    },
    _onTimerTick : function() 
    {
         if(this._inHover)
            this._onHover(); 
    },
    _onHover : function() 
    {
        //jslog.debug ("BreadcrumbHoverMenuBehavior._onHover ...");
        if (this._inHover) return;

        var eventArgs = new Sys.CancelEventArgs();
        this.raiseShowing(eventArgs);
        if (eventArgs.get_cancel()) {
            return;
        }

        this._inHover = true;
        
        this.showPopup();
        
        this.raiseShown(Sys.EventArgs.Empty);
    },
    _onUnhover : function() 
    {
        var eventArgs = new Sys.CancelEventArgs();
        this.raiseHiding(eventArgs);
        if (eventArgs.get_cancel()) {
            return;
        }
        
        this._timer.set_enabled(false);
        this._inHover = false;
        this._resetCssClass(); 
        this.hidePopup();
        
        this.raiseHidden(Sys.EventArgs.Empty);
    },
    showPopup : function() 
    {
        //jslog.debug ("BreadcrumbHoverMenuBehavior.showPopup ...");
        
        var old = BlueBridge.SharePointExtensions.Menu.Publishing.BreadcrumbHoverMenuBehavior.__VisiblePopup;
        if (old && old._popupBehavior) {
            old.hidePopup();
        }

        this.populate();
        
        this._popupBehavior.show();
        
        if ($common.getCurrentStyle(this._popupElement, 'display') == 'none') {
            this._popupElement.style.display = 'block';
        }
        this._popupBehavior.set_x(this._getLeftOffset());
        this._popupBehavior.set_y(this._getTopOffset());
        
        this._popupVisible = true;
        
        BlueBridge.SharePointExtensions.Menu.Publishing.BreadcrumbHoverMenuBehavior.__VisiblePopup = this;
    },
    
    hidePopup : function() 
    {
        this._popupBehavior.hide();
        this._popupVisible = false;
        BlueBridge.SharePointExtensions.Menu.Publishing.BreadcrumbHoverMenuBehavior.__VisiblePopup = null;
    },
    
    add_populate : function(handler) 
    {
        this.get_events().addHandler('populate', handler);
    },
    
    remove_populate : function(handler) 
    {
         this.get_events().removeHandler('populate', handler);
    },
    
    populate : function() 
    {
        if(this._populated == null)
        {
            var handler = this.get_events().getHandler('populate');
            if (handler) 
            {
                this._timer.set_enabled(true);
                handler(this, Sys.EventArgs.Empty);
            }
        }
    }
    
}

BlueBridge.SharePointExtensions.Menu.Publishing.BreadcrumbHoverMenuBehavior.registerClass('BlueBridge.SharePointExtensions.Menu.Publishing.BreadcrumbHoverMenuBehavior', AjaxControlToolkit.HoverMenuBehavior);
BlueBridge.SharePointExtensions.Menu.Publishing.BreadcrumbHoverMenuBehavior.__VisiblePopup = null;
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();