TheCarbonAccount = window.TheCarbonAccount || {};

TheCarbonAccount.coverpage = (function() {
    var flagPageIsCovered = false;
    var flagElementsHaveBeenAdded = false;
    function pageIsCovered() {
        return flagPageIsCovered;
    }
    
    $D = YAHOO.util.Dom;
    $E = YAHOO.util.Event;
    $C = YAHOO.util.Connect;
    
    var coverDiv;
    var sizeDetectorSpan;
    var currentSizeDetectorHeight = 0;
    
    function addElements() {
        /* Add the required elements to the DOM, initially hidden */
        if (flagElementsHaveBeenAdded) {
            return;
        }
        flagElementsHaveBeenAdded = true;
        
        // First the DIV that covers the entire page
        coverDiv = document.createElement('div');
        with(coverDiv.style) {
            height = $D.getDocumentHeight() + 'px';
            width = $D.getDocumentWidth() + 'px';
            position = 'absolute';
            background = 'url(/static/img/fills/black_75pc.png)';
            top = '0px';
            left = '0px';
            zIndex = 1;
            display = 'none';
        }
        //$D.setStyle(coverDiv, 'opacity', 0.6);
        document.body.appendChild(coverDiv);
        
        // Now the text resize detecting span, hidden off the side of the page
        sizeDetectorSpan = document.createElement('span');
        sizeDetectorSpan.innerHTML = 'foo';
        with(sizeDetectorSpan.style) {
            position = 'absolute';
            left = '-10000px';
            top = '-1000px';
        }
        document.body.appendChild(sizeDetectorSpan);
    }
    
    function getSizeDetectorHeight() {
        if (!sizeDetectorSpan) {
            return 0;
        }
        var r = $D.getRegion(sizeDetectorSpan);
        return r.bottom - r.top;
    }
    
    function resizeCoverDiv() {
        if (!pageIsCovered()) {
            return;
        }
        coverDiv.style.display = 'none'; // So it doesn't affect document size
        coverDiv.style.height = $D.getDocumentHeight() + 'px';
        coverDiv.style.width = $D.getDocumentWidth() + 'px';
        coverDiv.style.display = 'block';
    }
    
    var sizeInterval = false;
    function setSizeInterval() {
        if (sizeInterval) {
            return;
        }
        sizeInterval = setInterval(function() {
            var newHeight = getSizeDetectorHeight();
            if (newHeight != currentSizeDetectorHeight) {
                currentSizeDetectorHeight = newHeight;
                resizeCoverDiv();
            }
        }, 50);
    }
    function clearSizeInterval() {
        if (!sizeInterval) {
            return;
        }
        clearInterval(sizeInterval);
        sizeInterval = false;
    }
    
    function uncoverPage() {
        var containers = document.getElementsBySelector('div.flashContainer');
        for (var div, i = 0; div = containers[i]; i++) {
            div.style.display = div.coverpageOldDisplay;
        }
        if (TheCarbonAccount.buggy_flash) {
            $("embed.sIFR-flash").each(function () {
				this.style.display = this.coverpageOldDisplay;
            });
        }
        clearSizeInterval();
        coverDiv.style.display = 'none';
        flagPageIsCovered = false;
    }
    
    function coverPage() {
        /* Hide ALL flash on the page */
        var containers = document.getElementsBySelector('div.flashContainer');
        for (var div, i = 0; div = containers[i]; i++) {
            div.coverpageOldDisplay = div.style.display;
            div.style.display = 'none';
        }
        if (TheCarbonAccount.buggy_flash) {
            $("embed.sIFR-flash").each(function () {
				this.coverpageOldDisplay = this.style.display;
				this.style.display = 'none';
            });
        }
        addElements();
        setSizeInterval();
        coverDiv.style.display = 'block';
        flagPageIsCovered = true;
    }
    
    $E.on(window, 'resize', resizeCoverDiv);
    
    function showLoading() {
        var img = document.getElementById('coverpageAjaxload');
        if (!img) {
            img = document.createElement('img');
            img.src = '/static/img/ajaxload.gif';
            img.id = 'coverpageAjaxload';
            with(img.style) {
                position = 'absolute';
                left = ($D.getDocumentWidth() / 2 - (32 / 2)) + 'px';
                top = ($D.getDocumentHeight() / 2 - (32 / 2)) + 'px';
                zIndex = 1;
                display = 'block';
            }
            document.body.appendChild(img);
        }
    }
    function hideLoading() {
        var img = document.getElementById('coverpageAjaxload');
        if (img) {
            img.style.display = 'none';
        }
    }
    
    function expandCoverDiv() {
        // Call this if the cover div MAY need to expand to deal with the 
        // page body getting larger. This only calls resizeCoverDiv (which 
        // requires a visible 'flash') if the page height is now greater than
        // the height of the cover div.
        var r = $D.getRegion(coverDiv)
        if (pageIsCovered() && ($D.getDocumentHeight() > (r.bottom - r.top))) {
            resizeCoverDiv();
        }
    }
    
    return {
        'coverPage': coverPage,
        'uncoverPage': uncoverPage,
        'pageIsCovered': pageIsCovered,
        'showLoading': showLoading,
        'hideLoading': hideLoading,
        'expandCoverDiv': expandCoverDiv
    }
})();


$(function () {
	TheCarbonAccount.buggy_flash = true;
});