var ShepherdGroup = Class.create({
	initialize: function(){
//		debug("initialize")
		this.leaders = [];
		this.address = "";
		this.lat = "";
		this.lon = "";
		this.day = "";
		this.time = "";
		this.frequency = "";
		this.type = "";
		this.childcare = "";
		this.status = "";
		this.infoText = "";
		// this.imageLoc = "";
		// this.bio = "";
		this.marker = null;
	},
	
	loadDataAsXml: function(xml){
		// this.leader[this.leader.length] = this.loadLeader(xml);
		this.leaders = this.loadLeaders(xml);
		
		//debug("leaders:" + this.leaders);
		// this.address = this.loadAddress(xml);
		this.street = this.loadStreet(xml);
		this.city = this.loadCity(xml);
		this.state = this.loadState(xml);
		this.zip = this.loadZip(xml);
		this.lat = this.loadLat(xml);
		this.lon = this.loadLon(xml);
		this.day = this.loadDay(xml);
		this.time = this.loadTime(xml);
		this.frequency = this.loadFrequency(xml);
		this.type = this.loadType(xml);
		this.childcare = this.loadChildcare(xml);
		this.status = this.loadStatus(xml);
		// this.imageLoc = this.loadImageLoc(xml);
		// this.bio = this.loadBio(xml);
		this.marker = this.createMarker();
		
		// debug("leader:"+this.leader);
	},
	
	loadLeaders: function(shepherdGroup){
		//debug("load leaders: " + shepherdGroup);
		var rtnLeaders = [];
		var tempLeaders = shepherdGroup.getElementsByTagName("Leaders")[0].getElementsByTagName("Leader");
		//debug("Leaders: " + tempLeaders);
		//debug("Leaders Len: " + tempLeaders.length);
		for ( var i = 0; i < tempLeaders.length; i++ )
		{
			//debug("Leader: " + tempLeaders[i]);
			var leader = new Leader();
			leader.loadLeaderAsXml( tempLeaders[i] );
			//debug("leader name: " + leader.name);
			rtnLeaders[rtnLeaders.length] = leader;
		}
		
		// debug(rtnLeaders[0].name + ", " + rtnLeaders[0].imageLocation + "," + rtnLeaders[0].bio);
		// debug(rtnLeaders[1].name + ", " + rtnLeaders[1].imageLocation + "," + rtnLeaders[1].bio);
		
		return rtnLeaders;
	},
	
	// loadAddress: function(shepherdGroup){
	// 	// var x = getSimpleElement("Address",xml);
	// 	// alert("x:"+x);
	// 	// return "asdf";
	// 	return GXml.value(shepherdGroup.getElementsByTagName("Address")[0]);
	// },
	// 
	loadStreet : function(shepherdGroup){
		return GXml.value(shepherdGroup.getElementsByTagName("Address")[0].getElementsByTagName("Street")[0]);
	},
	
	loadCity : function(shepherdGroup){
		return GXml.value(shepherdGroup.getElementsByTagName("Address")[0].getElementsByTagName("City")[0]);
	},
	
	loadState : function(shepherdGroup){
		return GXml.value(shepherdGroup.getElementsByTagName("Address")[0].getElementsByTagName("State")[0]);
	},
	
	loadZip : function(shepherdGroup){
		return GXml.value(shepherdGroup.getElementsByTagName("Address")[0].getElementsByTagName("Zip")[0]);
	},
	
	loadLat: function(shepherdGroup){
		return GXml.value(shepherdGroup.getElementsByTagName("Lat")[0]);
	},
	
	loadLon: function(shepherdGroup){
		return GXml.value(shepherdGroup.getElementsByTagName("Lon")[0]);
	},
	
	loadDay: function(shepherdGroup){
		return GXml.value(shepherdGroup.getElementsByTagName("Meets")[0].getElementsByTagName("Day")[0]);
	},
	
	loadTime: function(shepherdGroup){
		return GXml.value(shepherdGroup.getElementsByTagName("Meets")[0].getElementsByTagName("Time")[0]);
	},
	
	loadFrequency: function(shepherdGroup){
		return GXml.value(shepherdGroup.getElementsByTagName("Meets")[0].getElementsByTagName("Frequency")[0]);
	},
	
	loadType: function(shepherdGroup){
		return GXml.value(shepherdGroup.getElementsByTagName("Type")[0]);
	},
	
	loadChildcare: function(shepherdGroup){
		// debug("childcare:"+GXml.value(shepherdGroup.getElementsByTagName("Childcare")[0]))
		return GXml.value(shepherdGroup.getElementsByTagName("Childcare")[0]);
	},
	
	loadStatus: function(shepherdGroup){
		// debug('load status');
		return GXml.value(shepherdGroup.getElementsByTagName("Status")[0]);
	},
	
	// loadImageLoc: function(shepherdGroup){
	// 	// debug("image loc");
	// 	return GXml.value(shepherdGroup.getElementsByTagName("Image")[0]);
	// },
	// 
	// loadBio: function(shepherdGroup){
	// 	// debug("bio:"+GXml.value(shepherdGroup.getElementsByTagName("Bio")[0]))
	// 	return GXml.value(shepherdGroup.getElementsByTagName("Bio")[0]);
	// },
	
	createMarker: function(){
		var closedSGIcon = new GIcon(G_DEFAULT_ICON);
		closedSGIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png";
		closedSGIcon.iconSize = new GSize(34, 34);
		
		var openSGIcon = new GIcon(G_DEFAULT_ICON);
		openSGIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
		openSGIcon.iconSize = new GSize(34, 34);
		
		// debug("lat,lon:" + this.lat + "," + this.lon);
		var point = new GLatLng( this.lat, this.lon );
		
		var markerOptions=null;
		if ( this.status == "Open" || this.status == "open" )
		{
			markerOptions = { icon:openSGIcon };
		}
		else if ( this.status == "Closed" || this.status == "closed" )
		{
			markerOptions = { icon:closedSGIcon };
		}

		var new_marker = new GMarker(point, markerOptions);
		this.marker = new_marker;
		//debug("createMarker");
		var text = this.getInfoWindowText();
		//debug("infoWindowText:\n\n:" + text);

		GEvent.addListener(new_marker, "click", function() {
			new_marker.openInfoWindowHtml( text, {maxWidth:500} );
		});

		return new_marker;
	},
	
	getAddressString : function(){
		return this.street + ", " + this.city + ", " + this.state + ", " + this.zip;
	},
	
	showInfoWindow : function(){
		var text = this.getInfoWindowText();
		this.marker.openInfoWindowHtml(text, {maxWidth:500});
	},
	
	getInfoWindowText: function(){
//		debug("getInfoTextWindow");
		var info = "<div class='infoWindow'>";
		
		info += "<div class='groupMeetingInfo'>"
		info += "<p>";
		if  (this.frequency)
		{
			if ( this.frequency == "Weekly" || this.frequency == "weekly" )
			{
				info += this.day + "s from " + this.time;
			}
			else if ( this.frequency == "Bi-Weekly" || this.frequency == "bi-weekly" )
			{
				info += "Every other " + this.day + " from " + this.time;
			}
			else if ( this.frequency == "Other" || this.frequency == "other")
			{
				info += "<p>" + this.day + "s from " + this.time + "(See description for details)";
//				info += "<p>" + this.day + "s from " + this.time + "</p><p span='frequencyNote'>See description for details</p>";
			}
			
		}
		
		if ( this.type )
		{
			info += " | For: " + this.type;
		}
	
		if (this.childcare)
		{
			info += " | Childcare: " + this.childcare;
		}
		info += "</p>";
		info += "</div>";

		info += "<div class='leadersDisplay'>";
		if ( this.leaders && this.leaders.length == 1 )
		{
			//debug("leaders len:"+ this.leaders.length);
			info += "<img src='"+this.leaders[0].imageLocation+"' class='leaderImg'/>";
			//debug("1");
			info += "<p><strong>" + this.leaders[0].name + "</strong></p>";
			//debug("1");
			info += "<p class='bio'>" + this.leaders[0].bio + "</p>";
			//debug("1");
		}
		else
		{
			//debug("else: " + this.leaders[0]);
			//debug("else: " + this.leaders[0].imageLocation);
			
			info += "<img src='"+this.leaders[0].imageLocation+"' class='leaderImg'/>";
			info += "<p><strong>" + this.leaders[0].name + "</strong></p>";
			info += "<p class='bio'>" + this.leaders[0].bio + "</p>";
			info += "<div class='clear' />";
			info += "<img src='"+this.leaders[1].imageLocation+"' class='leaderImg'/>";
			info += "<p><strong>" + this.leaders[1].name + "</strong></p>";			
			info += "<p class='bio'>" + this.leaders[1].bio + "</p>";
		}
		info += "</div>"

		info += "</div>";
		return info;
	}
});

var Leader = Class.create({
	
	initialize : function(){
		this.name = "";
		this.imageLocation = "";
		this.bio = "";
	},
	
	loadLeaderAsXml : function( xml ){
		// debug("loadLeader: "+ xml)
		this.name = this.loadName( xml );
		// debug("name: " + this.name);
		this.imageLocation = this.loadImageLocation( xml );
		// debug("image: " + this.imageLocation);
		this.bio = this.loadBio( xml );
		// debug("bio: " + this.bio);
	},
	
	loadName : function ( leader ){
		return GXml.value(leader.getElementsByTagName("Name")[0]);
	},
	
	loadImageLocation : function( leader ){
		// debug("in load image loc");
		// debug("xml value: " + GXml.value(leader.getElementsByTagName("Image")[0]));
		return GXml.value(leader.getElementsByTagName("Image")[0]);
	},
	
	loadBio : function( leader ){
		return GXml.value(leader.getElementsByTagName("Bio")[0]);
	}
});