/**
 *	Main Javascript Init
 */
$(document).ready(function() {
	//---> Main Nav
	$('a.nav-link')
		.css( {backgroundPosition: "right 0"} )
		.mouseover(function(){
			$(this).stop().animate(
					{backgroundPosition:"(0 -32px)"}, 
					{duration:200})
			})
		.mouseout(function(){
			$(this).stop().animate(
					{backgroundPosition:"(0 0)"}, 
					{duration:200})
			});
	
	//---> Footer Social Links
	$('#social-links li a')
		.css( {backgroundPosition: "right 0"} )
		.mouseover(function(){
			$(this).stop().animate(
					{backgroundPosition:"(right -21px)"}, 
					{duration:200})
			})
		.mouseout(function(){
			$(this).stop().animate(
					{backgroundPosition:"(right 0)"}, 
					{duration:200})
			});
		
	//---> Top Link
	$().UItoTop({ easingType: 'easeOutQuart' });
});

function social_share_popup(url) {
	window.open(url,'sharer','toolbar=0,status=0,width=800,height=600');
	return false;
}

function startPageHero() {
	//---> Set Option / Variables
	var frequency = 4000; // change img every 4 seconds
	var transition = 1000;
	var timeout = 240000; // stop transitions after 4 minutes (60000 = 1 minute)
	var unique_max = 10; // "wait time" before changing image again (prevents frequently changing the same image placement)
	var offset = 4;
	var total_imgs = $("#page-hero .overflow-wrap .center-wrap").children().length;
	var previous_imgs = [];
	
	//---> Start Interval
	var page_hero = window.setInterval(function() {
		//---> Recursively get a unique new random number
		function get_unique_random() {
			var rand_num = Math.floor(Math.random()*(total_imgs - offset + 1)) + (offset/2);
			
			return ((previous_imgs.length > 0) && ($.inArray(rand_num, previous_imgs) >= 0))
				? get_unique_random() : rand_num;
		}

		//---> Set Variables
		var imgs_array = $(".page-hero-img").map(function() { return this.id; }).get();
		var all_imgs = imgs_array.join('|');
		var random_number = get_unique_random();
		var random_image_id = "img_count-" + random_number;
		
		//---> make ajax call for new filename
		$.get("/scripts/ajax/random_hero_img.php", { imgs: all_imgs }, function(data) {
			$("#" + random_image_id).prepend('<img src="/media/db-image/hero_images/'+data+'" height="130" width="195" class="page-hero-img" id="'+data+'">');
			
			//---> select random div, fade out img, change img src/id, fade in img
			$("#" + random_image_id + " img").last().fadeOut(transition, function() {
				$("#" + random_image_id + " img").last().remove();
				if(previous_imgs.length >= unique_max) { previous_imgs.shift(); }
				previous_imgs.push(random_number);
			});
		});
	}, frequency);
	window.setTimeout(function() { clearInterval(page_hero); }, timeout);
}

