﻿$(document).ready(function() {  //DOM is up

//  LOCAL SCROLL INITS

	// resetwindow(); // set position after DOM ready
	
    $.localScroll.hash({
        target: '#content-area', // Could be a selector or a jQuery object too.
        queue: true,
        duration: 1250,
		reset: true,			// this starts the scroll from the beginning if you load diriectly to a hash
		onAfter: resetwindow()  // need for live server...
    });  
	
    $('.scroll').localScroll({
        target: '#content-area',
        axis: 'xy',
        duration: 1250,
        hash: true,
        queue: true,
		onAfter: resetwindow()
    });
	
    Shadowbox.init(); // initialize shadowbox

// PAGE LOAD EVENTS

	// selected hash synch on click event	
	$('ul#nav li a').click(function(){	
		$('ul#nav li a').removeClass('selected'); 
		$(this).addClass('selected'); 
	});

// LANDING PAGE LOAD EVENTS

	var hashload = window.location.hash;  // save landing page hash
	var hashregex = new RegExp("#.+-sub"); // sub page hash check
	var hashloadmatch = hashload.match(hashregex); // save sub page hash check

	if (hashload.length==0) {  // must've landed the home page
		$('ul#nav li:first-child a').addClass('selected'); 
	}
	else if (!hashloadmatch) { // must've landed on top level page
		$("ul#nav").find("li a[href='" + window.location.hash + "']").addClass('selected'); 
	}
	else { // select top level page from hash substring identifier  
		var strhashloadmatch = String(hashloadmatch);
		$("ul#nav").find("li a[href='" + strhashloadmatch.replace('sub', 'main') + "']").addClass('selected');
	}
	
// AJAX FORM HANDLER OPTIONS AND PREP

    var ajoptions = { 
        target:        '#formarea',    
        beforeSubmit:  showRequest,   
        success:      sayHello   
	};
	
	$('#contactform').validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit(ajoptions);
		}
	})

// HASH CHANGE HANDLER

	// the following requires the jquery hash plugin
	$(window).bind('hashchange', function(){
		$('.scroll').find("a[href='" + window.location.hash + "']").trigger('click');
	});
	
	// bind a reset to the footer links
	$('#sitemap a').bind('click', function(){
		resetwindow();
	});

});  // end dom

// AJAX CALLBACKS

function showRequest(formData, jqForm, options) { // pre-submit call
	$('#formarea').html('<img src="images/loader2.gif" />');
    return true; 
} 

function sayHello() { // post-submit call
	$('#formarea').html('<h3 class="vcenter">Thanks for submitting your message.   We will be in touch soon!</h3>');
}

function resetwindow() {

	// alert('reset window was called');
	setTimeout("window.scrollTo(0,0)", 50);

}



