var Popup = {

    tpl: [
        '<div id="popup-wrap">',
            '<div id="popup-cnt">',
                '<div id="popup-close"></div>',
                '<div id="popup-txt"></div>',
            '</div>',
        '</div>'
    ].join(''),

    insertTop: function(elem){
        var first = function(){
            var el = document.body.firstChild;
            while(el.nodeType != 1){el = el.nextSibling};
            return el;
        }, before = first();

        document.body.insertBefore(elem, before);
    },

    show: function(id){
		window.scroll(0,0);
		
        if (this.rendered) {
            this.display(true);
        } else {
            this.overlayEl = this.overlayEl || (this.overlayEl = this.overlay());
            this.el = this.el || (this.el = this.element());

            this.insertTop(this.overlayEl);
            this.insertTop(this.el);

            // Container for HTML text '#popup-txt'
            this.cnt = this.el.firstChild.firstChild.nextSibling;
            // Close button
            this.closeBtn = this.el.firstChild.firstChild;
            this.closeBtn.onclick = function(){
                Popup.hide();
            }

            this.rendered = true;
        }

        this.loadContent(id);
    },

    hide: function(){
        this.display(false);
        if(this.cnt) this.cnt.innerHTML = '';
    },

    display: function(b){
        if (this.overlayEl) this.overlayEl.style.display = !b ? 'none' : 'block';
        if (this.el) this.el.style.display = !b ? 'none' : 'block';
    },

    element: function(){
        var tmp = document.createElement('div');
        tmp.innerHTML = this.tpl;
        return tmp.firstChild;
    },

    overlay: function(){
        var el = document.createElement('div');
        el.id = "modal-overlay";        
        el.style.height = this.docHeight() + 'px';        
        return el;
    },
	
	updateOverlaySize: function(){
		var ol = document.getElementById('modal-overlay');
		ol.style.height = this.docHeight() + 'px'; 
	},

    docHeight: function() {
        var D = document;
        return Math.max(
            Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
            Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
            Math.max(D.body.clientHeight, D.documentElement.clientHeight)
        );
    },

    loadContent: function(id){
        var cnt = this.cnt,success = function(txt){
            if(cnt) cnt.innerHTML = txt;
			setTimeout("Popup.updateOverlaySize()", 100);
        };
        
        Ajax.request({
            method: 'GET',
            url: '/firm/?firmId='+id,
            onSuccess: success
        });
    }
    
};

var Ajax = {
    request: function(options){
        options = {
            method: options.type || 'POST',
            url: options.url || '',
            timeout: options.timeout || 5000,
            onComplete: options.onComplete || function(){},
            onError: options.onError || function(){},
            onSuccess: options.onSuccess || function(){}
        };

        // If IE6 is used
        if (typeof XMLHttpRequest == 'undefined') {
            XMLHttpRequest = function() {
                return new ActiveXObject('Msxml2.XMLHTTP');
            };
        }

        var xml = new XMLHttpRequest(),
            requestDone = false;

        // Fixes problem with scandinavian signs
        if (xml.overrideMimeType) xml.overrideMimeType("text/html; charset=ISO-8859-1");
        xml.open(options.method, options.url, true);

        // Cancel the request after timeout
        setTimeout(function(){
            requestDone = true;
        }, options.timeout);

        xml.onreadystatechange = function () {
            if (xml.readyState == 4 && !requestDone) {
                // Execute the success (or error) callback
                httpSuccess(xml)
                    ? options.onSuccess(httpData(xml))
                    : options.onError();

                // Execute the completion callback
                options.onComplete();
                xml = null;
            }
        };

        xml.send();

        function httpSuccess(r){
            try {
                return (r.status >= 200 && r.status < 300) || r.status == 304;
            } catch(e){}

            return false;
        }

        function httpData(r) {
            return r.responseText;
        }
    }
};
