// -------- trida pro zobrazovani zprav ---------
function messageBox(iTrgObj) {
    var self = this;

	this.trgObj = iTrgObj;
	this.trgObj.style.zIndex = 999999;
	this.counter = 0;
	this.hideStart = false;
	this.redirAfter = ""; // presmerovani po zmizeni zpravy

    this.mainMessageArea = document.createElement("DIV"); // hlavní oblast zprávy
    this.middleArea = document.createElement("DIV"); // oblast pro jiny bordel
	this.messageArea = document.createElement("DIV"); // oblast zpravy
	this.mainMessageArea.style.paddingTop = "10px";
	this.uploadStruct = new Array(); // upload bary (oblast, instance)

	this.trgObj.appendChild( this.messageArea );
    this.trgObj.appendChild( this.middleArea );
    this.trgObj.appendChild( this.mainMessageArea );

	this.blockDiv = null;

	this.loaderDiv = null;

    this.show = function(iMessage) {
        this.showOpacity(iMessage, 0);
    }

	this.showOpacity = function(iMessage, iOpacity) {
        if (this.trgObj.style.display != "block" || this.hideStart) {
            // div
            if (iOpacity > 0) {
                document.body.parentNode.style.overflow = "hidden";

        		this.blockDiv = document.createElement("DIV");
                this.blockDiv.style.position = "absolute";
                this.blockDiv.style.zIndex = parseInt(this.trgObj.style.zIndex) - 1;

                this.blockDiv.style.left = "0px";
                this.blockDiv.style.top = "0px";
                this.blockDiv.style.width = screen.width + "px";
                this.blockDiv.style.height = screen.height + "px";

                this.blockDiv.innerHTML = "&nbsp;";

                this.blockDiv.style.backgroundColor = "black";
                this.blockDiv.style.opacity = iOpacity / 100;
                this.blockDiv.style.filter = "alpha(opacity=" + iOpacity + ")";

                document.body.appendChild(this.blockDiv);
            }

            // zprava
    		this.hideStart = false;
    		this.trgObj.style.display = "block";
    		this.trgObj.style.filter = "";
    		this.trgObj.style.opacity = "1.0";

    		var left = Math.round(document.documentElement.clientWidth / 2) - 150;
    		var top  = Math.round(document.documentElement.clientHeight / 2) - 50 + scY();

    		this.trgObj.style.left = left + "px";
    		this.trgObj.style.top = top + "px";
		}

		this.message_set(iMessage);
	}

	this.pushLoader = function(iX, iY) {
        if (this.loaderDiv == null) {
            this.loaderDiv = document.createElement("DIV");
            this.loaderDiv.innerHTML = "<img src=\"/images/loader.gif\" alt=\"\" border=\"0\"/>";
            this.loaderDiv.style.border = "1px solid black";
            this.loaderDiv.style.display = "none";
            this.loaderDiv.style.position = "absolute";
            this.loaderDiv.style.zIndex = "999";

            document.body.appendChild(this.loaderDiv);
        }

        this.loaderDiv.style.left = iX + "px";
        this.loaderDiv.style.top = iY + "px";
        this.loaderDiv.style.display = "block";
    }

    this.killLoader = function() {
        this.loaderDiv.style.display = "none";
    }

	// --- upload bar funkce ---
	this.upload_setUploadBar = function(iUnits) {
		var uBar = new uploadBar();
		var toAdd = new Array();
		var uploadArea = document.createElement("DIV");

		uploadArea.style.paddingTop = "10px";
		this.middleArea.appendChild( uploadArea );

		toAdd[toAdd.length] = uBar;
		toAdd[toAdd.length] = uploadArea;

		uBar.initialize(uploadArea, iUnits);

		this.uploadStruct[ this.uploadStruct.length ] = toAdd;
	}

	this.upload_dropBar = function() {
		// odstrani posledni upload bar
		if (this.uploadStruct.length > 0) {
			var tIndex = this.uploadStruct.length - 1;

			this.uploadStruct[tIndex][0].destroy();
			this.middleArea.removeChild(this.uploadStruct[tIndex][1]);

			delete this.uploadStruct[tIndex][0];
			delete this.uploadStruct[tIndex][1];

			this.uploadStruct.pop();
		}
	}

	this.upload_setProgress = function(iNumUnits, iUploaded) {
		this.uploadStruct[ this.uploadStruct.length - 1 ][0].setProgress(iNumUnits, iUploaded);
	}

	this.upload_setUploaded = function(iUploaded) {
		this.uploadStruct[ this.uploadStruct.length - 1 ][0].setUploaded(iUploaded);
	}

	this.upload_finish = function() {
		this.uploadStruct[ this.uploadStruct.length - 1 ][0].finish();
	}

	this.upload_doProcess = function(iMidTime, iStepTime) {
		for (var f = 0; f < this.uploadStruct.length; f++)
			this.uploadStruct[f][0].doProcess(iMidTime, iStepTime);
		return true;
	}
	// -------------------------

	this.message_append = function(iMessage) {
        if (iMessage.substr(0, 1) == "!") this.mainMessageArea.innerHTML += iMessage.substr(1, iMessage.length - 1);
        else this.messageArea.innerHTML += iMessage;
    }
	this.message_set = function(iMessage) {
        this.login_div = null;
        if (iMessage.substr(0, 1) == "!") this.mainMessageArea.innerHTML = iMessage.substr(1, iMessage.length - 1);
        else this.messageArea.innerHTML = iMessage;
    }
	this.setButton = function(iValue) {
		this.messageArea.innerHTML += "<div style=\"margin-top : 15px;text-align : center\"><input type=\"button\" class=\"inputBlue\" onClick=\"msg.hide()\" value=\"" + iValue + "\"/></div>";
	}
	
	this.login_div = null;
	this.login_nameBox = null;
	this.login_passBox = null;
	this.login_butt = null;
	this.login_closeButt = null;
	this.login_location = null;
	this.login_regLink = null;

    this.loginSuccess = function() {
        this.show("přihlášení proběhlo v pořádku");
        document.location.href = this.login_location;
    }

    this.loginFail = function() {
        this.show("<span class='errorText'>chybné přihlašovací údaje</span>");
        this.setLoginForm(this.login_location, this.login_regLink);
    }

    this.loginProcess = function() {
        if (exec == null) alert("Chyba!");
        else exec.make("login_refresh(\"" + self.login_nameBox.value + "\",\"" + self.login_passBox.value + "\")");
    }

	this.setLoginForm = function(iLoc, iRegLink) {
        if (this.login_div == null) {
            this.login_location = iLoc == null ? document.location.href : iLoc;
            this.login_regLink = iRegLink == null ? "http://www.ecards.cz/registration.php" : iRegLink;

            this.login_div = document.createElement("DIV");
            this.login_div.style.paddingTop = "10px";
            this.login_div.style.width = "170px";
            this.login_div.style.marginLeft = "auto";
            this.login_div.style.marginRight = "auto";
    
            var topLeft  = document.createElement("DIV");
            var topRight = document.createElement("DIV");
            var botLeft  = document.createElement("DIV");
            var botRight = document.createElement("DIV");

            topLeft.style.styleFloat  = "left";
            topLeft.style.cssFloat    = "left";
            topLeft.style.fontSize    = "11px";
            topLeft.innerHTML = "e-mail:&nbsp;";
            topRight.style.styleFloat = "right";
            topRight.style.cssFloat   = "right";

            this.login_nameBox = document.createElement("INPUT");
            this.login_nameBox.type = "text";
            this.login_nameBox.className = "textBox";
            this.login_nameBox.style.fontSize = "11px";
            this.login_nameBox.style.marginBottom = "2px";
            this.login_nameBox.onkeydown = function(trgEvent) {
                var kCode = trgEvent == null ? event.keyCode : trgEvent.keyCode;
                if (kCode == 13) { self.loginProcess(); return false; } else return true;
            }
            topRight.appendChild(this.login_nameBox);

            botLeft.style.styleFloat  = "left";
            botLeft.style.cssFloat    = "left";
            botLeft.style.fontSize    = "11px";
            botLeft.innerHTML         = "heslo:&nbsp;";
            botRight.style.styleFloat = "right";
            botRight.style.cssFloat   = "right";

            this.login_passBox = document.createElement("INPUT");
            this.login_passBox.type = "password";
            this.login_passBox.className = "textBox";
            this.login_passBox.style.fontSize = "11px";
            this.login_passBox.onkeydown = function(trgEvent) {
                var kCode = trgEvent == null ? event.keyCode : trgEvent.keyCode;
                if (kCode == 13) { self.loginProcess(); return false; } else return true;
            }
            botRight.appendChild(this.login_passBox);

            this.login_butt = document.createElement("INPUT");
            this.login_butt.value = "přihlásit se";
            this.login_butt.type = "button";
            this.login_butt.style.width = "100px";
            this.login_butt.style.marginTop = "8px";
            this.login_butt.className = "inputBlue";
            this.login_butt.onclick = function() { self.loginProcess(); }

            this.login_closeButt = document.createElement("INPUT");
            this.login_closeButt.value = "zavřít";
            this.login_closeButt.type = "button";
            this.login_closeButt.style.marginTop = "8px";
            this.login_closeButt.style.marginRight = "5px";
            this.login_closeButt.style.width = "60px";
            this.login_closeButt.className = "inputGray";
            this.login_closeButt.onclick = function() { self.hide(); }

            var divA = document.createElement("DIV");
            divA.appendChild(topRight);
            divA.appendChild(topLeft);
            var divB = document.createElement("DIV");
            divB.appendChild(botRight);
            divB.appendChild(botLeft);
            var divC = document.createElement("DIV");
            divC.appendChild(this.login_closeButt);
            divC.appendChild(this.login_butt);

            this.login_div.appendChild(divA);
            this.login_div.appendChild(divB);
            this.login_div.appendChild(divC);

            var hrfDiv = document.createElement("DIV");
            hrfDiv.style.paddingTop = "15px";
            hrfDiv.innerHTML = "<a href='" + iRegLink + "' style='font-size : 11px'>Založit nový účet <strong>&gt;&gt</strong></a>";
            this.login_div.appendChild(hrfDiv);

            this.messageArea.appendChild(this.login_div);
        }
    }

	this.hide = function() {
		if (!this.hideStart) {
            // start mizeni
            if (this.trgObj.style.display == "block") {
                if (this.blockDiv != null) {
                    document.body.removeChild(this.blockDiv);
                    this.blockDiv = null;

                    document.body.parentNode.style.overflow = "auto";
                }

    			this.counter = 250;
    			this.hideStart = true;
    			setTimeout("msg.hideProcess()", 5);

    			if (this.redirAfter != "") {
        			document.location.href = this.redirAfter;
        			this.redirAfter = "";
    			}
			}
		}
	}

	this.hideProcess = function() {
		if (this.hideStart) {
			this.counter += -8;
			if (this.counter > 0) {
				if (this.counter < 100) {
                    this.trgObj.style.filter = "Alpha(opacity : " + this.counter + ")";
                    this.trgObj.style.opacity = this.counter / 100;
                }
				setTimeout("msg.hideProcess()", 5);
			} else {
				// finalni zmizeni
				this.trgObj.style.display = "none";

				// destrukce progressbaru
				if (this.uploadStruct.length > 0) {
					for (var f = 0; f < this.uploadStruct.length; f++) {
						this.uploadStruct[f][0].destroy();

						delete this.uploadStruct[f][0];
						delete this.uploadStruct[f][1];
					}
					this.uploadStruct = new Array();
				}
			}
		} // if
	}
}
