gShowDuration = 550;
gHideDuration = 350;
gNavIsExpanded = false;
gDelayedExpansionFunctionId = null;

window.addEvent('domready', function() {
	$$('body')[0].addClass(Browser.Engine.name);
	$$('body')[0].addClass('ver' + Browser.Engine.version);
	//new Element('p', { html: Browser.Engine.version }).inject($$('body')[0], 'top');
	
	$('nav_mainSections').addEvent('mouseenter', delayedOpen);
	$('nav_mainSections').addEvent('mouseleave', mouseLeftMainSections);
	$$('.mainSection_subsection').each(function(subsection) {
		subsection.addEvent('mouseenter', mouseEnteredSubsection);
		subsection.addEvent('mouseleave', mouseLeftSubsection);
	});
	
	$('layout_navExpandedBackground').fade('hide');
	$('layout_navExpandedBackground').setStyle('display', 'block');
	$('mainSections_subsections').fade('hide');
	
	$$('.cartSummaryLink').each(function(a) { a.set('html', 'Checkout'); });
	
	setSubscriptionListUsingReferrer();
	
	addHelperTextToSubscriptionStickyBoxFields();
	
	if ($('truncatedBlogPostPreviews')) {
		var ul = new Element('ul');
		ul.inject($('truncatedBlogPostPreviews'), 'top');
		$('truncatedBlogPostPreviews').getChildren('.blog-post').each(function(post) {
			var postBody = post.getFirst('.post-body');
			var children = postBody.getChildren();
			var oldHeader = post.getFirst('h1');
			var newHeader = new Element('p', { html: oldHeader.get('html') });
			newHeader.replaces(oldHeader);
			postBody.empty();
			var li = new Element('li');
			ul.adopt(li);
			li.adopt(post);
			
			/*if (children.length >= 5) {
				var link = newHeader.getFirst().clone();
				link.set('html', 'Read More...');
				var elements = [];
				(5).times(function(n) { elements.push(children[n].dispose()); });
				postBody.empty();
				elements.each(function(e) { postBody.adopt(e); });
				postBody.adopt(link);
			}*/
		});
		$('truncatedBlogPostPreviews').setStyle('display', 'block');
	}
});

function setSubscriptionListUsingReferrer() {
	var form = $('subscriptionCalloutForm');
	if (!form) return;
	var listId = 22178;
	if (window.location.search.match(/referrer=aspiremag.net/)) listId = 30082;
	form.set('action', form.get('action') + listId);
}

function addHelperTextToSubscriptionStickyBoxFields() {
	if (!$('subscriptionCalloutForm')) return;
	addHelperTextToField($('FirstName'), "Your First Name");
	addHelperTextToField($('LastName'), "Your Last Name");
	addHelperTextToField($('EmailAddress'),"Your Email Address");
	addHelperTextToField($('CaptchaV2'), "Verify the letters above");
	addHelperTextToField($('CLFullName'), "Your Full Name");
	addHelperTextToField($('CLEmailAddress'), 'Your Email Address');
}

function addHelperTextToField(elem, text) {
	if (!$(elem)) return;
	elem.set('value', text);
	elem.addEvent('focus', function() {
		if (elem.get('value') == text) elem.set('value', '');
	});
	elem.addEvent('blur', function() {
		if (elem.get('value') == '') elem.set('value', text);
	});
}

var delayedOpen = function(event) {
	if (gNavIsExpanded) return;
	gDelayedExpansionFunctionId = mouseEnteredMainSections.delay(500, event);
};

function submitSubscriptionCallout() {
	$('subscriptionCalloutForm').submit();
}

var mouseEnteredMainSections = function() {
	var event = this;
	gNavIsExpanded = true;
	// Accomodate the subsections
	new Fx.Tween('layout_nav', { property: 'height', duration: gShowDuration }).start(239);
	// Extend the mouseover area
	new Fx.Tween('nav_mainSections', { property: 'height', duration: gShowDuration }).start(180);
	new Fx.Tween('layout_navExpandedBackground', { property: 'opacity', duration: gShowDuration }).start(1);
	$('mainSections_subsections').setStyle('display', 'block');
	new Fx.Tween('mainSections_subsections', { property: 'opacity', duration: gHideDuration }).start(1);
};

var mouseLeftMainSections = function(event) {
	$clear(gDelayedExpansionFunctionId);
	gNavIsExpanded = false;
	new Fx.Tween('layout_nav', { property: 'height', duration: gHideDuration }).start(100);
	new Fx.Tween('nav_mainSections', { property: 'height', duration: gHideDuration }).start(50);
	new Fx.Tween('mainSections_subsections', { property: 'opacity', duration: gHideDuration }).start(0).chain(function() {
		$('mainSections_subsections').setStyle('display', 'none').fade('hide');
	});
	new Fx.Tween('layout_navExpandedBackground', { property: 'opacity', duration: gHideDuration }).start(0);
};

var mouseEnteredSubsection = function(event) {
	gNavIsExpanded = true;
	var subsection = event.target.hasClass('mainSection_subsection') ? event.target : event.target.getParent('.mainSection_subsection');
	if (subsection) new Fx.Tween(subsection, { property: 'color', duration: gShowDuration }).start('#FFFFFF');
};

var mouseLeftSubsection = function(event) {
	gNavIsExpanded = true;
	var subsection = event.target.hasClass('mainSection_subsection') ? event.target : event.target.getParent('.mainSection_subsection');
	if (subsection) new Fx.Tween(subsection, { property: 'color', duration: gShowDuration }).start('#D7C2A9');
}
