// ------------- trida pro spravu poznamek -----------
function noteElement(iParentHandler, iPosX, iPosY) {
    var self = this;
	this.parentHandler = iParentHandler;
	this.creatorIdent = this.parentHandler.creatorIdent;
	this.creatorName = this.parentHandler.creatorName;
	this.ident = 0;

	// ---------------------------------------------------
	// -------------- K O N S T R U K C E ----------------
	// ---------------------------------------------------
	this.rectangle = document.createElement("DIV"); // div vlastniho objektu
	this.rectangle.style.fontSize = "1px";
	this.rectangle.style.border = "1px dashed white";
	this.rectangle.style.position = "absolute";
	this.rectangle.style.display = "none";
	this.rectangle.id = "noteElement";

    this.blackRect = document.createElement("DIV");
    this.blackRect.style.fontSize = "1px";
    this.blackRect.style.border = "1px solid black";
    this.blackRect.style.position = "absolute";
    this.blackRect.style.display = "none";
    this.blackRect.id = "noteElement";

    this.leftTop = document.createElement("DIV");
    this.leftTop.style.width = "5px";
    this.leftTop.style.height = "5px";
    this.leftTop.style.position = "absolute";
    this.leftTop.style.fontSize = "1px";
    this.leftTop.style.left = "0px";
    this.leftTop.style.top = "0px";
    this.leftTop.style.border = "1px solid black";
    this.leftTop.style.backgroundColor = "white";
    this.leftTop.style.display = "none";
    this.leftTop.style.cursor = "nw-resize";
    this.leftTop.id = "noteElement";

    this.rightTop = document.createElement("DIV");
    this.rightTop.style.width = "5px";
    this.rightTop.style.height = "5px";
    this.rightTop.style.left = "0px";
    this.rightTop.style.top = "0px";
    this.rightTop.style.position = "absolute";
    this.rightTop.style.fontSize = "1px";
    this.rightTop.style.border = "1px solid black";
    this.rightTop.style.backgroundColor = "white";
    this.rightTop.style.display = "none";
    this.rightTop.style.cursor = "ne-resize";
    this.rightTop.id = "noteElement";

    this.leftBottom = document.createElement("DIV");
    this.leftBottom.style.width = "5px";
    this.leftBottom.style.height = "5px";
    this.leftBottom.style.position = "absolute";
    this.leftBottom.style.fontSize = "1px";
    this.leftBottom.style.border = "1px solid black";
    this.leftBottom.style.left = "0px";
    this.leftBottom.style.top = "0px";
    this.leftBottom.style.backgroundColor = "white";
    this.leftBottom.style.display = "none";
    this.leftBottom.style.cursor = "ne-resize";
    this.leftBottom.id = "noteElement";

    this.rightBottom = document.createElement("DIV");
    this.rightBottom.style.width = "5px";
    this.rightBottom.style.height = "5px";
    this.rightBottom.style.position = "absolute";
    this.rightBottom.style.fontSize = "1px";
    this.rightBottom.style.border = "1px solid black";
    this.rightBottom.style.left = "0px";
    this.rightBottom.style.top = "0px";
    this.rightBottom.style.backgroundColor = "white";
    this.rightBottom.style.display = "none";
    this.rightBottom.style.cursor = "nw-resize";
    this.rightBottom.id = "noteElement";

	// ---------------------------------------------------
	// ---------------------------------------------------

	this.rectangle.onselectstart = function() { return false; }
	this.rectangle.onmousemove = this.parentHandler.targetObj.onmousemove;
	this.rectangle.onmousedown = this.parentHandler.targetObj.onmousedown;
	this.rectangle.onmouseup = this.parentHandler.targetObj.onmouseup;
	this.rectangle.onmouseout = function(eventRef) {
        if (eventRef != null) event = eventRef;

        var target = (event.toElement == null) ? event.relatedTarget : event.toElement;
        self.parentHandler.event_onmouseout(this, target);
    }

	this.rightTop.onselectstart = this.rectangle.onselectstart;
	this.rightTop.onmousemove = this.rectangle.onmousemove;
	this.rightTop.onmousedown = this.rectangle.onmousedown;
	this.rightTop.onmouseup = this.rectangle.onmouseup;

	this.leftTop.onselectstart = this.rectangle.onselectstart;
	this.leftTop.onmousemove = this.rectangle.onmousemove;
	this.leftTop.onmousedown = this.rectangle.onmousedown;
	this.leftTop.onmouseup = this.rectangle.onmouseup;

	this.rightBottom.onselectstart = this.rectangle.onselectstart;
	this.rightBottom.onmousemove = this.rectangle.onmousemove;
	this.rightBottom.onmousedown = this.rectangle.onmousedown;
	this.rightBottom.onmouseup = this.rectangle.onmouseup;

	this.leftBottom.onselectstart = this.rectangle.onselectstart;
	this.leftBottom.onmousemove = this.rectangle.onmousemove;
	this.leftBottom.onmousedown = this.rectangle.onmousedown;
	this.leftBottom.onmouseup = this.rectangle.onmouseup;

    this.blackRect.onselectstart = this.rectangle.onselectstart;
	this.blackRect.onmousemove = this.rectangle.onmousemove;
	this.blackRect.onmousedown = this.rectangle.onmousedown;
	this.blackRect.onmouseup = this.rectangle.onmouseup;
	this.blackRect.onmouseout = function(eventRef) {
        if (eventRef != null) event = eventRef;

        var target = (event.toElement == null) ? event.relatedTarget : event.toElement;
        self.parentHandler.event_onmouseout(this, target);
    }

	document.body.appendChild( this.blackRect );
	document.body.appendChild( this.rectangle );

    document.body.appendChild( this.leftTop );
    document.body.appendChild( this.rightTop );
    document.body.appendChild( this.leftBottom );
    document.body.appendChild( this.rightBottom );

	this.relX = 0.0;
	this.relY = 0.0;
	this.relEndX = 0.0;
	this.relEndY = 0.0;
	
	this.dragged = false;
	this.drag_diffX = 0.0;
	this.drag_diffY = 0.0;

	this.endSet   = false;
	this.startSet = false;
	this.active   = false;
	this.creating = true;
	this.disabled = false;

	this.value = "";

	this.destroy = function() {
		this.deactivate();
		this.rectangle.style.display = "none";
		this.blackRect.style.display = "none";
        if (this.parentHandler.infoResX > 0 && this.parentHandler.infoResY > 0)
            this.parentHandler.sizeInfoDiv.style.display = "none";

		document.body.removeChild(this.leftTop);
		document.body.removeChild(this.rightTop);
		document.body.removeChild(this.rightBottom);
		document.body.removeChild(this.leftBottom);
		document.body.removeChild(this.blackRect);

		if (this.show) {
			this.hide();
			if (this.parentHandler.lastShowedNote == this) 
				this.parentHandler.lastShowedNote = null;
		}
		
		delete this.rectangle;
	}

	this.dataComposeSelf = function() {
		var toRetSelf = "";
		toRetSelf = this.relX + "," + this.relY + "," + this.relEndX + "," + this.relEndY + "," + this.creatorIdent + ",\"" + this.creatorName + "\",\"" + this.value + "\"," + this.ident + "";
		return toRetSelf;
	}
	
	this.temp_relX = new Array(0.0, 0.0);
	this.temp_relY = new Array(0.0, 0.0);
	this.temp_relEndX = new Array(0.0, 0.0);
	this.temp_relEndY = new Array(0.0, 0.0);
	
	this._saveCoords = function(index) {
		this.temp_relX[index] = this.relX;
		this.temp_relY[index] = this.relY;
		this.temp_relEndX[index] = this.relEndX;
		this.temp_relEndY[index] = this.relEndY;		
	}

	this._resetCoords = function(index, type) {
		if (type == 0 || type == 2) {
			this.relX = this.temp_relX[index];
			this.relEndX = this.temp_relEndX[index];
		}
		if (type == 1 || type == 2) {
			this.relY = this.temp_relY[index];
			this.relEndY = this.temp_relEndY[index];
		}
	}

	this.saveCoords = function() {
		this._saveCoords(0);
	}
	
	this.saveCallbackCoords = function() {
		this._saveCoords(1);
	}
	
	this.resetCoords = function(type) {
		this._resetCoords(0, type);
	}
	
	this.resetCallbackCoords = function() {
		this._resetCoords(1, 2);
	}

    this.fixSizes = function(part) {
		var diffX = Math.abs(this.relEndX - this.relX);
		var diffY = Math.abs(this.relEndY - this.relY);
		var ratio = 0;

        if (part == 2 || part == 0) {
            // check maximalni velikosti
            if (diffX > 0.3 && this.parentHandler.mode != "writemodify") {
                if (this.relEndX > this.relX) this.relEndX = this.relX + 0.3;
                else this.relX = this.relEndX + 0.3;
            }
            if (diffY > 0.3 && this.parentHandler.mode != "writemodify") {
                if (this.relEndY > this.relY) this.relEndY = this.relY + 0.3;
                else this.relY = this.relEndY + 0.3;
            }
        }
        if (part == 1 || part == 0) {
            // check minimalni velikosti
    		if (diffX * parseInt(this.parentHandler.targetObj.style.width) < 30) {
                ratio = 30 / parseInt(this.parentHandler.targetObj.style.width);
                if (this.relEndX > this.relX) this.relEndX = this.relX + ratio;
                else this.relX = this.relEndX + ratio;
            }
            if (diffY * parseInt(this.parentHandler.targetObj.style.height) < 30) {
                ratio = 30 / parseInt(this.parentHandler.targetObj.style.height);
                if (this.relEndY > this.relY) this.relEndY = this.relY + ratio;
                else this.relY = this.relEndY + ratio;
            }
        }
    }

	this.fixCoords = function() {
		if (this.relX > 1) this.relX = 1;
		if (this.relY > 1) this.relY = 1;
		if (this.relX < 0) this.relX = 0;
		if (this.relY < 0) this.relY = 0;

        if (this.relEndX > 1) this.relEndX = 1;
		if (this.relEndY > 1) this.relEndY = 1;
		if (this.relEndX < 0) this.relEndX = 0;
		if (this.relEndY < 0) this.relEndY = 0;
    }

	this.setRelativeStart = function(iPosX, iPosY) {
		this.relX = iPosX / parseInt(this.parentHandler.targetObj.style.width);
		this.relY = iPosY / parseInt(this.parentHandler.targetObj.style.height);

        this.fixCoords();

		this.startSet = true;
	}

	this.setRelativeEnd = function(iPosX, iPosY) {
		this.relEndX = iPosX / parseInt(this.parentHandler.targetObj.style.width);
		this.relEndY = iPosY / parseInt(this.parentHandler.targetObj.style.height);

        this.fixSizes(2);
        this.fixCoords();

		this.endSet = true;
	}

    this._setLeft = function(iPos) {
        var pW = parseInt(this.parentHandler.targetObj.style.width);
        if (this.relX < this.relEndX) {
            if ((this.relEndX - iPos) * pW > 30)
                this.relX = iPos;
        } else {
            if ((this.relX - iPos) * pW > 30)
                this.relEndX = iPos;
        }
    }

    this._setRight = function(iPos) {
        var pW = parseInt(this.parentHandler.targetObj.style.width);
        if (this.relX > this.relEndX) {
            if ((iPos - this.relEndX) * pW > 30)
                this.relX = iPos;
        } else {
            if ((iPos - this.relX) * pW > 30)
                this.relEndX = iPos;
        }
    }

    this._setTop = function(iPos) {
        var pH = parseInt(this.parentHandler.targetObj.style.width);
        if (this.relY < this.relEndY) {
            if ((this.relEndY - iPos) * pH > 30)
                this.relY = iPos;
        } else {
            if ((this.relY - iPos) * pH > 30)
                this.relEndY = iPos;
        }
    }

    this._setBottom = function(iPos) {
        var pH = parseInt(this.parentHandler.targetObj.style.width);
        if (this.relY > this.relEndY) {
            if ((iPos - this.relEndY) * pH > 30)
                this.relY = iPos;
        } else {
            if ((iPos - this.relY) * pH > 30)
                this.relEndY = iPos;
        }
    }

    this.setCorner = function(index, iPosX, iPosY) {
        switch (index) {
            case 0 : // topLeft
                this._setLeft(iPosX);
                this._setTop(iPosY);
                break;

            case 1 : // topRight
                this._setRight(iPosX);
                this._setTop(iPosY);
                break;

            case 2 : // bottomRight
                this._setRight(iPosX);
                this._setBottom(iPosY);
                break;

            case 3 : // bottomLeft
                this._setLeft(iPosX);
                this._setBottom(iPosY);
                break;
        }
        
        this.fixSizes(0);
        this.fixCoords();
    }

	this.setCursor = function(iVal) {
       if (iVal = "auto" && this.active) {
    	   this.blackRect.style.cursor = "Move";
    	   this.rectangle.style.cursor = "Move";
       } else {
    	   this.blackRect.style.cursor = iVal;
    	   this.rectangle.style.cursor = iVal;
	   }
	   this.parentHandler.targetObj.style.cursor = iVal;
    }

    this.draggedCorner;

    this.startMove = function() {
        this.draggedCorner = -1; 

		this.parentHandler.inputBox.style.display = "none";
		this.parentHandler.inputButtonOk.style.display = "none";
		this.parentHandler.inputButtonCancel.style.display = "none";
    }

    this.endMove = function() {
		this.parentHandler.inputBox.style.display = this.parentHandler.noTextArea ? "none" : "block";
		this.parentHandler.inputButtonOk.style.display = "block";
		this.parentHandler.inputButtonCancel.style.display = "block";
		
		this.setCursor("Auto");
    }

	this.setMove = function(iPosX, iPosY, iDiffX, iDiffY) {
        var newPosX   = iPosX / parseInt(this.parentHandler.targetObj.style.width);
        var newPosY   = iPosY / parseInt(this.parentHandler.targetObj.style.height);

        var cornerX = 20 / parseInt(this.parentHandler.targetObj.style.width);
        var cornerY = 20 / parseInt(this.parentHandler.targetObj.style.height);

        var iX;
        var iX;
        var iEX;
        var iEY;
        if (this.relX < this.relEndX) {
            iX  = this.relX;
            iEX = this.relEndX;
        } else {
            iX  = this.relEndX;
            iEX = this.relX;
        }
        if (this.relY < this.relEndY) {
            iY  = this.relY;
            iEY = this.relEndY;
        } else {
            iY  = this.relEndY;
            iEY = this.relY;
        }

        if (this.draggedCorner == -1) {
            if (newPosX < iX + cornerX && newPosY < iY + cornerY) {
                this.draggedCorner = 0;
        	    this.setCursor("nw-resize");
            } else if (newPosX > iEX - cornerX && newPosY < iY + cornerY) {
                this.draggedCorner = 1;
        	    this.setCursor("ne-resize");
            } else if (newPosX > iEX - cornerX && newPosY > iEY - cornerY) {
                this.draggedCorner = 2;
        	    this.setCursor("nw-resize");
            } else if (newPosX < iX + cornerX && newPosY > iEY - cornerY) {
                this.draggedCorner = 3;
        	    this.setCursor("ne-resize");
            } else {
                this.draggedCorner = 4;
        	    this.setCursor("Move");
            }
        }

        switch (this.draggedCorner) {
            case 4 :
                // posun celeho objektu
        		var width  = iEX - iX;
        		var height = iEY - iY;

        		this.relX = iPosX / parseInt(this.parentHandler.targetObj.style.width) - iDiffX;
        		this.relY = iPosY / parseInt(this.parentHandler.targetObj.style.height) - iDiffY;
        		this.relEndX = this.relX + width;
        		this.relEndY = this.relY + height;

        		var incX = 0;
        		var incY = 0;
        		if (this.relEndX > 1) incX = 1 - this.relEndX;
        		if (this.relEndY > 1) incY = 1 - this.relEndY;
        		if (this.relEndX < 0) incX = - this.relEndX;
        		if (this.relEndY < 0) incY = - this.relEndY;
        		if (this.relX < 0) incX = - this.relX;
        		if (this.relY < 0) incY = - this.relY;
        		if (this.relX > 1) incX = 1 - this.relX;
        		if (this.relY > 1) incY = 1 - this.relY;

        		this.relEndX += incX;
        		this.relX += incX;
        		this.relEndY += incY;
        		this.relY += incY;
        		break;

        	default :
        	    // posun specifickeho rohu
        	    this.setCorner(this.draggedCorner, newPosX, newPosY);
        	    break;
	    }
	}

	this.construct = function(x1, y1, x2, y2) {
        this.creating = false;

		this.relX = x1;
		this.relY = y1;
		this.relEndX = x2;
		this.relEndY = y2;
	}

	this.inSquare = function(iPosX, iPosY) {
        /*
		var relPosX = iPosX / parseInt(this.parentHandler.targetObj.style.width);
		var relPosY = iPosY / parseInt(this.parentHandler.targetObj.style.height);

		return this.parentHandler.inSquare(this.relX, this.relY, this.relEndX, this.relEndY, relPosX, relPosY);
        */
        var pW = parseInt(this.parentHandler.targetObj.style.width);
        var pH = parseInt(this.parentHandler.targetObj.style.height);
        var ovSize = 3;

		return this.parentHandler.inSquare(this.relX * pW - ovSize, this.relY * pH - ovSize, this.relEndX * pW + ovSize, this.relEndY * pH + ovSize, iPosX, iPosY);
	}

	this.updateObject = function(force) {
		if (!this.disabled || force) {
			var startX = 0;
			var startY = 0;
			var relWidth = 0;
			var relHeight = 0;
			var width = 0;
			var height = 0;

			// sumarizace
			if (this.relX < this.relEndX) {
				startX = this.relX;
				relWidth = this.relEndX - this.relX;
			} else {
				startX = this.relEndX;
				relWidth = this.relX - this.relEndX;
			}

			if (this.relY < this.relEndY) {
				startY = this.relY;
				relHeight = this.relEndY - this.relY;
			} else {
				startY = this.relEndY;
				relHeight = this.relY - this.relEndY;
			}

            var pW  = parseInt(this.parentHandler.targetObj.style.width);
            var pH  = parseInt(this.parentHandler.targetObj.style.height);
            var elX = elementPositionX(this.parentHandler.targetObj);
            var elY = elementPositionY(this.parentHandler.targetObj);

			// namapovani na specificky rozmer
			startX = Math.round(startX * pW);
			startY = Math.round(startY * pH);
			width  = Math.round(relWidth * pW);
			height = Math.round(relHeight * pH);

            // update event. informace o velikosti
            if (this.parentHandler.infoResX > 0 && this.parentHandler.infoResY > 0) {
                this.parentHandler.sizeInfoDiv.style.display = this.disabled ? "none" : "block";
                this.parentHandler.sizeInfoDiv.style.position = "absolute";
                this.parentHandler.sizeInfoDiv.style.left = (elX + startX + Math.round(width / 2) - 25) + "px";
                this.parentHandler.sizeInfoDiv.style.top = (elY + startY + Math.round(height / 2) - 15) + "px";
                this.parentHandler.sizeInfoDiv.style.backgroundColor = "white";
                this.parentHandler.sizeInfoDiv.style.color = "black";
                this.parentHandler.sizeInfoDiv.innerHTML = Math.round(this.parentHandler.infoResX * relWidth) + " x " + Math.round(this.parentHandler.infoResX * relHeight);
                this.parentHandler.sizeInfoDiv.innerHTML += "<br/>(" + Math.round(relWidth * 100) + "% x " + Math.round(relHeight * 100) + "%)";
            }

			// update divu
			if (width > 6 && height > 6) {
                // update skryte oblasti
                if (this.parentHandler.displayArea && (this.active || this.creating)) {
                    if (this.parentHandler.areaDivs.length == 0) {
                        for (var f = 0; f < 8; f++) {
                            this.parentHandler.areaDivs[f] = document.createElement("DIV");
                            this.parentHandler.areaDivs[f].style.display = this.disabled ? "none" : "block";
                            this.parentHandler.areaDivs[f].style.backgroundColor = "black";
                            this.parentHandler.areaDivs[f].style.position = "absolute";
                            this.parentHandler.areaDivs[f].style.fontSize = "1px";
                            this.parentHandler.areaDivs[f].innerHTML = "&nbsp;";
                            this.parentHandler.areaDivs[f].style.opacity = 0.5;
                            this.parentHandler.areaDivs[f].style.filter = "alpha(opacity=50)";

                        	this.parentHandler.areaDivs[f].onselectstart = this.rectangle.onselectstart;
                        	this.parentHandler.areaDivs[f].onmousemove = this.rectangle.onmousemove;
                        	this.parentHandler.areaDivs[f].onmousedown = this.rectangle.onmousedown;
                        	this.parentHandler.areaDivs[f].onmouseup = this.rectangle.onmouseup;

                            document.body.appendChild( this.parentHandler.areaDivs[f] );
                        }
                    }

                    var elEX = elX + pW;
                    var elEY = elY + pH;

                    var x_matrix = new Array(elX, elX + startX, elX + startX + width + 2, elEX);
                    var y_matrix = new Array(elY, elY + startY, elY + startY + height + 2, elEY);
                    var diffX = 0;
                    var diffY = 0;
                    var index = 0;

                    for (var fy = 0; fy < 3; fy++) {
                        for (var fx = 0; fx < 3; fx++) {
                            //var index = fy * 3 + fx;
                            if (fx != 1 || fy != 1) {
                                diffX = (x_matrix[fx + 1] - x_matrix[fx]);
                                diffY = (y_matrix[fy + 1] - y_matrix[fy]);

                                if (diffX > 2 && diffY > 2) {
                                    this.parentHandler.areaDivs[index].style.left    = x_matrix[fx] + "px";
                                    this.parentHandler.areaDivs[index].style.top     = y_matrix[fy] + "px";
                                    this.parentHandler.areaDivs[index].style.width   = diffX + "px";
                                    this.parentHandler.areaDivs[index].style.height  = diffY + "px";
                                    this.parentHandler.areaDivs[index].style.display = this.disabled ? "none" : "block";
                                } else this.parentHandler.areaDivs[index].style.display = "none";

                                index++;
                            }
                        } // for
                    } // for
                } // if

                // update ramecku
    			this.rectangle.style.left   = (elX + startX) + "px";
    			this.rectangle.style.top    = (elY + startY) + "px";
    			this.rectangle.style.width  = width + "px";
    			this.rectangle.style.height = height + "px";
    
    			this.rectangle.style.display = this.disabled ? "none" : "block";
    
    			this.blackRect.style.left   = (elX + startX + 1) + "px";
    			this.blackRect.style.top    = (elY + startY + 1) + "px";
    			this.blackRect.style.width  = (width - 2) + "px";
    			this.blackRect.style.height = (height - 2) + "px";
    
    			this.blackRect.style.display = this.disabled ? "none" : "block";
    
    			// update rohu
                this.leftTop.style.left = (elX + startX + 1) + "px";
                this.leftTop.style.top  = (elY + startY + 1) + "px";

                this.rightTop.style.left = (elX + startX + width - 6) + "px";
                this.rightTop.style.top  = (elY + startY + 1) + "px";

                this.rightBottom.style.left = (elX + startX + width - 6) + "px";
                this.rightBottom.style.top  = (elY + startY + height - 6) + "px";

                this.leftBottom.style.left = (elX + startX + 1) + "px";
                this.leftBottom.style.top  = (elY + startY + height - 6) + "px";
            }         
		}
	}

	this.updateInput = function() {
		if (!this.disabled) {
			var baseX = parseInt(this.rectangle.style.left);
			var baseY = (parseInt(this.rectangle.style.top) + parseInt(this.rectangle.style.height)) + 5;

			var minX = elementPositionX(this.parentHandler.targetObj);
			var minY = elementPositionY(this.parentHandler.targetObj);
			var maxX = minX + parseInt(this.parentHandler.targetObj.style.width);
			var maxY = minY + parseInt(this.parentHandler.targetObj.style.height);
			
			if (baseX + 255 > maxX) {
				baseX += maxX - (baseX + 255);
			}
			if (baseY + (this.parentHandler.noTextArea ? 20 : 70) > maxY) {
				//baseY += maxY - (baseY + 70);
				baseY += - ((this.parentHandler.noTextArea ? 20 : 80) + parseInt(this.rectangle.style.height));
			}

            if (!this.parentHandler.noTextArea) {
    			this.parentHandler.inputBox.style.left = baseX + "px";
    			this.parentHandler.inputBox.style.top = baseY + "px";
			}

			this.parentHandler.inputButtonOk.style.left = (baseX + 75) + "px";
			this.parentHandler.inputButtonOk.style.top = (baseY + (this.parentHandler.noTextArea ? 0 : 50)) + "px";

			this.parentHandler.inputButtonCancel.style.left = baseX + "px";
			this.parentHandler.inputButtonCancel.style.top = (baseY + (this.parentHandler.noTextArea ? 0 : 50)) + "px";
		}
	}
	
	this.show = function() {
		if (!this.disabled) {
			// zobrazeni poznamky
			var baseX = parseInt(this.rectangle.style.left) - 1;
			var baseY = parseInt(this.rectangle.style.top) + parseInt(this.rectangle.style.height) + 2;

			this.parentHandler.infoDiv.style.position = "absolute";
			this.parentHandler.infoDiv.style.left = baseX + "px";
			this.parentHandler.infoDiv.style.top = baseY + "px";
			this.parentHandler.infoDiv.className = "noteInfoDiv";
			this.parentHandler.infoDiv.innerHTML = "od:&nbsp;<strong class=\"blueText\">" + this.creatorName + "</strong><br/>" + this.value;
			this.parentHandler.infoDiv.style.display = "block";
			this.parentHandler.infoDiv.style.zIndex = 9999;
			
			this.rectangle.style.border = (this.parentHandler.creatorIdent != this.creatorIdent && this.parentHandler.mode == "write") ? "1px solid red" : "1px solid white";
		}
	}
	
	this.hide = function() {
		if (!this.disabled) {
			// schovani poznamky
			this.parentHandler.infoDiv.style.display = "none";

			this.rectangle.style.border = "1px dashed white";
		}
	}

	this.disable = function() {
		this.rectangle.style.display = "none";
		this.blackRect.style.display = "none";
		this.disabled = true;
	}

	this.enable = function() {
		this.rectangle.style.display = "block";
		this.blackRect.style.display = "block";
		this.disabled = false;
	}

	this.activate = function() {
		if (!this.active && !this.disabled) {		
			this.active = true;
			
			var commitText = "";
			var discardText = "";
			
			switch (this.parentHandler.cmd_add) {
                case "photo_crop" :
                    commitText = "Oříznout";
                    discardText = "Zrušit";
                    break;

                default :
                    commitText = " OK ";
                    discardText = "Zrušit";
                    break;
            }

			this.rectangle.style.border = "1px dashed yellow";

			this.parentHandler.inputBox.style.width  = "250px";
			this.parentHandler.inputBox.style.height = "40px";
			this.parentHandler.inputBox.style.position = "absolute";
			this.parentHandler.inputBox.value = this.value;
			this.parentHandler.inputBox.style.zIndex = 9998;

			this.parentHandler.inputButtonOk.value = commitText;
			this.parentHandler.inputButtonOk.style.width = "70px";
			this.parentHandler.inputButtonOk.style.position = "absolute";
			this.parentHandler.inputButtonOk.className = "inputBlue";
			this.parentHandler.inputButtonOk.style.zIndex = 9998;

			this.parentHandler.inputButtonCancel.value = discardText;
			this.parentHandler.inputButtonCancel.style.width = "70px";
			this.parentHandler.inputButtonCancel.style.position = "absolute";
			this.parentHandler.inputButtonCancel.className = "inputGray";
			this.parentHandler.inputButtonCancel.style.zIndex = 9998;
			
			this.rectangle.style.cursor = "Move";
			this.blackRect.style.cursor = "Move";

			this.updateInput();
			if (this.parentHandler.displayArea)
			    this.updateObject(false);

			this.parentHandler.inputButtonCancel.style.display = "block";
			this.parentHandler.inputButtonOk.style.display = "block";
			this.parentHandler.inputBox.style.display = this.parentHandler.noTextArea ? "none" : "block";

            this.leftTop.style.display = "block";
            this.rightTop.style.display = "block";
            this.rightBottom.style.display = "block";
            this.leftBottom.style.display = "block";
		}
	}

	this.deactivate = function() {
		if (this.active && !this.disabled) {
			this.active = false;

			this.rectangle.style.border = "1px dashed white";

			this.rectangle.style.cursor = "auto";
			this.blackRect.style.cursor = "auto";

			this.parentHandler.inputBox.style.display = "none";
			this.parentHandler.inputButtonOk.style.display = "none";
			this.parentHandler.inputButtonCancel.style.display = "none";

            this.leftTop.style.display = "none";
            this.rightTop.style.display = "none";
            this.rightBottom.style.display = "none";
            this.leftBottom.style.display = "none";
            
            if (this.parentHandler.areaDivs.length > 0) {
                for (var f = 0; f < 8; f++)
                    this.parentHandler.areaDivs[f].style.display = "none";
            }
		} // if
	}

	this.finish = function() {
        // finalizace vytvarene poznamky
        this.creating = false;

        this.fixSizes(0);
        this.fixCoords();
        this.updateObject(false);
        this.activate();
    }   

	// konstrukce
	this.setRelativeStart(iPosX, iPosY);
}

function noteHandler(iTargetObj, iMod, iCreatorIdent, iCreatorName, iPhotoID, iDecompose) {
	var self = this;

	this.collection = new Array(); // kolekce poznamek
	this.targetObj = null; // cilovy obrazek pro poznamky
	this.mode = iMod; // mod handleru - writemodify, write, read, hidden
	this.beforeMode = "hidden";
	this.creatorIdent = iCreatorIdent;
	this.creatorName = iCreatorName;
	this.photo_ID = iPhotoID;

    this.noTextArea = false;
    this.displayArea = false; // zobrazeni oblasti kolem ramecku
    this.areaDivs = new Array();
    this.infoResX = 0; // zobrazovani podrobnosti velikosti výřezu
    this.infoResY = 0;

    this.leftDependent = null; // odkaz na podrazeneho
    this.rightDependent = null; // odkaz na nadrazeneho
    
    this.cmd_add = "note_add";
    this.cmd_rem = "note_rem";
    this.cmd_update = "note_update";

	this.setTargetObj = function(iRef) {
		this.targetObj = iRef;

		//this.targetObj.onmousedown = this.event_onMouseDown;
		this.targetObj.onmousedown = function(eventRef) {
			if (eventRef != null) event = eventRef;
			if (event.preventDefault != null) event.preventDefault();
			self.event_down(event.clientX, event.clientY + scY());
		}

		this.targetObj.onmouseup = function(eventRef) {
			if (eventRef != null) event = eventRef;
			self.event_up(event.clientX, event.clientY + scY());
		}

		this.targetObj.onmousemove = function (eventRef) {
			if (eventRef != null) event = eventRef;
			self.event_move(event.clientX, event.clientY + scY());
		}

		this.targetObj.onmouseover = function () {
            if (self.mode == "hidden") self.enable();
        }

        this.event_onmouseout = function (iSelfEl, iTarget) {
            if (this.lastNote == null) {
                if (iTarget != null)
                    if (iTarget.id != "noteElement" && iTarget.parentNode != iSelfEl && iTarget != this.targetObj) this.disable();
            }
        }

        this.targetObj.onmouseout = function (eventRef) {
            if (eventRef != null) event = eventRef;

            if (self.lastNote == null) {
                var selfEl = (event.srcElement == null) ? event.target : event.srcElement;
                var target = (event.toElement == null) ? event.relatedTarget : event.toElement;

                self.event_onmouseout(selfEl, target);
            }
        }

		this.targetObj.onselectstart = function() { return false; }
		this.targetObj.ondragstart = function() { return false; }
	}

	this.lastNote = null;
	this.lastShowedNote = null;

	this.inputBox = null;
	this.inputButtonOk = null;
	this.inputButtonCancel = null;
	this.infoDiv = null;

	this.inputBox = document.createElement("TEXTAREA");
	this.inputButtonOk = document.createElement("INPUT");
	this.inputButtonCancel = document.createElement("INPUT");
	this.infoDiv = document.createElement("DIV");
	this.sizeInfoDiv = document.createElement("DIV");

	this.inputBox.style.display = "none";
	this.inputBox.id = "noteElement";

	this.inputButtonOk.type = "button";
	this.inputButtonOk.style.display = "none";
	this.inputButtonOk.onclick = function() { self.event_ok(); }
	this.inputButtonOk.id = "noteElement";

	this.inputButtonCancel.type = "button";
	this.inputButtonCancel.style.display = "none";
	this.inputButtonCancel.onclick = function() { self.event_cancel(); };
	this.inputButtonCancel.id = "noteElement";
	
	this.infoDiv.style.display = "none";
	this.infoDiv.id = "noteElement";
	this.infoDiv.onmouseout = function(eventRef) {
        if (eventRef != null) event = eventRef;

        var target = (event.toElement == null) ? event.relatedTarget : event.toElement;
        self.event_onmouseout(this, target);
    }
    
    this.sizeInfoDiv.style.display = "none";
    this.sizeInfoDiv.style.position = "absolute";

	var trash = document.getElementById("trashContainer");
	trash.appendChild(this.inputBox);
	trash.appendChild(this.inputButtonCancel);
	trash.appendChild(this.inputButtonOk);
	trash.appendChild(this.infoDiv);
	trash.appendChild(this.sizeInfoDiv);

	this.hideNote = function() {
		if (this.lastShowedNote != null) this.lastShowedNote.hide();
	}
	
	this.showNote = function(noteRef) {
		if (noteRef != null) {
			this.hideNote();
			noteRef.show();
			this.lastShowedNote = noteRef;
		}
	}
	
	this.rebuild = function() {
		for (var f = 0; f < this.collection.length; f++)
			this.collection[f].updateObject(true);

        if (this.lastNote != null) {
            this.lastNote.updateObject(true);
            this.lastNote.updateInput();
        }
	}

    this.disabled = false;

	this.disable = function() {
        if (!this.disabled) {
            if (this.lastNote != null) {
                this.lastNote.deactivate();

            	if (this.findNote(this.lastNote) == -1) {
                    // zruseni vytvarene poznamky
            		this.lastNote.destroy();
            		delete this.lastNote;
            		this.lastNote = null;
            	}

            	this.lastNote = null;
        	}

    		this.hideNote();

    		for (var f = 0; f < this.collection.length; f++)
    			this.collection[f].disable();

    		this.beforeMode = this.mode;
    		this.mode = "hidden";
    		this.disabled = true;
		}
	}

	this.enable = function() {
        if (this.disabled && this.rightDependent == null) {
    		for (var f = 0; f < this.collection.length; f++)
    			this.collection[f].enable();

    		this.mode = this.beforeMode;
    		this.disabled = false;
		}
	}
	
	this.destroy = function() {
        if (this.lastNote != null) {
            this.lastNote.deactivate();
    	    this.lastNote.destroy();
		    delete this.lastNote;
		    this.lastNote = null;
        }
        this.hideNote();
        this.disable();
        this.flushNotes();

    	trash.removeChild(this.inputBox);
    	trash.removeChild(this.inputButtonCancel);
    	trash.removeChild(this.inputButtonOk);
    	trash.removeChild(this.infoDiv);

    	this.leftDependent.rightDependent = null;
    	this.leftDependent.setTargetObj( this.leftDependent.targetObj ); // event reload
    }

	// prace s kolekci
	this.lastIdent = 1;
	
	this.addNote = function(noteRef) {
		this.collection[this.collection.length] = noteRef;
		noteRef.ident = this.lastIdent;
		this.lastIdent++;
	}
	
	this.remNote = function(noteIndex) {
		if (this.collection.length > 0) {
			for (var f = noteIndex; f < this.collection.length - 1; f++)
				this.collection[f] = this.collection[f + 1];
			return this.collection.pop();
		}
		return null;
	}

	this.flushNotes = function() {
		// resetuje kolekci
		if (this.collection.length > 0) {
			for (var f = 0; f < this.collection.length; f++) {
				this.collection[f].destroy();
				delete this.collection[f];
			}
			this.collection = new Array();
		}
	}
	
	this.findNote = function(noteRef) {
		var pos = 0;
		var founded = false;
		
		while (pos < this.collection.length && !founded) {
			if (!this.collection[pos].disabled && this.collection[pos] == noteRef) founded = true;
			else pos++;
		}
			
		return founded ? pos : -1;
	}
	
	this.findNoteByCoords = function(iX, iY) {
		var trX = this.trueX(iX);
		var trY = this.trueY(iY);
		
		var pos = 0;
		var founded = false;
		
		while (!founded && pos < this.collection.length) {
			if (!this.collection[pos].disabled && this.collection[pos].inSquare(trX, trY)) founded = true;
			else pos++;
		}
		
		return founded ? this.collection[pos] : null;
	}
	
	// ------ k o l i z e --------
	this.lineCross = function(aX1, aY1, aX2, aY2, 
							  bX1, bY1, bX2, bY2) {
							  
		// zjisti zda se protinaji dve kolme primky
		// 1 - vertikalni kolize
		// 2 - horizontalni kolize
		// 0 - zadna kolize
		
		var precision = 100000;
		aX1 = Math.round(aX1 * precision);
		aY1 = Math.round(aY1 * precision);
		aX2 = Math.round(aX2 * precision);
		aY2 = Math.round(aY2 * precision);

		bX1 = Math.round(bX1 * precision);
		bY1 = Math.round(bY1 * precision);
		bX2 = Math.round(bX2 * precision);
		bY2 = Math.round(bY2 * precision);		
		
		if (bX1 == bX2 && aY1 == aY2) {
			return ( ((bX1 >= aX1 && bX1 <= aX2) || (bX1 >= aX2 && bX1 <= aX1)) 
				&& ((aY1 >= bY1 && aY1 <= bY2) || (aY1 >= bY2 && aY1 <= bY1)) ) ? 3 : 0; // 1
		} else if (bY1 == bY2 && aX1 == aX2) {
			return ( ((aX1 >= bX1 && aX1 <= bX2) || (aX1 >= bX2 && aX1 <= bX1)) 
				&& ((bY1 >= aY1 && bY1 <= aY2) || (bY1 >= aY2 && bY1 <= aY1)) ) ? 3 : 0; // 2
		}
		return 0;
	}
	
	this.lineBehind_vert = function(aX, aY1, aY2, bX, bY1, bY2, tWidth) {
		var precision = 100000;
		aX  = Math.round(aX * precision);
		aY1 = Math.round(aY1 * precision);
		aY2 = Math.round(aY2 * precision);

		bX  = Math.round(bX * precision);
		bY1 = Math.round(bY1 * precision);
		bY2 = Math.round(bY2 * precision);
		
		tWidth = Math.round(tWidth * precision);
	
		if ( ((aY1 <= bY1 && aY2 >= bY1) || (aY1 <= bY2 && aY2 >= bY2))
		|| ((bY1 <= aY1 && bY2 >= aY1) || (bY1 <= aY2 && bY2 >= aY2)) ) {
			if (tWidth > 0) {
				if (aX >= bX && aX <= bX + tWidth) return 2;
			} else {
				if (aX <= bX && aX >= bX + tWidth) return 2;
			}
		} // if
		return 0;
	}
	
	this.lineBehind_horiz = function(aY, aX1, aX2, bY, bX1, bX2, tHeight) {
		var precision = 100000;
		aY  = Math.round(aY * precision);
		aX1 = Math.round(aX1 * precision);
		aX2 = Math.round(aX2 * precision);

		bY  = Math.round(bY * precision);
		bX1 = Math.round(bX1 * precision);
		bX2 = Math.round(bX2 * precision);
		
		tHeight = Math.round(tHeight * precision);

		if ( ((aX1 <= bX1 && aX2 >= bX1) || (aX1 <= bX2 && aX2 >= bX2))
		|| ((bX1 <= aX1 && bX2 >= aX1) || (bX1 <= aX2 && bX2 >= aX2)) ) {
			if (tHeight > 0) {
				if (aY >= bY && aY <= bY + tHeight) return 1;
			} else {
				if (aY <= bY && aY >= bY + tHeight) return 1;
			}
		} // if
		return 0;
	}
	
	this.inSquare = function(aX1, aY1, aX2, aY2, bX, bY) {
		return ((bX >= aX1 && bX <= aX2) && ((bY >= aY1 && bY <= aY2) ||  (bY >= aY2 && bY <= aY1)) )
			|| ((bX >= aX2 && bX <= aX1) && ((bY >= aY1 && bY <= aY2) ||  (bY >= aY2 && bY <= aY1)) );
	}
	
	this.collideAdd = function(collideVal, newVal) {
		switch (collideVal) {
			case 0 :
				return newVal;
				break;
				
			case 1 :
				if (newVal == 2) return 3;					
				break;
				
			case 2 :
				if (newVal == 1) return 3;
				break;			
		}
		return collideVal;
	}
	
	this.checkCollision = function(iX, iY, iEX, iEY, rectRef) {
		// zjisti zda na zadanych relativnich souradnicich je kolize
		var ref = null;
		var collide = 0;
		var pos = 0;
		var tmp = 0;
		
		var diffX = 0;
		var diffY = 0;
		var diffDiff = 0;

		if (iEX < iX) {
			tmp = iEX;
			iEX = iX;
			iX  = tmp;
		}
		if (iEY < iY) {
			tmp = iEY;
			iEY = iY;
			iY  = tmp;
		}
		
		var side = 0;
		
		while (pos < this.collection.length && collide == 0) {			
			ref = this.collection[pos];
			if (ref != rectRef && !ref.disabled) {
				
				sX  = ref.relX;
				sY  = ref.relY;
				sEX = ref.relEndX;
				sEY = ref.relEndY;
				if (sEX < sX) {
					tmp = sEX;
					sEX = sX;
					sX  = tmp;
				}			
				if (sEX < sX) {
					tmp = sEY;
					sEY = sY;
					sY  = tmp;
				}
				
				if (this.inSquare(iX, iY, iEX, iEY, sX, sY) || this.inSquare(iX, iY, iEX, iEY, sEX, sEY)
				|| this.inSquare(sX, sY, sEX, sEY, iX, iY) || this.inSquare(sX, sY, sEX, sEY, iEX, iEY)) {
					collide = 4;
				}

        		if (iX > 1 || iY > 1 || iX < 0 || iY < 0 ||
                iEX > 1 || iEY > 1 || iEX < 0 || iEY < 0) {
                    collide = 4;
                }

				// diffX = 0;
				// diffY = 0;

				//if (iX < sX) {
				//	tmp = iEX - sX;
				//	if (tmp > 0) diffX = Math.round(tmp * 1000);
				//} else {
				//	tmp = sEX - iX;
				//	if (tmp > 0) diffX = Math.round(tmp * 1000);
				//}

				//if (iY < sY) {
				//	tmp = iEY - sY;
				//	if (tmp > 0) diffY = Math.round(tmp * 1000);
				//} else {
				//	tmp = sEY - iY;
				//	if (tmp > 0) diffY = Math.round(tmp * 1000);
				//}
				//if (diffX >= diffY) {
				//	collide = this.collideAdd(collide, this.lineBehind_horiz(iEY, iX, iEX, sY, sX, sEX, sEY - sY));
				//	collide = this.collideAdd(collide, this.lineBehind_horiz(iY, iX, iEX, sEY, sX, sEX, sY - sEY));
				//
				//	collide = this.collideAdd(collide, this.lineBehind_vert(iEX, iY, iEY, sX, sY, sEY, sEX - sX));
				//	collide = this.collideAdd(collide, this.lineBehind_vert(iX, iY, iEY, sEX, sY, sEY, sX - sEX));
				//} else {
				//	collide = this.collideAdd(collide, this.lineBehind_vert(iEX, iY, iEY, sX, sY, sEY, sEX - sX));
				//	collide = this.collideAdd(collide, this.lineBehind_vert(iX, iY, iEY, sEX, sY, sEY, sX - sEX));
                //
				//	collide = this.collideAdd(collide, this.lineBehind_horiz(iEY, iX, iEX, sY, sX, sEX, sEY - sY));
				//	collide = this.collideAdd(collide, this.lineBehind_horiz(iY, iX, iEX, sEY, sX, sEX, sY - sEY));
				//}
				
				if (collide != 4) {
					// horizontalne x vertikalne
					collide = this.collideAdd(collide, this.lineCross(iX, iY, iEX, iY, ref.relX, ref.relY, ref.relX, ref.relEndY));
					collide = this.collideAdd(collide, this.lineCross(iX, iY, iEX, iY, ref.relEndX, ref.relY, ref.relEndX, ref.relEndY));
		
					collide = this.collideAdd(collide, this.lineCross(iX, iY, iEX, iY, ref.relX, ref.relY, ref.relX, ref.relEndY));
					collide = this.collideAdd(collide, this.lineCross(iX, iEY, iEX, iEY, ref.relEndX, ref.relY, ref.relEndX, ref.relEndY));
					
					// vertikalne x horizontalne
					collide = this.collideAdd(collide, this.lineCross(iX, iY, iX, iEY, ref.relX, ref.relY, ref.relEndX, ref.relY));
					collide = this.collideAdd(collide, this.lineCross(iX, iY, iX, iEY, ref.relX, ref.relEndY, ref.relEndX, ref.relEndY));
		
					collide = this.collideAdd(collide, this.lineCross(iEX, iY, iEX, iEY, ref.relX, ref.relY, ref.relEndX, ref.relY));
					collide = this.collideAdd(collide, this.lineCross(iEX, iY, iEX, iEY, ref.relX, ref.relEndY, ref.relEndX, ref.relEndY));
				}
			}
			pos++;
		}

		return collide;
	}

	this.collideCorrection = function(noteRef) {
		var collide = this.checkCollision(noteRef.relX, noteRef.relY, noteRef.relEndX, noteRef.relEndY, noteRef);

		switch (collide) {
			case 0 : // nic
				break;

			case 1 : // vertiakalni
				//this.lastNote.relEndY = befY;
				break;

			case 2 : // horizontalni
				//this.lastNote.relEndX = befX;
				break;
				
			case 5 :
			     noteRef.resetCoords(2);
			     break;

			case 4 :
			case 3 :
				// test zda koliduje po resetu X
				noteRef.resetCoords(0);

				collide = this.checkCollision(noteRef.relX, noteRef.relY, noteRef.relEndX, noteRef.relEndY, noteRef);

				switch (collide) {
					case 0 :
						// v poradku, staci reset X
						break;

					case 4 :
					case 3 :
						// test kolize reset Y
						noteRef.resetCallbackCoords();
						noteRef.resetCoords(1);

						collide = this.checkCollision(noteRef.relX, noteRef.relY, noteRef.relEndX, noteRef.relEndY, noteRef);

						switch (collide) {
							case 0 :
								// v poradku staci reset Y
								break;

							case 3 :
							case 4 :
								noteRef.resetCoords(2);
								break;
						} // switch
						break;
				} // switch
				break;
		} // switch
	}
	
	// ------ sestaveni dat -------	
	this.dataCompose = function() {
		var toRet = "";
		for (var f = 0; f < this.collection.length; f++) {
			if (!this.collection[f].disabled)
				toRet += "(" + this.collection[f].dataComposeSelf() + ")";
		}
		return toRet;
	}
	
	this.dataDecompose = function(iStr) {
		this.lastIdent = 1;
		var aktChar = "";
		var pos = 0;
		var words = new Array();
		var lastWord = "";
		var inStr = false;

		while (pos < iStr.length) {
			aktChar = iStr.substr(pos, 1);
			
			if (inStr) {
				switch (aktChar) {
					case "\"" :
						inStr = false;
						break;
						
					default :
						lastWord += aktChar;
						break;
				}
			} else {
				switch (aktChar) {
					case "(" :
						lastWord = ""; // start
						break;
						
					case " " :
						break;
						
					case "\"" :
						inStr = true;
						break;

					case ")" :
					case "," :
						words[words.length] = lastWord;
						lastWord = "";
						
						if (aktChar == ")") {
							// vytvoreni note elementu
							var newNote = new noteElement(this, 0, 0);
							newNote.construct(parseFloat(words[0]), parseFloat(words[1]), parseFloat(words[2]), parseFloat(words[3]));
							newNote.creatorIdent = words[4];
							newNote.creatorName = words[5];
							newNote.value = words[6];
							newNote.ident = parseInt(words[7]);
							newNote.updateObject(false);
							
							if (newNote.ident >= this.lastIdent) this.lastIdent = newNote.ident;
							
							this.addNote( newNote );
							
							words = new Array();
						}
						break;
						
					default :
						lastWord += aktChar;
						break;
				}
			} // if
			
			pos++;
		} // while
		
		this.lastIdent += 1;
		
		if (window.event != null) {
    		var cX = window.event.clientX;
    		var cY = window.event.clientY;

    		var stX = elementPositionX(this.targetObj);
    		var stY = elementPositionY(this.targetObj);
    		var eX  = stX + parseInt(this.targetObj.style.width);
    		var eY  = stY + parseInt(this.targetObj.style.height);

            // test zda je kurzor uvnitr
            if (cX > stX && cY > stY && cX < eX && cY < eY) this.enable();
    		else this.disable();
		} else this.enable();
	}
	// ----------------------------
	
	this.trueX = function(iVal) {
		return iVal - elementPositionX(this.targetObj);
	}
	
	this.trueY = function(iVal) {
		return iVal - elementPositionY(this.targetObj);
	}
	
	this.dragActive = function(iX, iY) {
		this.dragged = true;
		this.drag_diffX = this.trueX(iX) / parseInt(this.targetObj.style.width) - (this.lastNote.relX < this.lastNote.relEndX ? this.lastNote.relX : this.lastNote.relEndX);
		this.drag_diffY = this.trueY(iY) / parseInt(this.targetObj.style.height) - (this.lastNote.relY < this.lastNote.relEndY ? this.lastNote.relY : this.lastNote.relEndY);
	}

	this.dragDrop = function() {
		this.dragged = false;
	}
	
	this.eventFreezed = false;
	
	this.eventFreeze = function() {
		this.eventFreezed = true;
	}

	this.eventUnfreeze = function() {
		this.eventFreezed = false;
	}

	this.createDefaultNote = function() {
        if (this.lastNote == null) {
			this.lastNote = new noteElement(this, 0, 0);
			this.lastNote.setRelativeEnd(0, 0);
			
			this.lastNote.relX = 0.2;
			this.lastNote.relY = 0.2;
			this.lastNote.relEndX = 0.8;
			this.lastNote.relEndY = 0.8;

			this.lastNote.finish();
			
			return this.lastNote;
        }
    }

	this.event_down = function(iX, iY) {
		if (this.eventFreezed) return false;
		
		// -- schovani dialogu poznamky --
		var noteDialog = document.getElementById("dialog9no");
		if (noteDialog != null)
		    noteDialog.style.display = "none";
		// -------------------------------

		switch (this.mode) {
			case "writemodify" :
			case "write" :
				var newNote = this.findNoteByCoords(iX, iY);
				if (newNote == null && this.lastNote != null) {
                    // check zda jde o presun aktualni poznamky
                    if ( this.lastNote.inSquare(iX - elementPositionX(this.targetObj), iY - elementPositionY(this.targetObj)) ) 
                        newNote = this.lastNote;
                }

				if (newNote != null && newNote == this.lastNote) {
					// click na aktualni poznamku
					this.dragActive(iX, iY);
					this.lastNote.startMove();
				} else {
					if (this.lastNote != null) {
						// odstraneni poznamky pokud neni jiz vytvorena v seznamu
						if (this.findNote(this.lastNote) == -1) {
							this.lastNote.destroy();
							delete this.lastNote;
							this.lastNote = null;
						} else {
							if (this.event_ok()) // potvrdi zmeny
							    return false;
						}
					}
					if (newNote == null) {
						// novy
						this.lastNote = new noteElement(this, this.trueX(iX), this.trueY(iY));
						this.hideNote();
					} else {
						if (this.mode == "writemodify" || newNote.creatorIdent == this.creatorIdent) {
							// existujici
							this.hideNote();
							this.lastNote = newNote;
							this.lastNote.activate();
							this.dragActive(iX, iY);
							this.lastNote.startMove();
						}
					}
				}
				break;
				
			default :
				break;
		}
	}
	
	this.event_up = function(iX, iY) {
		if (this.eventFreezed) return false;

		if (this.lastNote != null) {
			if (!this.lastNote.active && this.lastNote.startSet) {
                if (this.lastNote.endSet) {
                    this.lastNote.finish();
                } else {
                    this.lastNote.destroy();
                    delete this.lastNote;
                    this.lastNote = null;
                }
			}
			
			if (this.dragged) {
                this.lastNote.endMove();
                this.dragDrop();
            }
		}
	}

	this.event_move = function(iX, iY) {
		if (this.eventFreezed) return false;
		
		if (this.lastNote != null) {
			if (this.dragged) {
				// posouvani
				this.lastNote.saveCoords();
				this.lastNote.setMove(this.trueX(iX), this.trueY(iY), this.drag_diffX, this.drag_diffY);
				this.lastNote.saveCallbackCoords();

				this.collideCorrection( this.lastNote );

				this.lastNote.updateObject(false);
				this.lastNote.updateInput();
			} else if (!this.lastNote.active) {
				// nastaveni ukonceni
				this.lastNote.saveCoords();
				this.lastNote.setRelativeEnd(this.trueX(iX), this.trueY(iY));
				this.lastNote.saveCallbackCoords();
				
				this.collideCorrection( this.lastNote );
				this.lastNote.updateObject(false);
			}
		} else {
			switch (this.mode) {
				case "writemodify" :
				case "write" :
				case "read" :
					this.showNote( this.findNoteByCoords(iX, iY) );
					break;
					
				default :
					break;
			}
		}
	}
	
	this.event_ok = function() {
		if (this.eventFreezed) return false;
		
		if (this.lastNote != null) {
			switch (this.mode) {
				case "write" :
				case "writemodify" :
					if (this.lastNote.creatorIdent == this.creatorIdent || this.mode == "writemodify") {
                        var boxPass = true;

                        if (!this.noTextArea) {
                            // pridani poznamky
                            this.inputBox.value = fixStr(this.inputBox.value, 150);

    						if (this.inputBox.value == "") {
                                boxPass = false;
                                alert("Je zapotřebí zadat hodnotu poznámky!");
                            } else {
                                if (this.inputBox.value.length >= 150) {
                                    boxPass = false;
                                    alert("Zadali jste příliš dlouhou poznámku!\r\nMaximální délka poznámky je 150 znaků. (delka vaší poznámky je " + this.inputBox.value.length + " znaků)");
                                }
                            }
                        } // if

                        if (boxPass) {
							this.eventFreeze();
							var index = this.findNote(this.lastNote);
							var cmd   = "";

							if (index != -1) {
								// pocatek update (co updatovat)
								cmd = this.cmd_update + "(" + this.photo_ID + "," + this.lastNote.dataComposeSelf() + ",";
							}

							this.lastNote.value        = this.inputBox.value;
							this.lastNote.creatorIdent = this.creatorIdent;
							this.lastNote.creatorName  = this.creatorName;
							this.lastNote.deactivate();

							if (index != -1) {
								// zaslani update executoru
								cmd += this.lastNote.dataComposeSelf(false) + ")";
								exec.makePost(cmd);
								// ------------------------
							} else {
								this.addNote( this.lastNote ); // pridani do kolekce
								this.lastNote.construct(this.lastNote.relX, this.lastNote.relY, this.lastNote.relEndX, this.lastNote.relEndY);
								// prikaz executoru
								cmd = this.cmd_add + "(" + this.photo_ID + "," + this.lastNote.dataComposeSelf() + ")";
								exec.makePost(cmd);
								// ----------------
							}

							this.lastNote = null;
							return true;
						}
					} else alert("Nepovolený přístup k poznámce");
					break;

				default : 
					alert("Nepovolený přístup k poznámce!");
					break;
			}			
		}
		return false;
	}
	
	this.event_cancel = function() {
		if (this.eventFreezed) return false;

		if (this.lastNote != null) {
			switch (this.mode) {
                case "write" :
				case "writemodify" :
					if (this.lastNote.creatorIdent == this.creatorIdent || this.mode == "writemodify") {
                        switch (this.cmd_rem) {
                            case "note_rem" :
                                // klasicka poznamka
        						var index = this.findNote(this.lastNote);

        						if (index != -1) {
        							this.eventFreeze();

        							// odstraneni ze seznamu
        							this.remNote(index);

        							// prikaz executoru
        							var cmd = this.cmd_rem + "(" + this.photo_ID + "," + this.lastNote.dataComposeSelf(true) + ")";
        							exec.makePost(cmd);
        							// ----------------
        						}

        						this.lastNote.destroy();
        						delete this.lastNote;
        						this.lastNote = null;
        						break;

        					case "photo_crop" :
        					    // zruseni vyberu
        					    dialogs.getDialog("dialog11no").toCancel();
        					    break;
        				}
					} else alert("Nepovolený přístup k poznámce!");
					break;
					
				default : 
					break;
			}
		}
		return false;
	}

    // namapovani objektu
	this.setTargetObj(iTargetObj);
	if (iDecompose != "") this.dataDecompose(iDecompose);
}

