var GB = {
	text_labels: function(){
		//to automatically erase text in a search field
		//$('input[type=text], textarea').each(function(){
		$('input[type=text]').each(function(){
			var $input = $(this);
			if(!$input.attr('placeholder'))
				$input.attr('placeholder', this.value);
			$input.focus(function(){
				if(this.value == $input.attr('placeholder'))
				this.value = '';
				$input.addClass('focus');
			});
			$input.blur(function(){
				if(this.value == '')
				this.value = $input.attr('placeholder');
			$input.removeClass('focus');
			});
		});
	},
	close_overlay: function(){
		$('#contactOverlay, #overlay, #overlay_container, .declineButton').remove();
		return false
	}
};

$(function(){
	//Navigation
	$('#defaultOpen').addClass('open');

	$('#mainNav > li').mouseover(function(){
		$('#mainNav > li.open').removeClass('open');
		$(this).addClass('open');
	});

	$('#mainNav > li').mouseout(function(){
		$(this).removeClass('open');
		$('#defaultOpen').addClass('open');
	});

	$('.expand').click(function(){
		var $div = $(this.parentNode);

		if($div.hasClass('viewAll'))
			$div.removeClass('viewAll');
		else if(!$div.hasClass('viewAll'))
			$div.addClass('viewAll');
	});

	//Toggle between short and long bios
	$('p.fullbioLink a').click(function(){
		$('.fullbioLink').toggleClass('hide');
		return false
	});

	//Expand tables to show complete list
	$('.viewAllLink').click(function(){
		var $table = $(this).parents('table');
		var $hide = $table.find('tr.hide');
		var $show = $table.find('tr.show');

		$hide.removeClass('hide').addClass('show');
		$show.removeClass('show').addClass('hide');
		return false
	});

	//Personal Injury Javascript, image and pop up narrative
	$('#popUpText h3').click(function(){
		var $pop = $(this.parentNode);

		if($pop.hasClass('show'))
			$pop.removeClass('show');
		else if(!$pop.hasClass('show'))
			$pop.addClass('show');
	});


	//to automatically erase text in a search field
	GB.text_labels()

	$('#langDrop').change(function(){ $('#langSelection').submit() });

	$('td a.more').click(function(){
		var $tr_hide = $(this).parents('tbody').find('tr.hide');
		var $tr_show = $(this).parents('tbody').find('tr.show');

		if($tr_hide.size())
		{
			$tr_hide.removeClass('hide');
			$tr_hide.addClass('show');
			$(this).text($(this).text().replace(/View More/,'Collapse'));
		}
		else if($tr_show.size())
		{
			$tr_show.removeClass('show');
			$tr_show.addClass('hide');
			$(this).text($(this).text().replace(/Collapse/,'View More'));
		}
		return false;
	});

	$('a.emailLink' ).click(function(){
		//create overlay div
		$('body').append('<div id="contactOverlay"></div>');
		//create overlay content div
		$('body').append('<div id="overlay_container"><div id="overlay_content"></div></div>');
		//get the content
		$.get(this.href, { overlay: true }, function(response){
			//display the content
			$('#overlay_content').html(response);
			GB.text_labels();
		});

		return false
	});

	$('#contactOverlay, .close_overlay, #overlay').live('click',function(){
		GB.close_overlay();
	});

});
