//	Copyright (c) 2009 Marc Fowler (http://dfr.me)
//	
//	Permission is hereby granted, free of charge, to any person
//	obtaining a copy of this software and associated documentation
//	files (the "Software"), to deal in the Software without
//	restriction, including without limitation the rights to use,
//	copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the
//	Software is furnished to do so, subject to the following
//	conditions:
//	
//	The above copyright notice and this permission notice shall be
//	included in all copies or substantial portions of the Software.
//	
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//	OTHER DEALINGS IN THE SOFTWARE.

var MooButton = new Class({
	Implements: [Options],
	options: {
		'height': 40,
		'width': 120,
		'styles': {
			'background': 'url(MooButtons/button_sprite.png) no-repeat 0 0',
			'text-align': 'center',
			'cursor': 'pointer',
			'color': '#fff',
			'margin': 0,
			'padding': 0,
			'border': 'none'
		},
		'classes': {
			'hover': 'moobutton_hover'
		}
	},
	initialize: function(el, options) {
		this.setOptions(options);
		var el = $(el);
		if(!el) return false;
		if($type(this.options.height) == 'string') this.options.height = this.options.height.toInt();
		if($type(this.options.width) == 'string') this.options.width = this.options.width.toInt();
		var klass = this;
		this.button = new Element('div', {
			'text': el.get('text'),
			'id': el.get('id'),
			'styles': $merge({'height': klass.options.height, 'width': klass.options.width, 'line-height': klass.options.height}, klass.options.styles),
			'events': {
				'mousedown': function() {
					this.setStyle('background-position', '0 -'+(2 * klass.options.height)+'px');
				},
				'mouseup': function() {
					this.setStyle('background-position', '0 -'+klass.options.height+'px');
				},
				'mouseenter': function() {
					this.setStyle('background-position', '0 -'+klass.options.height+'px').addClass(klass.options.classes.hover);
				},
				'mouseleave': function() {
					this.setStyle('background-position', '0 0').removeClass(klass.options.classes.hover);
				},
				'selectstart': $lambda(false)
			}
		}).replaces(el);
		this.button.cloneEvents(el);
		this.button.unselectable = 'on';
		if(el.get('href')) {
			this.button.addEvent('click', function() {
				window.location = el.get('href');
			});
		}
	}
});
