sense.ui = {};
sense.ui.Base = Class.create(sense.Base, {
	// Properties
	element: null,
	options: null,

	// Methods
	initialize: function($super, element, options, defaults) {
		// Call Parent Constructor
		$super();

		// Intialise properties
		this.element = element;
		this.options = {};

		// Initialise properties with any defaults passed into the constructor
		this.setOptions(defaults);
		// Over-ride properties with any options passed into the constructor
		this.setOptions(options);
		// Over-ride properties with any options specified via the "_options" attribute
		this.setOptions(this.parseElementOptions());
	},

	setOptions: function(options) {
		Object.extend(this.options, options);
	},
	
	parseElementOptions: function() {
		if (!this.element || !this.element.nodeType) return;

		// Extract any key-value option pairs from the _options="..." value
		// Options should be in the format "key1=val1;key2=val2;key3=val3"
		var optString = this.element.readAttribute('_options');
		if (!optString) return;

		var opts = {};
		var optPairs = optString.split(';');
		optPairs.each(function(pair){
			var pair = pair.split('=');
			var key = pair[0].strip();
			var val = unescape(pair[1].strip());
	
			// Try to do a bit of simple conversion so
			// everything isn't just a string by default
			if (val === "true") val = true;
			if (val === "false") val = false;
			if (val === Number(val).toString()) {
				val = Number(val);
			}

			opts[key] = val;
		});

		return opts;
	},
	
	px: function(n) {
		// Try and make sure we don't use any non-integer values for CSS
		// pixel measurements since they can cause slight visual oddities
		return (typeof n == 'string') ? n : Math.round(n || 0) + 'px';
	}
});