var mouse = {
	x: 0,
	y: 0
};

Event.observe(document, 'mousemove', function($event) {
	mouse.x = $event.pointerX();
	mouse.y = $event.pointerY();
});

String.prototype.reverse = function(){
	splitext = this.split("");
	revertext = splitext.reverse();
	reversed = revertext.join("");
	return reversed;
}
String.prototype.numberFormat = function(){
	var $string = this
	// Reverse the string
	$string = $string.reverse();
	// Format the number
	$string = $string.replace(/([0-9]{3})/g, '$1,');
	// Strip any wayward commas and turn the right way 'round
	$string = $string.reverse().replace(/^,/, '');
	return $string
}

// Asset Calculator
Event.observe(document, 'dom:loaded', function() {
	// Attach behaviours to list items and inputs
	$('asset-calculator').select('li').each(function($line_item, $i) {
		// Mouse over
		Event.observe($line_item, 'mouseenter', function ($event) {
			this.addClassName('hover');
			this.select('p')[0].show();
		});
		// Mouse out
		Event.observe($line_item, 'mouseleave', function ($event) {
			/*if(!Element.descendantOf($event.findElement(), this) && !this.select('input')[0].hasFocus) {*/
				this.removeClassName('hover');
				this.select('p')[0].hide();
			/*}*/
		});
		// Input focus
		Event.observe($line_item.select('input')[0], 'focus', function($event) {
			// Set focus attribute
			this.hasFocus = true;
			
			var line_item = this.parentNode;
			
			// Set parent display state
			line_item.addClassName('hover');
			line_item.select('p')[0].show();
			
			// Scroll window
			// if($('asset-calculator').select('li').indexOf(line_item) > 4 && !Prototype.Browser.IE) Effect.ScrollTo($('asset-calculator').select('li')[$('asset-calculator').select('li').indexOf(line_item)-5]);
		});
		// Input blur
		Event.observe($line_item.select('input')[0], 'blur', function($event) {
			// Set focus attribute
			this.hasFocus = false;
			
			var line_item = this.parentNode;
			
			var mouseInBounds = (mouse.x > line_item.cumulativeOffset().left && mouse.x < (line_item.cumulativeOffset().left+line_item.getDimensions().width)) && (mouse.y > line_item.cumulativeOffset().top && mouse.y < (line_item.cumulativeOffset().top+line_item.getDimensions().height));
			
			// Check mouse position
			if(!mouseInBounds) {
				// Set parent display state
				line_item.removeClassName('hover');
				line_item.select('p')[0].hide();
			}
		});
		// Input change
		Event.observe($line_item.select('input')[0], 'change', function($event) {
			var total = 0;
			var inputs = $('asset-calculator').select('input');
			
			// Loop over all input fields to get a total
			for(i = 0; i < inputs.length; i++) {
				var value = parseInt($F(inputs[i]).replace(/\..*$|[^0-9]*/g, ''));
				total += isNaN(value)? 0: value;
				inputs[i].value = isNaN(value)? '': value.toString().numberFormat();
			}
			
			$('asset-total').update((total > 0)?total.toString().numberFormat(): ' --');
		});
	});
});
