/* Global Fort Reno JavaScript
All Fort Reno JavaScript *must* live in the FORTRENO namespace
*/

/* Define Fort Reno namespace FORTRENO */
if( typeof FORTRENO === "undefined" || !FORTRENO ) { var FORTRENO = {}; }

FORTRENO.namespace = function() {
	var a=arguments, o=null, i, j, d;
	for (i=0; i<a.length; i=i+1) {
		if( typeof(a[i]) !== 'string' ) { continue; }
		d=a[i].split(".");
		o=window;
		for (j=0; j<d.length; j=j+1) {
			o[d[j]]=o[d[j]] || {};
			o=o[d[j]];
		}
	}
	return o;
};

FORTRENO.define = function( a ) {
	return FORTRENO.namespace( "FORTRENO." + a );
};

/* IE Polyfills */
FORTRENO.define( 'IE' );
FORTRENO.IE = function() {
	
	// <=IE8
	if( $.browser.version < 9 ) {
		
		// Input types and buttons
		$('input:text, input[type="text"]').addClass('input-text');
		$('input:checkbox, input:radio').addClass('input-check');
		$('input[type="submit"]').addClass( 'input-submit' );
		$('input[type="button"]').addClass( 'btn-submit' );
		$('input[type="search"]').addClass('input-search');
		$('input[type="email"]').addClass('input-email');
		
		// Last children
		//$('element:last-child').addClass('last-child');
	}
	
	// <=IE7
	if( $.browser.version < 8 ) {
		// ...
	}
	
	// <=IE6
	if( $.browser.version < 7 ) {
		// Hide value
		$('.search input[type="submit"]').val('');

		// First Children
		$('nav li:first-child').addClass('first-child');
		
		// Children
		//$('parent > child').addClass('child');
	}
};

FORTRENO.define( 'DonateLinks' );
FORTRENO.DonateLinks = function() {
	
	/* 
	Hide the donate form.
	Convert the link with class "donate" to do our bidding: submitting the PayPal goto Donate Form 
	*/
	if( $("#donate-form-wrapper") )
	{
		$("#donate-form-wrapper").hide();
		$(".donate").click(function(event){
			event.stopPropagation();
			$("#donate").submit();
			return false;
		});
	}
};

$(document).ready(function() {
	
	// Run polyfills
	if( $.browser.msie ) {
		var ie = new FORTRENO.IE();
	}
	
	var donateLinks = new FORTRENO.DonateLinks();
	
	/*
	Protects mailto: links from exploit - use JavaScript to create mailto:some.person@some.where.com on the fly
	*/
	if ($("span.emailaddress")) {
		$("span.emailaddress").each(function(){
			var mailtag = $(this);
			var at = / at /;
			var addr = $(mailtag).text().replace(at,"&#64;");
			$(mailtag).after('<a href="mailto:'+addr+'">Amanda</a>');
			$(mailtag).remove();
		});
	}
});
