$(document).ready(function() {
	$('body').addClass('hasJS');
	
	// News ticker
	var duration = 0.05;
	var iniOffset = $('#ticker-in').innerWidth();
	
	$('#ticker ul').width(9999);
	$('#ticker-in').css('height',($('#ticker-in li').height()));
	$('#ticker li').css({'float':'left'});
	
	var itemWidth = 0
	$('#ticker-in li').each(function(i){
		itemWidth += $(this, i).outerWidth();
	});

	$('#ticker ul').css({'left':iniOffset,'width':itemWidth,'position':'absolute'});
	
	function crawl(time){
		$('#ticker ul').animate({
			left:'-'+itemWidth
		},time,'linear',function(){
			$(this).css({'left':iniOffset});
			crawl(travelTime);
		});
	}
	
	var travelTime = (itemWidth+iniOffset)/duration;
	crawl(travelTime);
	
	$('#ticker ul').hover(function(){
		$(this).stop();
	}, function(){
		var position = $(this).position();
		var resTime = (itemWidth + position.left)/duration;
		crawl(resTime);
	});


	$('#header #secondaryNav > li, #header #secondaryNav .subNav li').hover(function() {	
		$(this).children('.subNav').addClass('current').show();
	}, function() {
		$(this).children('.subNav').removeClass('current').hide();
	});
	
	$('#home #checkerboard ul.section > li > div.inner > h2, #home #checkerboard ul.section > li > div.inner > p').each(function() {
		var elHeight = $(this).height();
		var parHeight = $(this).parent().parent('li').height();
		$(this).css('top',(parHeight-elHeight)/2);
	});
	
	if($.browser.msie) {
		$('#branding .section h2, #branding .quote blockquote').each(function() {
			var elHeight = $(this).height();
			var parHeight = $('#branding').height();
			$(this).css('top',(parHeight-elHeight)/2);								 
		});
	}
	
	/*
	$('#home #checkerboard ul.section > li.image > div.preview > div.content > p').each(function() {
		var elHeight = $(this).height();
		var sibHeight = $(this).siblings('img').attr('height');
		$(this).css('top',(sibHeight-elHeight)/2);
	});
	*/
	
	$('#home #checkerboard .section > li').hover(function() {
		$(this).children('.inner').children('.subNav').addClass('current').show();
	}, function() {
		$(this).children('.inner').children('.subNav').removeClass('current').hide();/*slideToggle('normal')*/						
	});
	
	$('#home #checkerboard .section > li.image').hover(function() {
		$(this).css('cursor','pointer');
		var imagePos = $(this).position();
		$(this).children('.overlay').css({'top': imagePos.top, 'left': imagePos.left}).show();
	}, function() {
		$(this).children('.overlay').hide();
		$(this).children('.preview').removeClass('current').hide();/*fadeOut('normal')*/
	});
	
	$('#home #checkerboard .section > li.image').click(function() {
		$(this).children('.preview').addClass('current').fadeIn('normal');
		if($(this).children('.preview').children('.content').children('p').css('top') == '0px') {
			var elHeight = $(this).children('.preview').children('.content').children('p').height();
			var sibHeight = $(this).children('.preview').children('.content').children('img').height();
			$(this).children('.preview').children('.content').children('p').css('top',(sibHeight-elHeight)/2);
		}
	});
	
	/*
	$('#expertiseSecondaryNav ul a').click(function() {
		var parClass = $(this).parent().attr('class');
		
		if($('#'+parClass).css('display') === 'none') {
			$('.expertiseModal:not(#'+parClass+')').fadeOut('normal',function() {
				$('#'+parClass).fadeIn('normal');									   
			});
		}
		return false;
	});
	*/
	
	//Expertise modal window
	/*
	$('#expertiseSecondaryNav > a').click(function() {
		$('#topicalIndex').fadeIn('normal');
		return false;
	});
	
	$('.expertiseModal .close').click(function() {
		$(this).parent().parent('.expertiseModal').fadeOut('normal');
		return false;									  
	});
	*/
	
	//Add/Remove Input Values
	$('input[title]').each(function() {
		if($(this).val() == '') {
			$(this).val($(this).attr('title'));	
		}
		
		$(this).focus(function() {
			if($(this).val() == $(this).attr('title')) {
				$(this).val('');	
			}
		});
		$(this).blur(function() {
			if($(this).val() == '') {
				$(this).val($(this).attr('title'));	
			}
		});
	});

	//Collapse/Expand
	$('.topics h3 + ul').hide();
	$('.topics h3').addClass('control').click(function() {
		$(this).toggleClass('collapse').next('ul').slideToggle('slow');																
	});
	
	//Text resizer
	if($.cookie('TEXT_SIZE')) {
		$('body').addClass($.cookie('TEXT_SIZE'));	
	}
	$('#footer ul li.resizer a').click(function() {
		var textSize = $(this).parent().attr('class');
		$('body').removeClass('small medium large').addClass(textSize);
		$.cookie('TEXT_SIZE',textSize, { path: '/', expires: 10000 });
		return false;
	});
	
	//Clients 2 cols
	var halfWay = Math.floor(($('.clients li').length) / 2);

	$('.clients').after('<ul class="listing clients alt"></ul>');
	$('.clients > li:gt('+halfWay+')').clone().appendTo('.clients.alt');
	$('.clients:not(.alt) > li:gt('+halfWay+')').remove();
	
	//Flickr Photos
	//$('.flickr li:not(:first-child)').hide();
	$('.intro .flickr li:nth-child(3)').addClass('alt');
	$('.flickr a').each(function() {
		var imgSrc = $(this).children().attr('src');
		$(this).attr('href',imgSrc.replace('_s.jpg','.jpg'));
	});
	
	//Lightbox
	$("a[rel*='lightbox']").lightBox();
	
	// Education Assessment
	$('#educational-assessment .more-content').hide();
	$('#educational-assessment .read-more').click(function(){
		$(this).parent().next().slideToggle();

		return false;
	});
	
	$('.tooltip').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		fade: 250
	});
});