(function($){
	$.fn.clearDefault = function(){
		return this.each(function(){
			var default_value = $(this).val();
			$(this).focus(function(){
				if ($(this).val() == default_value) $(this).val("");
			});
			$(this).blur(function(){
				if ($(this).val() == "") $(this).val(default_value);
			});
		});
	};
})(jQuery);

function adjustSidebarHeight()
{
	$('#sidebar, #main').css('height', 'auto');
	
	if($('#aside').outerHeight(true) < $('#main').outerHeight(true)) {
		var new_height = 100 * Math.ceil($('#main').outerHeight(true) / 100)
		$('#main').css('height', new_height - 10);
		$('#sidebar').css('height', new_height - ($('#aside').outerHeight(true) - $('#sidebar').outerHeight(true)));
		// $('#sidebar').css('height', $('#main').outerHeight(true) - ($('#aside').height() - $('#sidebar').height()) + 1);
	} else {
		var new_height = 100 * Math.ceil($('#aside').outerHeight(true) / 100)
		$('#main').css('height', new_height - 10);
		$('#sidebar').css('height', new_height - ($('#aside').outerHeight(true) - $('#sidebar').outerHeight(true)));
	}
}


$(document).ready(function() {

	Cufon.replace($('h4, h5, h6, p.headline_title, .cta_head, .cta label, p.headline_link a, #sb_links strong, .bar strong, .show_title, blockquote p, .caption, .dropdown strong, .comparison caption, .callout h3, .button-red, .callout .meta, .standout'));
 	Cufon.replace($('#sb_links a, .topics a, .filters, #h_tabs a, .more a, .project_link a, a.m_button, li.button a .social a, .relation a, a.r_button, .topics li'), { hover: true });

	$('h3, .role', '.pull_out:not(.main_article)').each(function() {
		Cufon.replace(this);
	});



	/* 	ADJUST SIDEBAR
		-------------------------------------------------------------*/
		$(window).ready(function()
		{
			adjustSidebarHeight();
		});

	/* 	FONT RESIZING
		-------------------------------------------------------------*/
		$('#tools .t_fontsize a').click(function() {
			if($(this).hasClass('on')) return false;
			$('#tools .t_fontsize a.on').removeClass('on');
			$(this).addClass('on');
			
			switch($(this).index()) {
				case 0 : $('.pull_out p').css({ fontSize: '14px', lineHeight: '18px', margin: '0 0 9px' }); break;
				case 1 : $('.pull_out p').css({ fontSize: '16px', lineHeight: '20px', margin: '0 0 11px' }); break;
				case 2 : $('.pull_out p').css({ fontSize: '18px', lineHeight: '22px', margin: '0 0 13px' }); break;
			}
			
			adjustSidebarHeight();
			
			return false;
		});


	/* 	EXTERNAL LINKS
		-------------------------------------------------------------*/
		$("a.external").click(function() {
			window.open( $(this).attr("href") );
			return false;
		});
	


	/*	CLEAR INPUT FIELDS ONFOCUS
		-------------------------------------------------------------*/
		
		$("input.clearinput").clearDefault();	
		
		
		$("#full_content").not('.project').hide();
		
		$("a#content_more").click(function() {
			$("#abstract").slideToggle();
			$("#full_content").slideToggle();
			return false;
		});
		
		
	/*	PRINT
	-------------------------------------------------------------*/

		$("a.print").click(function () {
			$('[rel="hidden"]').show();
			javascript:window.print();
			return false;
		});
		
		
		$('a.colorbox').colorbox();
	
	/*	Add classes to table
	-------------------------------------------------------------*/
	$('table tr:odd').addClass('alt');
	$('table tr:last-child').addClass('last-child');
	$('table tr:first-child').addClass('first-child');
	$('table tr:first-child').addClass('first-child');
	$('table tr td:last-child').addClass('last-child');
	$('table tr th:last-child').addClass('last-child');
	$('table tr td:first-child').addClass('first-child');
	$('table tr th:first-child').addClass('first-child');
	
	if ($('#headlines').size() > 0) {
		
		// flag to disable the slideshow from changing if a specific slide has been chosen for viewing
		var autoslide_enabled = true,
			home_width = 1000,
			project_width = 687,
			move_distance = 0;
		
		move_distance = ( $('#headlines').hasClass('projects') ? project_width : home_width );
		
		slideTo = function(index) {
			$('#h_tabs li.on').removeClass('on');
			$('#h_tabs li:eq('+index+')').addClass('on');
			$('#h_images').stop().animate({ left: (-move_distance * index) }); // AAAA
			$('#h_titles li.on').removeClass('on');
			$('#h_titles li:eq('+index+')').addClass('on');
		}

		$('#h_tabs a').click(function() {
			autoslide_enabled = false;			
			slideTo($(this).parent().index());
			return false;
		});

		setInterval(function() {
			var headline_index = $('#h_titles .on').index();
			var next_index = (headline_index + 1 == $('#h_titles li').length) ? 0 : headline_index + 1;
			
			if ( autoslide_enabled )
				slideTo(next_index);
		}, 10000);
	};
	
});
