Studio.ImgHandler =
{
    bPositionLayerNull : false,
    
    /**
     * Takes image URL based on object params using AJAX and created image object
     */
    makeImage : function(oLayer, bSync)
    {
        if (bSync)
        {
            bSyncReq = true;
        } else 
        {
            bSyncReq = false;
        }
        
        sUrlReq = Studio.sGetLayerUrl + '/layer/' + oLayer.iId + '/' + oLayer.getUrlAttach();
        aPostContent   = oLayer.getPostArray();
        sImgDiv        = 'imgdiv_' + oLayer.iId;
        iActionLayerId = oLayer.iId;
        oLayer.sImgDiv = sImgDiv;
        Studio.showProgress();
        dojo.xhrPost({
            url  : sUrlReq,
            content: aPostContent,
            sync : bSyncReq,
            load : function(sImageUrl)
            {
                oCanvas = dojo.byId('canvas');
                oImg = document.createElement('img');
                console.log(sImageUrl);
                dojo.attr(oImg, 'src', sImageUrl);
                oLayer.sImgFile = sImageUrl;
                oImg.setAttribute('id', sImgDiv);
                dojo.style(oImg, 'position', 'absolute');
                dojo.style(oImg, 'zIndex', oLayer.iDepth);
                if (Studio.ImgHandler.bPositionLayerNull)
                {
                    oImg.style.left   = '0px';
                    oImg.style.top    = '0px';
                    Studio.ImgHandler.bPositionLayerNull = false;
                } else 
                {
                	if ('text' == oLayer.oObject.sType)
                	{
                		oImg.style.left   = 0;
	                    oImg.style.top    = 0;
                	} else 
                	{
	                    oImg.style.left   = oLayer.oObject.oShape.getX() + 'px';
	                    oImg.style.top    = oLayer.oObject.oShape.getY() + 'px';
                	}
                }
                oImg.style.width  = oLayer.getCanvasWidth() + 'px';
                oImg.style.height = oLayer.getCanvasHeight() + 'px';
                oCanvas.appendChild(oImg);
            },
            handle : function()
            {
            	if (Studio.History.bSaveLayerStateOnUpdate)
                {
                    Studio.History.saveState(iActionLayerId);
                }
            	Studio.History.bSaveLayerStateOnUpdate = true;
                Studio.fixPng();
                Studio.endProgress();
            }
        });
    },
    
    updateImage : function(oLayer, bSync)
    {
        if (bSync)
        {
            bSyncReq = true;
        } else 
        {
            bSyncReq = false;
        }
        sUrlReq = Studio.sGetLayerUrl + '/layer/' + oLayer.iId + '/' + oLayer.getUrlAttach();
        aPostContent   = oLayer.getPostArray();
        sImgDiv        = 'imgdiv_' + oLayer.iId;
        iActionLayerId = oLayer.iId;
        oLayer.sImgDiv = sImgDiv;
        
        Studio.showProgress();

        dojo.xhrPost({
            url  : sUrlReq,
            content: aPostContent,
            sync : bSyncReq,
            load : function(sImageUrl)
            {
        		if (!sImageUrl)
        		{
        			dojo.byId(sImgDiv).style.visibility = 'hidden';
        		} else 
        		{
        			dojo.byId(sImgDiv).style.visibility = '';
        		}
                dojo.byId(sImgDiv).src = sImageUrl;
                oLayer.sImgFile = sImageUrl;
                if (dojo.isIE)
                {
                	document.getElementById(sImgDiv).style.width = oLayer.getCanvasWidth() + 'px';
                	document.getElementById(sImgDiv).style.height = oLayer.getCanvasHeight() + 'px';
                	setTimeout("Studio.Resizable.setSize('" + sImgDiv + "', '" + oLayer.getCanvasWidth() + "', '" + oLayer.getCanvasHeight() + "');", 50);
                } else 
                {
                	dojo.byId(sImgDiv).style.width  = '';
	                dojo.byId(sImgDiv).style.height = '';
                }
                //alert('width: ' + oLayer.getCanvasWidth() + '  height: ' + oLayer.getCanvasHeight());
                if ('text' == oLayer.oObject.sType)
                {
                    document.getElementById(sImgDiv).style.left = 0;
                    document.getElementById(sImgDiv).style.top  = 0;
                } else 
                {
                    document.getElementById(sImgDiv).style.left = oLayer.oObject.oShape.getX() + 'px';
                    document.getElementById(sImgDiv).style.top  = oLayer.oObject.oShape.getY() + 'px';
                }
            },
            handle : function()
            {
                if (Studio.History.bSaveLayerStateOnUpdate)
                {
                    Studio.History.saveState(iActionLayerId);
                }
                Studio.History.bSaveLayerStateOnUpdate = true;
                Studio.fixPng();
                Studio.endProgress();
            }
        });
    },
    
    updateBackgroundTemplate : function()
    {
        if (1 == Studio.Zoom.iZoom)
        {
            dojo.byId('canvas').style.backgroundImage = 'url(' + Studio.sTemplateUrl + ')';
            return true;
        }
        sUrlReq = Studio.sGetLayerUrl + '/layer/0/shapetype/image/x/0/y/0/width/' + 
                  Studio.Canvas.iWidth + '/height/' + Studio.Canvas.iHeight + '/origwidth/' + Studio.Canvas.iWidth + 
                  '/origheight/' + Studio.Canvas.iHeight + '/filename/zoom_template.jpg/opacity/1/canvas_width/' + 
                  Studio.Canvas.iWidth + '/canvas_height/' + Studio.Canvas.iHeight + '/' + Studio.Zoom.getUrlAttach();
                  
        Studio.showProgress();
        dojo.xhrPost({
            url  : sUrlReq,
            sync : true,
            load : function(sImageUrl)
            {
                dojo.byId('canvas').style.backgroundImage = 'url(' + sImageUrl + ')';
            },
            handle : function()
            {
                Studio.fixPng();
                Studio.endProgress();
            }
        });
        return true;
    },
    
    /**
     * Moves layer image 1 position up
     */
    moveUp : function(iLayerId)
    {
        iZindex = dojo.style( dojo.byId('imgdiv_' + iLayerId), 'zIndex' );
        dojo.style( dojo.byId('imgdiv_' + iLayerId), 'zIndex', Number(iZindex) + 1 );
    },
    
    /**
     * Moves layer image 1 position down
     */
    moveDown : function(iLayerId)
    {
        iZindex = dojo.style( dojo.byId('imgdiv_' + iLayerId), 'zIndex' );
        dojo.style( dojo.byId('imgdiv_' + iLayerId), 'zIndex', Number(iZindex) - 1 );
    },
    
    /**
     * Hides layer
     */
    hide : function(iLayerId)
    {
        dojo.style( dojo.byId('imgdiv_' + iLayerId), 'display', 'none' );
    },
    
    /**
     * Shows hidden layer
     */
    unhide : function(iLayerId)
    {
        dojo.style( dojo.byId('imgdiv_' + iLayerId), 'display', 'block' );
    }

};