/**
 * Created with JetBrains WebStorm.
 * User: dfwang
 * Date: 14-6-20
 * Time: 下午4:52
 * To change this template use File | Settings | File Templates.
 */
define(function (require, exports) {
    var _$object = function(){};
    _$object.prototype.remove = function(){                                        r
        delete this;
    }
    _$object.prototype.init = function(){

    }
    /**
     * 创建或更新对象
     * @param _img  base64;没有表示创建
     */
    _$object.prototype.update = function(_img,_pro){
        if(!this.phtml)
        {
            Global.CurrentSlide.c.add(this.dom,_pro);
        }else
        {
            this.dom._element.src = _img;
        }
        setTimeout(function(){Global.CurrentSlide.c._canvas.renderAll()},50);
    }

    _$object.prototype.print= function(img){
        var _img = new Image();
        _img.src = img;
        var imgInstance = new fabric.Image(_img,{left:50,top:100,width:Global.CurrentSlide.c._canvas.width-100});
        return imgInstance;
    }
    /**
     * 为对象添加属性
     * @param _object   Array
     */
    _$object.prototype.addProperty = function(_object){
        this.dom.toObject = (function(toObject) {
            return function() {
                var a = {};
                for(var i=0;i<_object.length;i++)
                {
                    for(var t in _object[i])
                    {
                        a[t] = _object[i][t];
                    }
                }
                return fabric.util.object.extend(toObject.call(this), a);
            };
        })(this.dom.toObject);
    }
    /**
     * 显示编辑按钮
     */
    _$object.prototype.showEditorBtn = function(){
        Global.CurrentSlide.c.activeObject = this;
        var input = $('<input type="button" value="编辑" id="inline-btn" style="position: absolute; left: 216px; top: 323px;">');
        $('body').append(input);
        var _that = this;
        function getAbsoluteCoords(object)
        {
            return {
                left: object.left + Global.CurrentSlide.c._canvas._offset.left,
                top: object.top + Global.CurrentSlide.c._canvas._offset.top,
                width:object.width*object.scaleX,
                height:object.height*object.scaleY,
                angle:object.angle
            };
        }
        function setCss(){
            var absCoords = getAbsoluteCoords(_that.dom);
            input.css('left',(absCoords.left - input.width()+absCoords.width) + 'px')
            input.css('top',(absCoords.top - input.height()) + 'px');
            //input.css('-webkit-transform','rotate('+absCoords.angle+'deg)');
        }
        this.dom.on('moving',function(){
            setCss();
        });
        $('#inline-btn').off();
        $('#inline-btn').on('click',function(e){
            _that.render();
        });
        setCss();
    }
    _$object.prototype.hideEditorBtn = function(){
        $('#inline-btn').remove();
    }
    exports.instance = _$object;
})
