$('#feedback').live('click', function(){
	// click to remove a flash message
	$(this).fadeOut();
});

function niceAlert(txt){
	// trigger a nicely styled alternative to alert
	var alert = '<div id="feedback"><div class="message">' + txt + '<div class="button">Close</div></div></div>';
	$('body').after(alert);
}

// validate email address on contact form 
function checkemail(){
	var testresults;
	var str = document.email.sender_email.value;
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str)){
		testresults = true;
	} else {
		niceAlert("Please input a valid email address!");
		testresults = false;
	}
	return (testresults);
}

// highlight form fields when selected
if ($('form#contact').length){
	$('#contact input[class!=submit], #contact textarea').each(function(){
		var field = $(this);
		field.focus(function(){
			field.addClass('selected');
		});
		field.blur(function(){
			field.removeClass('selected');
		});
	});
}

/*
 * Any input with populated class will remove its default text when focused on
 * if the field is made to be blank again, the default text will reappear
 */
$('input.populated').each(function(){
    $(this).attr('title', $(this).val());
	
	$(this).focus(function(){
		if (this.value == this.title) this.value = '';
	});
	$(this).blur(function(){
		this.value = (this.value != '') ? this.value : this.title;
	});
});

/*
 * check page for DIV.flash object, load swfObject library into page 
 * if its not already loaded. Then scrape the container for variables,
 * load the scraped variables into swfObject call
 */
var swfObjectLoaded = false;
$('.flash-object').each(function(i, container){
	// check if swfOject loaded, if not add to page
	if (!swfObjectLoaded){
		if ($('script[src*="swfobject"]').length > 0){
			swfObjectLoaded = true;
		} else {
			var script = document.createElement('script');
			var url = 'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js';
			script.setAttribute('src', url);
			document.body.appendChild(script);
		}
	}
	
	// gather all variables for the flash object
	var flashvars = {};
	var params = {};
	var attributes = {};
	var object = $(container);
	var obj_id = 'flash-object-' + i;
	object.attr('id', obj_id);
	var swf_path = $('#' + obj_id + ' .path').text();
	var width = parseInt(object.css('width'));
	var height = parseInt(object.css('height'));
	
	// collect flashvars / params / attributes
	$('#' + obj_id + ' .flashvars div').each(function(){
		var objectName = $(this).attr('class');
		flashvars[objectName] = $(this).text();
	});
	$('#' + obj_id + ' .params div').each(function(){
		var objectName = $(this).attr('class');
		params[objectName] = $(this).text();
	});
	$('#' + obj_id + ' .attributes div').each(function(){
		var objectName = $(this).attr('class');
		attributes[objectName] = $(this).text();
	});
	
	$(function() {
		// do this when the page has finished loading
		swfobject.embedSWF(swf_path, obj_id, width, height, "9.0.0", false, flashvars, params, attributes);
	});
});
