$(function() {

	// CONSTANTS
	var K_ANI_DURATION = 400;
	//var K_ANI_EASING = "easeOutCubic";

	// CACHE SOME ELEMENTS (FOR SPEED!)
	$sectionHeaders = $('h2 a').not('#clientlogin h2 a, #blog h2 a, .blog h2 a');
	$sectionBodies = $('.section-body');
	$lawyersHeaders = $('#lawyers h3 a');
	$lawyersBodies = $('#lawyers .entry-body');
	$blogHeadersMobile = $('.mobile.blog h3 a');
	$blogBodiesMobile = $('.mobile.blog .blog-body');

	// SET SOME PAGE DEFAULTS 
	$sectionBodies.css('display', 'none');  // hide the main section bodies
	$lawyersBodies.hide();
	$lawyersHeaders.last().css('border-bottom', 'none');
	$lawyersBodies.last().css('border-bottom', 'none');

	$blogBodiesMobile.css('display', 'none');  // hide the mobile blog bodies


	// LOAD THE OFFSET ARRAY FOR SCROLLING TO SECTION HEAD ON MOBILE DEVICES
	var offsetArray = new Array();
	$('div.section').each( function(index) {
		offsetArray[$(this).attr('id')] = $(this).offset().top - 10;
	});


/* ======================================================================================= */

	// SECTION HEADINGS 
	$sectionHeaders.
		click(function(event) {
			event.preventDefault();
			event.stopPropagation();
			if ( $(this).hasClass('active') ) {
				$(this).parent().next('.section-body').trigger('contract');
			} else {
				$sectionHeaders.trigger('reset'); // deactivate all headers and close their sections
				$(this).parent().next('.section-body').trigger('expand', [event.target] ); // activate current header and expand section
			}
			$(this).toggleClass('active');
		}).
		bind('reset', function(e) {
			$(this).removeClass('active');
			Cufon.refresh('h2 a, h2');
			$sectionBodies.trigger('contract'); // close all sections
			$lawyersHeaders.trigger('reset'); // close any open lawyer sections
		});

	// SECTION BODIES
	$sectionBodies.
		bind('expand', function(e, theTarget) {
			$(this).animate({
				height: 'toggle'
			}, K_ANI_DURATION, function() {
				//scroll to that header on mobile device after section expands (doesn't seem to be working correctly)
				if (window.innerWidth && window.innerWidth <= 480) {
					if (theTarget.hash) {
						var targetName = theTarget.hash.slice(1);
						if (targetName) {
							$('html,body').animate({scrollTop: offsetArray[targetName]}, K_ANI_DURATION);
						}
					}
				}
			}).addClass('active');
		}).
		bind('contract', function(e) {
			if ( $(this).hasClass('active') ) {
				$(this).animate({
					height: 'toggle'
				}, K_ANI_DURATION, function() { $(this).hide(); }).removeClass('active');
			}
		});	

/* ======================================================================================= */

	/* LAWYERS SUB-SECTIONS */
	$lawyersHeaders.
		click(function(event) {
			event.preventDefault();
			
			if ( $(this).hasClass('active') ) {
				$(this).parent().next('.entry-body').trigger('contract');
				$(this).css('border-bottom', '1px dashed #5E6E66');
			} else {
				$lawyersHeaders.trigger('reset');
				$lawyersHeaders.removeClass('active');
				Cufon.refresh();
				$(this).parent().next('.entry-body').trigger('expand');
				if (window.innerWidth && window.innerWidth <= 480) {
					$(this).css('border-bottom', 'none');
				} else {
					$(this).css('border-bottom', '1px dashed #56A0D3');
				}
			}
			$(this).toggleClass('active');
			return false;
		}).
		bind('reset', function(e) {
			$lawyersHeaders.removeClass('active').css('border-bottom', '1px dashed #5E6E66').last().css('border-bottom', 'none');
			$lawyersBodies.trigger('contract');
		});


	// LAWYERS SUB-SECTION BODIES
	$lawyersBodies.
		bind('expand', function(e) {
			e.stopPropagation();
			$(this).animate({
				height: 'toggle'
			}, K_ANI_DURATION).addClass('active');
		}).
		bind('contract', function(e) {
			e.stopPropagation();
			if ( $(this).hasClass('active') ) {
				$(this).animate({
					height: 'toggle'
				}, K_ANI_DURATION, function() { $(this).hide(); }).removeClass('active');
			}
		});

/* ======================================================================================= */

	/* MOBILE BLOG POSTS */
	$blogHeadersMobile.
		click(function(event) {
			event.preventDefault();
			
			if ( $(this).hasClass('active') ) {
				$(this).parent().next('.blog-body').trigger('contract');
				//$(this).css('border-bottom', '1px dashed #5E6E66');
			} else {
				$blogHeadersMobile.trigger('reset');
				$blogHeadersMobile.removeClass('active');
				Cufon.refresh();
				$(this).parent().next('.blog-body').trigger('expand');
/*
				if (window.innerWidth && window.innerWidth <= 480) {
					$(this).css('border-bottom', 'none');
				} else {
					$(this).css('border-bottom', '1px dashed #56A0D3');
				}
*/
			}
			$(this).toggleClass('active');
			return false;
		}).
		bind('reset', function(e) {
			$blogHeadersMobile.removeClass('active');	//.css('border-bottom', '1px dashed #5E6E66').last().css('border-bottom', 'none');
			$blogBodiesMobile.trigger('contract');
		});

	$blogBodiesMobile.
		bind('expand', function(e) {
			e.stopPropagation();
			$(this).animate({
				height: 'toggle'
			}, K_ANI_DURATION).addClass('active');
		}).
		bind('contract', function(e) {
			e.stopPropagation();
			if ( $(this).hasClass('active') ) {
				$(this).animate({
					height: 'toggle'
				}, K_ANI_DURATION, function() { $(this).hide(); }).removeClass('active');
			}
		});






	// POP OPEN BLOG POSTS
	//$('#news h2 a').trigger('click');


	// BLOG SECTION
	var authorDefault = "Your name";
	var emailDefault = "Your e-mail address";

	//pre-populate comment form fields
	$('input#author').val(authorDefault);
	$('input#email').val(emailDefault);

	$('input#author')
		.focus(function(event) {
			if ( $(this).val() == authorDefault ) {
				$(this).val("");
			}
		})
		.blur(function(event) {
			if ( $(this).val() == "" ) {
				$(this).val(authorDefault);
			}
		});

	$('input#email')
		.focus(function(event) {
			if ( $(this).val() == emailDefault ) {
				$(this).val("");
			}
		})
		.blur(function(event) {
			if ( $(this).val() == "" ) {
				$(this).val(emailDefault);
			}
		});

});

function initScreen() { 
	setTimeout("window.scrollTo(0,1);",100);
}

function updateOrientation() {
	//initScreen();
}
