$(document).ready(function() {

// INDEX SLIDER

    $('.slider .images').cycle({ 
	    fx:     'scrollHorz', 
	    speed:  1000, 
	    timeout: 6000, 
	});

// CHURCHES AND PARTNERS ACCORDION
	
	$("ul.churches, div.churchmap").hide();

	$("h2 a.toggler").click(function(){
		$(this).parent().next().next().siblings("ul.churches:visible").slideUp("slow");
		$(this).parent().next().next().slideToggle("slow");
		return false;
	});
	
	$("h2 a.map").click(function(){
		$(this).parent().next().siblings("div.churchmap:visible").slideUp("slow");
		$(this).parent().next().slideToggle("slow", function(){
			//Trigger the resize event to force the Gmap tiles to render correctly
			//This is needed since the Gmap is being initialized whilst hidden.
			google.maps.event.trigger(this, 'resize');
			
			//Also update the bounds to center & scale the map to our markers
			locations[$(this).data('mapid')].gmap.fitBounds(locations[$(this).data('mapid')].GLatLngBounds);
		});
		return false;
	});

});  





// ==================================================
// = Churches and Partners Google Map Functionality =
// ==================================================

var maps, gmaps = new Array(), locations = new Array(), activeMarkers = new Array();

function gmaps_init () {
	maps = $('body').find('.gmap');
	
	
	// ===================================
	// = Build an array of all locations =
	// ===================================
	
	//Find each list category of locations
	$('body').find('ul.churches').each(function(i){
		
		//Create a GLatLngBounds Object and add all the points to it so we can figure out the center & zoom for this map
		var GBounds = new google.maps.LatLngBounds();
		
		//Find each location within that category adn save to an array as an object containing the name and GLatLng
		var locationArray = new Array()
		$(this).find('li').each(function(i, listItem){
			locationArray.push({
				name 	: $(listItem).find('a').text(),
				url		: $(listItem).find('a').attr('href'),
				gLatLng : new google.maps.LatLng($(listItem).data('location').lat, $(listItem).data('location').lng)
			});
			
			//Add this point to the GLAtLngBounds, but only if valid point
			if(locationArray[i].gLatLng.lat()){
				GBounds.extend(locationArray[i].gLatLng);
			}
		})
		
		//Create an object containing the name and an array of all the information found
		locations.push(
			{ 
				name : $(this).prev().prev().find('a:first').text(),
				points: locationArray,
				GLatLngBounds: GBounds
			});
	})
	
	
	// =======================
	// = Initialize each map =
	// =======================
	
	//Loop through all the maps found and initialize each
	for(var i = 0; i < maps.length; i++){

		if($("body").hasClass("russia")){
			locations[i].gmap = new google.maps.Map(maps[i], {
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				maxZoom: 3
			});
		}
		else if($("body").hasClass("sudan")){
			locations[i].gmap = new google.maps.Map(maps[i], {
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				maxZoom: 4
			});
		}
		else if($("body").hasClass("mongolia") || $("body").hasClass("zambia")){
			locations[i].gmap = new google.maps.Map(maps[i], {
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				maxZoom: 5
			});
		}
		else if($("body").hasClass("zimbabwe") || $("body").hasClass("nicaragua")){
			locations[i].gmap = new google.maps.Map(maps[i], {
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				maxZoom: 6
			});
		}
		else if($("body").hasClass("cambodia") || $("body").hasClass("guatemala") || $("body").hasClass("syria")){
			locations[i].gmap = new google.maps.Map(maps[i], {
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				maxZoom: 7
			});
		}
		else if($("body").hasClass("usa")){
			locations[i].gmap = new google.maps.Map(maps[i], {
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				maxZoom: 8
			});
		}
		else if($("body").hasClass("south-africa")){
			locations[i].gmap = new google.maps.Map(maps[i], {
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				maxZoom: 10
			});
		}
		else {
			locations[i].gmap = new google.maps.Map(maps[i], {
				mapTypeId: google.maps.MapTypeId.ROADMAP
			});
		}




		locations[i].gmap.fitBounds(locations[i].GLatLngBounds);
		
		//Store the map offset
		$(maps[i]).data('mapid', i);
				
		//Create a marker array and infowindow array in the location array
		locations[i].markers = new Array();
		locations[i].infowindows = new Array();
		
		
		// ===========
		// = Markers =
		// ===========
		
		for(var j = 0; j < locations[i].points.length; j++){
			//Marker
			var marker = new google.maps.Marker({
				position: locations[i].points[j].gLatLng,
				title : locations[i].points[j].name,
				map : locations[i].gmap
			});
			
			//Store the associates GMap and InfoWindow inside the marker to avoid problems with events
			marker.gmap = locations[i].gmap;
			marker.infoWindow = new google.maps.InfoWindow({
				content : "<div class='gmap_infowindow'><h2>" + locations[i].points[j].name +"</h2><p><a href='" + locations[i].points[j].url + "'>More info</a></p></div>"
			});
			
			//Click handler
			google.maps.event.addListener(marker, 'click', function(){
				
				//Cycle through all open windows and close them
				for(i in activeMarkers){
					activeMarkers[i].infoWindow.close();	
				}
				activeMarkers.length = 0;
				
				//Open the window
				this.infoWindow.open(this.gmap, this);
				
				//Add this marker to the activeMarkers array so we can remove the InfoWindow on the next click
				activeMarkers.push(this);
			})
			
			//Store the marker for later
			locations[i].markers.push(marker)
		
		}
	}
	
	
}








