﻿function TinyPopup(options)
{
    var _element = null;
    var _popup = null;
    var _container = $("FORM:first"); // Element within which borders the popup should be located
    var _self = this;
    var _getContentUrl = null;
    var _onBeforeOpen = null;
    var _onBeforeClose = null;
    var _onAfterLoad = null;
    var __tinyPopupTimer = null;
    var _locateToMousePos = false;
    var _mousePos = function() {
        this.X = 0;
        this.Y = 0;
    }

    this.Open = function (calledFromTimer) {
        if (_element && _popup && calledFromTimer == true && $.isFunction(_getContentUrl)) {
            if (_popup.attr("initialized") != "true") {
                if (_popup.attr("initializing") != "true") {
                    _popup.attr("initializing", "true");
                    _popup.load(_getContentUrl(),
                                function () {
                                    _popup.attr("initialized", "true");
                                    _popup.attr("initializing", "false");
                                    _locate();
                                    if (_onAfterLoad != null) { _onAfterLoad(); }
                                });
                }
                _popup.fadeIn();
            } else {
                _popup.fadeIn();
                _locate();
            }
        }
        else {
            try {
                clearTimeout(__tinyPopupTimer);
            }
            catch (e) { /* ignore all errors */ }
            __tinyPopupTimer = setTimeout(function () { _self.Open(true); }, 500);
        }
    }

    this.Close = function () {
        try {
            clearTimeout(__tinyPopupTimer);
        }
        catch (e) { /* ignore all errors */ }
        _popup.hide();
    }

    this.SetMousePosition = function (x, y) {
        _mousePos.X = x;
        _mousePos.Y = y;
    }

    /* Locate the popup inside container */
    function _locate(relocate) {
        if (_container && _popup) {
            var containerWidth = _container.outerWidth(true);
            var containerLeft = _container.offset().left;
            var containerRight = containerLeft + containerWidth;
            var popupWidth = _popup.outerWidth(true);
            var popupLeft = (_locateToMousePos) ? _mousePos.X : _popup.offset().left;
            var popupRight =  popupLeft + popupWidth;

            if (containerWidth >= popupWidth) {
                if (popupRight > containerRight) {
                    SetPopupLeft(popupLeft - (popupRight - containerRight));
                } else if (popupLeft < containerLeft) {
                    SetPopupLeft(containerLeft);
                } else if (_locateToMousePos) {
                    SetPopupLeft(popupLeft);
                } else {
                    _popup.css("position", "absolute").css("left", "auto");
                    if (!relocate) _locate(true);
                }
            } else {
                //SetPopupLeft(containerLeft - (popupWidth - containerWidth) / 2);
                SetPopupLeft(containerLeft);
            }
        }
    }

    function SetPopupLeft(value) {
        if (_popup.offsetParent().offset().left == 0) {
            _popup.css({ left: value });
        } else {
            _popup.offset({ left: value });
        }
    }

    function _init() {
        if (options) {
            _element = options.Element ? options.Element : null;
            _popup = options.Popup ? options.Popup : null;
            _container = options.Container ? options.Container : _container;
            if ($.isFunction(options.GetContentUrl)) {
                _getContentUrl = options.GetContentUrl;
            }
            if ($.isFunction(options.OnBeforeOpen)) {
                _onBeforeOpen = options.OnBeforeOpen;
            }
            if ($.isFunction(options.OnBeforeClose)) {
                _onBeforeClose = options.OnBeforeClose;
            }
            if ($.isFunction(options.OnAfterLoad)) {
                _onAfterLoad = options.OnAfterLoad;
            }
            if (options.LocateToMousePosition) {
                _locateToMousePos = true;
            }
            if (_element) {
                $(_element).TinyPopup = _self;

                if (!options.OuterControl)
                {
                    _element.bind("mouseover", function (e)
                    {
                        _self.SetMousePosition(e.pageX, e.pageY);
                        _self.Open();
                    });
                    _element.bind("mouseout", function () { _self.Close(); });
                }
            }
        }
    }
    _init();
}
