var Component = Class.create ({

	initialize: function(name, owner, editMode){
		this.name = name;
		this.owner = owner;
		this.editMode = editMode;
		this.components = [];
		this.listeners = [];
		this.popup = null;
	},

	getClass: function() {
		return "Component";
	},

	addComponent: function(component){
		if (component.owner.name == this.name)
		{
			this.components.push(component);
			return true;
		}
		else for (var i = 0; i < this.components.length; i++)
		{
			if (this.components[i].addComponent(component))
				break;
		}
	},

	removeComponent: function(name){
		for (var i = 0; i < this.components.length; i++)
		{
			if (this.components[i].name == name)
			{
				return true;
			}
		}

		return false;
	},

	getComponent: function(name){
		for (var i = 0; i < this.components.length; i++)
			if (this.components[i].name == name)
				return this.components[i];

		return null;
	},

	getComponentPath: function(){
		if (this.owner == null)
			return this.name;
		else return this.owner.getComponentPath() + '.getComponent("' + this.name + '")';
	},

	addListener: function(component){
		this.listeners.push(component);
	},

	sendEvent: function(event, args){
		for (var i = 0; i < this.listeners.length; i++)
			this.listeners[i].receiveEvent(this, event, args);

		return true;
	},

	receiveEvent: function(sender, event, args){
		return true;
	},

	setEditMode: function(editMode, overwrite){
		this.editMode = editMode;

		for (var i = 0; i < this.components.length; i++)
			this.components[i].setEditMode(editMode, overwrite);

		return true;
	},

	getEditMode: function(){
		return this.editMode;
	},

	setOpacity: function(el, value)
	{
		el.style.opacity = value/10;
		el.style.filter = 'alpha(opacity=' + value*10 + ')';
		return true;
	},

	fadeIn: function(el){
		new Effect.Opacity(el, { from: 0, to: 1.0, duration: 0.3 });
		return true;
	},

	fadeOut: function(el){
		new Effect.Opacity(el, { from: 1.0, to: 0, duration: 0.2 });
		return true;
	},

	openDown: function(el){
		new Effect.BlindDown(el, { duration: 0.2});
		return true;
	},

	closeUp: function(el){
		new Effect.BlindUp(el, { duration: 0.2});
		return true;
	},

	show: function(){
		el = $(this.owner.name + ":" + this.name) != null ? $(this.owner.name + ":" + this.name) : $(this.name);

		if (el != null)
		{
			this.setOpacity(el, 0);
			el.show();
			this.fadeIn.delay(0.1, el);
		}

		return true;
	},

	hide: function(){
		el = $(this.owner.name + ":" + this.name) != null ? $(this.owner.name + ":" + this.name) : $(this.name);

		if (el != null)
			el.hide();

		return true;
	},

	getVisible: function(){
		el = $(this.owner.name + ":" + this.name) != null ? $(this.owner.name + ":" + this.name) : $(this.name);
		return el.visible();
	},

	clear: function(){
		for (var i = 0; i < this.components.length; i++)
			this.components[i].clear();

		return true;
	},

	removeChildNodes: function(el)
	{
		while (el.childNodes.length > 0)
			el.removeChild(el.lastChild);
	},

	popupMessage: function(title, message, image, buttons, className){
		var template = '<div class="component popup"><div class="popupTitle">' + title + '<div class="panel-hd"></div></div><div class="popupContent"><img src="TrinityLogoSmall.gif"/><div class="panel-bd"></div></div><div class="popupBottom"></div></div>';

		this.hidePopupMessage();

		$("popupMessage").appendChild(document.createTextNode(message));
		
		if ((buttons != null) && (buttons.length > 0))
		{
			for (var i = 0; i < buttons.length; i++)
			{
				var button = new Element('button', {className: 'component button'});
				button.appendChild(document.createTextNode(buttons[i][0]));
				$("popupButtons").appendChild(button);

				switch (buttons[i][1])
				{
					case "mbOK":
						button.onclick = Function(this.getComponentPath() + ".hidePopupMessage(); return false;");
						break;
					default:
						alert("Unknown Button Type: " + buttons[i][1]);
				}
			}

			$("popupButtons").appendChild(new Element('div', {className: 'clear'}));
		}

		this.popup = new glow.widgets.Panel(
			  "#popup", {
			  modal: true,
			  anim: "fade",
			  width: 300,
			  template: template,
			  closeOnMaskClick: false
			}
		);

		window.scroll(0,0);
		this.popup.show();
	},

	hidePopupMessage: function(){
		if (this.popup)
			this.popup.hide();

		this.removeChildNodes($("popupMessage"));
		this.removeChildNodes($("popupButtons"));
	},
	
	pleaseWait: function(message, image){
		this.popupMessage.delay(0.3, "Please Wait", message, image, null);
	},

	confirm: function(message){
		return confirm(message);
	},

	alert: function(title, message){
		button = ["OK", "mbOK"];
		this.popupMessage(title, message, null, [button]);
	}
});

