var anecdoteDone;

$('.googlemap').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });

function popups(){
	$("a[rel='popup'], a[rel='external']").attr("title",function(){return $(this).attr("title") + " (Opens in a new window) "}).click(function(){
		window.open($(this).attr('href'));
		return false;
	})
}

function positionLogo(timer){

	$('#logo').dequeue();

	setTimeout(function(){

	if (!timer){timer = 1;}

	wHeight= $(window).height();

	lHeight = $('#logo').height();

	maxTop=  $('#content').height() - lHeight  - $('#content_right_top').outerHeight();

	colOffset = $("#content_right").offset().top;

	newTop = wHeight - lHeight - colOffset + $(window).scrollTop() - $('#content_right_top').outerHeight();

	if (newTop > maxTop){newTop = maxTop;}

	if (newTop<0){newTop = 0;}

	$('#logo').css({position:'relative'}).animate({top:newTop},timer,function(){$(this).show();})
	},200);
}

function attachSearchFormHandlers() {
	$('#search_box').focus(function(){
		if($(this).val() == 'keyword(s)'){$(this).val('');$(this).css({ color:"#000"});}
	})
	$('#search_box').blur(function(){
		if($(this).val() == ''){$(this).val('keyword(s)');$(this).css({ color:"#999"});}
	})
}

	$("#search_box").focus(function(){
		if (this.value == 'keyword(s)') {
			this.value = ''
		}	
	});

	$("#content_right_top input").focus(function(){
		if (this.value == 'keyword(s)') {
			this.value = ''
		}	
	});


// AAR Menu
$(document).ready(function(){
	
	popups();

	$('#logo').hide();
	
	positionLogo();

	attachSearchFormHandlers();
	
	//Tell me more link
	$('#tell_me_more').mouseover(function(){
		$('#red_anecdote').css({opacity:0.8}).slideDown();
		$('#red_anecdote').animate({opacity: 0.8}, 5000);

	 })

	$('#red_anecdote').mouseout(function(){
		$('#red_anecdote').slideUp();
	 })

	//Summon testimonial
	$('.summon_testimonial').click(function(){
	
		target = $(this).attr('href');
		$('#quote').remove();
			$.ajax({
			  url: target,
			  cache: true,
			  success: function(html){
              html = $(html).find('#quote').hide();
			  $('#content_right').prepend(html).find('#quote').fadeIn();
			  $('#view_letter').lightBox()
	  		}
		});

	return false;
	 })
	
	
	$('.hoverstate').css({display:'block'});
	
	$("#menu a").hover(function(){$(this).find(".hoverstate").slideUp(150,"swing");},function(){$(this).find(".hoverstate").slideDown(150,"swing");}).focus(function(){$(this).find(".hoverstate").slideUp(150,"swing");}).blur(function(){$(this).find(".hoverstate").slideDown(150,"swing");});

	$(window).resize(function(){positionLogo(100)});

	$(window).scroll(function(){positionLogo(100)});
	
	$('.hidden').hide();
	
	$('#location_c').change(function(){$('#clients_form').submit();});
	$('#location_a').change(function(){$('#agencies_form').submit();});

	//Team page

	function biogLink(){
		$('#team #team_right textarea').hide();

		$('#team #team_right a[rel="biog-link"]').click(function(){$('#team #team_right textarea').slideDown(100,function(){positionLogo(50);}); return false;})
		
	}
	
	biogLink();


	$('#team #team_left a').click(function(){

		$('.red').removeClass('red');

		$(this).addClass('red');

		url = this.href;

		$('#team').css({minHeight:$('#team').height()});

		$('#team #team_right').fadeOut(100).load(url+" #team_right > *", function(){
			biogLink();
			$(this).fadeIn(150,function(){
				positionLogo(200)}
			)
		});

		return false;
	})

});


