$(document).ready( function(){
														
//	showDebug();

	// remove non-breaking spaces and whitespace between tags from search form
	$('#srchForm').html( $('#srchForm').html().replace( '&nbsp;', '' ).replace( />[\s]+</g, '><' ) );
	
	// set column heights on home page to column with the greatest height
	var homeSummaryHeight = 0;
	$('.four-column .summary').each( function(){
		if( $(this).height() > homeSummaryHeight){
			homeSummaryHeight = $(this).height()
		}
	});
	$('.four-column .summary').height( homeSummaryHeight );
	
	// set bio row height based on content
	$('.bio-row').each( function(){
		var thisRowHeight = 0;
		var outerElement = $(this);
		outerElement.children('.bio-block').each( function(){
//$('#debug').append( ' block height: ' + $(this).height() + '<br />' );
			if( $(this).outerHeight() > thisRowHeight ){
				thisRowHeight = $(this).outerHeight() - 14;
//$('#debug').append( 'setting max row height to: ' + thisRowHeight + '<br />' );
			}
		});
//$('#debug').append( 'setting row height to: ' + thisRowHeight + '<br />' );
		outerElement.height( thisRowHeight );
		
	});
	$('.bio-block p:last-child').addClass( 'last-child' );
	
	
	$('li:last-child').addClass( 'last' );

// clear search box on click
	$('.search-box').click( function(){
		$(this).attr('value', '');
	});
	
	$('#banner').click( function(){
//		document.location = '/pomeroy/pageTemplate.html';
	});

	// animated quote
	fader = setInterval( fadeInQuote, 5000 );
	
	// menu rollovers
	sfHovers();
	
	$('table.modContainer').attr( 'width', '' );
	
	// contact form required field switcher
	$('#contact-about').change( function(){
		var selectedOption = $('#contact-about option:selected').text();
		if( selectedOption == 'Sales' ){
			$('#company_name').attr( 'name', 'company_name_' );
			$('#city').attr( 'name', 'city_' );
			$('#state').attr( 'name', 'state_' );
			$('#label-company-name').html( 'Company Name: <span class="cyan80">*</span>');
			$('#label-city').html( 'City: <span class="cyan80">*</span>');
			$('#label-state').html( 'State: <span class="cyan80">*</span>');
		}else{
			$('#company_name').attr( 'name', 'company_name' );
			$('#city').attr( 'name', 'city' );
			$('#state').attr( 'name', 'state' );
			$('#company_name').removeClass( 'error' );
			$('#city').removeClass( 'error' );
			$('#state').removeClass( 'error' );
			$('#label-company-name').html( 'Company Name: ');
			$('#label-city').html( 'City: ');
			$('#label-state').html( 'State: ');
		}
	});
	
	// contact form validation
	$('#contact-form').submit( function(){
		var selectedOption = $('#contact-about option:selected').text();
		$('#subject').attr( 'value', 'Website contact form submission: ' + selectedOption );
		return validateContactForm();
	});
	
	$('.menu-right-image').html( $('.menu-right-image-placeholder').html() );
	var rightImageTimer = setTimeout( "fadeHeaderRightImage()" , 3000);	
	
});

function fadeHeaderRightImage(){
	if( $('body.proposition').length > 0 ){
		$('div.quote-block .heading1').show();
		$('.menu-right-image img').fadeTo( 750, 0 );
	}
}

function showDebug(){
	$('body').append( '<div id="debug" style="text-align:left; position:absolute; top:0; left:0; border:1px solid #CCC; padding: 1em; background: #FFE; font-size: 10px;"><p>DEBUG INFO</p></div>');
}

function sfHovers() {
	var sfEls = document.getElementById("navigation").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover"
		}
		sfEls[i].onmouseout=function() {
			this.timer = new Timer(this);
			this.timer.setTimeout("out",200);
		}
		sfEls[i].out=function() {
			this.className=this.className.replace(new RegExp("\\bsfhover\\b"), "");
		}
	}
}

function validateContactForm(){
	var isFormValid = true;
	$("input[name$='_']").each( function(){
		if( $(this).attr( 'value' ) == '' ){
			$(this).addClass( 'error' );
			isFormValid = false;
		}else{
			$(this).removeClass( 'error' );
		}
	});
	$("textarea[name$='_']").each( function(){
		if( $(this).val() == '' ){
			$(this).addClass( 'error' );
			isFormValid = false;
		}else{
			$(this).removeClass( 'error' );
		}
	});
	$("select[name$='_']").each( function(){
		if( $('option:selected', this).attr( 'value' ) == '' ){
			$(this).addClass( 'error' );
			isFormValid = false;
		}else{
			$(this).removeClass( 'error' );
		}
	});
	$("#contact-about").each( function(){
		if( $('option:selected', this).attr( 'value' ) == '' ){
			$(this).addClass( 'error' );
			isFormValid = false;
		}else{
			$(this).removeClass( 'error' );
		}
	});
	if( isValidEmailAddress( $('#email').attr( 'value' ) ) == false ){
		$('#email').addClass( 'error' );
		isFormValid = false;
	}else{
		$('#email').removeClass( 'error' );
	}
	return isFormValid;
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function fadeInQuote(){
	$('#animated-quote').fadeIn('slow');
}


/*
Useful timer functions used for menu mouseout delays
Source: http://www.codingforums.com/showthread.php?t=10531
*/
function Timer(){
    this.obj = (arguments.length)?arguments[0]:window;
    return this;
}

// The set functions should be called with:
// - The name of the object method (as a string) (required)
// - The millisecond delay (required)
// - Any number of extra arguments, which will all be
//   passed to the method when it is evaluated.

Timer.prototype.setInterval = function(func, msec){
    var i = Timer.getNew();
    var t = Timer.buildCall(this.obj, i, arguments);
    Timer.set[i].timer = window.setInterval(t,msec);
    return i;
}
Timer.prototype.setTimeout = function(func, msec){
    var i = Timer.getNew();
    Timer.buildCall(this.obj, i, arguments);
    Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec);
    return i;
}

// The clear functions should be called with
// the return value from the equivalent set function.

Timer.prototype.clearInterval = function(i){
    if(!Timer.set[i]) return;
    window.clearInterval(Timer.set[i].timer);
    Timer.set[i] = null;
}
Timer.prototype.clearTimeout = function(i){
    if(!Timer.set[i]) return;
    window.clearTimeout(Timer.set[i].timer);
    Timer.set[i] = null;
}

// Private data

Timer.set = new Array();
Timer.buildCall = function(obj, i, args){
    var t = "";
    Timer.set[i] = new Array();
    if(obj != window){
        Timer.set[i].obj = obj;
        t = "Timer.set["+i+"].obj.";
    }
    t += args[0]+"(";
    if(args.length > 2){
        Timer.set[i][0] = args[2];
        t += "Timer.set["+i+"][0]";
        for(var j=1; (j+2)<args.length; j++){
            Timer.set[i][j] = args[j+2];
            t += ", Timer.set["+i+"]["+j+"]";
    }}
    t += ");";
    Timer.set[i].call = t;
    return t;
}
Timer.callOnce = function(i){
    if(!Timer.set[i]) return;
    eval(Timer.set[i].call);
    Timer.set[i] = null;
}
Timer.getNew = function(){
    var i = 0;
    while(Timer.set[i]) i++;
    return i;
}
