
// events.js - applies default event handler functionality

$(function() {
	
	// mouseover/out handler
	// applies relevant classes, img src swapping and status text
	var movement = function(element, over) {
		
		var img, status = '';
		
		// toggles img src
		var src = function(img, to, from) {
			var src = String(img.attr('src'));
			if (src.indexOf(from) > 0) {
				img.attr('src', src.replace(from, to));
			}
		};

		if (element.tagName == 'INPUT') {
			if (element.type == 'image') {
				// input type of image will swap src
				img = $(element);
			} else {
				// non-image button input types swap class
				$(element).addClass(over ? 'bnover' : 'bnout');
				$(element).removeClass(over ? 'bnout' : 'bnover');
				// use button input type's value as status
				if (over) {statusText = element.value;}
			}
		} else {
			// non-input buttons - typically an anchor
			// swap the src of the first image found
			img = $(element).find('img:first');
			if (!img) {
				// swap the class if no image found
				$(this).attr('class', over ? 'rollover' : '');
			}
			// use the anchor's text content for status
			if (over) {
				status = element.innerText;
				if (!status) {status = element.text;}
			}
		}
		
		// toggle img src if we have a valid img
		if (img) {
			src(img, '-' + (over ? 'b' : 'a') + 
				'.', '-' + (over ? 'a' : 'b') + '.');
			src(img, '_' + (over ? 'b' : 'a') + 
				'.', '_' + (over ? 'a' : 'b') + '.');
		}
		
		// if we don't have status text and we have an img, use its alt text
		if (over && !status && img) {status = $(img).attr('alt');}
		// apply status
		window.status = status;
						
	};
	
	// apply mouseover/out handlers to button type elements
	var buttons = $('a, area, :image, :button, :reset, :submit');
	buttons.mouseover(function() {movement(this, true);});
	buttons.mouseout(function() {movement(this, false);});
	
	// apply focus/blur handlers to text form field elements
	var fields = $(':text, :password, textarea, select');
	fields.focus(function() {$(this).addClass('focus');});
	fields.blur(function() {$(this).removeClass('focus');});
	
	$("a").focus( function () { $(this).blur(); } )

});
