Studio.Zoom = 
{
    iZoom : 1,
    
    iX    : 0,
    iY    : 0,
    
    iStartX : 0,
    iStartY : 0,
    
    aConnection : new Array(),
    
    initZoom : function()
    {
        if (null == this.aConnection[0])
        {
            dojo.style('canvasDrawArea', 'display', 'block');
            dojo.style('canvasDragArea', 'display', 'block');
            dojo.style(dojo.byId('canvasDrawArea'), 'cursor', 'crosshair');
            this.aConnection[0] = dojo.connect(dojo.byId('canvasDrawArea'), 'onmousedown', this, 'processZoom');
        }
    },
    
    processZoom : function(evt)
    {
        if (this.iZoom >= 8)
        {
            return false;
        }
        
        var pos = dojo.coords(dojo.byId('canvasDrawArea'));
        
        var iClickX, iClickY;
        iClickX = evt.pageX - pos.x;
        iClickY = evt.pageY - pos.y;
        
        iStartX = Math.min(Studio.iWidth - Studio.iWidth / Studio.Zoom.iZoom, Math.max(Studio.Zoom.iX - Studio.iWidth / ( 2 * Studio.Zoom.iZoom ), 0));
        iStartY = Math.min(Studio.iHeight - Studio.iHeight / Studio.Zoom.iZoom, Math.max(Studio.Zoom.iY - Studio.iHeight / ( 2 * Studio.Zoom.iZoom ), 0));
        
        this.iX = Math.round(iStartX + iClickX / this.iZoom);
        this.iY = Math.round(iStartY + iClickY / this.iZoom);
        this.iZoom *= 2;
        
        Studio.showProgress();
        
        setTimeout("Studio.showProgress(); Studio.Zoom.zoom(); Studio.Zoom.endZoom();", 500);
    },
    
    zoom : function()
    {
    	this.iStartX = Math.min(Studio.iWidth - Studio.iWidth / Studio.Zoom.iZoom, Math.max(Studio.Zoom.iX - Studio.iWidth / ( 2 * Studio.Zoom.iZoom ), 0));
        this.iStartY = Math.min(Studio.iHeight - Studio.iHeight / Studio.Zoom.iZoom, Math.max(Studio.Zoom.iY - Studio.iHeight / ( 2 * Studio.Zoom.iZoom ), 0));
        switch (this.iZoom)
    	{
	    	case 1:
	    		dojo.byId("divRuleTop").style.backgroundImage = 'url(/img/design-studio/top_rule.gif)';
	            dojo.byId("divRuleLeft").style.backgroundImage = 'url(/img/design-studio/left_rule.gif)';
	    		break;
	    	case 2:
	    		dojo.byId("divRuleTop").style.backgroundImage = 'url(/img/design-studio/top_200.gif)';
	            dojo.byId("divRuleLeft").style.backgroundImage = 'url(/img/design-studio/left_200.gif)';
	    		break;
	    	case 4:
	    		dojo.byId("divRuleTop").style.backgroundImage = 'url(/img/design-studio/top_400.gif)';
	            dojo.byId("divRuleLeft").style.backgroundImage = 'url(/img/design-studio/left_400.gif)';
	    		break;
	    	case 8:
	    		dojo.byId("divRuleTop").style.backgroundImage = 'url(/img/design-studio/top_800.gif)';
	            dojo.byId("divRuleLeft").style.backgroundImage = 'url(/img/design-studio/left_800.gif)';
	    		break;
    	}
        dojo.byId("divRuleTop").style.backgroundPosition = '-' + (this.iStartX * Studio.Zoom.iZoom) + 'px top';
        dojo.byId("divRuleLeft").style.backgroundPosition = 'left -' + (this.iStartY * Studio.Zoom.iZoom) + 'px';
        Studio.ImgHandler.updateBackgroundTemplate();
        Studio.LayerHandler.updateAllLayers();
    },
    
    endZoom : function()
    {
        dojo.style(dojo.byId('canvasDrawArea'), 'cursor', 'default');
        dojo.style('canvasDrawArea', 'display', 'none');
        dojo.style('canvasDragArea', 'display', 'none');
        dojo.forEach(this.aConnection, 
                     function(oConn, idx)
                     {
                         dojo.disconnect(oConn);
                         Studio.Zoom.aConnection[idx] = null;
                     });
        Studio.Options.hidePanel('tool', 'zoom');
    },
    
    zoomOut : function()
    {
        if (this.iZoom >= 2)
        {
            this.iZoom /= 2;
            
            Studio.showProgress();
            setTimeout("Studio.showProgress(); Studio.Zoom.zoom(); Studio.Zoom.endZoom(); Studio.endProgress();", 500);
        }
    },
    
    zoomToOriginal : function()
    {
        this.iZoom = 1;
        
        Studio.showProgress();
        setTimeout("Studio.showProgress(); Studio.Zoom.zoom(); Studio.Zoom.endZoom(); Studio.endProgress();", 500);
    },
    
    getUrlAttach : function()
    {
    	if (this.iX < 1)
    	{
    		this.iX = 1;
    	}
    	if (this.iY < 1)
    	{
    		this.iY = 1;
    	}
        var sUrl = 'zoom/' + this.iZoom + '/';
        sUrl += 'zoom_x/' + this.iX + '/';
        sUrl += 'zoom_y/' + this.iY + '/';
        return sUrl;
    }
    
};