/*
  By Hangring
  #2008.11.01#
  ---
*/
if (typeof Diagram == 'undefined')
	throw new Error('Diagarm is not defined');

Diagram.Class = function (name /* : String */) {
	this.Name = name;
};

Diagram.Class.prototype = {
	Name : '',
	
	// class level
	Level : 0,
	
	// HTMLElement
	Container : null,
	Title : null,
	AttributeContainer : null,
	SAttributeContainer : null,
	FunctionContainer : null,
	SFunctionContainer : null,
	Attribute : null,
	SAttribute : null,
	Function : null,
	SFunction : null,
	
	// Class.Frame
	Frame : null,
	
	// Class.Diagram.Attribute, Class.Diagram.Function
	// 若为浮动，则该参数必须设置
	PopupParent : null,
	
	// display close button or not
	Closeable : false,
	
	// size
	Width : 200,
	Height : 0,
	
	// stylesheet
	StyleName : '',
	
	// Enum Of Class Diagram.Attribute
	Attributes : {},
	// static
	SAttributes : {},
	// Enum Of Class Diagram.Function
	Functions : {},
	// static
	SFunctions : {},
	
	Create : function () {
		if (this.Name == '') throw new Error('Diagram.Class : Name is not empty');
		
		var c = this.Container = oNode.CreateNode('div');
		CSS.AddClass(c, 'class');
		if (this.StyleName) CSS.AddClass(c, this.StyleName);
		
		// setting visible
		c.Visible = false;
		
		if (this.Width)
			c.style.width = this.Width + (Number.IsInt(this.Width) ? 'px' : '');
		if (this.Height)
			c.style.height = this.Height + (Number.IsInt(this.Height) ? 'px' : '');
		
		// title
		var t = this.Title = oNode.CreateNode('div');
		CSS.AddClass(t, 'title');
		oNode.AddNode(t, c);
		t.innerHTML = '&lt;&lt; ' + this.Name;
		
		if (this.Closeable) {
			var close = oNode.CreateNode('span');
			oNode.InsertBefore(close, t.firstChild);
			CSS.AddClass(close, 'close');
			close.innerHTML = 'x';
			
			var self = this;
			close.onclick = function () {
				//c.Visible = false;
				//c.style.visibility = 'hidden';
				
				self.PopupParent &&
				typeof self.PopupParent.PopupClose == 'function' &&
				self.PopupParent.PopupClose();
				
				// 移除当前级别及所有关联级别
				self.Frame.Popup.RemoveAllFromLevel(self.Level);
			};
		}
		
		// attribute
		var a1 = this.AttributeContainer = oNode.CreateNode('div');
		oNode.AddNode(a1, c);
		CSS.SetDisplay(a1, false);
		var a_label = oNode.CreateNode('label');
		oNode.AddNode(a_label, a1);
		a_label.innerHTML = 'attribute:';
		var a = this.Attribute = oNode.CreateNode('div');
		CSS.AddClass(a, 'in-attribute');
		oNode.AddNode(a, a1);
		
		// static attribute
		var a2 = this.SAttributeContainer = oNode.CreateNode('div');
		oNode.AddNode(a2, c);
		CSS.SetDisplay(a2, false);
		var sa_label = oNode.CreateNode('label');
		oNode.AddNode(sa_label, a2);
		sa_label.innerHTML = 'static attribute:';
		var sa = this.SAttribute = oNode.CreateNode('div');
		CSS.AddClass(sa, 'in-attribute');
		oNode.AddNode(sa, a2);
		
		// function
		var f1 = this.FunctionContainer = oNode.CreateNode('div');
		oNode.AddNode(f1, c);
		CSS.SetDisplay(f1, false);
		var f_label = oNode.CreateNode('label');
		oNode.AddNode(f_label, f1);
		f_label.innerHTML = 'function:';
		var f = this.Function = oNode.CreateNode('div');
		CSS.AddClass(f, 'in-function');
		oNode.AddNode(f, f1);
		
		// static function
		var f2 = this.SFunctionContainer = oNode.CreateNode('div');
		oNode.AddNode(f2, c);
		CSS.SetDisplay(f2, false);
		var sf_label = oNode.CreateNode('label');
		oNode.AddNode(sf_label, f2);
		sf_label.innerHTML = 'static function:';
		var sf = this.SFunction = oNode.CreateNode('div');
		CSS.AddClass(sf, 'in-function');
		oNode.AddNode(sf, f2);
		
		return c;
	},
	
	AddAttribute : function (attribute /* : Class Diagram.Attribute */) {
		if (this.AttributeContainer.style.display == 'none')
			this.AttributeContainer.style.display = '';
		this.Attributes[attribute.Name] = attribute;
		oNode.AddNode(attribute.Container, this.Attribute);
		attribute.Level = this.Level;
		attribute.Frame = this.Frame;
	},
		
	AddAttributes : function (attributes /* : Array */) {
		for (var i = 0, len = attributes.length; i < len; i++) {
			var attr = new Diagram.Attribute(attributes[i][0]);
			attr.Comment = attributes[i][2];
			attr.Create();
			this.AddAttribute(attr);
		}
	},
	
	AddSAttribute : function (attribute /* : Class Diagram.Attribute */) {
		if (this.SAttributeContainer.style.display == 'none')
			this.SAttributeContainer.style.display = '';
		this.SAttributes[attribute.Name] = attribute;
		oNode.AddNode(attribute.Container, this.SAttribute);
		attribute.Level = this.Level;
		attribute.Frame = this.Frame;
	},
	
	AddSAttributes : function (attributes /* : Array */) {
		for (var i = 0, len = attributes.length; i < len; i++) {
			var attr = new Diagram.Attribute(attributes[i][0]);
			attr.Comment = attributes[i][2];
			attr.Create();
			this.AddSAttribute(attr);
		}
	},

	AddFunction : function (_function /* : Class Diagram.Function */) {
		if (this.FunctionContainer.style.display == 'none')
			this.FunctionContainer.style.display = '';
		this.Functions[_function.Name] = _function;
		oNode.AddNode(_function.Container, this.Function);
		_function.Level = this.Level;
		_function.Frame = this.Frame;
	},
	
	AddFunctions : function (functions /* : Array */) {
		for (var i = 0, len = functions.length; i < len; i++) {
			var func = new Diagram.Function(functions[i][0]);
			func.Comment = functions[i][2];
			func.Create();
			this.AddFunction(func);
		}
	},

	AddSFunction : function (_function /* : Class Diagram.Function */) {
		if (this.SFunctionContainer.style.display == 'none')
			this.SFunctionContainer.style.display = '';
		this.SFunctions[_function.Name] = _function;
		oNode.AddNode(_function.Container, this.SFunction);
		_function.Level = this.Level;
		_function.Frame = this.Frame;
	},
	
	AddSFunctions : function (functions /* : Array */) {
		for (var i = 0, len = functions.length; i < len; i++) {
			var func = new Diagram.Function(functions[i][0]);
			func.Comment = functions[i][2];
			func.Create();
			this.AddSFunction(func);
		}
	}
};