var WIDTH_NORMAL = 4550;
var HEIGHT_NORMAL = 2730;
var MAX_WIDTH = 7280;
var MIN_WIDTH = 910;
var MAX_HEIGHT = 7280;
var MIN_HEIGHT = 910;
var scale = 1;
var textWidth;
var textHeight;
var ratioPixelMM = 100 / 455;
var floor, balcony, scaleTopRight, scaleBottomLeft, scaleBottomRight;
var startX = 400, startY = 300;
var balconyDrop;
var isMoveFloor = false;
var scaleTopLeftBalcony = null, scaleTopRightBalcony = null, scaleBottomLeftBalcony = null, scaleBottomRightBalcony = null;
var widthB = 910, heightB = 910;
var startX_B, startY_B;
var deltaX, deltaY;
var incBtn, desBtn;
var scaleBalcony = false;
var widthText, heightText;
var balconyPos = 4; // 1: left of floor, 2: top of floor, 3: right of floor, 4: bottom of floor
var wallInBalcony = [];
var typeBalcony;
var listLineGrid = [];
var listTextGrid = [];
var zoom = 0.75;
var isZoom = 0;
var isPillar = 0;
var isChoose = 0;

function createText(position, x, y) {
    var strPosition = position + "mm";
    var text = new createjs.Text(strPosition, "14px Arial", "#000");
    text.x = x;
    text.y = y;
    text.textBaseline = "alphabetic";
    return text;
}

function drawLineDash(shape, color, startX, startY, endX, endY) {
    shape.graphics.setStrokeDash([2, 2]);
    shape.graphics.setStrokeStyle(2).beginStroke(color).moveTo(startX, startY).lineTo(endX, endY).endStroke();
}

function getRectInit(stage, width, height, x, y) {
    scale = getScale(width, height);
    var g = new createjs.Graphics().beginStroke('#800000').beginFill('#F8CBAC').drawRect(0, 0, width * scale * 100 / 455, height * scale * 100 / 455);
    var rect = new createjs.Shape(g);
    rect.x = x;
    rect.y = y;
    rect.width = width * scale * 100 / 455;
    rect.height = height * scale * 100 / 455;
    return rect;
}

function getScale(width, height) {
    var scaleX, scaleY, scale;
    scaleX = WIDTH_NORMAL / (width);
    scaleY = HEIGHT_NORMAL / (height);
    var minScale = Math.min(scaleX, scaleY);
    if (minScale < 1) {
        scale = minScale;
    } else {
        scale = 1;
    }
    return scale;
}

// show home screen
function onDisplayScreen(canvas, modal) {
    // hide modal setting
    modal.style.display = "none";

    // show content page
    handleComplete();
    
    // draw init object();
    drawInitObject();

    // show canvas content
    canvas.style.display = 'block';
    
}
function drawInitObject()
{	
	if(initObject.length > 0)
	{
		rotate = parseInt(initObject[0][1]);
		
	    for(var i = 2; i<initObject.length; i++)
	    {
	    	createInitObjectInstance(initObject[i]);
	    }
	    
	    for(var i = 2; i<initObject.length; i++)
	    {
	    	createInitPillarInstance(initObject[i]);
	    }
	    
	    for(var i = 2; i<initObject.length; i++)
	    {
	    	createInitRoofInstance(initObject[i]);
	    }
	    setTransitionState(STATE_TRANSITION_SET_3DVIEW);
    }
}

function drawGridDash(stage, widthFloor, heightFloor) {
    // draw horizon
    var scale = getScale(widthFloor, heightFloor);
    var unit = zoom * scale * 100;
    var shape = new createjs.Shape();
    var value1 = 0;
    for (var j = 0; j < listLineGrid.length; j++) {
        stage.removeChild(listLineGrid[j]);
    }
    for (var j = 0; j < listTextGrid.length; j++) {
        stage.removeChild(listTextGrid[j]);
    }
    for (var i = unit; i < 650; i = i + unit) {
        var text1, text2;
        if (i * 2 >= 100 && i * 2 <= 1030) {
            drawLineDash(shape, '#000000', i * 2, 50, i * 2, 830);
            text2 = createText(value1, i * 2, 830);
            text1 = createText(value1, i * 2, 60);
        } else if (i * 2 > 1030) {
            drawLineDash(shape, '#000000', i * 2, 50, i * 2, 730);
            text1 = createText(value1, i * 2, 60);
        } else {
            drawLineDash(shape, '#000000', i * 2, 355, i * 2, 830);
            text2 = createText(value1, i * 2, 830);
        }
        listLineGrid.push(shape);
        stage.addChild(shape);

        listTextGrid.push(text1);
        stage.addChild(text1);
        listTextGrid.push(text2);
        stage.addChild(text2);

        value1 += 910;

    }

    // draw vertical
    var value2 = 0;
    for (var i = unit; i <= 420; i = i + unit) {
        var text1, text2;
        if (i * 2 >= 350 && i * 2 <= 730) {
            drawLineDash(shape, '#000000', 0, i * 2, 1280, i * 2);
            text1 = createText(value2, 0, i * 2);
            text2 = createText(value2, 1220, i * 2);
        } else if (i * 2 >= 730) {
            drawLineDash(shape, '#000000', 0, i * 2, 1020, i * 2);
            text1 = createText(value2, 0, i * 2);
        } else {
            drawLineDash(shape, '#000000', 100, i * 2, 1280, i * 2);
            text2 = createText(value2, 1220, i * 2);
        }

        listLineGrid.push(shape);
        stage.addChild(shape);
        listTextGrid.push(text1);
        stage.addChild(text1);
        listTextGrid.push(text2);
        stage.addChild(text2);

        value2 += 910;
    }

    var value3 = 455;
    for (var i = 3 * unit; i < 1300; i = i + 2 * unit) {
        var text1, text2;
        if (i >= 100 && i <= 1030) {
            drawLineDash(shape, '#CCCCCC', i, 50, i, 830);
            text2 = createText(value3, i, 830);
            text1 = createText(value3, i, 60);
        } else if (i > 1030) {
            drawLineDash(shape, '#CCCCCC', i, 50, i, 730);
            text1 = createText(value3, i, 60);
        } else {
            drawLineDash(shape, '#CCCCCC', i, 355, i, 830);
            text2 = createText(value3, i, 830);
        }
        listLineGrid.push(shape);
        stage.addChild(shape);

        listTextGrid.push(text1);
        stage.addChild(text1);
        listTextGrid.push(text2);
        stage.addChild(text2);

        value3 += 910;
    }

    // draw vertical
    var value4 = 455;
    for (var i = 3 * unit; i <= 800; i = i + 2 * unit) {

        var text1, text2;
        if (i >= 350 && i <= 730) {
            drawLineDash(shape, '#CCCCCC', 0, i, 1280, i);
            text1 = createText(value4, 0, i);
            text2 = createText(value4, 1220, i);
        } else if (i * 2 >= 730) {
            drawLineDash(shape, '#CCCCCC', 0, i, 1020, i);
            text1 = createText(value4, 0, i);
        } else {
            drawLineDash(shape, '#CCCCCC', 100, i, 1280, i);
            text2 = createText(value4, 1220, i);
        }

        listLineGrid.push(shape);
        stage.addChild(shape);
        listTextGrid.push(text1);
        stage.addChild(text1);
        listTextGrid.push(text2);
        stage.addChild(text2);
        value4 += 910;
    }
}

// ------------------- REFACTOR CODE ------------------------
// draw floor
function drawFloor(x, y, widthFloor, heightFloor, isScaleShape) {
    if (widthFloor <= MIN_WIDTH) {
        widthFloor = MIN_WIDTH;
    }

    if (heightFloor <= MIN_HEIGHT) {
        heightFloor = MIN_HEIGHT;
    }
    if (widthFloor >= MAX_WIDTH && !isScaleShape) {
        widthFloor = MAX_WIDTH;
    }

    if (heightFloor >= MAX_HEIGHT) {
        heightFloor = MAX_HEIGHT;
    }

    var scale = getScale(widthFloor, heightFloor);
    var g = new createjs.Graphics().beginStroke('#800000').beginFill('#F8CBAC').drawRect(0, 0, widthFloor * scale * zoom * 100 / 455, heightFloor * scale * zoom * 100 / 455);
    var floor = new createjs.Shape(g);
    floor.x = x * scale * zoom;
    floor.y = y * scale * zoom;
    floor.width = widthFloor;
    floor.height = heightFloor;

    var floorList = getArray3dCadByType(TYPE_FLOOR);
    var currentFloor = getByTypeAndXY(TYPE_FLOOR, floor.x, floor.y);
    if (currentFloor != null && floorList.length == 1) {
        updateArray3dCad(currentFloor[0][0], null, TYPE_FLOOR, currentFloor[2][0], currentFloor[2][1], currentFloor[2][2], currentFloor[2][3], 0, 0, 0, 0, 0, 0);
    } else {
        for (var i = 0; i < floorList.length; i++) {
            deleteArray3dCad(floorList[i][0][0]);
        }
        setArray3dCad(null, TYPE_FLOOR, x * 455 / 100, y * 455 / 100, widthFloor, heightFloor);
    }
    return floor;
}

// draw balcony
function drawBalcony(x, y, widthFloor, heightFloor, widthBalcony, heightBalcony, oldX, oldY) {
    var position = getPositionBalcony(x, y, widthFloor, heightFloor, widthBalcony, heightBalcony);
    var oldWidthB = widthB;
    var oldHeightB = heightB;
    if (position == 2 || position == 4) {
        widthBalcony = widthFloor;
        heightB = oldHeightB;
    } else {
        heightBalcony = heightFloor;
        widthB = oldWidthB;
    }

    var scale = getScale(widthFloor, heightFloor);
    var g = new createjs.Graphics().beginStroke('#800000').beginFill('#FFD78F').drawRect(0, 0, widthBalcony * scale * zoom * ratioPixelMM, heightBalcony * scale * zoom * ratioPixelMM);
    var balcony = new createjs.Shape(g);
    if (position == 1) {
        balcony.x = (x - widthBalcony) * scale * zoom * 100 / 455;
        balcony.y = y * scale * zoom * 100 / 455;

        var balconyList = getArray3dCadByType(TYPE_BALCONY);
        for (var i = 0; i < balconyList.length; i++) {
            deleteArray3dCad(balconyList[i][0][0]);
        }
        
        var handrailList = getArray3dCadByType(TYPE_HANDRAIL);
        for (var i = 0; i < handrailList.length; i++) {
            deleteArray3dCad(handrailList[i][0][0]);
        }
        
        if (typeBalcony != TYPE_BALCONY_NONE) {
            setArray3dCad(null, TYPE_BALCONY, x - widthBalcony, y, widthBalcony, heightBalcony);
            
            // set handrail
            setArray3dCad(null, TYPE_HANDRAIL, x - widthBalcony , y, widthBalcony, WIDTH_TYPE_HANDRAIL);
            setArray3dCad(null, TYPE_HANDRAIL, x - widthBalcony, y, WIDTH_TYPE_HANDRAIL, heightBalcony );
            setArray3dCad(null, TYPE_HANDRAIL, x - widthBalcony, y+heightBalcony - WIDTH_TYPE_HANDRAIL, widthBalcony, WIDTH_TYPE_HANDRAIL);
        }

    } else if (position == 2) {
        balcony.x = x * scale * zoom * 100 / 455;
        balcony.y = (y - heightBalcony) * scale * zoom * 100 / 455;
        var balconyList = getArray3dCadByType(TYPE_BALCONY);
        for (var i = 0; i < balconyList.length; i++) {
            deleteArray3dCad(balconyList[i][0][0]);
        }
        var handrailList = getArray3dCadByType(TYPE_HANDRAIL);
        for (var i = 0; i < handrailList.length; i++) {
            deleteArray3dCad(handrailList[i][0][0]);
        }
        
        if (typeBalcony != TYPE_BALCONY_NONE) {
            setArray3dCad(null, TYPE_BALCONY, x, y - heightBalcony, widthBalcony, heightBalcony);
            
            // set handrail
            setArray3dCad(null, TYPE_HANDRAIL, x, y - heightBalcony  , widthBalcony, WIDTH_TYPE_HANDRAIL);
            setArray3dCad(null, TYPE_HANDRAIL, x , y - heightBalcony, WIDTH_TYPE_HANDRAIL, heightBalcony);
            setArray3dCad(null, TYPE_HANDRAIL, x+widthBalcony - WIDTH_TYPE_HANDRAIL, y - heightBalcony, WIDTH_TYPE_HANDRAIL, heightBalcony);
        }
        
    } else if (position == 3) {
        balcony.x = (x + widthFloor) * scale * zoom * 100 / 455;
        balcony.y = y * scale * zoom * 100 / 455;

        var balconyList = getArray3dCadByType(TYPE_BALCONY);
        for (var i = 0; i < balconyList.length; i++) {
            deleteArray3dCad(balconyList[i][0][0]);
        }
        var handrailList = getArray3dCadByType(TYPE_HANDRAIL);
        for (var i = 0; i < handrailList.length; i++) {
            deleteArray3dCad(handrailList[i][0][0]);
        }
        
        if (typeBalcony != TYPE_BALCONY_NONE) {
            setArray3dCad(null, TYPE_BALCONY, x + widthFloor, y, widthBalcony, heightBalcony);
                    
            // set handrail
            setArray3dCad(null, TYPE_HANDRAIL, x + widthFloor, y, widthBalcony, WIDTH_TYPE_HANDRAIL);
            setArray3dCad(null, TYPE_HANDRAIL, x + widthFloor + widthBalcony - WIDTH_TYPE_HANDRAIL, y, WIDTH_TYPE_HANDRAIL, heightBalcony);
            setArray3dCad(null, TYPE_HANDRAIL, x + widthFloor, y+heightFloor - WIDTH_TYPE_HANDRAIL, widthBalcony, WIDTH_TYPE_HANDRAIL);
        }
    } else {
        balcony.x = x * scale * zoom * 100 / 455;
        balcony.y = (y + heightFloor ) * scale * zoom * 100 / 455;

        var balconyList = getArray3dCadByType(TYPE_BALCONY);
        for (var i = 0; i < balconyList.length; i++) {
            deleteArray3dCad(balconyList[i][0][0]);
        }
        var handrailList = getArray3dCadByType(TYPE_HANDRAIL);
        for (var i = 0; i < handrailList.length; i++) {
            deleteArray3dCad(handrailList[i][0][0]);
        }
        
        if (typeBalcony != TYPE_BALCONY_NONE) {
            setArray3dCad(null, TYPE_BALCONY, x, y + heightFloor, widthBalcony, heightBalcony);
            
            // set handrail
            setArray3dCad(null, TYPE_HANDRAIL, x, y + heightFloor, WIDTH_TYPE_HANDRAIL, heightBalcony);
            setArray3dCad(null, TYPE_HANDRAIL, x + widthFloor - WIDTH_TYPE_HANDRAIL , y + heightFloor, WIDTH_TYPE_HANDRAIL, heightBalcony);
            setArray3dCad(null, TYPE_HANDRAIL, x, y + heightFloor + heightBalcony - WIDTH_TYPE_HANDRAIL , widthBalcony, WIDTH_TYPE_HANDRAIL);
            
        
        }
    }
    startX_B = balcony.x;
    startY_B = balcony.y;
    balcony.width = widthBalcony;
    balcony.height = heightBalcony;

    return balcony;
}

function drawScale(xFloor, yFloor, width, height, isTop, isRight) {
    var x, y;

    // scale is top left
    if (isTop && !isRight) {
        x = xFloor - 15;
        y = yFloor - 15;
    }

    // scale is top right
    if (isTop && isRight) {
        x = width * zoom * scale * ratioPixelMM + xFloor - 15;
        y = yFloor - 15;
    }

    // scale is bottom left
    if (!isTop && !isRight) {
        x = xFloor - 15;
        y = height * zoom * scale * ratioPixelMM + yFloor - 15;
    }

    // scale is bottom right
    if (!isTop && isRight) {
        x = width * zoom * scale * ratioPixelMM + xFloor - 15;
        y = height * zoom * scale * ratioPixelMM + yFloor - 15;
    }

    var g = new createjs.Graphics().beginStroke('#000000').beginFill('#ffffff').drawRect(x, y, 30, 30).moveTo(x, y).lineTo(x + 30, y + 30).moveTo(x, y).lineTo(x + 5, y + 10).moveTo(x, y).lineTo(x + 10, y + 5).moveTo(x + 30, y + 30).lineTo(x + 25, y + 20).moveTo(x + 30, y + 30).lineTo(x + 20, y + 25).endStroke();
    var scaleShape = new createjs.Shape(g);
    return scaleShape;

}



// screen home page
function handleComplete(isInit) {
    // hide preload
    var preloaderDiv = document.getElementById("_preload_div_");
    preloaderDiv.style.display = 'none';

    // draw content to page
    homePage = new lib._3dhouse();

    // stage handle canvas
    stage = new createjs.Stage(canvas);
    // add content to stage
    //stage.addChild(homePage);
    drawHomepage();
    // get width and height floor(setting - unit mm)
    var floors = searchInitObject(TYPE_FLOOR);
    if( floors.length > 0)
    {
	    widthFloor = parseInt(floors[0][4]);
	    heightFloor = parseInt(floors[0][5]);
	    console.log("widthFloor" + widthFloor);
	    console.log("heightFloor" + heightFloor);
	}
    else
    {
	    widthFloor = document.getElementsByClassName('shapeWidth')[0].value;
	    heightFloor = document.getElementsByClassName('shapeHeight')[0].value;
    }
    typeBalcony = document.querySelector('input[name="wooddeck"]:checked').value;


    var scale = getScale(widthFloor, heightFloor);
    startX_B = startX;
    startY_B = startY + scale * heightFloor * ratioPixelMM;
    deltaX = 0;
    deltaY = scale * heightFloor * ratioPixelMM;
    drawGridDash(stage);
    incBtn = btnzoomIn;
    widthText = widthFloor;
    heightText = heightFloor;
    if (!isZoom) {
        incBtn.addEventListener('click', incZoom);
    }
    desBtn = btnzoomOut;
    if (!isZoom) {
        desBtn.addEventListener('click', desZoom);
    }
    if (isZoom) {
        var opacityShape = new createjs.Shape();
        opacityShape.graphics.setStrokeStyle(padding).beginStroke('#000000').beginFill('#FFFFFF')
            .moveTo(0, 0).lineTo(259.6, 0).lineTo(259.6, 102.6).lineTo(0, 102.6).lineTo(0, 0);
        opacityShape.x = 1019.45;
        opacityShape.y = 727.2;
        opacityShape.alpha = 0.5;
        stage.addChild(opacityShape);
    }
    // draw content canvas
    drawContentCanvas(widthFloor, heightFloor, false);
    createImages(stage);
    // update screen
    stage.update();

    stage.enableMouseOver();
    //Registers the "tick" event listener.
    createjs.Ticker.setFPS(lib.properties.fps);
    createjs.Ticker.addEventListener("tick", stage);
    //Code to support hidpi screens and responsive scaling.
    (function (isResp, respDim, isScale, scaleType) {
        var lastW, lastH, lastS = 1;
        window.addEventListener('resize', resizeCanvas);
        resizeCanvas(canvas);
        function resizeCanvas() {
            var w = lib.properties.width, h = lib.properties.height;
            var iw = window.innerWidth, ih = window.innerHeight;
            var pRatio = window.devicePixelRatio, xRatio = iw / w, yRatio = ih / h, sRatio = 1;
            if (isResp) {
                if ((respDim == 'width' && lastW == iw) || (respDim == 'height' && lastH == ih)) {
                    sRatio = lastS;
                }
                else if (!isScale) {
                    if (iw < w || ih < h)
                        sRatio = Math.min(xRatio, yRatio);
                }
                else if (scaleType == 1) {
                    sRatio = Math.min(xRatio, yRatio);
                }
                else if (scaleType == 2) {
                    sRatio = Math.max(xRatio, yRatio);
                }
            }
            canvas.width = w * pRatio * sRatio;
            canvas.height = h * pRatio * sRatio;
            canvas.style.width = preloaderDiv.style.width = w * sRatio + 'px';
            canvas.style.height = preloaderDiv.style.height = h * sRatio + 'px';
            canvasInit.width = w * pRatio * sRatio;
            canvasInit.height = h * pRatio * sRatio;
            canvasInit.style.width = preloaderDiv.style.width = w * sRatio + 'px';
            canvasInit.style.height = preloaderDiv.style.height = h * sRatio + 'px';
            stage.scaleX = pRatio * sRatio;
            stage.scaleY = pRatio * sRatio;
            lastW = iw;
            lastH = ih;
            lastS = sRatio;
        }
    })(true, 'both', false, 1);
	// draw NextButton
	drawNextButton(stage);
	
	creaetOpacityList();
	
	setTransitionState(STATE_TRANSITION_RESIZE_FLOOR);
    //
    //diablog(stage, 'まず床のサイズを決めてください');
}

function refreshListObject() {
    for (var i = 0; i < arrObjectInBottom.length; i++) {
        stage.removeChild(arrObjectInBottom[i]);
    }
    for (var t = 0; t < arrNewInstanceObject.length; t++) {
        stage.removeChild(arrNewInstanceObject[t]);
    }
    for (var z = 0; z < arrObjectInWall.length; z++) {
        stage.addChild(arrObjectInWall[z]);
    }
    // add new object
    arrObjectInBottom = [];
    var j = 0;
    for (var i = startIndex; i < startIndex + maxNumObject; i++) {
        var object = arrImage[i].clone(true);
        object.width = arrImage[i].width;
        object.height = arrImage[i].height;
        var x = getXValue(arrStandardPoint[i], object, j);
        var y = getYValue(arrStandardPoint[i], object);
        object.x = x;
        object.y = y;
        object.oldX = x;
        object.oldY = y;
        object.oldObject = arraySVGObject[i];
        object.index = i;
        object.standardPoint = arrStandardPoint[i];
        object.typeObject = arrTypeObject[i];
        // mouse down
        object.on("mousedown", function (evt) {
            imageMouseDown(evt, this.oldObject, this.index, this.rotation);
        });
        j++;
        arrObjectInBottom.push(object);
    }
    createListObject();
    stage.update();
}

//#################### START - CHANGE OBJECT WHEN FLOOR SCALE ###########################
// Top-Right scale
function handleScaleTopRight(rectangle, newWidth, scale) {
    for (var i = 0; i < arrObjectInWall.length; i++) {
        var position = getPositionOfRectangle(rectangle.x * 455 / (100 * scale), rectangle.y * 455 / (100 * scale), rectangle.width, rectangle.height,
            arrObjectInWall[i].x * 455 / (100 * scale), arrObjectInWall[i].y * 455 / (100 * scale));
        if (position == 1 || position == 3) {
            // top, bottom
            var floorWidth = rectangle.width * 100 * scale / 455;
            var floorNewWidth = newWidth * 100 * scale / 455;
            var newObjectX = ((arrObjectInWall[i].x - rectangle.x) / floorWidth) * floorNewWidth + rectangle.x;

            // update memory
            var newObject = arrObjectInWall[i];
            newObject.x = newObjectX;
            newObject.oldZoomX = newObject.x;
            arrObjectInWall[i] = newObject;
        } else if (position == 4) {
            var newObject = arrObjectInWall[i];
            newObject.x = rectangle.x + newWidth * 100 * scale / 455;
            newObject.oldZoomX = newObject.x;
            arrObjectInWall[i] = newObject;
        }
    }
    stage.update();
}

function handleScaleBottomLeft(rectangle, newHeight, scale) {
    for (var i = 0; i < arrObjectInWall.length; i++) {
        var position = getPositionOfRectangle(rectangle.x * 455 / (100 * scale), rectangle.y * 455 / (100 * scale), rectangle.width, rectangle.height,
            arrObjectInWall[i].x * 455 / (100 * scale), arrObjectInWall[i].y * 455 / (100 * scale));
        if (position == 2 || position == 4) {
            // left, right
            var floorHeight = rectangle.height * 100 * scale / 455;
            var floorNewHeight = newHeight * 100 * scale / 455;
            var newObjectY = ((arrObjectInWall[i].y - rectangle.y) / floorHeight) * floorNewHeight + rectangle.y;

            // update memory
            var newObject = arrObjectInWall[i];
            newObject.y = newObjectY;
            newObject.oldZoomY = newObject.y;
            arrObjectInWall[i] = newObject;
        } else if (position == 3) {
            var newObject = arrObjectInWall[i];
            newObject.y = rectangle.y + newHeight * 100 * scale / 455;
            newObject.oldZoomY = newObject.y;
            arrObjectInWall[i] = newObject;
        }
    }
    stage.update();
}

function handleScaleBottomRight(rectangle, newWidth, newHeight, scale) {
    for (var i = 0; i < arrObjectInWall.length; i++) {
        var position = getPositionOfRectangle(rectangle.x * 455 / (100 * scale), rectangle.y * 455 / (100 * scale), rectangle.width, rectangle.height,
            arrObjectInWall[i].x * 455 / (100 * scale), arrObjectInWall[i].y * 455 / (100 * scale));
        if (position == 1) {
            // top
            var floorWidth = rectangle.width * 100 * scale / 455;
            var floorNewWidth = newWidth * 100 * scale / 455;
            var newObjectX = ((arrObjectInWall[i].x - rectangle.x) / floorWidth) * floorNewWidth + rectangle.x;

            // update memory
            var newObject = arrObjectInWall[i];
            newObject.x = newObjectX;
            newObject.oldZoomX = newObject.x;
            arrObjectInWall[i] = newObject;
        } else if (position == 2) {
            // left
            var floorHeight = rectangle.height * 100 * scale / 455;
            var floorNewHeight = newHeight * 100 * scale / 455;
            var newObjectY = ((arrObjectInWall[i].y - rectangle.y) / floorHeight) * floorNewHeight + rectangle.y;

            // update memory
            var newObject = arrObjectInWall[i];
            newObject.y = newObjectY;
            newObject.oldZoomY = newObject.y;
            arrObjectInWall[i] = newObject;
        } else if (position == 3) {
            // bottom
            var floorWidth = rectangle.width * 100 * scale / 455;
            var floorNewWidth = newWidth * 100 * scale / 455;
            var newObjectX = ((arrObjectInWall[i].x - rectangle.x) / floorWidth) * floorNewWidth + rectangle.x;

            // update memory
            var newObject = arrObjectInWall[i];
            newObject.x = newObjectX;
            newObject.y = rectangle.y + newHeight * 100 * scale / 455;
            newObject.oldZoomX = newObject.x;
            newObject.oldZoomY = newObject.y;
            arrObjectInWall[i] = newObject;
        } else if (position == 4) {
            // right
            var floorHeight = rectangle.height * 100 * scale / 455;
            var floorNewHeight = newHeight * 100 * scale / 455;
            var newObjectY = ((arrObjectInWall[i].y - rectangle.y) / floorHeight) * floorNewHeight + rectangle.y;

            // update memory
            var newObject = arrObjectInWall[i];
            newObject.x = rectangle.x + newWidth * 100 * scale / 455;
            newObject.y = newObjectY;
            newObject.oldZoomX = newObject.x;
            newObject.oldZoomY = newObject.y;
            arrObjectInWall[i] = newObject;
        }
    }
    stage.update();
}

function handleZoom() {
    var length = arrObjectInWall.length;
    for (var i = 0; i < arrObjectInWall.length; i++) {
        arrObjectInWall[i].x = arrObjectInWall[i].oldZoomX * zoom;
        arrObjectInWall[i].y = arrObjectInWall[i].oldZoomY * zoom;

        arrObjectInWall[i].scaleX = zoom;
        arrObjectInWall[i].scaleY = zoom;
    }
    stage.update();
}

function handleMoveFloor(oldFloor, newX, newY) {
    for (var i = 0; i < arrObjectInWall.length; i++) {
        var position = getPositionOfRectangle(oldFloor.x * 455 / 100, oldFloor.y * 455 / 100, oldFloor.width, oldFloor.height,
            arrObjectInWall[i].x * 455 / 100, arrObjectInWall[i].y * 455 / 100);
        if (position == 1) {
            //top
            // update memory
            var newObject = arrObjectInWall[i];
            newObject.x = arrObjectInWall[i].x - oldFloor.x + newX;
            newObject.y = newY;
            newObject.oldZoomX = newObject.x;
            newObject.oldZoomY = newObject.y;
            arrObjectInWall[i] = newObject;
            arrObjectInWall[i].visible = true;
        } else if (position == 2) {
            // left
            // update memory
            var newObject = arrObjectInWall[i];
            newObject.x = newX;
            newObject.y = arrObjectInWall[i].y - oldFloor.y + newY;
            newObject.oldZoomX = newObject.x;
            newObject.oldZoomY = newObject.y;
            arrObjectInWall[i] = newObject;
            arrObjectInWall[i].visible = true;
        } else if (position == 3) {
            // bottom
            // update memory
            var newObject = arrObjectInWall[i];
            newObject.x = arrObjectInWall[i].x - oldFloor.x + newX;
            newObject.y = arrObjectInWall[i].y + newY - oldFloor.y;
            newObject.oldZoomX = newObject.x;
            newObject.oldZoomY = newObject.y;
            arrObjectInWall[i] = newObject;
            arrObjectInWall[i].visible = true;
        } else if (position == 4) {
            // right
            // update memory
            var newObject = arrObjectInWall[i];
            newObject.x = arrObjectInWall[i].x - oldFloor.x + newX;
            newObject.y = arrObjectInWall[i].y - oldFloor.y + newY;
            newObject.oldZoomX = newObject.x;
            newObject.oldZoomY = newObject.y;
            arrObjectInWall[i] = newObject;
            arrObjectInWall[i].visible = true;
        }
    }
    stage.update();

}

// Return 1: Top, 2: Left, 3: Bottom, 4: Right
function getPositionOfRectangle(x, y, width, height, xObject, yObject) {
    if (x < xObject && xObject < x + width && Math.abs(yObject - y) <= 150) {
        return 1;
    } else if (Math.abs(xObject - x) <= 150 && y < yObject && yObject < y + height) {
        return 2;
    } else if (x < xObject && xObject < x + width && Math.abs(yObject - y - height) <= 150) {
        return 3;
    } else if (Math.abs(xObject - x - width) <= 150 && y < yObject && yObject < y + height) {
        return 4;
    } else {
        return 0;
    }
}

//#################### END - CHANGE OBJECT WHEN FLOOR SCALE ###########################

function drawScaleFloor(floor) {
    // draw scale in top right
    scale = getScale(floor.width, floor.height);
    scaleTopRight = drawScale(floor.x, floor.y, floor.width, floor.height, true, true);
    stage.addChild(scaleTopRight);

    // draw scale in bottom left
    scaleBottomLeft = drawScale(floor.x, floor.y, floor.width, floor.height, false, false);
    stage.addChild(scaleBottomLeft);

    // draw scale in bottom right
    scaleBottomRight = drawScale(floor.x, floor.y, floor.width, floor.height, false, true);
    stage.addChild(scaleBottomRight);

    // scale floor to right
    scaleTopRight.on('pressmove', function (event) {
        if ( transitionState >= STATE_TRANSITION_SET_OBJECT ) return ; 
        if (!isZoom) {
            cleanObjectWhenScale(stage);
            // re-draw floor
            var scale = getScale(floor.width, floor.height);
            var newWidth = (event.localX - floor.x) * 455 / (100 * scale);
            scaleTopRight.visible = false;
            scaleBottomRight.visible = false;
            scaleBottomLeft.visible = false;
            var position = getPositionBalcony(floor.x, floor.y, floor.width, floor.height, balcony.width, balcony.height);
            if (position == 3) {
                deltaX = floor.x - event.localX;
            }
            if (position == 4) {
                var numberScale = Math.round(newWidth / 455);
                scale = getScale(455 * numberScale, floor.height);
                deltaY = scale * floor.height * ratioPixelMM;
            }
            widthText = newWidth;

            // change floor size
            var floorList = getArray3dCadByType(TYPE_FLOOR);
            for (var i = 0; i < floorList.length; i++) {
                deleteArray3dCad(floorList[i][0][0]);
            }
            setArray3dCad(null, TYPE_FLOOR, floor.x * 455 / 100, floor.y * 455 / 100, newWidth * 1, floor.height * 1);
            // change balcony
            var balconyList = getArray3dCadByType(TYPE_BALCONY);
            for (var i = 0; i < balconyList.length; i++) {
                deleteArray3dCad(balconyList[i][0][0]);
            }

            drawContentCanvas(newWidth, floor.height);
        }
    });

    scaleTopRight.on('pressup', function (event) {
        if ( transitionState >= STATE_TRANSITION_SET_OBJECT ) return ; 
        if (!isZoom) {
           // diablog(stage, '柱を立ててください');
            cleanObjectWhenScale(stage);
            // re-draw floor
            var scale = getScale(floor.width, floor.height);
            var newWidth = (event.localX - floor.x) * 455 / (100 * scale);
            var numberScale = Math.round(newWidth / 455);
            scaleTopRight.visible = false;
            scaleBottomRight.visible = false;
            scaleBottomLeft.visible = false;
            scale = getScale(455 * numberScale, floor.height);
            var position = getPositionBalcony(floor.x, floor.y, floor.width, floor.height, balcony.width, balcony.height);
            if (position == 3) {
                deltaX = -455 * numberScale * ratioPixelMM * scale;
            }
            if (position == 4) {
                var numberScale = Math.round(newWidth / 455);
                scale = getScale(455 * numberScale, floor.height);
                deltaY = scale * floor.height * ratioPixelMM;
            }
            widthText = 455 * numberScale;

            // change floor size
            var floorList = getArray3dCadByType(TYPE_FLOOR);
            for (var i = 0; i < floorList.length; i++) {
                deleteArray3dCad(floorList[i][0][0]);
            }
            setArray3dCad(null, TYPE_FLOOR, floor.x * 455 / 100, floor.y * 455 / 100, 455 * numberScale, floor.height);
            // change balcony
            var balconyList = getArray3dCadByType(TYPE_BALCONY);
            for (var i = 0; i < balconyList.length; i++) {
                deleteArray3dCad(balconyList[i][0][0]);
            }

            handleScaleTopRight(floor, 455 * numberScale, scale);
            drawContentCanvas(455 * numberScale, floor.height);
            refreshListObject();
        }
    });

    // scale floor bottom left
    scaleBottomLeft.on('pressmove', function (event) {
        if ( transitionState >= STATE_TRANSITION_SET_OBJECT ) return ; 
        if (!isZoom) {
            cleanObjectWhenScale(stage);
            // re-draw floor
            var scale = getScale(floor.width, floor.height);
            var newHeight = (event.localY - floor.y) * 455 / (100 * scale);
            scaleTopRight.visible = false;
            scaleBottomRight.visible = false;
            scaleBottomLeft.visible = false;
            var position = getPositionBalcony(floor.x, floor.y, floor.width, floor.height, balcony.width, balcony.height);
            if (position == 3) {
                var numberScale = Math.round(newHeight / 455);
                var newScale = getScale(floor.width, 455 * numberScale);
                deltaX = -floor.width * ratioPixelMM * newScale;
            }
            if (newHeight <= MIN_HEIGHT) {
                deltaY = 0;
                newHeight = MIN_HEIGHT;
            }
            var heightBalcony = balcony.height * newScale * ratioPixelMM;
            var heightFloor = newHeight * ratioPixelMM * newScale;
            if (heightBalcony + balcony.y >= floor.y + heightFloor) {
                deltaY = heightFloor - heightBalcony;
            }

            if (position == 4) {
                deltaY = event.localY - floor.y;
            }
            heightText = newHeight;

            // change floor size
            var floorList = getArray3dCadByType(TYPE_FLOOR);
            for (var i = 0; i < floorList.length; i++) {
                deleteArray3dCad(floorList[i][0][0]);
            }
            setArray3dCad(null, TYPE_FLOOR, floor.x * 455 / 100, floor.y * 455 / 100, floor.width, newHeight);
            // change balcony
            var balconyList = getArray3dCadByType(TYPE_BALCONY);
            for (var i = 0; i < balconyList.length; i++) {
                deleteArray3dCad(balconyList[i][0][0]);
            }

            drawContentCanvas(floor.width, newHeight);
        }
    });

    scaleBottomLeft.on('pressup', function (event) {
        if ( transitionState >= STATE_TRANSITION_SET_OBJECT ) return ; 
        if (!isZoom) {
            //diablog(stage, '柱を立ててください');
            cleanObjectWhenScale(stage);
            // re-draw floor
            var scale = getScale(floor.width, floor.height);
            var newHeight = (event.localY - floor.y) * 455 / (100 * scale);
            var numberScale = Math.round(newHeight / 455);
            scaleTopRight.visible = false;
            scaleBottomRight.visible = false;
            scaleBottomLeft.visible = false;
            var position = getPositionBalcony(floor.x, floor.y, floor.width, floor.height, balcony.width, balcony.height);
            if (position == 3) {
                var numberScale = Math.round(newHeight / 455);
                var newScale = getScale(floor.width, 455 * numberScale);
                deltaX = -floor.width * ratioPixelMM * newScale;
            }
            if (position == 4) {
                var numberScale = Math.round(newHeight / 455);
                scale = getScale(floor.width, 455 * numberScale);
                deltaY = 455 * numberScale * ratioPixelMM * scale;
            }
            heightText = 455 * numberScale;

            // change floor size
            var floorList = getArray3dCadByType(TYPE_FLOOR);
            for (var i = 0; i < floorList.length; i++) {
                deleteArray3dCad(floorList[i][0][0]);
            }
            setArray3dCad(null, TYPE_FLOOR, floor.x * 455 / 100, floor.y * 455 / 100, floor.width, 455 * numberScale);
            // change balcony
            var balconyList = getArray3dCadByType(TYPE_BALCONY);
            for (var i = 0; i < balconyList.length; i++) {
                deleteArray3dCad(balconyList[i][0][0]);
            }

            handleScaleBottomLeft(floor, 455 * numberScale, scale);
            drawContentCanvas(floor.width, numberScale * 455);
            refreshListObject();
        }
    });

    // scale floor bottom right
    scaleBottomRight.on('pressmove', function (event) {
        if ( transitionState >= STATE_TRANSITION_SET_OBJECT ) return ; 
        if (!isZoom) {
            cleanObjectWhenScale(stage);
            // re-draw floor
            var scale = getScale(floor.width, floor.height);
            var newWidth = (event.localX - floor.x) * 455 / (100 * scale);
            var newHeight = (event.localY - floor.y) * 455 / (100 * scale);
            // re-draw floor
            var scale = getScale(floor.width, floor.height);
            var newHeight = (event.localY - floor.y) * 455 / (100 * scale);
            scaleTopRight.visible = false;
            scaleBottomRight.visible = false;
            scaleBottomLeft.visible = false;
            var numberScale = Math.round(newHeight / 455);
            var position = getPositionBalcony(floor.x, floor.y, floor.width, floor.height, balcony.width, balcony.height);
            if (position == 3) {
                deltaX = floor.x - event.localX;
            }
            if (position == 4) {
                deltaY = event.localY - floor.y;
            }

            widthText = newWidth;
            heightText = newHeight;

            // change floor size
            var floorList = getArray3dCadByType(TYPE_FLOOR);
            for (var i = 0; i < floorList.length; i++) {
                deleteArray3dCad(floorList[i][0][0]);
            }
            setArray3dCad(null, TYPE_FLOOR, floor.x * 455 / 100, floor.y * 455 / 100, newWidth, newHeight);
            // change balcony
            var balconyList = getArray3dCadByType(TYPE_BALCONY);
            for (var i = 0; i < balconyList.length; i++) {
                deleteArray3dCad(balconyList[i][0][0]);
            }

            drawContentCanvas(newWidth, newHeight);
        }
    });

    scaleBottomRight.on('pressup', function (event) {
        if ( transitionState >= STATE_TRANSITION_SET_OBJECT ) return ; 
        if (!isZoom) {
            //diablog(stage, '柱を立ててください');
            cleanObjectWhenScale(stage);
            // re-draw floor
            var scale = getScale(floor.width, floor.height);
            var newWidth = (event.localX - floor.x) * 455 / (100 * scale);
            var newHeight = (event.localY - floor.y) * 455 / (100 * scale);
            // re-draw floor
            var numberScaleX = Math.round(newWidth / 455);
            var numberScaleY = Math.round(newHeight / 455);
            scaleTopRight.visible = false;
            scaleBottomRight.visible = false;
            scaleBottomLeft.visible = false;
            var position = getPositionBalcony(floor.x, floor.y, floor.width, floor.height, balcony.width, balcony.height);
            if (position == 3) {
                newScale = getScale(455 * numberScaleX, 455 * numberScaleY);
                deltaX = -455 * numberScaleX * ratioPixelMM * newScale;
            }
            if (position == 4) {
                var numberScale = Math.round(newHeight / 455);
                scale = getScale(floor.width, 455 * numberScale);
                deltaY = 455 * numberScale * ratioPixelMM * scale;
            }
            widthText = numberScaleX * 455;
            heightText = numberScaleY * 455;

            // change floor size
            var floorList = getArray3dCadByType(TYPE_FLOOR);
            for (var i = 0; i < floorList.length; i++) {
                deleteArray3dCad(floorList[i][0][0]);
            }
            setArray3dCad(null, TYPE_FLOOR, floor.x * 455 / 100, floor.y * 455 / 100, 455 * numberScaleX, 455 * numberScaleY);
            // change balcony
            var balconyList = getArray3dCadByType(TYPE_BALCONY);
            for (var i = 0; i < balconyList.length; i++) {
                deleteArray3dCad(balconyList[i][0][0]);
            }

            handleScaleBottomRight(floor, numberScaleX * 455, numberScaleY * 455, scale);
            drawContentCanvas(numberScaleX * 455, numberScaleY * 455);
            refreshListObject();
        }
    });
}

function drawContentCanvas(widthFloor, heightFloor, isScaleShape) {
    widthFloor = parseInt(widthFloor);
    heightFloor = parseInt(heightFloor);
    // draw Floor
    if (floor) {
        stage.removeChild(floor);
    }
    drawGridDash(stage, widthFloor, heightFloor);
    floor = drawFloor(startX, startY, widthFloor, heightFloor, isScaleShape);
    stage.addChild(floor);

    // draw balcony
    if (balcony) {
        stage.removeChild(balcony);
    }
    var floorInMemory = getArray3dCadByType(1)[0][2];
    balcony = drawBalcony(floorInMemory[0], floorInMemory[1], floor.width, floor.height, widthB, heightB);
    var balconyInMemory = [];
    if (typeBalcony != TYPE_BALCONY_NONE) {
        stage.addChild(balcony);
        balconyInMemory = getArray3dCadByType(2)[0][2];
    }

    drawText(floor.width, floor.height, isScaleShape);

    // add wall and pillar
    createWallInMemory(floorInMemory[0], floorInMemory[1], floorInMemory[2], floorInMemory[3],
        balconyInMemory[0], balconyInMemory[1], balconyInMemory[2], balconyInMemory[3]);
    scale = getScale(floor.width, floor.height);
   // handleWallAndPillar(stage, zoom);
    // add object to wall from memory
    for (var i = 0; i < arrObjectInWall.length; i++) {
        stage.addChild(arrObjectInWall[i]);
        if (!getByTypeAndXY(arrObjectInWall[i].oldObject[2], arrObjectInWall[i].x, arrObjectInWall[i].y)) {
            setArray3dCad(arrObjectInWall[i].oldObject[1], arrObjectInWall[i].oldObject[2], event.localX, event.localY,
                arrObjectInWall[i].oldObject[3], arrObjectInWall[i].oldObject[4], 0, 0, 0, 0, 0, 0);
        }
    }

    isMoveFloor = false;

    // draw scale for floor
    drawScaleFloor(floor);
    addEventFloor();

    balcony.oldX = balcony.x;
    balcony.oldY = balcony.y;
    // drag and drop balcony
    balcony.addEventListener("pressmove", function (event) {
        if ( transitionState >= STATE_TRANSITION_SET_OBJECT ) return ; 
        if (!isZoom) {

            cleanObjectWhenScale(stage);
            var x = event.stageX;
            var y = event.stageY;
            event.currentTarget.x = x;
            event.currentTarget.y = y;
            scaleTopRight.visible = false;
            scaleBottomLeft.visible = false;
            scaleBottomRight.visible = false;
            if (scaleTopLeftBalcony) {
                scaleTopLeftBalcony.visible = false;
                scaleTopRightBalcony.visible = false;
                scaleBottomLeftBalcony.visible = false;
                scaleBottomRightBalcony.visible = false;
            }

            // var scale = getScale(floor.width, floor.height);
            var balconyWidth = zoom * scale * event.currentTarget.width * ratioPixelMM;
            var balconyHeight = zoom * scale * event.currentTarget.height * ratioPixelMM;

            var floorWidth = zoom * scale * floor.width * ratioPixelMM;
            var floorHeight = zoom * scale * floor.height * ratioPixelMM;

            // check point to drop balcony
            if (x + balconyWidth <= floor.x && y >= floor.y - balconyHeight && y <= floor.y + floorHeight) {
                stage.removeChild(balconyDrop);
                drawBalconyDrop(floor.x - balconyWidth, y, balconyWidth, balconyHeight);
                balconyPos = 1;
            } else if (x >= floor.x - balconyWidth && x <= floor.x + floorWidth && y + balconyHeight <= floor.y) {
                stage.removeChild(balconyDrop);
                drawBalconyDrop(x, floor.y - balconyHeight, balconyWidth, balconyHeight);
                balconyPos = 2;
            } else if (x >= floor.x + floorWidth && y >= floor.y - balconyHeight && y <= floor.y + floorHeight) {
                stage.removeChild(balconyDrop);
                drawBalconyDrop(floor.x + floorWidth, y, balconyWidth, balconyHeight);
                balconyPos = 3;
            } else if (x >= floor.x - balconyWidth && x <= floor.x + floorWidth && y >= floorHeight + floor.y) {
                stage.removeChild(balconyDrop);
                drawBalconyDrop(x, floor.y + floorHeight, balconyWidth, balconyHeight);
                balconyPos = 4;
            } else {
                stage.removeChild(balconyDrop);
            }
            startX_B = x;
            startY_B = y;

            scaleTopLeftBalcony = drawScale(startX_B, startY_B, event.currentTarget.width, event.currentTarget.height, true, false);
            stage.addChild(scaleTopLeftBalcony);

            scaleTopRightBalcony = drawScale(startX_B, startY_B, event.currentTarget.width, event.currentTarget.height, true, true);
            stage.addChild(scaleTopRightBalcony);

            scaleBottomLeftBalcony = drawScale(startX_B, startY_B, event.currentTarget.width, event.currentTarget.height, false, false);
            stage.addChild(scaleBottomLeftBalcony);

            scaleBottomRightBalcony = drawScale(startX_B, startY_B, event.currentTarget.width, event.currentTarget.height, false, true);
            stage.addChild(scaleBottomRightBalcony);
        }
    });

    // drag and drop balcony


    balcony.addEventListener("pressup", function (event) {
        if ( transitionState >= STATE_TRANSITION_SET_OBJECT ) return ; 
        if (!isZoom) {
            //diablog(stage, '柱を立ててください');
            cleanObjectWhenScale(stage);
            var x = event.stageX;
            var y = event.stageY;
            event.currentTarget.x = x;
            event.currentTarget.y = y;
            if (scaleTopLeftBalcony) {
                scaleTopLeftBalcony.visible = false;
                scaleTopRightBalcony.visible = false;
                scaleBottomLeftBalcony.visible = false;
                scaleBottomRightBalcony.visible = false;
            }

            var balconyWidth = zoom * scale * event.currentTarget.width * ratioPixelMM;
            var balconyHeight = zoom * scale * event.currentTarget.height * ratioPixelMM;

            var floorWidth = zoom * scale * floor.width * ratioPixelMM;
            var floorHeight = zoom * scale * floor.height * ratioPixelMM;

            // check point to drop balcony
            if (x + balconyWidth <= floor.x && y >= floor.y - balconyHeight && y <= floor.y + floorHeight) {
                stage.removeChild(balconyDrop);
                deltaX = balconyWidth;
                if (y >= floor.y) {
                    if (y - floor.y >= floorHeight - balconyHeight) {
                        deltaY = floorHeight - balconyHeight;
                    } else {
                        deltaY = y - floor.y;
                    }
                } else {
                    deltaY = 0;
                }
                startX_B = floor.x - balconyWidth;
                startY_B = y;
                event.currentTarget.oldX = floor.x - balconyWidth;
                event.currentTarget.oldY = y;
                drawContentCanvas(floor.width, floor.height);
                scaleTopRight.visible = false;
                scaleBottomLeft.visible = false;
                scaleBottomRight.visible = false;
            } else if (x >= floor.x - balconyWidth && x <= floor.x + floorWidth && y + balconyHeight <= floor.y) {
                stage.removeChild(balconyDrop);
                if (x <= floor.x) {
                    deltaX = 0;
                } else if (x - floor.x >= floorWidth - balconyWidth) {
                    deltaX = balconyWidth - floorWidth;
                } else {
                    deltaX = floor.x - x;
                }
                deltaY = -balconyHeight;
                startX_B = x;
                startY_B = floor.y - balconyHeight;
                event.currentTarget.oldX = x;
                event.currentTarget.oldY = floor.y - balconyHeight;
                drawContentCanvas(floor.width, floor.height);
                scaleTopRight.visible = false;
                scaleBottomLeft.visible = false;
                scaleBottomRight.visible = false;
            } else if (x >= floor.x + floorWidth && y >= floor.y - balconyHeight && y <= floor.y + floorHeight) {
                stage.removeChild(balconyDrop);
                deltaX = -floorWidth;
                if (y >= floor.y) {
                    if (y - floor.y >= floorHeight - balconyHeight) {
                        deltaY = floorHeight - balconyHeight;
                    } else {
                        deltaY = y - floor.y;
                    }
                } else {
                    deltaY = 0;
                }
                startX_B = floor.x + floorWidth;
                startY_B = y;
                event.currentTarget.oldX = floor.x + floorWidth;
                event.currentTarget.oldY = y;
                drawContentCanvas(floor.width, floor.height);
                scaleTopRight.visible = false;
                scaleBottomLeft.visible = false;
                scaleBottomRight.visible = false;
            } else if (x >= floor.x - balconyWidth && x <= floor.x + floorWidth && y >= floorHeight + floor.y) {
                stage.removeChild(balconyDrop);
                if (x <= floor.x) {
                    deltaX = 0;
                } else if (x - floor.x >= floorWidth - balconyWidth) {
                    deltaX = balconyWidth - floorWidth;
                } else {
                    deltaX = floor.x - x;
                }
                deltaY = floorHeight;
                startX_B = x;
                startY_B = floor.y + floorHeight;
                event.currentTarget.oldX = x;
                event.currentTarget.oldY = floor.y + floorHeight;
                drawContentCanvas(floor.width, floor.height);
                scaleTopRight.visible = false;
                scaleBottomLeft.visible = false;
                scaleBottomRight.visible = false;
            } else {
                stage.removeChild(balconyDrop);
                event.currentTarget.x = event.currentTarget.oldX;
                event.currentTarget.y = event.currentTarget.oldY;
                startX_B = event.currentTarget.oldX;
                startY_B = event.currentTarget.oldY;
                drawContentCanvas(floor.width, floor.height);
                scaleTopRight.visible = false;
                scaleBottomLeft.visible = false;
                scaleBottomRight.visible = false;
            }

            scaleTopLeftBalcony = drawScale(startX_B, startY_B, balcony.width, balcony.height, true, false);
            stage.addChild(scaleTopLeftBalcony);

            scaleTopRightBalcony = drawScale(startX_B, startY_B, balcony.width, balcony.height, true, true);
            stage.addChild(scaleTopRightBalcony);

            scaleBottomLeftBalcony = drawScale(startX_B, startY_B, balcony.width, balcony.height, false, false);
            stage.addChild(scaleBottomLeftBalcony);

            scaleBottomRightBalcony = drawScale(startX_B, startY_B, balcony.width, balcony.height, false, true);
            stage.addChild(scaleBottomRightBalcony);

            scaleTopLeftBalcony.addEventListener("pressmove", function (event) {
                cleanObjectWhenScale(stage);
                startX_B = event.stageX;
                drawContentCanvas(floor.width, floor.height);
            });

            scaleBottomLeftBalcony.addEventListener("pressmove", function (event) {
                cleanObjectWhenScale(stage);
            });
        }
    });

    balcony.addEventListener("mousedown", function (event) {
        if ( transitionState >= STATE_TRANSITION_SET_OBJECT ) return ; 
        if (!isZoom) {
            scaleBalcony = true;
            cleanObjectWhenScale(stage);
            scaleTopRight.visible = false;
            scaleBottomLeft.visible = false;
            scaleBottomRight.visible = false;

            if (scaleTopLeftBalcony) {
                scaleTopLeftBalcony.visible = true;
                scaleTopRightBalcony.visible = true;
                scaleBottomLeftBalcony.visible = true;
                scaleBottomRightBalcony.visible = true;
            }
        }
    });
}


var createArrayWallInBalcony = function (no, fileName, type, start_X, start_Y, width_X, width_Y, height_startpoint, height_endpoint, widthObject_x, widthObject_y, widthObject_r, pointObject_r) {
    array3dCad0 = [no];
    array3dCad1 = [type];
    array3dCad2 = [start_X, start_Y, width_X, width_Y, height_startpoint, height_endpoint];
    array3dCad3 = [fileName, widthObject_x, widthObject_y, widthObject_r, pointObject_r];

    if (typeBalcony == TYPE_BALCONY_HASROOF) {
        wallInBalcony.push([array3dCad0, array3dCad1, array3dCad2, array3dCad3]);
    }

}

function createWallInMemory(xFloor, yFloor, widthFloor, heightFloor, xBalcony, yBalcony, widthBalcony, heightBalcony) {
    cleanObjectWhenScale(stage);
    wallInBalcony = [];
    if (xBalcony == null || yBalcony == null || widthBalcony == null || heightBalcony == null) {
        setArray3dCad(null, TYPE_WALL, xFloor, yFloor, widthFloor, 18, 0, 0, 0, 0, 0, 0);
        setArray3dCad(null, TYPE_WALL, xFloor, yFloor, 18, heightFloor, 0, 0, 0, 0, 0, 0);
        setArray3dCad(null, TYPE_WALL, xFloor + widthFloor, yFloor, 18, heightFloor, 0, 0, 0, 0, 0, 0);
        setArray3dCad(null, TYPE_WALL, xFloor, yFloor + heightFloor, widthFloor, 18, 0, 0, 0, 0, 0, 0);
    } else {
        if (yBalcony + heightBalcony == yFloor) {
            // balcony at the top of floor
            setArray3dCad(null, TYPE_WALL, xFloor, yFloor, 18, heightFloor, 0, 0, 0, 0, 0, 0);
            setArray3dCad(null, TYPE_WALL, xFloor, yFloor + heightFloor, widthFloor, 18, 0, 0, 0, 0, 0, 0);
            setArray3dCad(null, TYPE_WALL, xFloor + widthFloor, yFloor, 18, heightFloor, 0, 0, 0, 0, 0, 0);
            if (xBalcony - xFloor > 0) {
                setArray3dCad(null, TYPE_WALL, xFloor, yFloor, xBalcony - xFloor, 18, 0, 0, 0, 0, 0, 0);
            }
            setArray3dCad(null, TYPE_WALL, xBalcony, yBalcony + heightBalcony, widthBalcony, 18, 0, 0, 0, 0, 0, 0);

            // wall in balcony
            createArrayWallInBalcony(1, null, TYPE_WALL, xBalcony, yBalcony, 18, heightBalcony, 0, 0, 0, 0, 0, 0);
            createArrayWallInBalcony(2, null, TYPE_WALL, xBalcony, yBalcony, widthBalcony, 18, 0, 0, 0, 0, 0, 0);
            createArrayWallInBalcony(3, null, TYPE_WALL, xBalcony + widthBalcony, yBalcony, 18, heightBalcony, 0, 0, 0, 0, 0, 0);

            if (xFloor + widthFloor - xBalcony - widthBalcony > 0) {
                setArray3dCad(null, TYPE_WALL, xFloor + widthBalcony, yFloor, xFloor + widthFloor - xBalcony - widthBalcony, 18, 0, 0, 0, 0, 0, 0);
            }
        } else if (xFloor + widthFloor == xBalcony) {
            // balcony at the right of floor
            setArray3dCad(null, TYPE_WALL, xFloor, yFloor, widthFloor, 18, 0, 0, 0, 0, 0, 0);
            setArray3dCad(null, TYPE_WALL, xFloor, yFloor, 18, heightFloor, 0, 0, 0, 0, 0, 0);
            setArray3dCad(null, TYPE_WALL, xFloor, yFloor + heightFloor, widthFloor, 18, 0, 0, 0, 0, 0, 0);
            if (yFloor - yBalcony > 0) {
                setArray3dCad(null, TYPE_WALL, xFloor + widthFloor, yFloor, 18, yFloor - yBalcony, 0, 0, 0, 0, 0, 0);
            }
            setArray3dCad(null, TYPE_WALL, xBalcony, yBalcony, 18, heightBalcony, 0, 0, 0, 0, 0, 0);

            // wall in balcony
            createArrayWallInBalcony(1, null, TYPE_WALL, xBalcony, yBalcony, widthBalcony, 18, 0, 0, 0, 0, 0, 0);
            createArrayWallInBalcony(2, null, TYPE_WALL, xBalcony + widthBalcony, yBalcony, 18, heightBalcony, 0, 0, 0, 0, 0, 0);
            createArrayWallInBalcony(3, null, TYPE_WALL, xBalcony, yBalcony + heightBalcony, widthBalcony, 18, 0, 0, 0, 0, 0, 0);

            if (yFloor + heightFloor - yBalcony - heightBalcony > 0) {
                setArray3dCad(null, TYPE_WALL, xBalcony, yBalcony + heightBalcony, 18, yFloor + heightFloor - yBalcony - heightBalcony, 0, 0, 0, 0, 0, 0);
            }
        } else if (yFloor + heightFloor == yBalcony) {
            // balcony at the bottom of floor
            setArray3dCad(null, TYPE_WALL, xFloor, yFloor, widthFloor, 18, 0, 0, 0, 0, 0, 0);
            setArray3dCad(null, TYPE_WALL, xFloor, yFloor, 18, heightFloor, 0, 0, 0, 0, 0, 0);
            setArray3dCad(null, TYPE_WALL, xFloor + widthFloor, yFloor, 18, heightFloor, 0, 0, 0, 0, 0, 0);
            if (xBalcony - xFloor > 0) {
                setArray3dCad(null, TYPE_WALL, xFloor, yFloor + heightFloor, xBalcony - xFloor, 18, 0, 0, 0, 0, 0, 0);
            }
            setArray3dCad(null, TYPE_WALL, xBalcony, yBalcony, widthBalcony, 18, 0, 0, 0, 0, 0, 0);

            // wall in balcony
            createArrayWallInBalcony(1, null, TYPE_WALL, xBalcony, yBalcony, 18, heightBalcony, 0, 0, 0, 0, 0, 0);
            createArrayWallInBalcony(2, null, TYPE_WALL, xBalcony, yBalcony + heightBalcony, widthBalcony, 18, 0, 0, 0, 0, 0, 0);
            createArrayWallInBalcony(3, null, TYPE_WALL, xBalcony + widthBalcony, yBalcony, 18, heightBalcony, 0, 0, 0, 0, 0, 0);

            if (xFloor + widthFloor - xBalcony - widthBalcony > 0) {
                setArray3dCad(null, TYPE_WALL, xBalcony + widthBalcony, yBalcony, xFloor + widthFloor - xBalcony - widthBalcony, 18, 0, 0, 0, 0, 0, 0);
            }
        } else if (xFloor - widthBalcony == xBalcony) {
            // balcony at the left of floor
            setArray3dCad(null, TYPE_WALL, xFloor, yFloor, widthFloor, 18, 0, 0, 0, 0, 0, 0);
            setArray3dCad(null, TYPE_WALL, xFloor + widthFloor, yFloor, 18, heightFloor, 0, 0, 0, 0, 0, 0);
            setArray3dCad(null, TYPE_WALL, xFloor, yFloor + heightFloor, widthFloor, 18, 0, 0, 0, 0, 0, 0);
            if (yFloor - yBalcony > 0) {
                setArray3dCad(null, TYPE_WALL, xFloor, yFloor, 18, yFloor - yBalcony, 0, 0, 0, 0, 0, 0);
            }
            setArray3dCad(null, TYPE_WALL, xFloor, yBalcony, 18, heightBalcony, 0, 0, 0, 0, 0, 0);

            // wall in balcony
            createArrayWallInBalcony(1, null, TYPE_WALL, xBalcony, yBalcony, widthBalcony, 18, 0, 0, 0, 0, 0, 0);
            createArrayWallInBalcony(2, null, TYPE_WALL, xBalcony, yBalcony, 18, heightBalcony, 0, 0, 0, 0, 0, 0);
            createArrayWallInBalcony(3, null, TYPE_WALL, xBalcony, yBalcony + heightBalcony, widthBalcony, 18, 0, 0, 0, 0, 0, 0);

            if (yFloor + heightFloor - yBalcony - heightBalcony > 0) {
                setArray3dCad(null, TYPE_WALL, xBalcony + widthBalcony, yBalcony + heightBalcony, 18, yFloor + heightFloor - yBalcony - heightBalcony, 0, 0, 0, 0, 0, 0);
            }
        }
    }
}

function drawBalconyDrop(x, y, width, height) {
    var scale = getScale(floor.width, floor.height);
    var g = new createjs.Graphics().setStrokeDash([2, 2]).beginStroke('#800000').drawRect(0, 0, width, height);
    balconyDrop = new createjs.Shape(g);
    balconyDrop.x = x;
    balconyDrop.y = y;
    balconyDrop.width = width;
    balconyDrop.height = height;
    stage.addChild(balconyDrop);
}

function addEventFloor() {
    if (floor) {
        var oldFloor = floor;
        // drag and drop floor
        floor.addEventListener("pressmove", function (event) {
        if ( transitionState >= STATE_TRANSITION_SET_OBJECT ) return ; 
            if (!isZoom) {
                cleanObjectWhenScale(stage);
                startX = event.stageX;
                startY = event.stageY;

                stage.removeChild(floor);
                stage.removeChild(balcony);
                scaleTopRight.visible = false;
                scaleBottomLeft.visible = false;
                scaleBottomRight.visible = false;
                for (var i = 0; i < arrObjectInWall.length; i++) {
                    arrObjectInWall[i].visible = false;
                }
                if (scaleTopLeftBalcony) {
                    scaleTopLeftBalcony.visible = false;
                    scaleTopRightBalcony.visible = false;
                    scaleBottomLeftBalcony.visible = false;
                    scaleBottomRightBalcony.visible = false;
                }

                drawContentCanvas(floor.width, floor.height);
            }
        });

        floor.addEventListener("pressup", function (event) {
        if ( transitionState >= STATE_TRANSITION_SET_OBJECT ) return ; 
            if (!isZoom) {
                //diablog(stage, '柱を立ててください');
                cleanObjectWhenScale(stage);

                stage.removeChild(floor);
                stage.removeChild(balcony);
                scaleTopRight.visible = false;
                scaleBottomLeft.visible = false;
                scaleBottomRight.visible = false;
                if (scaleTopLeftBalcony) {
                    scaleTopLeftBalcony.visible = false;
                    scaleTopRightBalcony.visible = false;
                    scaleBottomLeftBalcony.visible = false;
                    scaleBottomRightBalcony.visible = false;
                }
                handleMoveFloor(oldFloor, floor.x, floor.y);
                drawContentCanvas(floor.width, floor.height);
            }
        });

        floor.addEventListener("mousedown", function (event) {
        if ( transitionState >= STATE_TRANSITION_SET_OBJECT ) return ; 
            if (!isZoom) {
                //cleanObjectWhenScale(stage);
                scaleTopRight.visible = true;
                scaleBottomLeft.visible = true;
                scaleBottomRight.visible = true;
                if (scaleTopLeftBalcony) {
                    scaleTopLeftBalcony.visible = false;
                    scaleTopRightBalcony.visible = false;
                    scaleBottomLeftBalcony.visible = false;
                    scaleBottomRightBalcony.visible = false;
                }
            }
        });
    }
}

function getPositionBalcony(xFloor, yFloor, widthFloor, heightFloor, widthBalcony, heightBalcony) {
    return balconyPos;
}

function drawText(widthFloor, heightFloor, isScaleShape) {
    // draw text
    if (textWidth) {
        textWidth.visible = false;
        textHeight.visible = false;
    }

    if (isScaleShape) {
        widthFloor = widthText;
        heightFloor = heightText;
    }

    var scale = getScale(widthFloor, heightFloor);
    var position = getPositionBalcony(floor.x, floor.y, floor.width, floor.height, balcony.width, balcony.height);
    if (position == 2) {
        textWidth = createText(widthFloor, floor.x + widthFloor * scale * zoom * ratioPixelMM / 2 - 10, floor.y + heightFloor * scale * zoom * ratioPixelMM + 15);
    } else {
        textWidth = createText(widthFloor, floor.x + widthFloor * scale * zoom * ratioPixelMM / 2 - 10, floor.y - 5);
    }
    if (position == 1) {
        textHeight = createText(heightFloor, floor.x + widthFloor * scale * zoom * ratioPixelMM + 5, floor.y + heightFloor * scale * zoom * ratioPixelMM / 2 - 10);
    } else {
        textHeight = createText(heightFloor, floor.x - 15, floor.y + heightFloor * scale * zoom * ratioPixelMM / 2 - 10);
    }

    textHeight.rotation = 90;
    stage.addChild(textWidth);
    stage.addChild(textHeight);
    if (textWidth) {
        textWidth.visible = true;
        textHeight.visible = true;
    }
    stage.update();
}

function cleanObjectWhenScale(stage) {
    // remove old pillar from stage
    for (var i = 0; i < pillarIndexStage.length; i++) {
        stage.removeChild(pillarIndexStage[i]);
    }
    pillarIndexStage = [];
    // remove old wall from stage
    for (var i = 0; i < listWallInStage.length; i++) {
        stage.removeChild(listWallInStage[i]);
    }
    listWallInStage = [];
    // remove old pillar from memory
    var pillarList = getArray3dCadByType(4);
    for (var i = 0; i < pillarList.length; i++) {
        deleteArray3dCad(pillarList[i][0][0]);
    }
    // remove old wall from memory
    var wallList = getArray3dCadByType(3);
    for (var i = 0; i < wallList.length; i++) {
        deleteArray3dCad(wallList[i][0][0])
    }
}
function desZoom() {
    if (transitionState < STATE_TRANSITION_SET_OBJECT) {
        //diablog(stage, '柱を立ててください');
        var position = getPositionBalcony(floor.x, floor.y, floor.width, floor.height, balcony.width, balcony.height);
        if (floor) {
            // textWidth.visible = false;
            scaleTopRight.visible = false;
            scaleBottomRight.visible = false;
            scaleBottomLeft.visible = false;
            var diff = Math.min(227.5 / floor.height, 227.5 / floor.width);
            // var scale;
            if (zoom - diff >= 0.5 && startX * (zoom - diff) * scale >= 100) {
                zoom -= diff;
                handleZoom();
            }
            drawContentCanvas(floor.width, floor.height, true);
            refreshListObject();
        }
    }
}
function incZoom() {
    if (transitionState < STATE_TRANSITION_SET_OBJECT) {
        //diablog(stage, '柱を立ててください');
        var position = getPositionBalcony(floor.x, floor.y, floor.width, floor.height, balcony.width, balcony.height);
        if (floor) {
            scaleTopRight.visible = false;
            scaleBottomLeft.visible = false;
            scaleBottomRight.visible = false;

            if (scaleBalcony) {
                scaleTopRightBalcony.visible = false;
                scaleTopLeftBalcony.visible = false;
                scaleBottomLeftBalcony.visible = false;
                scaleBottomRightBalcony.visible = false;
            }

            var diff = Math.min(227.5 / floor.height, 227.5 / floor.width);
            var xFloor = floor.x * (zoom + diff);
            var yFloor = floor.y * (zoom + diff);
            var widthFloor = floor.width * (zoom + diff) * scale * 100 / 455;
            var heightFloor = floor.height * (zoom + diff) * scale * 100 / 455;

            var xBalcony = balcony.x * (zoom + diff);
            var yBalcony = balcony.y * (zoom + diff);
            var widthBalcony = balcony.width * (zoom + diff) * scale * 100 / 455;
            var heightBalcony = balcony.height * (zoom + diff) * scale * 100 / 455;
            if (typeBalcony != TYPE_BALCONY_NONE) {
                switch (position) {
                    case 1:
                        if (yFloor + heightFloor <= 830 && xBalcony + widthFloor + widthBalcony <= 1280) {
                            zoom += diff;
                            handleZoom();
                        }
                        break;
                    case 2:
                        if (yBalcony + heightFloor + heightBalcony <= 830 && xFloor + widthFloor <= 1280) {
                            zoom += diff;
                            handleZoom();
                        }
                        break;
                    case 3:
                        if (yFloor + heightFloor <= 830 && xFloor + widthFloor + widthBalcony <= 1280) {
                            zoom += diff;
                            handleZoom();
                        }
                        break;
                    case 4:
                        if (yFloor + heightFloor + heightBalcony <= 830 && xFloor + widthFloor <= 1280) {
                            zoom += diff;
                            handleZoom();
                        }
                        break;
                    default:
                        break;
                }
            } else {
                if (yFloor + heightFloor <= 830 && xFloor + widthFloor <= 1280) {
                    zoom += diff;
                    handleZoom();
                }
            }

            drawContentCanvas(floor.width, floor.height, true);
            refreshListObject();
        }
    }

}
function searchInitObject(type)
{
	var ret = [];
	// in csv file, object list is start with row 2
	for( var i = 2; i < initObject.length; i++)
	{
		if (initObject[i][1] == type)
		{
			ret.push( initObject[i]);
		}
	}
	return ret;
}
