
btns = {};

ButtonComponent = Class.create();
ButtonComponent.prototype = {
	
	initialize: function(parent, target, icon, label, action, disable_behaviour, openWin) {
		
		this.parent = parent;
				
		if (typeof(target) == 'string') {
			this.id = target;
			this.self = $(this.id);
		} else {
			this.self = target;
			this.id = target.id ? target.id : target.name;
		}
		
		if (action) {
			if (openWin) {
				this.action = function() {
					window.open(action);
				};
			} else {
				this.action = function() {
					document.location = action;	
				};
			}
		} 
		
		this.injectBehaviour(icon, label, action, disable_behaviour);
		
	},
	
	injectBehaviour: function(icon, label, action, disable_behaviour) {
		
		this.self.className = 'button';
		
		if (this.self.getElementsByTagName('div').length >= 4) {
			this.self.innerHTML = '';
		}
		
		this.createBtnStructure(icon, label);
				 		
		if (!disable_behaviour) {			
			this.self.onclick = this.click.bindAsEventListener(this);
			this.self.onmouseover = this.over.bindAsEventListener(this);
			this.self.onmouseout = this.out.bindAsEventListener(this);
		}
		
		//if (console) console.log(this.self.onclick);
	},
	
	createBtnStructure: function(icon, label) {
		
		var lcap = document.createElement('div');
		lcap.className = 'leftcap';
		
		var fill = document.createElement('div');
		fill.className = 'fill';
		
		if (icon) {
			var icon_div = document.createElement('div');
			icon_div.className = 'icon '+icon;
			fill.appendChild(icon_div);
		}
		
		if (label) {
			var label_div = document.createElement('div');
			label_div.className = 'btn_labels';
			label_div.appendChild(document.createTextNode(label));
			fill.appendChild(label_div);
		}
		
		var rcap = document.createElement('div');
		rcap.className = 'rightcap';
		
		this.self.appendChild(lcap);
		this.self.appendChild(fill);
		this.self.appendChild(rcap);
		
		// if (console) console.log(RicoUtil.getContentAsString(this.self));
		
	},
	
	click: function(e) {
		if (!this.disabled && this.action) {
			this.action();
		}
		//this.self.style.cursor = '';					   
	}, 
	
	over: function(e) {
		if (!this.disabled) {
			this.self.style.cursor = 'pointer';	
		}
	},
	
	out: function(e) {
		if (!this.disabled) {
			this.self.style.cursor = '';
		}
	},
	
	setAction: function(func) {
		if (typeof(func) == 'function') {
			this.action = func.bindAsEventListener(this.parent);
		} else {
			this.action = function() {
				document.location = func;	
			};
		}
	},
	
	disable: function() {
		this.disabled = true;	
		new Effect.Opacity(this.id,
							{ duration: 0.0, 
							  from: 1.0, to: 0.5 });
	},
	
	enable: function() {
		this.disabled = false;	
		new Effect.Opacity(this.id,
							{ duration: 0.0, 
							  from: 0.5, to: 1.0 });
	}
	
};