/*
  By Hangring
  #2008.11.01#
  ---
*/
if (typeof Diagram == 'undefined')
	throw new Error('Diagarm is not defined');

Diagram.Attribute = function (name /* : String */) {
	this.Name = name;
};

Diagram.Attribute.prototype = {
	Name : '',
	Comment : '',
	
	// class level
	Level : 0,
	
	// 容器, HTMLElement
	Container : null,
	// 说明信息
	CommentContainer : null,
	
	// Class.Frame
	Frame : null,
	// 关联的浮动对象, Diagram.Class 或 其他对象
	Popup : null,
	
	// private function
	_click : null,
	_mouseover : null,
	_mouseout : null,
	
	// public function
	Click : function () {},
	
	Create : function () {
		if (this.Name == '') throw new Error('Diagram.Attribute : Name is not empty');
		
		var c = this.Container = oNode.CreateNode('div');
		CSS.AddClass(c, 'attribute');
		
		var attrName, attrType;
		var arr = /([a-z_0-9]+?)\s*:\s*([a-z0-9]+)/i.exec(this.Name)
		if (arr.length > 1) {
			attrName = arr[1];
			attrType = arr[2];
		}
		else {
			attrName = this.Name;
			attrType = '';
		}
		
		var id = Global.Random();
		c.innerHTML = [
			// float left
			'<span class="attribute-name">', attrName, '</span>',
			// float right
			this.Comment != '' ? ['<span class="attribute-info">(<span id="attribute_info_', id, '" class="info-sign">i</span>)</span>'].join('') : '',
			'<span class="attribute-type">', attrType, (attrType ? '(<span class="type-sign" title="Object Type">T</span>)' : ''), '</span>',
		].join('');
		
		var self = this;
		var _click = this._click = function () {
			self.Click();
			if (self.Popup) {
				if (self.Popup.Container.Visible)
					CSS.AddClass(c, 'attribute-focus');
				else 
					CSS.RemoveClass(c, 'attribute-focus');
			}
		};
		var _mouseover = this._mouseover = function () {
			CSS.AddClass(c, 'attribute-over');
		};
		var _mouseout = this._mouseout = function () {
			CSS.RemoveClass(c, 'attribute-over');
		};
		
		Events.AttachEvent(c, 'click', _click);
		Events.AttachEvent(c, 'mouseover', _mouseover);
		Events.AttachEvent(c, 'mouseout', _mouseout);
		
		c.AddToStage = function () {
			$('attribute_info_' + id) && 
			($('attribute_info_' + id).onclick = function (e) {
				self.CommentShow();
				Events.CancelAll(e);
			});
		};
		
		return c;
	},
	
	// 如果Popup存在，则当Popup关闭时根据情况调用该方法
	PopupClose : function () {
		CSS.RemoveClass(this.Container, 'attribute-focus');
	},
	
	CommentShow : function () {
		var self = this;
		
		var cc = this.CommentContainer = oNode.CreateNode('div');
		cc.Level = this.Level + 1;
		this.Frame.Popup.Add(cc, this.Level + 1);
		CSS.AddClass(cc, 'comment');
		Global.SetPosition(cc, this.Container);
		
		cc.innerHTML = this.Comment.HtmlEntities();
		
		var close = oNode.CreateNode('span');
		oNode.InsertBefore(close, cc.firstChild);
		CSS.AddClass(close, 'close');
		close.innerHTML = 'x';
		close.onclick = function () {
			self.Frame.Popup.RemoveAllFromLevel(cc.Level);
		};
		
		cc.Close = function () {
			self.Frame.Popup.RemovePopupFromContainer(this);
		};
	},
	
	CommentHide : function () {
		this.CommentContainer && 
		(this.CommentContainer.style.visibility = 'hidden');
	}
};