﻿// This file defines some of the basic sitewide functionality.

$(function() {
	addArrows();	// Displays arrows on the nav menu
	deactivateCurrentPageLink();	// Kills the nav menu link to the current page (redundant)
	adjustTopNavbar();	// Rearranges the top nav menu into two rows if there are too many items
	//adjustSearch();		// Controls the default text in the search box, and makes it disappear when the user clicks inside the textbox
	$('#zz1_QuickLaunchMenu table tr td:first-child:has(a)').add('div[id^=zz1_QuickLaunchMenu] table table tr td:first-child:has(a)').not(':has(span.voidLink)')
		.bind('click',redirectPage);	// Makes the entire cells in the menu clickable, not just the actual link
	formEffects();		// Controls the form effects (such as how textareas disappear when content has been added)
});

function addArrows() {
	$('#zz1_QuickLaunchMenu table tr td').not(':only-child').not(':nth-child(2)')
		.append('<span class="flyoutArrowIndicator"><img src="/_layouts/images/largearrowright.gif" /></span>');
	$('#zz1_QuickLaunchMenu table tr td:nth-child(2)')
		.remove();
}

function deactivateCurrentPageLink() {
	var currentURL = window.location.href;
	currentURL = currentURL.substr(currentURL.length - 15);
	$('#menu a').each(function() {
		var anchorURL = $(this).attr('href');
		anchorURL = anchorURL.substr(anchorURL.length - 15);
		if (currentURL == anchorURL) {
			$('td',$(this).parents('td'))
				.css({
					background:'#DD6600',
					cursor:'default'
				});
			var replacementText = '<span class="voidLink">' + $(this).html() + '</span>';
			$(this).after(replacementText).css('display','none');
		}
	});
	$('#zz2_GlobalNav a').each(function() {
		var anchorURL = $(this).attr('href');
		anchorURL = anchorURL.substr(anchorURL.length - 15);
		if (currentURL == anchorURL) {
			$('td',$(this).parents('td'))
				.css({'background':'#DD6600', 'border-radius':'5px'});
			var replacementText = '<span class="voidLink2">' + $(this).text() + '</span>';
			$(this).after(replacementText).css('display','none');
		} else {
			$(this).wrap('<span class="topMenuLink"></span>');
		}
	});

	if (currentURL.match("index.aspx") == null) {
		var curURLArray = window.location.href.split("/");
		var curSection = curURLArray[curURLArray.length - 2];
		$('#menu a').each(function() {
			var anchorSection = $(this).attr('href').split("/");
			if ((curSection == anchorSection[anchorSection.length - 2]) && (anchorSection[anchorSection.length - 1] == 'index.aspx')) {
				$('td',$(this).parents('td'))
					.css({
						background:'#DD6600',
						cursor:'pointer'
					})
					.bind('click', redirectPage);
				var replacementText = '<span class="voidLink">' + $(this).text() + '</span>';
				$(this).after(replacementText).css('display','none');
			}
		});
	}
}

function adjustTopNavbar() {
	var charCount = ($('#zz2_GlobalNav a').text()).length;
	
	if (charCount > 80) {
		var Menu = document.getElementById("zz2_GlobalNav");
		var counter = Math.round(Menu.getElementsByTagName("table").length/2);
		Menu.insertRow(0);
		if (counter != Menu.getElementsByTagName("table").length/2) {
			counter--;
			for (i=0;i<3;i++) {
				Menu.rows[0].insertCell(0);
			}
		}
		for (i=0;i<counter*3;i++) {
			Menu.rows[0].appendChild(Menu.rows[1].cells[0]);
		}
		
		$('#topMenu').addClass('longNavbar');
	} else {
		$('#topMenu').addClass('shortNavbar');
	}
}

function redirectPage(event) {
	var hrefDestination = $(this).children().attr('href');
	window.location = hrefDestination;
	return false;
}

function adjustSearch() {
	var defaultText = 'Search ESR';
	$('#search :text')
		.attr('value',defaultText)
		.bind('focus',function() {
			if ($(this).attr('value') == defaultText) {
				$(this).attr('value','');
			}
		})
		.bind('blur',function() {
			if ($(this).attr('value') == '') {
				$(this).attr('value',defaultText);
			}
		});
	
}

function formEffects() {
	$('#dataForm input')
		.add('#dataForm textarea')
		.add('#dataForm select')
		.bind('blur',function () {
			$(this).css('border','solid 1px #EEEEEE');
			if($(this).attr('value') != '') {
				$(this).css('background-color','#EEEEEE');
			} else {
				$(this).css('background-color','white');
			}
	});
	$('#dataForm select')
		.bind('blur',function () {
			$(this).css('border','solid 1px #EEEEEE');
			if($(this).attr('value') != 'Select a State') {
				$(this).css({
					"background-color": "#EEEEEE",
					"font": "bold 14px Arial, sans"
				});
			} else {
				$(this).css({
					"background-color": "white",
					"font-weight": "normal"
				});
			}
	});
	$('#dataForm input')
		.add('#dataForm select')
		.add('#dataForm textarea')
		.bind('focus',function () {
			$(this)
				.css({
					"background-color": "white",
					"border": "solid 1px #1171A4"
				});
	});
}

