function getPostLink(id)
{
	output = $('#blogpost_'+id+' h1 a').attr('href');
	
	if( !output )
		output = document.location.href;
		
	return output;
}

function getPostTitle(id)
{
	return $('#blogpost_'+id+' h1').text();
}


function animateSidebars()
{
	// first, wrap those silly text nodes in a paragraph
	var sidebars = $("#expandingSidebar, #navExpandingSidebar .expandable");
	
	sidebars.contents()
	.not("[nodeType=1]")
	.wrap('<p class="textNodeWrapper" />');
	
	// now, get rid of the text nodes wrappers that are just whitespace
	$('p.textNodeWrapper', sidebars)
	.filter(function(){ return !jQuery.trim($(this).text()) })
	.remove();
	
	sidebars.children('br')
	.remove();
	
	// animate expanding sidebars
	jQuery(function(){
		
		$("#expandingSidebar h3, #navExpandingSidebar h3")
		.mouseover(function(){ $(this).addClass('hover') })
		.mouseout(function(){ $(this).removeClass('hover') })
		
		var sidebars = $("#expandingSidebar, #navExpandingSidebar .expandable");
		$('h3', sidebars)
		.click(function(event, instant){
				
				if( instant )
					speed = '1';
				else if(event.originalEvent && 
					event.originalEvent.shiftKey)
					speed = 1800;
				else
					speed = 'medium';
				
				if(event.originalEvent && 
					event.originalEvent.altKey)
		            what = $('h3', sidebars)
		        else
		        	what = $(this);
		        
		        if( what.hasClass('expanded') )
		        {
		        	what.each(function(){
		        		$(this).removeClass('expanded') // remove the expanded class
			        	.nextAll().each(function(){ 
			        		// keep going until we hit the next header
			        		if( $(this).is('h3') )
			        			return false;
			        		else
			        			$(this).slideUp(speed)
		        		});
		        	});
		        }
		        else
		        {
		        	what.each(function(){
		        		$(this).addClass('expanded') // add the expanded class
			        	.nextAll().each(function(){ 
			        		// keep going until we hit the next header
			        		if( $(this).is('h3') )
			        			return false;
			        		else
			        		{
			        			$(this).find('*').show();
			        			
			        			if( instant )
			        				$(this).show();
			        			else
				        			$(this).slideDown(speed)
			        		}	
		        		});
		        	});
		        }
		        
		        $(this).removeClass('hover')
		});
		
		$('h3 a', sidebars)
		.click(function(event){
			event.stopPropagation();
		})
		.mouseover(function(event){
			event.stopPropagation();
		});

		
		// now expand these
		$('h3:contains(Industries):not(:contains(Associated)), '+
			'h3:contains(Practices):not(:contains(Associated)), '+
			'h3:contains(Education), '+
			'h3:contains(Press Room Contact), '+
			'h3:contains(Categories), '+
			'h3:contains(Recent Posts)',
			sidebars)
		.trigger('click', [true]);
	});
}

function animateMultiList()
{
	// animate multi lists sidebars
	jQuery(function(){
		
		// remove the arrow from items
		$('#contentMain > div.multiListBox li.prac_bullet_child')
		.prev('.prac_bullet_parent')
		.addClass('hasChildren')
		//.attr('title', 'hold ALT while clicking to expand/contract all items')
		.hover(function(){ $(this).addClass('hover') },
			function(){ $(this).removeClass('hover') })
		.click(function(event, instant){
				
				if( instant )
					speed = '1';
				else if(event.originalEvent && 
					event.originalEvent.shiftKey)
					speed = 1800;
				else
					speed = 'medium';
				
				if(event.originalEvent && 
					event.originalEvent.altKey)
				{
		            what = $('#contentMain > div.multiListBox li.prac_bullet_parent.hasChildren')
				}
				else
		        	what = $(this);
		        
		        if( $(this).hasClass('expanded') )
		        {
		        	what.each(function(){
		        		$(this).removeClass('expanded') // remove the expanded class
			        	.nextAll().each(function(){ 
			        		if( $(this).is('.prac_bullet_parent') )
			        			return false;
			        		else
			        			$(this).slideUp(speed)
		        		});
		        	});
		        }
		        else
		        {
		        	what.each(function(){
		        		$(this).addClass('expanded') // remove the expanded class
			        	.nextAll().each(function(){ 
			        		if( $(this).is('.prac_bullet_parent') )
			        			return false;
			        		else
			        		{
			        			if( instant )
			        				$(this).show();
			        			else
				        			$(this).slideDown(speed)
			        		}
		        		});
		        	});
		        }
		        
		        $(this).removeClass('hover')
		});
		
		$('#contentMain > div.multiListBox a')
		.click(function(event){
			event.stopPropagation();
		})
		.mouseover(function(event){
			event.stopPropagation();
		});
	});
}

// function stolen from http://blog.deconcept.com/code/qtobject/qtobject.html
// for detecting quicktime
function isQTInstalled() {
	
	var qtInstalled = false;
	qtObj = false;
	if (navigator.plugins && navigator.plugins.length) {
		for (var i=0; i < navigator.plugins.length; i++ ) {
         var plugin = navigator.plugins[i];
         if (plugin.name.indexOf("QuickTime") > -1) {
			qtInstalled = true;
         }
      }
	} else if(jQuery.browser.msie) {
		execScript('on error resume next: qtObj = IsObject(CreateObject("Quicktime.Quicktime"))','VBScript');
		qtInstalled = qtObj;
	}

	return qtInstalled;
}

function startRotatingBox( boxObj )
{
	divToFade = $(boxObj).children('div:last');
	
	if( $(boxObj).data('started') )
	{
		divToFade.fadeOut('slow', function(){
			$(this).parent().prepend(this);
			$(this).fadeIn(0);			
		})
	}
	else
	{
		$(boxObj).data('started', true);
		$(boxObj).children('div').css('display','block');
	}
	
	divToFade.animate({opacity: 1.0}, 4000, function(){
		startRotatingBox( $(this).parent() )
	});
}

function embedVideo(selector, smallBox)
{
	if((navigator.userAgent.match(/(iPhone|iPod)/i)))
		return;
		
	if( window.location.search == '?forcelink' )
		return;
	
	
	smallBox = !!smallBox;
	
	// magically embed Flash/Quicktime player for video links
	// using jquery, swfobject, and jquery.media
	// tested in Firefox, Safari, iPhone, Opera, IE6, and IE7
	// with and without QT/Flash/Javascript - degrades gracefully
	
	$.fn.media.defaults.flashVersion = "9.0.115";
	$.fn.media.defaults.expressInstaller = "i/expressInstall.swf";
	$.fn.media.defaults.flvPlayer = "i/player-custom.swf";
	
	// make mp4s default to playing just live FLVs
	$.fn.media.mp4 = $.fn.media.flv;
	
	// figure out which player to use
	if( window.location.search == '?forceqt' )
		useqt = true;
	else if( window.location.search == '?forceflash' )
		useqt = false;
	else if( jQuery.browser.mozilla ) // firefox has issues with qt
		useqt = false;
	else if( !swfobject.hasFlashPlayerVersion("7") )
		useqt = true;
	else
		useqt = false;
		
	// if we have quicktime, use that as the default
	if( useqt )
	{
		$.fn.media.mapFormat('mp4', 'quicktime');
		$.fn.media.mapFormat('jpg', 'quicktime');
		var theWidth = 234;
		var theHeight = 152;
		var theClass = 'qtMovie';
	}
	else
	{
		var theWidth = 234;
		var theHeight = 157;
		var theClass = 'flashMovie';
	}
	
	if( smallBox )
	{
		theWidth = 179;
		theHeight = 124;
	}
		
	// set the size of the video object (actual video is 234x137)
		
	// remove onclick event, remove "target", and embed media
	$(selector+' a').eq(0).each(function(){
		thumbnail = $('img', this).attr('src');
		
		attrs = {
				showlogo: 'false',
				controller: !smallBox,
				enablejavascript: true,
				qtsrc: thumbnail,
				qtnext1: '<'+this.href+'> T<myself>',
				qtnext2: '<'+thumbnail+'> T<myself>',
				//qtsrcdontusebrowser: true,
				'class': theClass,
				onmouseup: 'playMovie(this)',
				wmode: 'transparent'
			}
		
		$(this)
		.unbind('click')
		.media({ 
			width: theWidth, 
			height: theHeight,
			attrs: attrs,
			params: attrs,
			flashvars: {
				image: thumbnail,
				controlbar: (smallBox) ? 'none' : 'bottom',
				showicons: false,
				displayclick: 'none',
				stretching: 'exactfit',
				screencolor: 'ffffff',
				bufferlength: '4'
			}
		});
	});
	
	// add video controls box
	if( smallBox )
	{
		$('<a class="vidControls" />')
		.click(function(){
			$(this).prev().children('object, embed').trigger('mouseup');
		})
		.appendTo(selector);
	}
}

function playMovie( obj )
{
	playing = ($(obj).data('playing') == true);
	
	if( typeof(obj.sendEvent) == 'undefined' &&
		typeof(obj.Play) == 'undefined'  )
		return;
	
	if( playing )
	{
		if( $(obj).hasClass('qtMovie') )
			obj.Stop();
		else
			obj.sendEvent('PLAY', false);
			
		$(obj).data('playing', false);
		$('body').removeData('playingMovie');
	}
	else
	{
		// pause the currently playing movie
		if( playingMovie = $('body').data('playingMovie') )
			playMovie( playingMovie);
		
		if( $(obj).hasClass('qtMovie') )
		{
			obj.SetAutoPlay(true);
			obj.Play();
		}
		else
		{
			obj.sendEvent('PLAY', true);
			if( !$(obj).data('hasListener') )
			{
				obj.addModelListener("STATE","stateTracker");
				$(obj).data('hasListener', true);
			}
		}
		
		$(obj).data('playing', true)
		$('body').data('playingMovie', obj);
	}
	
	$(obj).parent().next('a.vidControls').toggleClass('vidControlsPlaying');			
}


function stateTracker(event)
{ 
	//alert(obj.newstate);
	
	if( event.newstate=='COMPLETED' )
	{
		obj = $('body').data('playingMovie').sendEvent('SEEK', 0);
		obj = $('body').data('playingMovie').sendEvent('PLAY', false);
		
		$(obj).data('playing', false);
		$('body').removeData('playingMovie')
		$(obj).parent().next('a.vidControls').toggleClass('vidControlsPlaying');
	}
};

function magicTextFields( selector )
{
	// they want to have default text for name/keyword fields
	// but we don't want that default text submitted
	jQuery(function(){
		$(selector).each(function(){
			this.title = $(this).prev('label.jsHide[for='+this.id+']')
				.text().toLowerCase();
			
			// if we have no defaultValue, set one
			if( !this.defaultValue )
			{
				this.defaultValue = this.title;
				this.value = this.defaultValue;
				$(this).addClass('defaultValue');
			}
		})
		
		// add focus listener
		.blur(function(){
			// if the value is empty, set it to the label
			if( !$.trim(this.value) || (this.value == this.title) )
			{
				this.value = this.title;
				$(this).addClass('defaultValue');
			}
		})
		.focus(function(){
			if( this.value == this.title )
			{
				this.value = '';
			}
			$(this).removeClass('defaultValue');
		})
		.parents('form').submit(function() {
			$('input.txt', this).each(function(){
				if( this.value == this.title )
					this.value = '';
			});
			return true;
		});
	});
}

function addEmailPopup()
{
	$mailLinks = $("#content a[href^=mailto:]")
	
	$mailLinks
	.click(function(){
		email = escape(unescape(this.href).substring(7));
		
		emailWindow = window.open("email-popup.html?email=" + email, "EmailNotice",
			"toolbar=no,location=no,directories=no,status=no,scrollbars=no,"+
			"menubar=no,resizable=yes,width=500,height=380");
		
		emailWindow.focus();
		
		return false;
	});	
}

function blogDisclaimer(theLink)
{
	disclaimerWindow = window.open(theLink.href, "BlogNotice",
		"toolbar=no,location=no,directories=no,status=no,scrollbars=no,"+
		"menubar=no,resizable=yes,width=500,height=320");
		
	disclaimerWindow.focus();
		
	return false;
}

// things that get executed on every page
jQuery(function(){
	animateSidebars();
	addEmailPopup();
});