﻿// Lavender website - modal popup
function LavModal(config) {
    this.blocker = $("<div id='preload_blocker'></div>");
    this.graphic = $(config.popupElem) || $("");
    this.appendto = config.parentElem || $("body");
    this.pwidth = config.popupWidth || 0;
    this.pheight = config.popupHeight || 0;
    this.bgcolor = config.blockerBgColor || "#000000";
    this.opacity = config.blockerOpacity || 0.5;
    this.initialize();
    this.shown = false;
}
var lavmodal = LavModal.prototype;

lavmodal.initialize = function () {
    $(window).scroll($lavdelegate(this, this.onWindowChanged));
    $(window).resize($lavdelegate(this, this.onWindowChanged));
}
lavmodal.onWindowChanged = function () {
    if (this.shown) {
        var myTop = $(window).scrollTop() + ($(window).height() / 2) - (this.pheight / 2);
        var myLeft = ($(document).width() / 2) - (this.pwidth / 2);
        this.graphic.css({ top: myTop, left: myLeft });
        this.blocker.css({ width: $(document).width(), height: $(document).height() });
    }
}
lavmodal.show = function () {
    this.shown = true;
    var myTop = $(window).scrollTop() + ($(window).height() / 2) - (this.pheight / 2);
    var myLeft = ($(document).width() / 2) - (this.pwidth / 2);
    this.graphic.css({ opacity: 0, position: "absolute", top: myTop, left: myLeft });
    this.blocker.css({ width: $(document).width(), height: $(document).height(), background: this.bgcolor, position: "absolute", top: 0, left: 0, opacity: this.opacity }); 
    this.blocker.css("z-index", 99999999); 
    this.graphic.css("z-index", 999999999);
    this.appendto.append(this.blocker);
    this.appendto.append(this.graphic);
    this.graphic.stop().delay(100).animate({ opacity: 1, top: myTop }, { duration: 300, ease: "easeInQuint" }); 
}
lavmodal.hide = function () {
    setTimeout($lavdelegate(this, function () {
        this.shown = false;
        try {
            this.graphic.stop();
            $("#" + this.blocker.attr("id")).remove();
            $("#" + this.graphic.attr("id")).remove();
            this.graphic.remove();
            this.blocker.remove();
            this.blocker = null;
        } catch (err) { }
    }), 1);
}
