Studio.Resizable = 
{
    sCurrentResizableImg : "",
    
    oCurrentLayer        : null,
    
    bResize : false,

    iInitialX           : 0,
    iInitialY           : 0,
    
    aConnection : new Array(),
        
    setResizable : function(oLayer)
    {
        this.resetResizable();
        Studio.Moveable.resetMovable();
        
        if (null == this.aConnection[0])
        {
            this.oCurrentLayer        = oLayer;
            this.sCurrentResizableImg = 'imgdiv_' + oLayer.iId;
            dojo.style(dojo.byId('canvasDragArea'), 'display', 'block');
            dojo.style(dojo.byId('canvasDragArea'), 'cursor', 'se-resize');
            this.aConnection[0] = dojo.connect(dojo.byId('canvasDragArea'), 'onmousedown', this, 'startResizing');
            this.aConnection[1] = dojo.connect(dojo.byId('canvasDragArea'), 'onmousemove', this, 'processResizing');
            this.aConnection[2] = dojo.connect(dojo.byId('canvasDragArea'), 'onmouseup',   this, 'stopResizing');
        }
    },
    
    startResizing : function(evt)
    {
        this.bResize = true;
        var pos = dojo.coords(dojo.byId('canvasDragArea'));
        this.iInitialX = evt.pageX - pos.x;
        this.iInitialY = evt.pageY - pos.y;
        
        dojo.style('canvasWrapArea', 'display', 'block');
        if (Studio.bIsIE) dojo.style(this.sCurrentResizableImg, 'display', 'none');
        document.getElementById('canvasWrapArea').style.width  = document.getElementById(this.sCurrentResizableImg).style.width;
        document.getElementById('canvasWrapArea').style.height = document.getElementById(this.sCurrentResizableImg).style.height;
        document.getElementById('canvasWrapArea').style.top    = document.getElementById(this.sCurrentResizableImg).style.top;
        document.getElementById('canvasWrapArea').style.left   = document.getElementById(this.sCurrentResizableImg).style.left;
    },
    
    processResizing : function(evt)
    {
        if (!this.bResize)
        {
            return false;
        }
        
        var pos = dojo.coords(dojo.byId('canvasDragArea'));
        iX = evt.pageX - pos.x - this.iInitialX;
        iY = evt.pageY - pos.y - this.iInitialY;
        
        if ('text' == this.oCurrentLayer.oObject.sType)
        {
        	iKZoom = 1;
        } else 
        {
        	iKZoom = Studio.Zoom.iZoom;
        }
        
        var iWidth = Number(this.oCurrentLayer.oObject.oShape.getCanvasWidth()) * iKZoom + Number(iX);
        if (iWidth <= 0)
        {
            return false;
        }
        document.getElementById(this.sCurrentResizableImg).style.width = iWidth + 'px';
        
        var iHeight = Number(this.oCurrentLayer.oObject.oShape.getCanvasHeight()) * iKZoom + Number(iY);
        if (iHeight <= 0)
        {
            return false;
        }
        document.getElementById(this.sCurrentResizableImg).style.height = iHeight + 'px';
        
        document.getElementById('canvasWrapArea').style.width  = (iWidth) + 'px';
        document.getElementById('canvasWrapArea').style.height = (iHeight) + 'px';
    },
        
    stopResizing : function()
    {
        var aParams = new Array();
        aParams['width'] = parseInt(document.getElementById(this.sCurrentResizableImg).style.width);
        aParams['height'] = parseInt(document.getElementById(this.sCurrentResizableImg).style.height);
        
        if ('text' == this.oCurrentLayer.oObject.sType)
        {
            this.oCurrentLayer.oObject.oShape.dwidth  = Math.max(this.oCurrentLayer.oObject.oShape.dwidth  - (1 - aParams['width']  / Studio.Canvas.iWidth), 0.01);
            this.oCurrentLayer.oObject.oShape.dheight = Math.max(this.oCurrentLayer.oObject.oShape.dheight - (1 - aParams['height'] / Studio.Canvas.iHeight), 0.01);
            
            document.getElementById(this.sCurrentResizableImg).style.left = this.oCurrentLayer.oObject.oShape.x + 'px';
            document.getElementById(this.sCurrentResizableImg).style.top  = this.oCurrentLayer.oObject.oShape.y + 'px';
        } else 
        {
        	aParams['width']  = Math.round(aParams['width'] / Studio.Zoom.iZoom);
        	aParams['height'] = Math.round(aParams['height'] / Studio.Zoom.iZoom);
        }
        
        dojo.forEach(this.aConnection, 
                function(oConn, idx)
                {
                    dojo.disconnect(oConn);
                    Studio.Resizable.aConnection[idx] = null;
                });
        
        Studio.LayerHandler.resizeLayer(this.oCurrentLayer.iId, aParams);
        this.resetResizable();
    },
    
    resetResizable : function()
    {
        dojo.forEach(this.aConnection, 
                function(oConn, idx)
                {
                    dojo.disconnect(oConn);
                    Studio.Resizable.aConnection[idx] = null;
                });
        dojo.style(dojo.byId('canvasDragArea'), 'cursor', 'default');
        dojo.style(dojo.byId('canvasDragArea'), 'display', 'none');
        dojo.style(dojo.byId('canvasWrapArea'), 'display', 'none');
        if (Studio.bIsIE && this.sCurrentResizableImg) 
        {	
        	dojo.style(dojo.byId(this.sCurrentResizableImg), 'display', 'block');
        }
        this.bResize = false;
        this.oCurrentLayer = null;
        this.sCurrentResizableImg = "";
        Studio.Toolbar.resetButtons();
        return false;
    },
    
    setSize : function(sId, iWidth, iHeight)
    {
    	document.getElementById(sId).style.width  = (iWidth + 'px');
    	document.getElementById(sId).style.height = (iHeight + 'px');
    }
    
};