var DOMUtilities = {
	targetBlank: function(locality){
		// XHTML 1.0 Strict work around for external links
		$(locality+' a[rel*="external"]').attr("target","_blank");
	},
	inputAutoClear: function(locality){
		$(locality+' input.clearField').focus(function(){
			if(this.defaultValue == this.value){
				this.value='';
				$(this).addClass('entered');
			}
		}).blur(function(){
			if(this.value == ''){
				this.value = this.defaultValue;
				$(this).removeClass('entered');
			}
		});
	},
	imgRollover: function(locality){
		// Image roll-over setup
		$(locality+' img.rollOver, '+locality+' input[type="image"].rollOver')
			.mouseenter(function(){
				if (this.src.indexOf("_i.") != -1) {
					this.src = this.src.replace("_i.", "_o.");
				}
			}).mouseleave(function(){
				if (this.src.indexOf("_o.") != -1) {
					this.src = this.src.replace("_o.", "_i.");
				}
				if(this.src.indexOf("_a.")) {
					this.src = this.src.replace("_a.","_i.");
				}
			}).filter("input").mousedown(function(){
				this.src = this.src.replace("_o.","_a.");
			}).mouseup(function(){
				this.src = this.src.replace("_a.","_i.");
			});
	},
	initialize: function(locality){
		if(locality == null) {
			locality = "body";
		}
		this.targetBlank(locality);
		this.inputAutoClear(locality);
		this.imgRollover(locality);
	}
}


var MembershipInformation = {
	navs: [],
	sections: [],
	speed: 500,

	initialize: function(){
		var self = this;
		
		if ($('#membership_tabs').length) {
			window.scrollTo(0,0)
			
			this.navs = $('#membership_tabs .tabs a');
			this.sections = $('#membership_tabs .tabBody .tab');
			this.sectionContainer = $('#membership_tabs .tabBody');
			
			var selected_navigation = (document.location.hash == "" ? "#" + this.sections[0].id : document.location.hash);
			
			for(var i=0; i<this.navs.length; i++){
				if(this.navs[i].href.match(selected_navigation)){
					$(this.navs[i]).addClass('active');
				}
				if(this.sections[i].id != selected_navigation.replace("#","")){
					this.sections[i].style.display = "none";
				}
			}
			
			this.sections.css({
				position: 'absolute',
				top: 0,
				left: 0,
				borderTop: 'none'
			});

			this.sectionContainer.css({
				height: $(selected_navigation).outerHeight()
			});
			
			this.navs.click(function(event){
				event.preventDefault();
				var hash = this.href.split('#')[1];
				
				self.navs.removeClass('active')
				$(this).addClass('active');
				
				if($.browser.msie){
					self.speed = 0;
				}
				
				self.sections.not('#' + hash).fadeOut(self.speed);
				$('#' + hash).fadeIn(self.speed);
				self.sectionContainer.animate({
					height: $('#' + hash).outerHeight()
				}, self.speed);
			});
		}
	}
};


$(document).ready(function(){
	DOMUtilities.initialize();
	MembershipInformation.initialize();

	if(typeof($.fn.scrollTo) == "function"){
		$('a.scrollTo').click(function(event){
			event.preventDefault();
			
			var destination = this.href.split("#")[1];
			
			$.scrollTo('#' + destination, 750, {
				easing: 'swing',
				offset: {
					top: -30
				}
			});
		});
	}
	
	$('a.btnPrint').click(function(event){
		event.preventDefault();
		window.print();
	});
	
	$('.feed a').removeAttr('target');
	
});
