var globals = {
    err: new Object(),
    errIDs: new Array(),
    errSlideFx: null,
    errOpacityFx: null,
    errorAnchorPrefix: "errorAnchor_",
    errorIndicatorPrefix: "errorIndicator_",
    errorMessagePrefix: "errorMessage_"
}

String.prototype.startsWith = function(str) {
    if (str == "" || str == null)
        return true;
        
    if (str.length > this.length)
        return false;
    
    if (this.substr(0, str.length) == str)
        return true;
    
    return false;
}

function popup(url, width, height) {
    if (width == null)
        width = 500;
    if (height == null)
        height = 500;
    
    var x = (window.getWidth() / 2) - (width / 2);
    if (x < 0)
        x = 0;
    
    var y = (window.getHeight() / 2) - (height / 2);
    if (y < 0)
        y = 0;
    
    window.open(url, '', 'width=' + width + ',height=' + height + ',top=' + y + ',left=' + x + ',resizable=yes,scrollbars=yes');
    return false;
}

function removeDefault(ctrl, str) {
    if (ctrl != null) {
        if (ctrl.value == str)
            ctrl.value = "";
    }
}

function setDefault(ctrl, str) {
    if (ctrl != null) {
        if (ctrl.value == "")
            ctrl.value = str;
    }
}

function Modal() {
    this.title = null;
    this.message = null;
    this.callback = null;
    
    this.iframeShield = null;
    this.pageblock = null;
    this.dlg = null;
    this.dlgCenter = null;
    
    this.dlgWidth = 430;
    
    // Preload images referenced in the CSS
    var preload = new Image();
    //preload.src = "images/modal-frame-top.png";
    //preload.src = "images/modal-frame-bottom.png";
    
    preload.src = "_images/modal-frame-top-left.png";
    preload.src = "_images/modal-frame-bottom-left.png";
    preload.src = "_images/modal-frame-top-right.png";
    preload.src = "_images/modal-frame-bottom-right.png";
    
    this.close = function () {
        if (this.iframeShield != null)
            this.iframeShield.parentNode.removeChild(this.iframeShield);
        document.body.removeChild(this.pageblock);
        document.body.removeChild(this.dlg);
        
        if (this.callback != null)
            this.callback();
    }
    
    this.go = function () {
        this._setupDialog();
        this._setTitleAndMessage();
        
        // OK button
        elem = document.createElement("p");
        elem.className = "buttons";
        this.dlgCenter.appendChild(elem);
        var button = document.createElement("a");
        button.href = "#";
        var _this = this;
        button.onclick = function () {
            _this.close();
            return false;
        };
        button.className = "button-ok";
        button.appendChild(document.createTextNode("OK"));
        elem.appendChild(button);
        
        elem.style.height = "28px";
        elem.style.margin = "0";
        
        this._centerDialog();
    };
    
    this.interstitial = function (url) {
        this._setupDialog();
        this._setTitleAndMessage();
        
        // OK & Cancel buttons
        elem = document.createElement("p");
        elem.className = "buttons";
        this.dlgCenter.appendChild(elem);
        
        var button = document.createElement("a");
        button.href = "#";
        var _this = this;
        button.onclick = function () {
            _this.close();
            return false;
        };
        button.className = "button-cancel";
        button.appendChild(document.createTextNode("Cancel"));
        elem.appendChild(button);
        
        var button = document.createElement("a");
        button.href = url;
        var _this = this;
        button.onclick = function () {
            window.open(url);
            _this.close();
            return false;
        };
        button.className = "button-ok";
        button.appendChild(document.createTextNode("OK"));
        elem.appendChild(button);
        
        elem.style.height = "28px";
        elem.style.margin = "0";
        
        this._centerDialog();
    };
    
    this.showImage = function (url, width, height, caption) {
        // Prep dialog
        this.dlgWidth = 200;
        this.title = caption;
        
        this._setupDialog("center-image");
        
        // Create image container div - this will display a progress indicator by default
        elem = document.createElement("div");
        elem.className = "finalist-modal-image";
        this.dlgCenter.appendChild(elem);
        
        // Display dialog
        this._centerDialog();
        
        // Load image
        this.loadImg = new Image();
        var _this = this;
        this.loadImg.onload = function () {_this._showImageCallback();};
        this.loadImg.src = url;
    };
    
    this._showImageCallback = function () {
        // Hide dialog
        this.dlg.style.visibility = "hidden";
        
        // Clear progress indicator
        while (this.dlgCenter.hasChildNodes())
            this.dlgCenter.removeChild(this.dlgCenter.childNodes[0]);
        
        // Calculate dialog width - set a minimum so that the caption text will fit
        var innerWidth = this.loadImg.width;
        if (innerWidth < 360)
            innerWidth = 360;
        
        // Resize dialog and recenter
        this.dlg.style.width = (innerWidth + 40) + "px";
        this.dlgTopCenter.style.width = this.dlgBottomCenter.style.width = (innerWidth + 20) + "px";
        this.dlgCenter.style.height = this.loadImg.height + "px";
        this._centerDialog();
        
        // Add caption
        var header = document.createElement("strong");
        header.appendChild(document.createTextNode("Submitted by "));
        
        var elem = document.createElement("div");
        elem.className = "finalist-modal-caption";
        elem.appendChild(header);
        elem.appendChild(document.createTextNode(this.title));
        this.dlgCenter.appendChild(elem);
        
        // Add close button
        var button = document.createElement("a");
        button.href = "#";
        var _this = this;
        button.onclick = function () {
            _this.close();
            return false;
        };
        button.className = "button-close-small finalist-modal-button-close";
        button.appendChild(document.createTextNode("Close"));
        this.dlgCenter.appendChild(button);
        
        // Add image
        var button = document.createElement("a");
        button.href = "#";
        button.className = "noFloat";
        var _this = this;
        button.onclick = function () {
            _this.close();
            return false;
        };
        button.appendChild(this.loadImg);
        this.dlgCenter.appendChild(button);
        
        // Show dialog
        this.dlg.style.visibility = "visible";
    }
    
    this._setupDialog = function (centerClass) {
        // Nasty hack to prevent <select> elements from appearing above our modal elements
        if (window.navigator != null) {
            if (window.navigator.userAgent != null) {
                var ua = window.navigator.userAgent;
                var msie = ua.indexOf("MSIE ");
                if (msie > 0) {
                    if (parseInt(ua.substring(msie+5, ua.indexOf (".", msie ))) <= 6) {
                        // Setup iframe shield
                        this.iframeShield = document.createElement("iframe");
                        this.iframeShield.id = "iframeshield";
                        document.getElementById('subContainLeft').parentNode.insertBefore(this.iframeShield, document.getElementById('subContainLeft'));
                    }
                }
            }
        }
        
        // Setup page blocking div
        this.pageblock = document.createElement("div");
        this.pageblock.id = "pageblock";
        $(this.pageblock).setOpacity(0.5);
        document.body.appendChild(this.pageblock);
        
        // Setup modal div
        this.dlg = document.createElement("div");
        this.dlg.id = "modal";
        this.dlg.style.width = this.dlgWidth + "px";
        
        div = document.createElement("div");
        div.className = "top-left";
        this.dlg.appendChild(div);
        
        div = this.dlgTopCenter = document.createElement("div");
        div.className = "top-center";
        div.style.width = (this.dlgWidth - 20) + "px";
        this.dlg.appendChild(div);
        
        div = document.createElement("div");
        div.className = "top-right";
        this.dlg.appendChild(div);
        
        div = this.dlgCenter = document.createElement("div");
        div.className = centerClass == null ? "center" : centerClass;
        this.dlg.appendChild(div);
        
        div = document.createElement("div");
        div.className = "bottom-left";
        this.dlg.appendChild(div);
        
        div = this.dlgBottomCenter = document.createElement("div");
        div.className = "bottom-center";
        div.style.width = (this.dlgWidth - 20) + "px";
        this.dlg.appendChild(div);
        
        div = document.createElement("div");
        div.className = "bottom-right";
        this.dlg.appendChild(div);
        
        document.body.appendChild(this.dlg);
    };
    
    this._centerDialog = function () {
        var dlgSize = $(this.dlg).getSize();
        var windowSize = window.getSize();
        var windowScroll = window.getScroll();
        
        var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
		if (rslt != null && Number(rslt[1]) < 7.0) {
		    this.pageblock.style.top = windowScroll.y + "px";
            this.pageblock.style.left = windowScroll.x + "px";
		    this.dlg.style.top = (((windowSize.y - dlgSize.y) / 2) + windowScroll.y) + "px";
            this.dlg.style.left = (((windowSize.x - dlgSize.x) / 2) + windowScroll.x) + "px";
		} else {
            this.dlg.style.top = ((windowSize.y - dlgSize.y) / 2) + "px";
            this.dlg.style.left = ((windowSize.x - dlgSize.x) / 2) + "px";
        }
    };
    
    this._setTitleAndMessage = function () {
        // Title
        if (this.title != null) {
            var elem = document.createElement("h2");
            elem.appendChild(document.createTextNode(this.title));
            this.dlgCenter.appendChild(elem);
        }
        
        // Message
        if (this.message != null) {
            var arr = this.message.split('\n');
            for (var i=0; i<arr.length; i++) {
                elem = document.createElement("p");
                elem.appendChild(document.createTextNode(arr[i]));
                this.dlgCenter.appendChild(elem);
            }
        }
    }
}

window.addEvent('domready', function() {
    // Create floating div effect, if present
    var fixedDiv = $("fixedDiv");
    if (fixedDiv != null) {
        var slideEffect = new Fx.Tween(fixedDiv, {
            wait: false,
            duration: 600,
            transition: Fx.Transitions.Circ.easeOut
        });
        
        var top = fixedDiv.getPosition().y - 10;
        window.addEvent("scroll", function() {
            slideEffect.start.delay(500, slideEffect, [ 'margin-top', Math.max(0, document.documentElement.scrollTop - top)]);
        });
    }
});
