﻿$(function() {
	$('#nav li').bind('click', function(e) { Open($(this)); })
				.bind('mouseover', function() { ToggleHover($(this), true, false); })
				.bind('mouseout', function() { ToggleHover($(this), false, false); });
	$('#nav li:first').css('margin-left', '10px'); // override left margin on first menu item
	$('#nav li:last').css('margin-right', '0'); // override right margin on last menu item
	$('#logo').bind('click', function(e) { Home(); });
	$('.arrow, .arrow2').bind('click', function(e) { Close(); });
	StyleHeadings();
});

var isAnimating = false;
var openPage = 'home';

function Open(nav) 
{
	if (isAnimating) return;
	
	isAnimating = true;
	
	// hide mission statement if visible, asynchronously
	if ($('#missionstatement').is(':visible'))
		$('#missionstatement').slideToggle('slow');
 
 	var pageName = nav.children(':first').attr('alt').replace(' ', '').toLowerCase();
	var divToShow = 'content_' + pageName;
	if ($('#' + divToShow).is(':visible'))
		{
		isAnimating = false;
		return;
		}

	if ($('#content').is(':visible'))
		$('#content').slideToggle('slow', function() {
			$('#content > div').hide();
			$('#' + divToShow).show();
			
			var isProposalRequest = (pageName == 'proposalrequest');
			$('#content').css('left', isProposalRequest ? '0' : '298px')
				 .css('width', isProposalRequest ? '100%' : '672px');
			
			$('#content').slideToggle('slow', function() { isAnimating = false; } );
		});
	else
		{
		$('#content > div').hide();
		$('#' + divToShow).show();
		
		var isProposalRequest = (pageName == 'proposalrequest');
		$('#content').css('left', isProposalRequest ? '0' : '298px')
				 .css('width', isProposalRequest ? '100%' : '672px');
				 
		$('#content').slideToggle('slow', function() { isAnimating = false; });
		}
		
	$('#nav li').each(function(index) { ToggleHover($(this), false, true); });
	ToggleHover(nav, true, true);
	openPage = pageName;
        _gaq.push(['_trackEvent', 'Menu', 'Page Change', pageName]);
		_gaq.push(['_trackPageview', '/' + pageName + '/']);
}


function ToggleHover(nav, isHovering, force)
{
	var img = $(nav).children(':first');
	
	var pageName = img.attr('alt').replace(' ', '').toLowerCase();
	if (openPage == pageName && !force) return;
	
	var oldEnding = isHovering ? '.png' : '2.png';
	var newEnding = isHovering ? '2.png' : '.png';

	var source = img.attr('src').replace(oldEnding, newEnding);
	
	img.attr('src', source);
}

function Close()
{
	if (isAnimating) return;
	
	isAnimating = true;

	if ($('#content').is(':visible'))
		$('#content').slideToggle('slow', function() {
				$('#content > div').hide();
				isAnimating = false;
			});
	if ($('#missionstatement').is(':visible'))
		$('#missionstatement').slideToggle('slow', function() { isAnimating = false; });
}

function Home()
{	
	if (isAnimating) return;
	
	isAnimating = true;
	
	if ($('#content').is(':visible'))
		$('#content').slideToggle('slow', function() { isAnimating = false; });
	if (!$('#missionstatement').is(':visible'))
		$('#missionstatement').slideToggle('slow', function() { isAnimating = false; });
	else
		isAnimating = false;
		
	$('#nav li').each(function(index) { ToggleHover($(this), false, true); });
	openPage = 'home';
        _gaq.push(['_trackEvent', 'Menu', 'Page Change', 'Home']);
		_gaq.push(['_trackPageview', '/home-click/']);
}

function StyleHeadings()
{
	$('.heading').each(function(index) {
		var text = $(this).text();
		var output = '';
		
		var spaceSeen = true; // first letter s/b tall
		for (var i = 0; i < text.length; i++)
			{
			if (text.charAt(i) == ' ')
				{
				spaceSeen = true;
				output += text.charAt(i);
				}
			else if (spaceSeen)
				{
				output += '<span class="tall">' + text.charAt(i) + '</span>';
				spaceSeen = false;
				}
			else
				{
				spaceSeen = false;
				output += text.charAt(i);
				}
			}
			
    	$(this).html(output);
  });
}

function ChangeMaintenancePackage(type)
{
	$('.basic, .full').attr('checked', false);
	$('.' + type).attr('checked', 'checked');
	$('.basic').attr('disabled', type == 'basic');
	$('#mowingEdgingTrimmingPruningHidden').attr('value', type == 'basic' ? 
					'Turf Cutting, Edging, Trimming, Pruning' : 
					'');
}

function RequestProposal()
{
	var name = $('#txtName').val();
	var address = $('#txtAddress').val();
	var zip = $('#txtZip').val();
	var email = $('#txtEmail').val();
	var phone = $('#txtPhone').val();

	if (name == '' || address == '' || zip == '' || email == '' || phone == '') 
		{
		alert('Fields marked with an asterisk ( * ) are required.');
                _gaq.push(['_trackEvent', 'Menu', 'Proposal', 'Blank Field']);
				_gaq.push(['_trackPageview', '/proposalrequest/blankfield/']);
		return false;
		}
		
	alert('Thank you- your proposal request is being sent.');
        _gaq.push(['_trackEvent', 'Menu', 'Proposal', 'Success']);
		_gaq.push(['_trackPageview', '/proposalrequest/success/']);
	return true;
}