/**
 * dependcies:
 * mootools v1.2 (http://www.mootools.net)
 * Author: Mark Hudson, mark@ritetek.com
 */
 

// set MaxxFitness Namespace if not already defined
if (typeof MF == "undefined") {
	var MF = {};
}

MF.winLoaded = false;
MF.domReady = false;
MF.section = null;
 
 /* ------------------------------------------------------------------------------------------------
 * Page initialization
 * ------------------------------------------------------------------------------------------------
 */


window.addEvent('domready',function() {
	MF.domReady = true;
	MF.section = document.body.id;
	MF.pageName = getPageName();
	
	MF.mainNav.init('mainNav');
	
	var tabContent = document.getElements('.tabContentContainer');
	if($chk(tabContent)) {
		tabContent.each(function(tabbedContent) {
			new Tabs(tabbedContent);
		});
	}
	
	if($chk($('mainBanner'))) {
		MF.bannerFader.init('mainBanner');
	}
	if(MF.section == 'contact') {
		MF.requestForm.init('contactForm', 'requestStatus');
	}
});

function getArguments() {
	if(window.location.search.split('?').length < 2) return false;
	
	var args = window.location.search.split('?')[1].split('&');
	var response = Array();
	
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		response[pair[0]] = pair[1];
	}
	
	return response;
}

function getPageName() {
	var path = window.location.pathname;
	var page = path.substring(path.lastIndexOf('/') + 1);
	if(page == '') page = 'index.html';
	
	return page;
}

function ieNavFix(navContainer) {
	if(navigator.appName == "Microsoft Internet Explorer") {
		$$('#' + navContainer + ' li').each(function(elAye) {
			var children = elAye.getChildren();
			if(children.length > 1) {
				elAye.addEvents({
					'mouseover': function() {
						children[1].setStyle('display', 'block');
					},
					'mouseout': function() {
						children[1].setStyle('display', 'none');
					}
				});
				children[1].setStyle('display', 'none');
			}
		});
	}
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=500,left = 590,top = 275');");
}

MF.mainNav = {
	init: function(navContainer) {
		$$('#' + navContainer + ' li').each(function(elAye) {
			var children = elAye.getChildren();
			if(children.length > 1) {
				elAye.set('tween', {duration: 200});
				elAye.addEvents({
					'mouseenter': function(e) {
						var action = 'in';
						if(navigator.appName == "Microsoft Internet Explorer" && typeof document.body.style.maxHeight == "undefined") {
							action = 'show';
						}
						children[1].fade(action);
					},
					'mouseleave': function(e) {
						var action = 'out';
						if(navigator.appName == "Microsoft Internet Explorer" && typeof document.body.style.maxHeight == "undefined") {
							action = 'hide';
						}
						children[1].fade(action);
					}
				});
				children[1].setStyle('display', 'block');
				children[1].fade('hide');
			}
		});
	}
}

MF.bannerFader = {
	init: function(container) {
		container = $(container);
		this.banners = container.getElements('li');
		this.active = 0;
		
		this.banners.each(function(banner, i) {
			if(i == 0) {
				banner.setStyle('z-index', 51);
				banner.fade('show');
			}
			else {
				banner.setStyle('z-index', 50);
				banner.fade('hide');
			}
			banner.set('tween', {duration: 2000});
		});
		
		this.swap.periodical(7000, this);
	},
	swap: function() {
		var next = this.active == this.banners.length - 1 ? 0 : this.active + 1;
		
		this.banners.each(function(banner, i) {
			banner.setStyle('z-index', i == next ? 51 : 50);
		});
		
		this.banners[this.active].fade('out');
		this.banners[next].fade('in');
		
		this.active = next;
	}
}

MF.requestForm = {
	init: function(formId) {
		document.getElement('.verifyBox').getElement('img').set('src', 'includes/verificationImage.php?' + $random(1000, 9999));
		$('verif_code').set('value', '');
		var myFormValidation = new Validate(formId, {
			errorClass: 'error',
			onFail: 'MF.requestForm.validationFail',
			onSuccess: 'MF.requestForm.submitRequest'
		});
	},
	validationFail: function(list) {
		var listHTML = this.parseList(list);
		$('requestStatus').set('html', '<div class="error">Please correct the following fields: ' + listHTML + '</div>');
	},
	submitRequest: function(form) {
		new Request({
			url:'includes/contactProxy.php',
			method: 'post',
			onSuccess: function(content) {
				$('requestStatus').set('html', content);
				$('contactForm').destroy();
			}
		}).send(form.toQueryString());
	},
	parseList: function(list) {
		var ret_val = '';
		list = list.getChildren();
		for(var i = 0; i < list.length; i++) {
			ret_val += '<span>' + list[i].getFirst().get('text') + '</span>';
		}
		return ret_val;
	}
}