/*
 * Global Vars
 */
 
	var menuSpeed = 400;
	var menuIsOpen = false;
	var mobileHasRun = false;
	var isScrolling;
	var searchToggled = false;

/*
 * End Global Vars
 */


function magicSelectLabel( selector )
{
	$(selector).each(function(){
		this.title = $(this).prev('label[for='+this.id+']')
			.hide()
			.text();
		
		// get the first item and change the text
		var optionLabel = $(this).children('option[value="**"]')
		
		optionLabel.text('- '+this.title+' -')
	})
}

jQuery(function($){
	
	isMobile = ( screen.width < 641 ) ? true : false;
	mobile( isMobile );
	
});

function mobile( isMobile )
{
	if( isMobile )
	{
		$('#mobile-menu-button div.menu').click(function(){
			if( !menuIsOpen )
				showMenu();
			else
				hideMenu();
		});
		
		$('.historyLink')
			.css({width:'50%'})
			.live('touchend', function(){
				$(this).trigger('mouseover');
			});
			
		var lettersShowing = true;
		$('#showAttySearch').click(function(){
			if( lettersShowing )
			{
				$(this).text('Select Alphabet Letter');
				$('.bioLetters.mobile, .viewAllAtty').toggle();
				$('.stndForm').toggle();
				lettersShowing = false;
			}
			else
			{
				$(this).text('Attorney Search');
				$('.bioLetters.mobile, .viewAllAtty').toggle();
				$('.stndForm').toggle();
				lettersShowing = true;
			}
			$(this).trigger('mouseout');
		});
		
		$('body#attorneys input.txt')
			.focus(function(){
				$(this).parent().find('label').hide();
			})
			.blur(function(){
				if( $(this).val() == "" )
					$(this).parent().find('label').show();
			});
			
		$('.practiceFamily.hasChildren').click(function(){
			$('ul', this).slideToggle(200);
		});
		
		$('#viewExpanded, #viewCondensed').click(function(){
			$('.practiceFamily.hasChildren ul').slideToggle(200);
			return false;
		});
		
		
		attorneys();
		practices();
		
		magicSelectLabel( 'select' );
	}
}


function showMenu()
{
	menuIsOpen = true;
	
	$('#menu')
		.css({
			top: $(window).height() * 2,
			background:"#FFF"
		})
		.show()
		.animate({
			top:$('#mobile-menu-button').height()+68
		}, menuSpeed, function(){
			$('#content').hide();
		});
}

function hideMenu()
{
	menuIsOpen = false;
	$('#menu')
		.animate({
			top: $(window).height() * 2
		}, menuSpeed, function(){
			$(this).hide();
		});
	$('#content').show();
}

function attorneys()
{
	if( $('body#attorneys').length && $('.bioName').length )
		{
			//Add mobile buttons
			$('.bioTitle').before('<div id="mobileSections"></div>');
			
			//Add hidden sections
			$('.hidden-section').each(function(){
				var oid = $(this).attr('id');
				var id = oid.replace('section_','');
				
				if( oid == 'section_overview' )
					hclass=' here';
				else
					hclass='';
					
				$('#mobileSections').append("<div class='mobile-section-button " + id + "-mitem" + hclass + "' rel='" + oid + "'>" + id + "</div>");
			});
				
			//Custom code for these because they're weird and in the sidebar
			if( $('.mareaList').length )
				$('#mobileSections').append("<div class='mobile-section-button practices-mitem' rel='class-mareaList'>practices</div>");
				
			if( $('.industryList').length )
				$('#mobileSections').append("<div class='mobile-section-button industries-mitem' rel='class-industryList'>industries</div>");
				
			
			
			$('.mobile-section-button').each(function(){
				
				//Fix sidebar stuff
				var rel = $(this).attr('rel');
				if( rel.match('class') )
				{
					rel = rel.replace('class-', '.');
					
					if( rel == '.mareaList' )
						$(this).attr('rel', 'section_practices' );
					else if( rel == '.industryList' )
						$(this).attr('rel', 'section_industries' );
				}
				else
				{
					rel = '#' + rel;
				}
				
				//Remove anchor tags so we can test for empty sections
				$(rel + ' > a:first').remove();
				if( $(rel).children().length == 0 )
				{
					$(this).remove();
				}
				
				//Custom stuff for sidebar things
				if( rel == '.mareaList' )
				{
					$('.contentMain').append("<div id='section_practices' class='hidden-section'><h5>Practices</h5></div>");
					$('#section_practices').append( $(rel) );
				}
				else if( rel == '.industryList' )
				{
					$('.contentMain').append("<div id='section_industries' class='hidden-section'><h5>Industries</h5></div>");
					$('#section_industries').append( $(rel) );
				}
				else
				{
					$('.contentMain').append( $(rel) );
				}
				
				
				$(this).click(function(){
				
					if( $(this).text() == 'overview' )
						$('.bioTitle').show();
					else
						$('.bioTitle').hide();
						
					$('.here').removeClass('here');
					$(this).addClass('here');
					
					var thisRel = '#' + $(this).attr('rel');
					$('.hidden-section').hide();
					
					if( $(thisRel).children().length > 0 )
						$(thisRel).show();
				});
				
				$('li').each(function(){
					if( $(this).text().trim() == "" )
						$(this).remove();
				});
				
			});
			
			//More custom sidebar stuff
			$('.mareaList, .industryList').show();
				
			var numSec = $('.mobile-section-button').length;
			for( i=0; i<(3-(numSec%3)); i++)
			{
				if(3-(numSec%3) < 3 )
					$('#mobileSections').append("<div class='mobile-section-button blank-mitem'>&nbsp;</div>");
			}
			$('#mobileSections').append("<div class='clear'></div>");
		}
}

function practices()
{
	if( $('body.pracIndItem').length )
	{
		
		$('.expandOff, .sideNav').remove();
		
		if( $('h1').text().match('Labor and Employment') )
		{
			$('.contentSide h5:last').after("<ul class='statelist'></ul>");
			$('.contentSide > p').each(function(){
				$('ul.statelist').append( $(this) );
			});
		}
		
		$('h1').after( "<div id='mobile-indprac-buttons'></div>" );
		$('#mobile-indprac-buttons').append( $('.contentSide') );
		
		$('#mobile-indprac-buttons .contentSide ul').each(function(){
			$('#mobile-indprac-buttons .contentSide').prepend( $(this) );
		});
		
		$('#mobile-indprac-buttons .contentSide h5').each(function(){
			$('#mobile-indprac-buttons .contentSide').prepend( $(this) );
		});
		
		$('#mobile-indprac-buttons .contentSide').prepend( "<h5 class='here'>Overview</h5>" );
		
		if( $('#mobile-indprac-buttons .contentSide h5').length % 2 != 0 )
			$('#mobile-indprac-buttons .contentSide h5:last').after( "<h5 class='noClick'>&nbsp;</h5>" );
		
		$('#mobile-indprac-buttons .contentSide h5 a').each(function(){
			$(this).parent().html( $(this).text() );
		});
		
		$('#mobile-indprac-buttons .contentSide h5').click(function(){
			if( !$(this).hasClass('noClick') )
			{
				$('.here').removeClass('here');
				$(this).addClass('here');
				if( $(this).text() == "Overview" )
				{
					$('#mobile-indprac-buttons .contentSide ul').hide();
					$('#area-overview').show();
				}
				else
				{
					var ind = $('#mobile-indprac-buttons .contentSide h5').index(this);
					$('#mobile-indprac-buttons .contentSide ul').hide();
					$('#area-overview').hide();
					$('#mobile-indprac-buttons .contentSide ul').eq( ind-1 ).show();
				}
			}
		});
	}
}

