// global.js


/* opening events */

window.addEvent('domready', function() {

	// fix flash
	fixFlash($('flash'));
	
	// ajax links
	readyLinks();
	
	
	// email links
	$$('a.email').addEvent('click', function() {
		noSpam(this.innerHTML);
	});
	
	// mailing list form target
	$('form_ml').setProperty('target', 'ml_frame');
	
	// blog comment form ajax variable
	readyCommentForm();
	
});


/* fix flash */
function fixFlash(el) {
	if (window.ie) {
		if (!el) el = this;
		el.innerHTML = el.innerHTML;
	}
}

/* ready ajax links */
function readyLinks() {
	$$('a.ajax').addEvent('click', function() {
		var p = this.href.split('http://').join('');
		p = p.split('www.').join('');
		p = p.split('cannonsonline.org').join('');
		if (p.charAt(0) == '/') {
			p = p.substr(1, p.length);
		}

		getPage(p);
	});
}

/* ready ajax comment forms */
function readyCommentForm() {
	if ($('c_ajax')) {
		$('c_ajax').value = '1';
	}
}

/* validate forms */
function validate(instance) {
	var email_re = /(@\w[-._\w]*\w\.\w{2,4})$/;
	var message = false;
	var focused = false;	
	
	if (instance == 'ml') {
		var form = $('form_ml');

		if (!email_re.test($('ml_email').getValue())) {
			message = 'Please enter a valid email address.';
			focused = $('ml_email');
		}
		if ($('ml_name').getValue() == '' || $('ml_name').getValue() == 'name') {
			message = 'Please enter your name.';
			focused = $('ml_name');
		}
		
		if (message) { // invalid
			alert(message);
			focused.focus();
		} else { // valid
			if ($('ml_name').getValue() == 'name') {
				$('ml_name').value = "";
			}
			form.submit();
			$('ml_name').value = "Thank You!"
			$('ml_email').value = '';			
		}
		
	} else if (instance == 'c') {
		var form = $('form_comment');
		
		if ($('c_body').getValue() == '') {
			message = 'Please enter your comment.';
			focused = $('c_body');
		}
		if (!email_re.test($('c_email').getValue())) {
			message = 'Please enter a valid email address.';
			focused = $('c_email');
		}
		if ($('c_name').getValue() == '') {
			message = 'Please enter your name.';
			focused = $('c_name');
		}
		
		if (message) { // invalid
			alert(message);
			focused.focus();
		} else { // valid
			// send comment via ajax
			showLoading();
			form.send({evalResponse: true});
		}
	}
}

// mailing list
function mlFocus(el) {
	if (el.getValue() == el.id.substr(3)) {
		el.value = '';
	}
}
function mlBlur(el) {
	if (el.getValue() == '') {
		el.value = el.id.substr(3);
	}
}

// no spam
function noSpam(string) {
	string = string.split(' [at] ').join('@');
	window.location = 'mailto:'+string;
}

// load page with ajax
var ajaxing = false;
function getPage(page) {
	if (!ajaxing) {
		showLoading();
		var ajax = new Ajax('/ajax/'+page, {method: 'get', update: 'content', onComplete: hideLoading}).request();
	}
}
function showLoading() {
	ajaxing = true;
	$('content').setStyle('display', 'none');
	$('loading_msg').setStyle('display', 'block');
}
function hideLoading() {
	$('loading_msg').setStyle('display', 'none');
	$('content').setStyle('display', 'block');
	readyCommentForm();
	readyLinks();
	ajaxing = false;
}

