﻿// constructor
function Overlay(Id, ClientId, CloseId, ShowClose, ShowMask, Width, Height, MaxWidth, MaxHeight, ContentId, AutoShow, ClientHandlerId, SetFocusId) {
    this.Id = Id;
    this.ClientId = ClientId;
    this.CloseId = CloseId;
    this.ShowClose = ShowClose;
    this.ShowMask = ShowMask;
    this.IsRepositioned = false;
    this.Width = Width;
    this.Height = Height;
    this.MaxWidth = MaxWidth;
    this.MaxHeight = MaxHeight;
    this.AutoShow = AutoShow;
    this.ContentId = ContentId;
    this.ClientHandlerId = ClientHandlerId;
    this.SetFocusId = SetFocusId;
    this.OverlayWindow = $(ClientId);
    this.CloseContainer = $(CloseId);
    this.ContentContainer = $(ContentId);
    this.SetFocusAnchor = $(SetFocusId);
    this.Mask = null;
    this.Hide = new Event();
    this.Show = new Event();
    this.Close = new Event();
    this.ButtonClick = new Event();
    addEvent(window, "resize", function() { Overlay.Reposition(Id); });
    addEvent(window, "scroll", function() { Overlay.Reposition(Id); });
    if (AutoShow)
        this.OnShow();
    return this;
}

Overlay.prototype.Init = function() {
    try {
        var clientVar = eval(GetOverallBase(this).ClientHandlerId);
        clientVar.AddHandlers(this);
    } catch (e) { }
}
Overlay.prototype.GetOverlayWindow = function() {
    if (this.OverlayWindow == null || this.OverlayWindow == undefined)
        return $(this.ClientId);
    else
        return this.OverlayWindow;
}
Overlay.prototype.OnShow = function() {
    document.documentElement.style.overflow = "hidden";
    var ctl = GetOverallBase(this);
    var overlay = ctl.GetOverlayWindow();
    if (ctl.ShowMask) {
        if (ctl.Mask == null) {
            var m = new Mask();
            m.Id = "overlayMask" + ctl.Id;
            ctl.Mask = m;
            m.Show();
        } else ctl.Mask.Show();
    }
    if (this.ShowClose)
        $(ctl.CloseId).style.display = "block";
    ctl.Reposition();

    overlay.style.display = "block";
    ctl.SetFocusAnchor.focus();

    ctl.Show.Execute(ctl);
}
Overlay.prototype.OnHide = function(RaiseCloseEvent) {
    this.GetOverlayWindow().style.display = "none";
    if (this.ShowMask && this.Mask != null)
        this.Mask.Hide();
    if (RaiseCloseEvent)
        this.Close.Execute(this);
    document.documentElement.style.overflow = "auto";
    this.Hide.Execute(this);
}
Overlay.prototype.Reposition = function() {
    var ctl = this;
    while (ctl.base != null)
        ctl = ctl.base;
    var el = ctl.GetOverlayWindow();
    if (el == null) return;
    var content = ctl.ContentContainer;
    if (!ctl.IsRepositioned) {
        el.parentNode.removeChild(el);
        document.getElementsByTagName("body")[0].appendChild(el);
        ctl.IsRepositioned = true;
    }
    if (el) {
        var vp = getViewport();
        var vpPadding = 50;
        var yDif, xDif;

        // Check Initial Dimensions
        dm = getElementDimensions(el, content);
        yDif = dm.height - dm.contentHeight;
        xDif = dm.width - dm.contentWidth;

        // set width
        if (dm.width < ctl.Width)
            el.style.width = ctl.Width + "px";
        else if (dm.width > ctl.MaxWidth)
            el.style.width = ctl.MaxWidth + "px";
        else
            el.style.width = dm.width + "px";

        // query dimensions again
        dm = getElementDimensions(el, content);

        // set height
        if (dm.height < ctl.Height)
            el.style.height = ctl.Height + "px";
        else if (dm.height > ctl.MaxHeight)
            el.style.height = ctl.MaxHeight + "px";
        else
            el.style.height = dm.height + "px";

        dm = getElementDimensions(el, content);
        content.style.overflow = "auto";
        if ((dm.height - yDif) > 0)
            content.style.height = (dm.height - yDif) + "px";
        if ((dm.width - xDif) > 0)
            content.style.width = (dm.width - xDif) + "px";

        //TODO: adjust window handling
        /*
        // query new dimensions
            
        dm = getElementDimensions(el, content);
        if (dm.height >= (vp.height - vpPadding)) {
        var nonContentHeight = dm.height - dm.contentHeight;
        var nonContentWidth = dm.width - dm.contentWidth;
        content.style.overflow = "auto";
        content.style.height = (vp.height - vpPadding - nonContentHeight) + "px";
        dm = getElementDimensions(el, content);
        }
        else {
        content.style.height = "";
        dm = getElementDimensions(el, content);
        }*/
        sp = getScrollPosition();

        el.style.top = (sp.y + ((vp.height - dm.height) / 2)) + 'px';
        el.style.left = (sp.x + ((vp.width - dm.width) / 2)) + 'px';
    }
}
Overlay.prototype.Escape = function(event) {
    var ctl = GetOverallBase(this);
    var key = event.which == undefined ? event.keyCode : event.which;
    if (ctl.GetOverlayWindow().style.display != "none") {
        if (key == 27) {
            event.returnValue = false;
            ctl.OnHide();
        }
        if (key == 13) event.returnValue = false;
    }
}
Overlay.ShowOverlay = function(Id) {
    var ctl = GetOverallBase(eval(Id));
    if (ctl != null) ctl.OnShow();
}
Overlay.HideOverlay = function(Id) {
    var ctl = GetOverallBase(eval(Id));
    if (ctl != null) ctl.OnHide();    
}
Overlay.Reposition = function(Id) {
    var ctl = GetOverallBase(eval(Id));
    if (ctl != null) ctl.Reposition();
}
Overlay.prototype.RemoveFromDOM = function() {
    var element = $(this.ClientId)
    if (element != null || element != undefined)
        element.parentNode.removeChild(element);
}
