
Control=function(arr)
{
    this.Attribute=arr;
    this.Element;
    this.ElementContent;
    this.ElementLabel;
    
    this.DragHandler;
    this.ResizeHandler;
    
    this._drag;
    this._resize;
    
    
    
    this.initialize=function()
    {
        this.Element=document.createElement("div");
        this.Element.setAttribute("id",this.Attribute.ID);
        this.Element.className="control";
        
        
        
//        this.ElementContent=document.createElement("div");
//        this.ElementContent.className="content";
//        this.Element.appendChild(this.ElementContent);

        this.move(this.Attribute.Left,this.Attribute.Top);
        this.resize(this.Attribute.Width,this.Attribute.Height);
        
        
        this.Attribute.Owner.appendChild(this.Element);
        
        this.create();
        
        this.DragHandler=document.createElement("div");
        this.DragHandler.className="drag";
        this.Element.appendChild(this.DragHandler);
        
        this.ResizeHandler=document.createElement("div");
        this.ResizeHandler.className="resize";
        this.Element.appendChild(this.ResizeHandler);
        
        this._drag=new Drag(this.DragHandler,this.Element);
        _moveHandler=Function.createDelegate(this,this.move);
        this._drag.onmove=_moveHandler;
        
        this._resize=new Resize(this.ResizeHandler,this.Element);
        _resizeHandler=Function.createDelegate(this,this.resize);
        this._resize.onchangesize=_resizeHandler;
        
        var selectHandler=Function.createDelegate(this,this.selected);
        this.Element.attachEvent("onmousedown",selectHandler);
        
        
    }
    
    this.create=function()
    {
        if(this.Attribute.Type.toLowerCase()=="obxform")
        {
            this.Element.className="form";
            var nodes=this.Attribute.Content.selectNodes("/content/obxControl");
            
            var zGroup=MaxZindex();
            var zControl=MaxZindex();
            
            for(var i=0;i<nodes.length;i++)
            {
                var arr=new ControlAttribute();
                arr.loadXML(new XMLDOM(nodes[i].xml));
                
                if(arr.Type.toLowerCase()=="obxgroupbox")
                {
                    arr.Zindex=zGroup;
                }
                else
                {
                    arr.Zindex=zControl;
                }
                this.Attribute.Controls.add(arr.ID,new Control(arr));
            }
        }
        else
        {
            if(this.Attribute.Selected>0)this.Element.className="control_select";
            this.Element.innerHTML=this.Attribute.Content;
            
            this.ElementLabel=$(this.Attribute.ID+"_label");
        }
    }
    
    this.move=function(left,top)
    {
        if(this.Attribute.Type.toLowerCase()!="obxform")
        {
            if(left>this.Attribute.Owner.offsetWidth-10-this.Attribute.Width)left=this.Attribute.Owner.offsetWidth-10-this.Attribute.Width;
            if(top>this.Attribute.Owner.offsetHeight-10-this.Attribute.Height)top=this.Attribute.Owner.offsetHeight-10-this.Attribute.Height;
            
            
        }
        
        if(left<10)left=10;
        this.Attribute.Left=left;
        this.Element.style.left=left+"px";
        
        if(top<10)top=10;
        this.Attribute.Top=top;
        this.Element.style.top=top+"px";
    }
    this.resize=function(width,height)
    {
        if(width<10)width=10;
        this.Attribute.Width=width;
        this.Element.style.width=width+"px";
        //alert(width);
        
        if(height<10)height=10;
        this.Attribute.Height=height;
        this.Element.style.height=height+"px";
        //alert(height);
        if(this.ElementLabel)this.ElementLabel.style.right=this.Element.offsetWidth+10+"px";
    }
    this.selected=function(e)
    {
        //alert(this.Element.className);
        if((this.Element.className=="control_select")||(this.Element.className=="control"))
        {
            var ctrls=FormList.item(this.Attribute.Owner.id).Attribute.Controls;
            if(event==null||!event.ctrlKey )
            {
                for(var i in ctrls._hash)
                {
                    ctrls.item(i).cancelSelected();
                }
            }
            this.Element.className="control_select";
            
            //this.Attribute.Selected
            //0:未选中
            //1:选中
            //2:选中并作为标准
            var isSelected=2;
            if(this.Attribute.Selected==2)this.Attribute.Selected=1;
            for(var i in ctrls._hash)
            {
                if(ctrls.item(i).Attribute.Selected==2)isSelected=1;
            }
            this.Attribute.Selected=isSelected;
        }
    }
    this.cancelSelected=function()
    {
        this.Element.className="control";
        this.Attribute.Selected=0;
    }
    this.initialize();
}


