//////////////////////////////////////////////////////////////////////
// File: heattrans.js												//
// Usage: JavaScript and jQuery functions for Heat Trans revamp.	//
// Created: 12/12/11 by Jonathon VanHamlin							//
//////////////////////////////////////////////////////////////////////


//DOM ready///////////////////////////////////////////
$(document).ready(function() {
  //search click and blur handler-----------------
		$('.search-box input').click(function() {
			if ($(this).val()=='Search'){
				$(this).val('');
			}				
		});
		$('.search-box input').blur(function() {
  			if ($(this).val()==''){
				$(this).val('Search');
			}	
		});
	//----------------------------------------------
	
	//Scroll------------------------------------------------------
	//Functionality: Smooth scrolls a page to the location of a selector
	//Use: <a class="scroll" href="#" title=".selector">Link</a>
	$(function() {
		$('.scroll').bind('click',function(event){
			var anchor = $(this).attr('title');
			
			var windowLimit = $(document).height()-$(window).height();
			if($(window).scrollTop()<$(anchor).offset().top){
				$('html, body').animate({
					scrollTop: [windowLimit, 'swing']
				}, 1500);
			}
			else{
				$('html, body').stop().animate({
					scrollTop: [$(anchor).offset().top, 'swing']
				}, 1500);
			}
			event.preventDefault();
		});
	});
	//------------------------------------------------------------
	
		var thisHost = location.hostname;
	var thisPage = location.pathname;

	// First remove the host name.
	var re = new RegExp('.*' + thisHost);
	thisPage = thisPage.replace(re,'');

	// Then remove TeamSite-specific path components.
	re = new RegExp('.*(/STAGING|/WORKAREA/[^/]+)');
	thisPage = thisPage.replace(re,'');

	// Remove trailing "index.htm", if any.
	re = new RegExp('/(index.htm?)?\$');
	thisPage = thisPage.replace(re,'');

	var navCell = document.getElementById('content-row-navigation');
	var links = navCell.getElementsByTagName('a');
	var currentPageLink;
	if (thisPage != '') {
		for (var i = 0; i < links.length; i++) {
			re = new RegExp(thisPage + '(/(index.htm?)?)?\$');
			if (links[i].href.match(re) && !hasClass(links[i], 'navigation-alias')) {
				currentPageLink = links[i];
			}
		}
	}

	// Highlight the current page.
	addClass(currentPageLink, 'current-page');

	//test
	//alert(currentPageLink); //returns http://www.site.com/sitefolder/folder/page.htm

	// Collapse all ULs.
	var uls = navCell.getElementsByTagName('ul');
	for (var i = 0; i < uls.length; i++) {
		addClass(uls[i], 'navigation-collapsed');
	}

	if (currentPageLink) {
		// Expand relevant navigation tree sections.
		var currentNode = currentPageLink;

		while (currentNode) {
			if (currentNode.nodeName.toUpperCase() == 'UL') {
				removeClass(currentNode, 'navigation-collapsed');
			}

            if (currentNode.nodeName.toUpperCase() == 'LI') {
				addClass(currentNode, 'current-folder');
			}

			currentNode = currentNode.parentNode;

		}
		
		var siblingNodes = currentPageLink.parentNode.childNodes;
		for (var i = 0; i < siblingNodes.length; i++) {
			var showCurrentLink = 'n';
			if (siblingNodes[i].nodeName.toUpperCase() == 'UL') {
				var babyNodes = siblingNodes[i].childNodes;
				for (var g = 0; g < babyNodes.length; g++) {
					if (babyNodes[g].nodeName.toUpperCase() == 'LI') {
						if (babyNodes[g].className != 'breadcrumb-only') {
							showCurrentLink = 'y'
						}
					}
				}
				if (showCurrentLink == 'y') {
					removeClass(siblingNodes[i], 'navigation-collapsed');
				}
			}
		}
	}

	/**
	* Determine if a DOM element has a given class.
	*/

	function hasClass(element, value) {
		if (!element || !element.className) {
			return false;
		}
		else {
			var classes = element.className.split(' ');
			for (var i = 0; i < classes.length; i++) {
				if (classes[i] == value) {
					return true;
				}
			}
		}
		return false;
	}
	
}); //End .ready()

//Page and graphics have been loaded//////////////////
$(window).load(function() {
  // run code
}); //End .load()


