$(document).ready(function() {

	// OVERLAYS
	$(".overlay, .overlay-home").nyroModal();

	$('.overlay-refresh').nyroModal({
        callbacks: {
            afterClose: function() {
                parent.window.location.reload();
            }
        }
	});

	$(".trailer-overlay").click(function(e) {
		e.preventDefault();
		$.nmManual($(this).find("a").attr('href'));
	});
	
	// HOME PAGE FADER
	if ($("#home-slider ul").length > 0) {
		$("#home-slider ul").cycle({ 
			fx: 'fade',
			timeout: 5000,
			pager: '#controls'
		});
	}

	// INTERNAL FADER
	if ($(".cta-show").length > 0) {
		$(".cta-show").cycle({ 
			fx: 'fade',
			timeout: 5000
		});
	}

	// INPUT LABELS
	$(".text").example(function() {
		return $(this).attr('title');
	});

	//FOOTER SEARCH AUTOCOMPLETE
	$( '.event-search-bar input[name="geoquery"]' ).autocomplete({
		source: "/assets/js/autocomplete.php",
		minLength: 2,
		open: function(event, ui) {
			// open function is called before autocomplete menu is displayed, 
			// so use timeout of 0 trick to let autocomplete finish 
			setTimeout(function() { checkAutocompletePosition($( '.event-search-bar input[name="geoquery"]' ),'.event-search-bar'); }, 0);
		}
	});
	$( '#footer input[name="geoquery"]' ).autocomplete({
		source: "/assets/js/autocomplete.php",
		minLength: 2,
		open: function(event, ui) {
			// open function is called before autocomplete menu is displayed, 
			// so use timeout of 0 trick to let autocomplete finish 
			setTimeout(function() { checkAutocompletePosition($( '#footer input[name="geoquery"]' ), '#footer'); }, 0);
		}
	});

	$('#on-season input[name="geoquery"]' ).autocomplete({
		source: "/assets/js/autocomplete.php",
		minLength: 2,
		open: function(event, ui) {
			// open function is called before autocomplete menu is displayed, 
			// so use timeout of 0 trick to let autocomplete finish 
			setTimeout(function() { checkAutocompletePosition($( '.event-search-bar input[name="geoquery"]' ),'#on-season'); }, 0);
		}
	});
	$('.get-another').click(function() {
		$(this).siblings('.bright-idea-copy').load($(this).attr('href'));
		return false;
	});
	//Launch video dialogue if #video hash is passed:
	if ((window.location.hash.length > 0) && (window.location.hash == '#video')) {
		$.nmManual('/overlays/video/28651696');
	}
	
	//automatically add http:// at the beginning of an url
	$('input#event_beneficiary_website').live('change',function() {
		if ($(this).val().indexOf('http://') !== 0) {
			$(this).val('http://'+$(this).val());
		}
	});
	
});

// position autocomplete menu above autocomplete component if no room below
checkAutocompletePosition = function(inputObj,parentStr) {
	var offset = $('.ui-autocomplete').offset(),
		heightMenu = $('.ui-autocomplete:visible').outerHeight();
		heightComp = inputObj.outerHeight(),
		spaceAbove = undefined,
		spaceBelow = undefined;
	
	if(parentStr == '#footer') {
		// not enough room below component; check if above is better
		spaceBelow = window.innerHeight - offset.top;
		spaceAbove = inputObj.offset().top;
		
		if(spaceAbove > spaceBelow) {
			$('.ui-autocomplete').css('top', (spaceAbove - heightMenu) + 'px');
		}
	}
};

