(function() { 

	var map, geocoder, marker, infowindow;
	var weatherIcons = [];
	var churchIcons = [];
	
	// Defining the shadow image for the marker
  var shadow = new google.maps.MarkerImage(
    'img/my_icons/shadow.png',
    new google.maps.Size(32, 28),
    new google.maps.Point(0, 0),
    new google.maps.Point(8, 30)
  );

  // Defining different MarkerImages for different states
	
	var myImageMarker = function(url,iconSize){
		this.url = url,
		this.mi = new google.maps.MarkerImage(url, new google.maps.Size(17, 28), null, null),
		this.get_mi = function(){ 
			return this.mi;
		},
		this.get_url = function(){
			return this.url;
		}
		return this;
	}
	
	churchIcons[1] = new myImageMarker('img/my_icons/blue.png'); 
	churchIcons[2] = new myImageMarker('img/my_icons/beige.png');
	churchIcons[3] = new myImageMarker('img/my_icons/aqua.png');
	churchIcons[4] = new myImageMarker('img/my_icons/bright_blue.png');
	churchIcons[5] = new myImageMarker('img/my_icons/burnt.png');
	churchIcons[6] = new myImageMarker('img/my_icons/dpurple.png');
	churchIcons[7] = new myImageMarker('img/my_icons/gray.png');
	churchIcons[8] = new myImageMarker('img/my_icons/hot_pink.png');
	churchIcons[9] = new myImageMarker('img/my_icons/light_green.png');
	churchIcons[10] = new myImageMarker('img/my_icons/light_olive.png');
	churchIcons[11] = new myImageMarker('img/my_icons/lime.png');
	churchIcons[12] = new myImageMarker('img/my_icons/orange.png');
	churchIcons[13] = new myImageMarker('img/my_icons/pink.png');
	churchIcons[14] = new myImageMarker('img/my_icons/purple.png');
	churchIcons[15] = new myImageMarker('img/my_icons/dolive.png'); // not used
	churchIcons[16] = new myImageMarker('img/my_icons/navy.png'); // not used
	churchIcons[17] = new myImageMarker('img/my_icons/violet.png');
	churchIcons[18] = new myImageMarker('img/my_icons/yellow.png');
	churchIcons[19] = new myImageMarker('img/my_icons/red.png');
	churchIcons[20] = new myImageMarker('img/my_icons/turquise.png');
	churchIcons[21] = new myImageMarker('img/my_icons/burgandy.png');
	churchIcons[22] = new myImageMarker('img/my_icons/dbrown.png');
	churchIcons[23] = new myImageMarker('img/my_icons/plum.png');
	churchIcons[24] = new myImageMarker('img/my_icons/teal.png'); 
	
	window.onload = function() {
		
	  var options = {  
	    zoom: 14,  
	    center: new google.maps.LatLng(47.68026, -116.78040),  
	    mapTypeId: google.maps.MapTypeId.ROADMAP  
	  };  

	  // Calling the constructor, thereby initializing the map  
		map = new google.maps.Map(document.getElementById('map'), options);  
		
		var bounds = new google.maps.LatLngBounds ();
		
		// Looping through the churches array in weatherData
	  //for (var i = 0; i < churchData.churches.length; i++) {
		for (var type in churchData.churches) {
			//console.log(churchIcons2[1].get_url());
			$("<h4 />") 
			  .html('<img src="' + churchIcons[churchData.churches[type][0].type_id].get_url() + '" width="8" height="13" alt="" /> ' + type) 
			  .appendTo("#list");
			
			for (var i = 0; i < churchData.churches[type].length; i++){
				
			var church = churchData.churches[type][i];
	       // creating a variable that will hold the current weather object
	       //var churches = churchData.churches[i];
    
	       // Creating marker
	       var marker = new google.maps.Marker({
	           position: new google.maps.LatLng(church.lat, church.lng), 
	           map: map,
	           shadow: shadow,
	           icon: churchIcons[church.type_id].get_mi(),
               title: church.name
	       });

			bounds.extend (new google.maps.LatLng(church.lat, church.lng));
			
			// Wrapping the event listener inside an anonymous function
			// that we immediately invoke and passes the variable i to.
			(function(church, marker) {

				google.maps.event.addListener(marker, 'click', function() { 
					// Check to see if an InfoWindow already exists
					if (!infowindow) {
						infowindow = new google.maps.InfoWindow();
					}
					
					var content = document.createElement('div');
					content.id = 'info';
					
					var static_content = '<h2>' + church.name + '</h2>';
					
					if(church.premium == 1){
						if(church.photo){
							static_content += '<div class="item photo"><a href="uploads/' + church.photo + '" onclick="Shadowbox.open(this);return false;" title="' +  church.name + '"><img src="uploads/thumbs/' + church.photo + '" alt="" /></a></div>';
						}
					}
					
					static_content += '<div class="type">' + church.type + '</div>';
					static_content += '<div class="address">' + church.street_address + '</div>';
					static_content += '<div class="item city">' + church.city + ', ' + church.state + ' ' + church.zip + '</div>';
					
					if(church.premium == 1){
						static_content += '<div class="item description">' + church.description + '</div>'; 
						static_content += '<div class="item hours">Hours: ' + church.hours + '</div>';
						static_content += '<div class="item phone">Phone: ' + church.phone + '</div>';
						if(church.video){
							static_content += '<div class="item video"><img src="img/video_icon.gif" alt="" /> <a href="' + church.video + '" onclick="Shadowbox.open({ content: \'' + church.video + '\', width: 640, height: 480, player:\'iframe\' }); return false;" title="' +  church.name + '">Watch video</a></div>';
						}
						if(church.website){
							static_content += '<div class="item website"><a href="' + church.website + '">' + church.website + '</a></div>';
						}
						if(church.email){
							static_content += '<div class="item email"><a href="mailto:' + church.email + '">' + church.email + '</a></div>';
						}
					}
					
					var a = document.createElement('a');
					a.innerHTML = '<img src="img/zoom-icon.gif" alt="zoom in" />';
					a.href = '#';
					a.setAttribute('class', 'zoom'); 
					a.onclick = function() {
						map.setCenter(marker.getPosition());
						map.setZoom(15);
						return false;
					};
					
					content.innerHTML = static_content;
					content.appendChild(a);

					// Setting the content of the InfoWindow
					infowindow.setContent(content);
					// Opening the InfoWindow
					infowindow.open(map,marker);
					
					
				});
				
				// add list items
				$("<ul />") 
				  .html('<li><a href="">' + church.name + '</a></li>') 
				  .click(function(){ 
				    google.maps.event.trigger(marker,'click');
						return false;
				  })
					.appendTo("#list");
				})(church, marker);
			}
	  };
		map.fitBounds (bounds);
	}
})();
