/* ------------------------------------------------------------------------

    Title:      Westwood Historical Society JavaScript (behavior) file
    Filename:   main.js
    Method:     <link>
    Author:     Bob Prokop | bobprokop@yahoo.com
    Updated:    March 2010
    Notes:      ...		

------------------------------------------------------------------------- */
/*------ what to do onload of document ------*/
	// W3C DOM-standards user agents //
	if(window.addEventListener){
		window.addEventListener("load",init_page,false);
	}
	// Internet Explorer //
	else{
		window.attachEvent("onload",init_page);
	}
/*-------------------------------------------*/
function init_page(){
	/*--- make Moz happy and clean-up whitespace ---*/
	if(window.addEventListener){
		treeWalker(document);
	}
}

//-------------------------------------------------------------------------------------------------------
// jQuery + plug-in stuff
//-------------------------------------------------------------------------------------------------------
$(document).ready(function(){

/* prevent widows and orphans by inserting a non-breaking space between the last two words in a given
element or element type */
$('p').widont();
						   
/* Equalize column height */
$('.col').equalHeights();
						   
//--- scroll anchor links ---//
$('a[rel=anchor]').live("click", function(){
	var full_url = this.href;
	var parts = full_url.split("#");
	var trgt = parts[1];
	var target_offset = $("#"+trgt).offset();
	var target_top = target_offset.top;
	$('html, body').animate({scrollTop:target_top}, 325);
	return false;
});

/* FAQ expand */
$(function(){
	if(document.getElementById('faq')){
		// open~close single question //
		$('#faq dt').click(function(){
			$(this).toggleClass('open');
			$(this).next().slideToggle(150);
		});
		// open~close all questions //
		var isToggled=false;
		$('#toggle-faq').click(function(){
		
			if(isToggled == false){
				$(this).addClass('open');
				$(this).html('Hide all');
				isToggled = true;
				$('#faq dt').addClass('open');
	   			$('#faq dt').next().show('fast');
			}
			else{
				$(this).removeClass('open');
				$(this).html('Show all');
				isToggled = false;
				$('#faq dt').removeClass('open');
	   			$('#faq dt').next().hide('fast');
			}
	   	return false;
		});
	}
});
});

//-------------------------------------------------------------------------------------------------------
// The following scripts are called from the init_page function (and some of these call other functions) 
//-------------------------------------------------------------------------------------------------------
/* equal height columns (used for multi-col taxonomy browse elements), courtesy of Rob Glazerbrook */
(function($){
	$.fn.equalHeights = function(minHeight, maxHeight){
		tallest = (minHeight) ? minHeight : 0;
		this.each(function(){
			if($(this).height() > tallest){
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function(){
			if ($(this).height() < tallest){
			$(this).height(tallest).css('overflow','auto');
			}
		});
	}
})(jQuery);
/* make Moz happy and clean up any whitespace */
function treeWalker(node){
	var notWhitespace = /\S/
	for(var x = 0; x < node.childNodes.length; x++){
		var childNode = node.childNodes[x];
			if((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))){
      			node.removeChild(node.childNodes[x])
     			 x--
    		}
    		if(childNode.nodeType == 1){
      			treeWalker(childNode)
    		}
  		}
}
