var currentQuote = 0;
var quotes;

$(document).ready(function($) {
	initRollovers();
});

function addPermissionAutocheck(inputs, permissionCheckbox) {	
	$(document).ready(function() {		
		$(inputs).click(function() {
			checkPermissionBox(permissionCheckbox);
		});
	});
}
function checkPermissionBox(permissionCheckbox) {
	$(permissionCheckbox).attr('checked', 'checked');
}
function initGeoBoxes() {
	hideUnselectedGeos();
	$('#geoSelects [type=checkbox]').click(function() {
		var parentId = $(this).attr('geoid');
		var parentIdSelctor = '[parent='+parentId+']';
		if($(this).is(':checked')) {
			$(parentIdSelctor).removeClass('hidden');
		} else {
			$(parentIdSelctor + ', ' + parentIdSelctor + ' .branch').addClass('hidden');
			$(parentIdSelctor + ' :checked').attr('checked', false);
		}
		
	});
}
function hideUnselectedGeos() {
	var geoCheckboxes = $('.parentCheck');
	for(i=0; i < geoCheckboxes.length; i++) {
		var currentCheckboxId = '#'+geoCheckboxes[i].id;
		if(!($(currentCheckboxId).is(':checked'))) {
			var unchekedParentId = $(currentCheckboxId).attr('geoid');
			$('[parent='+unchekedParentId+'], [parent='+unchekedParentId+'] .branch').addClass('hidden');
		} 
	}
}

function initJournalGuidlines() {
	$('#toggleJournalGuidlines').click(function() {
		var displayValue = $('#journalTerms').css('display');
		if(displayValue == 'none') {
			$('#toggleJournalGuidlines').text('Hide Journal Guidelines');
			$('#journalTerms').show();
		} else {
			$('#toggleJournalGuidlines').text('View Journal Guidelines');
			$('#journalTerms').hide();
		}
	});
}
function initRollovers() {
	$(".rollover").hover(
		function() {
			this.src = this.src.replace("_off","_on");
		},
		function() {
			this.src = this.src.replace("_on","_off");
		}
	);
}
function initSearchHide() {
	var initalText = $('#showHideSearch').text();
	$('#showHideSearch').click(function() {
		displayClass = $('#searchFields').attr('class');
		if(displayClass == 'hidden') {
			$('#searchFields').attr('class', '');
			$('#showHideSearch').text('hide search options');
		} else {
			$('#searchFields').attr('class', 'hidden');
			$('#showHideSearch').text(initalText);
		}
	});
}
function initSplashPageToggle() {
	var dropHeight = 320;
	$('#enter').css({'display':'block'});
	$('#dropdownBox').css({'top':'-'+dropHeight+'px'});
	
	$('#closeDropdown').click(function(){
		$('#dropdownBox').animate({'top':'-'+dropHeight+'px'}, function() {
			$('#enter').css({'display':'block'});	
		});
		
	});
	$('#enter').click(function(){
		$('#dropdownBox').animate({'top':'0px'});
		$('#dropdownButton').css({'display':'block'});
		$('#enter').css({'display':'none'});
	});
	$('#whatsTakeActionButton, #whatsTheExhibitButton, #whyLoginButton').click(function(){
		var targetID = $(this).attr('id');
		var targetToShow = '';
		
		switch(targetID) {
			case 'whatsTakeActionButton':
				targetToShow = '#whatsTakeAction';
				break;
			case 'whatsTheExhibitButton':
				targetToShow = '#whatsTheExhibition';
				break;
			case 'whyLoginButton':
				targetToShow = '#whyLogin';
				break;
		}
		
		$('.content').animate({'right':'100%'});
		$(targetToShow).animate({'left':'0%'});
		$('.close').click(function(){
			$('.content').animate({'right':'0%'});
			$(targetToShow).animate({'left':'100%'});
		});
	});
}
function setKeyNavigation(previous, next) {
	previousURL = previous;
	nextURL = next;
	$('html').keydown(function(event){
		if(event.keyCode == 37) {
			location.href = previousURL;
		}else if(event.keyCode == 39){
			location.href = nextURL;
		}
	});
}
function rotateFacts() {
	var i = 1;
	var previousSlide = '';
	
	playFacts();
	var interval = setInterval(function() {
		var factsList = $('#openingFacts li');
		var currentSlide = factsList[i];
		if(previousSlide == '') previousSlide = factsList[0];
		$(previousSlide).hide();
		$(currentSlide).show();
		previousSlide = currentSlide;
		i++;
		if(i == (factsList.length - 1)) i = 0;
		playFacts();
	}, 5000);
}
function playFacts() {
	var tTime = 1500;
	$('#openingFacts, #openingFacts .red').css({opacity:0});
	$('#openingFacts .red').animate({opacity:1}, tTime);
	$('#openingFacts').animate({opacity:1}, tTime);
}
