﻿$(function() {
	// readjust the navigation menu hyperlinks' right and left padding
	if ($('nav').length > 0)
	{
		// initializing some variables
		var listTotalWidth = 0;
		var padding = 0;
		var subMenuWidth = 250;
		
		// calculate the total width of the navigation menu's list items
		$('nav > ul > li').each(function() {
			listTotalWidth += $(this).outerWidth();
		});
		
		// calculate the right and left padding for each hyperlink
		padding = Math.floor((($('nav ul').width() - listTotalWidth) / $('nav > ul > li > a').length) / 2);
		// reset listTotalWidth so that we can reuse it further down
		listTotalWidth = 0;
		
		// set the initial padding
		$('nav a').css({
			paddingRight: padding + 'px',
			paddingLeft: padding + 'px'
		});
		/*
		// calculate the new total width of the navigation menu's list items
		$('nav > ul > li').each(function() {
			listTotalWidth += $(this).outerWidth();
		});
		
		// adjust the first hyperlink's padding with any additional (leftover) padding
		$('nav a:first').css({
			paddingRight: padding + Math.floor(($('nav ul').width() - listTotalWidth) / 2) + 'px',
			paddingLeft: padding + Math.floor(($('nav ul').width() - listTotalWidth) / 2) + 'px'
		});
		*/
		// set the width of any submenus
		$('nav ul li ul').css({
			width: subMenuWidth + 'px'
		});
		
		// calculate and set the initial width of each submenu's list items
		$('nav ul li ul li a').css({
			width: (subMenuWidth - (padding * 2)) + 'px'
		});
	}
	
	
	/* ========================================================================================== */
	
	
	$('form').jqTransform();
	
	
	/* ========================================================================================== */
	
	
	// enable form field placeholders if HTML5 placeholder attribute is not supported
	$('input[placeholder], textarea[placeholder]').placeholder();
	
	
	/* ========================================================================================== */
	
	
	// disable the enter key on forms
	// binds the keypress event to current and future instances of input
	$('input').live('keypress', function(e) {
		return !(e.keyCode == 13);
	});
	
	
	/* ========================================================================================== */
	
	
	// enable client-side validation of the login form
	$('#LoginForm').validate({
		invalidHandler: function(form, validator) {
			$('p.error', '#LoginForm').hide();
		}
	});
	
	
	/* ========================================================================================== */
	
	
	$.fn.equalHeights = function() {
		$(this).each(function() {
			var tallest = 0, minHeight = 0;
			
			$(this).children().each(function() {
				tallest = ($(this).outerHeight() > tallest) ? $(this).outerHeight() : tallest;
			});
			
			$(this).children().each(function() {
				//minHeight = tallest - parseInt($(this).css('border-top-width')) - parseInt($(this).css('border-top-width')) - parseInt($(this).css('padding-top')) - parseInt($(this).css('padding-bottom'));
				
				$(this).css({
					minHeight: tallest + 'px'
				});
			});
		});
		
		return this;
	};
	
	//#Register,
	$('#ShareYourStory, #TellAFriend, #EditMyProfile, #ContactUs').equalHeights();
	
	
	/* ========================================================================================== */
	
	
	// hide all widget panels except for the first
	$('.tabs-container .tabs-content:gt(0)').hide();
	
	$('.tabs-container .tabs li a').click(function(e) {
		// disable default hyperlink behavior
		e.preventDefault();
		
		// only execute if the clicked tab is inactive
		if (!$(this).hasClass('active'))
		{
			// set all tabs as "inactive"
			$('.tabs-container .tabs li a').removeClass('active');
			// set the clicked tab as "active"
			$(this).addClass('active');
			// hide all widget panels
			$('.tabs-container .tabs-content').hide();
			// show the widget panel based on the the tab's "href"
			$($(this).attr('href')).show();
		}
	});
});
