


function setupMoreLinks() {
	$('.moreLink').click(function(e) {
		e.preventDefault();
		$(this).hide().siblings('.moreText').slideDown(200,function(){$(this).attr('style','display: inline;')});
	});
}

function setupInputFields() {
	$('.field')
		.focus(function(e) {
			$(this).addClass('focus');
			$('label[for='+$(this).attr('id')+']').addClass('focus');		
		})
		.blur(function(e) {
			$(this).removeClass('focus');
			$('label[for='+$(this).attr('id')+']').removeClass('focus');			
		});
}

function popupOverlay(element) {
	$('#overlay').css('opacity', 0).removeClass('hide').fadeTo(300, 0.6).one('click', function(e) {
		closeOverlay(element);
	});
	$(element).removeClass('hide').css({
		'opacity': 0,
		'margin-top': -($(element).height()/2 + 10) + 'px',
		'margin-left': -($(element).width()/2 + 10) + 'px'
	}).fadeTo(300, 1);
	
// this code did not seem to work correctly - the result was that a click anywhere inside
// sign up overlay would close the box
//	$(element).children('.overlayContent h4 .closeOverlay').one('click', function(e) {
// this one seems to work
	$('.overlayContent h4 .closeOverlay').one('click', function(e) {
	// But this is not good in the future as we add in more and more overlay windows as
	// everyone of them will get assigned the click event. But this will do for now. :)
		e.preventDefault();
		closeOverlay(element);		
	});
}

function closeOverlay(element) {
	$('#overlay').fadeTo(300, 0, function() { $('#overlay').addClass('hide') });
	$(element).fadeTo(300, 0, function() { $(element).addClass('hide') });
}

$(document).ready(function(){
	setupMoreLinks();
	setupInputFields();

	// Sets textarea search box to autosubmit on 'enter' key
	$('#search_term').keydown(function(e) {
		 if (e.keyCode == 13) {
			 $('#mainSearch').submit();
			 return false;
		 }
	});
	
	$('.signup').click(function(e) {
		e.preventDefault();
		popupOverlay($('#overlay-SignUp'));
	});
	
	$('.feedback').click(function(e) {
		e.preventDefault();
		window.open('http://www.deepdyve.com/lp/feedback.php', 'newwindow', 'height=380, width=480, top='+(window.screen.height-360)/2+', left='+(window.screen.width-480)/2+', toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no');
	});

	// Infers search terms from the referring page and puts them into the search box
	var query = new Query();
	if (query.chooseBest()) {
		document.getElementById('search_term').value = query.chooseBest();
	}

});

