jQuery(document).ready(function($) {

	if (typeof AOS == 'object') {
		/*
		 * AOS (animation on scroll)
		*/
		AOS.init({
		 offset: 0,
		 duration: 1000
		});
	}

	/**
	 * Lazy Load Images
	 */
	//$('.lazy').lazy();

	var $doc = $(document);

	function getQueryStringParameterByName(name, searchLocation) {
		name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
		var regexS = "[\\?&]" + name + "=([^&#]*)";
		var regex = new RegExp(regexS);

		if (!searchLocation) {
			searchLocation = window.location.search
		}
		var results = regex.exec(searchLocation);

		if (results == null) {
			return "";
		}
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	}

	$('.search-wrap').on('click', '.search-toggle', function(e) {
		var $this = $(this);
		var target = $this.attr('data-target');

		if (target) {
			$(target).slideToggle(400, function(){});
			$('.search-toggle').toggleClass('toggle-closed');

			if ($('.search-toggle').hasClass('toggle-closed')) {
				$('.hdr .search-form input').focus();
				$('.hdr .search-form input').select();
			}
			e.preventDefault();
		}
	});

	$('.menu-secondary-menu-container > ul > li.menu-item-has-children > a').on('click', function (e) {
		var $li = $(this).parent();
		if ($li.hasClass('ddactive')) {
			$li.children('.sub-menu').slideUp(300, function () {
				$li.removeClass('ddactive');
				$(this).removeClass('ddopen');
			});
		} else {
			$li.addClass('ddactive').children('.sub-menu').slideDown(300, function () {
				$(this).addClass('ddopen').css('display', '');
			});
		}
		e.preventDefault();
	});

	$('.nav-primary .sub-menu ul ul li.hide').each(function (a, b) {
		if ( a == 0 ) {
			$(this).parent('ul').append('<span id="more-less">More</span>');
		}
	});

	$('#more-less').on('click', function () {
		if ( $(this).text() == 'More' ) {
			$(this).parent('ul').children('li.hide').fadeIn();
			$(this).text('Less');
		} else {
			$(this).parent('ul').children('li.hide').fadeOut();
			$(this).text('More');
		}
	});

	if ( $('.block table').length ) {
		$('.block table').wrap('<div class="table-wrap"></div>');
	}

	$(window).on('load', function () {
		/* scroll to functionality */
		if (window.location.hash && ($(window.location.hash).length || $("[data-id='" + window.location.hash + "']").length )) {
			if ($("[data-id='" + window.location.hash + "']").length) {
				$el = $("[data-id='" + window.location.hash + "']");

			} else {
				$el = $(window.location.hash);
			}
			go_to_by_scroll($($el));
		}

		$(document).on('click', '.link-down', function (e) {
			e.preventDefault();

			var href = $.attr(this, 'href');

			if ($(this).data('href')) {
				href = $(this).data('href');
			}

			go_to_by_scroll($(href));
		});

		function go_to_by_scroll($el, offset_num) {
			if (offset_num == undefined) {
				offset_num = 0;
			}

			$('html, body').animate({
				scrollTop: $el.offset().top - offset_num
			}, 1000);
		}
	});

	if (jplGetQueryStringParameterByName('s')) {
		$('.search-wrap .search-toggle').trigger('click');
		if ($('.pg-title').length && $('.pg-title').text() == 'Search Results' ) {
			$('.pg-title').html('Search Results: <span>'+ jplGetQueryStringParameterByName('s') +'</span>');
		}
	}

	$('.library-finder form').on('submit', function(e) {
		e.preventDefault();
		if ( $(this).siblings('.no-results').length ) $(this).siblings('.no-results').remove();

		$val = $("#libary-finder").val();

		if ( $val ) {
			window.location.href = 'https://suny-gcc.primo.exlibrisgroup.com/discovery/search?vid=01SUNY_GCC:01SUNY_GCC&tab=Everything&search_scope=MyInst_and_CI&mode=Basic&displayMode=full&bulkSize=10&highlight=true&dum=true&query=any,contains,'+ $val +'&displayField=all&pcAvailabiltyMode=true';
		} else {
			$(this).after('<p class="no-results">Please enter a value</p>');
		}
	});

	if ( $('.nav-library').length ) {
		$nav_top = $('.nav-library').offset().top;
		$nav_height = $('.nav-library').height();
		if ($('.nav-toggle').css('display') == 'none') {
			$(window).on('scroll', function(){
				$win_top = $(this).scrollTop();
				if ($win_top > $nav_top) {
					$('body').addClass('nav-library-sticky').css('padding-top', $nav_height);

				} else {
					$('body').removeClass('nav-library-sticky').css('padding-top', '0');
				}
			});
		}
	}

	$('.search-directory a').on('click', function(e) {
		e.preventDefault();
		$('.search-directory').submit();
	});


	$('.search-directory').on('submit', function(e) {
		e.preventDefault();

		var $first_name_val = $("#search-first-name").val();
		var $last_name_val = $("#search-last-name").val();
		var directory_url = 'https://directory.genesee.edu/';

		if ( $first_name_val ) {
			directory_url += '?first_name=' + $first_name_val;
		}

		if ( $last_name_val ) {
			var sep = ( $first_name_val ) ? '&' : '?';
			directory_url += sep + 'last_name=' + $last_name_val;
		}

		window.location.href = directory_url;
	});

	$('#search-form').prepend('<div class="search-options"><button class="active search-tab" data-target="#search-website">Search by Website</button><button class="search-tab" data-target="#search-directory">Search by People</button></div>');

	/* toggle */
	$doc.on('click', '.search-tab', function(e) {
		/* toggle content with ID of elements data-target */
		var target = $(this).attr('data-target');
		if (target) {
			if ( !$(this).hasClass('active') ) {
				$('.search-content').hide();
				$('.search-options').children('button').removeClass('active');
				$(target).slideToggle();
				$(this).toggleClass('active');

			}
			e.preventDefault();
		}
	});
});
