﻿var shadow = {

    /*
    * Public Methods
    */
    show: function(element, hideOnClick, disposeOnHide) {

        shadow.element = document.getElementById(element) || element;
        shadow.hoc = hideOnClick || false;
        shadow.dispose = disposeOnHide || false;

        if (!shadow.background) {
            
            shadow.background = document.createElement('IFRAME');
            shadow.background.src = "nothing.txt";
            shadow.background.style.backgroundColor = 'gray';
            shadow.background.style.position = 'absolute';
            shadow.background.style.opacity = 0.5;
            shadow.background.style.filter = 'alpha(opacity=50);-moz-opacity=.1';
            

            document.body.appendChild(shadow.background);



            document.body.appendChild(shadow.background);
        }
        if (shadow.hoc) {

            $(shadow.background).click(function() { shadow.hide(); });
        }

        shadow.positionBackground();
        shadow.background.style.zIndex = 999;
        
        if (shadow.element) {
            if (!shadow.element.parentNode || !shadow.element.parentNode.tagName) {
                document.body.appendChild(shadow.element);
            }

            shadow.element.style.zIndex = 1000;
            shadow.element.style.position = 'absolute';
            shadow.positionElement(shadow.element);

             $(shadow.background).fadeIn("slow");
//             $(shadow.element).fadeIn("slow");
              shadow.element.style.display = "block";
             $(window).scroll(function(){shadow.positionBackground();});

            
        }

    },

    hide: function() {


        $(shadow.background).fadeOut("slow");
        $(shadow.element).fadeOut("slow");
        if (shadow.background) {
            shadow.background.onclick = function() { };
            document.body.removeChild(shadow.background);
            shadow.background = null;

        }
        if (shadow.element) {
            shadow.element = null;
        }
        
        $(window).scroll(function(){});


    },

    /*
    * Private Stuff
    */
    element: null,
    background: null,
    interval: null,
    hoc: false,
    dispose: false,

    positionBackground: function(obj) {
        if(shadow.background!=null){
            shadow.background.style.left = document.body.scrollLeft+'px';
            shadow.background.style.top = document.body.scrollTop+'px';
            //shadow.background.style.height = document.body.clientHeight + 1000;
            shadow.background.style.height = document.body.clientHeight+'px';
            shadow.background.style.width = document.body.clientWidth-4+'px';
        }
    },

    positionElement: function(obj) {

        function constraints() {
            var clientWidth = 964; // $(window).width(); 
            var clientHeight = $(window).height();
            var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
            var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
            return { top: scrollTop, left: scrollLeft, width: clientWidth, height: clientHeight };
        }
        var winBox = constraints();
        var objBox = { width: $(obj).width(), height: $(obj).height() };
        var top = winBox.top + (winBox.height / 3) - (objBox.height / 3);
        var left = winBox.left + (winBox.width / 2) - (objBox.width / 2);
        top = top > winBox.top ? top : winBox.top;
        left = left > winBox.left ? left : winBox.left;
        obj.style.top = top;
        obj.style.left = left;
    }
};

