$(document).ready(function(){
	
	// Array and variables for later
	var sponsors = new Array();
	var i = 0;
	var j = 4; // The number of sponsors that will display at one time
	
	// Remove all sponsors after the first set
	$('#slider p').children().each(function(index){
		sponsors[index] = $(this);
		if(index >= j){
			$(this).remove();
		}
	});
	
	// Execute showhide() at the set interval
	var interval = setInterval(showhide, 4000);
	
	// Sponsor animation function
	function showhide(){
		
		var count = sponsors.length;

		$(sponsors[i]).hide(800).delay(800).queue(function(){
			$(this).remove();
		});
		$(sponsors[j]).appendTo('#slider p').hide().show(800);
		
		i++;
		j++
		
		if(i >= count){
			i = 0;
		}

		if(j >= count){
			j = j - count;
		}
	}

	
	// Tap to hover for touchscreen devices
	$('header nav > ul > li').click(function(){
		if($(this).hasClass('hover')){
			$(this).removeClass('hover');
		} else {
			$('header nav > ul > li').removeClass('hover');
			$(this).addClass('hover');
		}
		
		$(this).mouseout(function(){
			$('header nav > ul > li').removeClass('hover');
		});
	});
	
	
/*
	$('body').click(function(e){
		if(!$(e.target).is('header nav > ul > li')){
			$('header nav > ul > li').removeClass('hover');
		}
	});
*/



	function limitChars(textid, limit, infocontainer){
		var text = $('#'+textid).val(); 
		var textlength = text.length;
		if(textlength > limit){
			$('#' + infocontainer).html('You cannot write more then '+limit+' characters!');
			$('#'+textid).val(text.substr(0,limit));
			return false;
		} else {
			$('#' + infocontainer).html('You have '+ (limit - textlength) +' characters left.');
			return true;
		}
	}

	$('#prInterest').keyup(function(){
		limitChars('prInterest', '300', 'charlimitinfo');
	})

	/* Initialize the Stepping Stones map on the home page. */
	initialize_map();
});
