// JavaScript Document

/* --------------- Functions that need to run after the document loads -------------------*/

$(document).ready(function(){
				 
	// quick nav dropdown functionality
	$("#quick-nav li.quick-link").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
   });

	// Load Images for rotation
	if($("body").is("#main")) {
		$.get("/site-resources/home-page-image-rotation.html", function(data){
			setupRotation(data);
		});
	}
	
	// News, Events, Articles Feed for Homepage
	$("#info-list-nav li a").click(function (event) {
		event.preventDefault();
		elementID = $(this).attr('href');
		$(this).addClass('active-list');
		$(this).parent().siblings().children('a').removeClass('active-list');
		$(elementID).show().siblings().hide();
	});
	
// map-nav functionality
	$("#momentum-church-missions188 #loc1-nav a").hover(function(){
		$("#momentum-church-missions188 #loc1 a").addClass("hover");
	},function(){
		$("#momentum-church-missions188 #loc1 a").removeClass("hover");
   });
	
	$("#momentum-church-missions188 #loc2-nav a").hover(function(){
		$("#momentum-church-missions188 #loc2 a").addClass("hover");
	},function(){
		$("#momentum-church-missions188 #loc2 a").removeClass("hover");
   });
	
	$("#momentum-church-missions188 #loc3-nav a").hover(function(){
		$("#momentum-church-missions188 #loc3 a").addClass("hover");
	},function(){
		$("#momentum-church-missions188 #loc3 a").removeClass("hover");
   });
	
	$("#momentum-church-missions188 #loc4-nav a").hover(function(){
		$("#momentum-church-missions188 #loc4 a").addClass("hover");
	},function(){
		$("#momentum-church-missions188 #loc4 a").removeClass("hover");
   });
	
	$("#momentum-church-missions188 #loc5-nav a").hover(function(){
		$("#momentum-church-missions188 #loc5 a").addClass("hover");
	},function(){
		$("#momentum-church-missions188 #loc5 a").removeClass("hover");
   });
	
	$("#momentum-church-missions188 #loc6-nav a").hover(function(){
		$("#momentum-church-missions188 #loc6 a").addClass("hover");
	},function(){
		$("#momentum-church-missions188 #loc6 a").removeClass("hover");
   });
	
	$("#momentum-church-missions188 #loc7-nav a").hover(function(){
		$("#momentum-church-missions188 #loc7 a").addClass("hover");
	},function(){
		$("#momentum-church-missions188 #loc7 a").removeClass("hover");
   });

});

/* -------------------------------------------------------------------- */

/* ---------- START EXTRA FUNCTIONS --------- */

function setupRotation (data) {

	//default values
	itemClass = "nav-item";
	itemFirstClass = "first-item";
	itemLastClass = "last-item";
	timerLen = 8000; // equals 3 secs
	elementID = "banner-counter";
	
	$('#featured-content').append('<a id="mainbanner-link"></a><ul id="banner-counter"></ul>');	

	//parse data
	itemArray = data.split(",");
	itemArray.splice(itemArray.length - 1, 1); // remove the extra data
	itemArrayLen = itemArray.length;
	// data format: pagetitle,imageurl,linkurl

	//load images
	for (x = 1; x < itemArrayLen; x = x + 3) {		
		//image
		tmpValue = itemArray[x];
		itemArray[x] = new Image();
		itemArray[x].src = tmpValue;
	}
	
	//create html for the gallery nav
	for (x = 0; x < (itemArrayLen/3); x++) {
		
		if (x == 0) {
			itemTmpClass = itemClass + ' ' + itemFirstClass;
		} else if (x == itemArrayLen - 1) {
			itemTmpClass = itemClass + ' ' + itemLastClass;
		} else {
			itemTmpClass = itemClass;
		}
			
		$('#' + elementID).append('<li class="' + itemTmpClass + '"><a>' + (x+1) + '</a></li>');	
	}
	
	// When the gallery nav is clicked switch banner
	$('#' + elementID + ' > ' + '.' + itemClass).click(function () {
		// Clear the old timer. Switch to the clicked item, and restart the timer.
		clearTimeout(bannerTimer);
		switchActive(this);
		bannerTimer = setTimeout("advanceActive()", timerLen);		
	});
	
	switchActive();
	bannerTimer = setTimeout("advanceActive()", timerLen);
}

function activateEl(elementRef) {
	$(elementRef).addClass("active");
	$(elementRef).siblings().removeClass("active");
}

function switchActive (elementRef) {
	
	// Find the next element or use the passed element reference
	if (elementRef == null) {
		object = $('#' + elementID + ' > ' + '.active').next('.' + itemClass);
	} else {
		object = elementRef;
	}
	
	// If at the end then start over
	if ($(object).length == 0) {
		object = $('#' + elementID + ' > ' + '.' + itemClass + ':first-child');
	}

	// Find the element index
	var itemIndex = $('#' + elementID + ' > ' + '.' + itemClass).index(object);
	
	// Set the image, title, and url live
	$("#mainbanner-link > span").text(itemArray[(itemIndex * 3)]);
	$("#featured-content").css("background-image", 'url(' + itemArray[(itemIndex * 3) + 1].src + ')');
	$("#mainbanner-link").attr('href', itemArray[(itemIndex * 3) + 2]);

	activateEl(object);
}

function advanceActive () {
	switchActive();
	bannerTimer = setTimeout("advanceActive()", timerLen);		
}

function get(param) {		
	var qsParm = new Array();
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
		
	if (param) {
		return qsParm[param]; // if param is specified then just pass one back.
	} else {	
		return qsParm; // if no param spec. then pass all back.
	}
}
