﻿function SpinnerControl(spinnerElementId, contentElementId, loadingText, loadingTextId) {
    this.SpinnerElementID = spinnerElementId;
    this.ContentElementID = contentElementId;
    this.LoadingText = loadingText;
    this.LoadingTextID = loadingTextId;
    
    this.IsSpinning = false;
    
    this.ShowSpinner = function() {
        $get(this.ContentElementID).style.display = "none";
        $get(this.SpinnerElementID).style.display = "block";
        $get(this.LoadingTextID).innerHTML = this.LoadingText;
        this.IsSpinning = true;
    }
    this.HideSpinner = function() {
        $get(this.ContentElementID).style.display = "block";
        $get(this.SpinnerElementID).style.display = "none";
        $get(this.LoadingTextID).innerHTML = "";
        this.IsSpinning = false;
    }
}

function SubMenu(elementId, spinner) {
    this.ElementID = elementId;
    this.SpinnerControl = spinner;    
    this.IsOpened = false;
    this.PostID = null;
    
    // events
    this.OnPreOpen   = null;
    this.OnPostOpen  = null;
    this.OnPreClose  = null;
    this.OnPostClose = null;
    // end events
    
    this.Open = function(postId) {    
        if (this.OnPreOpen != null)
                this.OnPreOpen();
        
        var element = $get(this.ElementID);
        if (element != null) {
            element.style.display = "block";
        }
        
        this.PostID = postId;
        this.IsOpened = true;
        
        if (this.OnPostOpen != null)
                this.OnPostOpen();
    }
    this.Close = function() {        
        if (this.OnPreClose != null)
            this.OnPreClose();
    
        var element = $get(this.ElementID);
        if (element != null) {
            element.style.display = "none";
        }
        
        this.PostID = null;
        this.IsOpened = false;
        
        if (this.OnPostClose != null)
            this.OnPostClose();    
    }
}

function DropDownMenu(elementId, referencer, allowCloseWhenMouseOver, closeOnMouseOut, posX, posY) {

    // globals
    this.IsMouseOver                = false;    
    this.CanOpen                    = true;
    this.CanClose                   = true;
    this.JustOpened                 = false;
    // end globals
    
    // Init
    this.ElementID                  = elementId;
    this.AllowCloseWhenMouseOver    = allowCloseWhenMouseOver;
    this.CloseOnMouseOut            = closeOnMouseOut;
    this.Referencer                 = referencer;
    
    $get(this.ElementID).style.top  = posY + "px";
    $get(this.ElementID).style.left = posX + "px";
    
        
    $get(this.ElementID).onmouseover = function() {
        eval(referencer + ".IsMouseOver = true;");
    }
    $get(this.ElementID).onmouseout = function() {
        eval(referencer + ".IsMouseOver = false;");
        if (this.CloseOnMouseOut) 
            eval(referencer + ".Close();");
    }
    // end Init
    
    // events
    this.OnPreOpen   = null;
    this.OnPostOpen  = null;
    this.OnPreClose  = null;
    this.OnPostClose = null;
    // end events
    
    // functions
    this.SetPosition = function(posX, posY) {        
        $get(this.ElementID).style.left = posX + "px";
        $get(this.ElementID).style.top  = posY + "px";
    }
    this.Open = function() {
        this.OpenAt(-1, -1);
    }
    this.OpenAt = function(posX, posY) {
        if (this.CanOpen) { 
              
            if (this.OnPreOpen != null)
                this.OnPreOpen();
              
            $get(this.ElementID).style.display = "block";
            
            if (posX >= 0 && posY >= 0)         
                this.SetPosition(posX, posY);
            
            this.IsOpened   = true;
            this.JustOpened = true;
            
            if (this.OnPostOpen != null)
                this.OnPostOpen();
        }
    }    
    this.Close = function() {
        this.Close(false);   
    }
    this.Close = function(forceClose) {
        if (this.JustOpened) {
            this.JustOpened = false;
            return;
        }
        
        if (!forceClose) {
            if (!this.CanClose)
                return;           
            if (!this.AllowCloseWhenMouseOver && this.IsMouseOver)
                return;
        }
                
        if (this.OnPreClose != null)
            this.OnPreClose();
            
        $get(this.ElementID).style.display = "none";
        this.IsOpened = false; 
        
        if (this.OnPostClose != null)
            this.OnPostClose();            
    }
    // end functions
}