// Additions to jQuery Core
jQuery.fn.mailto = function() {
   return this.each(function(){
     var email = $(this).html().replace(/\s*\(.+\)\s*/, "@");
     $(this).before('<a href="mailto:' + email + '" rel="nofollow" title="Email ' + email + '">' + email + '</a>').remove();
   });
};
jQuery.fn.valueFx = function() {
	return this.each(function(){
		// Vars
		var $field = $(this);
		var $value = $(this).val();
		var $newVal;
		
		$field.addClass('placeholder');

		// Event handlers
		$field.focus(function(){
			if($field.hasClass('valueFx')){
				if (($value == '') || ($value == $value)) {
					$field.removeClass('placeholder').addClass('focus').val('');
					$newVal = $(this).val();
				}
			}
		});
		$field.blur(function(){
			$newVal = $(this).val();
			if ($newVal == '') {
				$field.val($value).removeClass('focus').addClass('placeholder');
			} else {
				$field.removeClass('placeholder').removeClass('valueFx');
			}
		});
	});
};
jQuery.fn.fadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle'}, speed, easing, callback);
};
// jQuery NoConflict Wrapper (don't conflict with other JavaScript libraries used in plugins)
(function($){ 
	$(function(){
		// Portfolio: Extra entries on demand
		$('#view-extra a').click(function(){
			$(this).parent().hide();
			$('#content li.extra').fadeIn();
			return false;
		});
		// Citations
		if ($('p:has(cite)').length > 0){
			$('p:has(cite)').addClass('citation');	
		}
		// Navigation: Third Level
		if ($('ul#thirdLevelNav').length > 0){
			$thirdLevelNavSize = $('#thirdLevelNav li').size() - 1;
			$('#thirdLevelNav li').each(function(index){
				if(index < $thirdLevelNavSize){
					$(this).append('<span class="spacer"> / </span>');
				}
			});
			$('#thirdLevelNav li:last').addClass('last');
		}
		// Homepage Expertise Callout
		if ($('#areas-of-expertise').length > 0){
			$extertiseCalloutSize = $('#areas-of-expertise li').size() - 1;
			$('#areas-of-expertise li').each(function(index){
				if(index < $extertiseCalloutSize){
					$(this).append('<span class="spacer"> / </span>');
				}
			});
			$('#areas-of-expertise li:last').addClass('last');
		}
		// SubNav
		$('#subNav li:last').addClass('last');
		// Forms
		$('input:text').addClass('text');
		// Sidebar: Archives
		$('#sidebar #archives ul').each(function(){
			var $totalArchives = $(this).find('li').size();
			$totalArchives -= 1;
			$(this).find('li').each(function(index){
				if(index < $totalArchives){
					$(this).append('<span class="spacer">  /  </span>');
				}
			});
		});
		/* Sidebar:  */
		$('#sidebar:has(#callout-content)').addClass('hasCallout');
		// IE6
		if ($.browser.msie){
			$('div.inner').css({
				height: $('div.inner').height()
			});
		}
		// Viral Video Page Specific Styles
		if ($('body.page-id-111').length > 0){
			$('img.alignleft').parent().addClass('clearleft');
		}
		// Manifesto
		if ($('body.page-template-manifesto-php').length > 0){
			$('img.aligncenter').parent().addClass('aligncenter');
			// Smooth scrolling anchors
			$('#mfNav li a, a#backtotop').live('click',function(){
				if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
					var $target = $(this.hash);
					$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
					if ($target.length) {
						var targetOffset = $target.offset().top;
						$('html,body').animate({scrollTop: targetOffset}, 1000);
						return false;
					}
				}
			});
		}
		// Focus & Blur
		if ($('input.valueFx').length > 0) {
			$('input.valueFx').valueFx();
		}
		// Google Maps for Contact
		if ($('body.page-template-contact-php').length > 0) {
			var map = new GMap2(document.getElementById('contact-map'));
			var captainsOfficeBoston = new GLatLng(42.360961,-71.056923);
			map.addOverlay(new GMarker(captainsOfficeBoston));
			map.setCenter(captainsOfficeBoston, 14);
		}
	});
})(jQuery);