var Component = Class.create ({

	initialize: function(name, owner, editMode){
		this.name = name;
		this.owner = owner;
		this.editMode = editMode;
		this.components = [];
		this.listeners = [];
	},

	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;
		}
	},

	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;
	},

	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)
		{
			el.show();
			//this.fadeIn(el);
		}
		return true;
	},

	hide: function(){
		el = $(this.owner.name + ":" + this.name) != null ? $(this.owner.name + ":" + this.name) : $(this.name);
		if (el != null)
		{
			//this.fadeOut(el);
			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;
	},

	showElement: function(el){
		$(el).show();
	},

	hideElement: function(el){
		$(el).hide();
	},

	removeElement: function(el){
		el.remove();
	},

	popupMessage: function(title, message, image, buttons){
		var popup = $(this.name + 'Popup');

		if (!popup)
		{
			popup = new Element('div', {id: this.name + "Popup", className: 'popup component'});
			popup.appendChild(new Element('div', {id: this.name + "PopupTitle", className: 'componentTitle popupTitle'}));
			popup.appendChild(new Element('div', {id: this.name + "PopupContent", className: 'popupContent'}));
			popup.appendChild(new Element('div', {id: this.name + "PopupButtons", className: 'popupButtons'}));

			$('messages').appendChild(popup);
		}
		else 
		{
			$(this.name + "PopupTitle").removeChild($(this.name + "PopupTitle").firstChild);

			while($(this.name + "PopupContent").hasChildNodes())
				$(this.name + "PopupContent").removeChild($(this.name + "PopupContent").firstChild);

			while($(this.name + "PopupButtons").hasChildNodes())
				$(this.name + "PopupButtons").removeChild($(this.name + "PopupButtons").firstChild);
		}

		$(this.name + "PopupTitle").appendChild(document.createTextNode(title));

		if (image != null)
			$(this.name + "PopupContent").appendChild(new Element('img', {className: 'popupIcon', src: image}));

		$(this.name + "PopupContent").appendChild(document.createTextNode(message));

		if (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]));
				$(this.name + "PopupButtons").appendChild(button);
			}

			$(this.name + "PopupButtons").appendChild(new Element('div', {className: 'clear'}));
		}

		new Popup(popup.id, null, {modal: true});
		$(popup.id).popup.show();		
	},

	pleaseWait: function(message, image){
		this.popupMessage.delay(0.5, "Please Wait", message, image, null);
	},

	confirm: function(message){
		return confirm(message);
	}
});
