/*
*	@class: Model
*	Exposes database access methods 
*/
function Model()
{
	/*
	*	@properties:
	*	@property: Model.sites.collection containing all Site objects
	*	@property: Model.towns.collection of towns and their coordinates
	*/	
	this.sites = [];	
	this.towns = [];	
	
	/*
	*	@method: Model.loadData
	*	Creates Site objects from the database
	*/
	this.loadData = function()
	{	
		for(var i in json.sites)
		{
			var site = new Site();
			site.name = json.sites[i].LocationName;
			site.address1= json.sites[i].Address1;
			site.address2= json.sites[i].Address2;
			site.address3= json.sites[i].Address3;
			site.city= json.sites[i].City;
			site.day= json.sites[i].Hours1;
			site.hours= json.sites[i].Hours2;
			site.phone= json.sites[i].WorkPhone;
			site.comments= json.sites[i].Comments;
			site.restrictions = json.sites[i].Restrictions;
			site.B_Battery = json.sites[i].B_Battery;
			site.B_Lamps = json.sites[i].B_Lamps;
			site.B_Thermostats = json.sites[i].B_Thermostats;
			site.B_ElectronicDevs = json.sites[i].B_ElectronicDevs;
			site.B_OtherHgProducts = json.sites[i].B_OtherHgProducts;
			site.R_Battery = json.sites[i].R_Battery;
			site.R_Lamps = json.sites[i].R_Lamps;
			site.R_Thermostats = json.sites[i].R_Thermostats;
			site.R_ElectronicDevs = json.sites[i].R_ElectronicDevs;
			site.R_OtherHgProducts = json.sites[i].R_OtherHgProducts;				
			
			site.lat= json.sites[i].POINT_Y;
			site.lng= json.sites[i].POINT_X;
			
			this.sites.push(site);
		}	
			
		for(var i in json.towns)
		{
			var town = {name: '', lat: '', lng: '', LONG_MIN_X : '', LAT_MIN_Y : '', LONG_MAX_X : '', LAT_MAX_Y : ''};
			town.name =  json.towns[i].TOWN;
			town.lat = json.towns[i].LAT_CENTER_Y;
			town.lng = json.towns[i].LONG_CENTER_X;
			town.LONG_MIN_X= json.towns[i].LONG_MIN_X
			town.LAT_MIN_Y= json.towns[i].LAT_MIN_Y
			town.LONG_MAX_X= json.towns[i].LONG_MAX_X
			town.LAT_MAX_Y= json.towns[i].LAT_MAX_Y
			this.towns.push(town); 
		}
	}		
	
		
	/*
	*	@class: Site
	*	A mercury product dropoff location
	*/
	function Site()
	{
		/*
		*	@properties:
		*	@property: Site.name.The name of the location
		*	@property: Site.address1.Location address information 
		*	@property: Site.address2.Location address information 
		*	@property: Site.address3.Location address information 
		*	@property: Site.city.Location address information 
		*	@property: Site.day.Location operating hours information
		*	@property: Site.hours.Location operating hours information
		*	@property: Site.phone.Location phone number
		*	@property: Site.comments.Other location information
		*	@property: Site.lat.Location latitude
		*	@property: Site.lng.Location longitude
		*	@property: Site.B_Battery."Y" or "F" if this site accepts business batteries
		*	@property: Site.B_Lamps."Y" or "F" if this site accepts business lamps
		*	@property: Site.B_Thermostats."Y" or "F" if this site accepts business thermostats 
		*	@property: Site.B_ElectronicDevs."Y" or "F" if this site accepts business electronic devices
		*	@property: Site.B_OtherHgProducts."Y" or "F" if this site accepts business other mercury products
		*	@property: Site.R_Battery."Y" or "F" if this site accepts residential batteries
		*	@property: Site.R_Lamps."Y" or "F" if this site accepts residential lamps
		*	@property: Site.R_Thermostats."Y" or "F" if this site accepts residential thermostats 
		*	@property: Site.R_ElectronicDevs."Y" or "F" if this site accepts residential electronic devices
		*	@property: Site.R_OtherHgProducts."Y" or "F" if this site accepts residential other mercury products
		*/
		this.name;
		this.address1;
		this.address2;
		this.address3;
		this.city;
		this.day;
		this.hours;
		this.phone;
		this.comments;
		this.lat;
		this.lng;
		this.restrictions;
		this.B_Battery;
		this.B_Lamps;
		this.B_Thermostats;
		this.B_ElectronicDevs;
		this.B_OtherHgProducts;
		this.R_Battery;
		this.R_Lamps;
		this.R_Thermostats;
		this.R_ElectronicDevs;
		this.R_OtherHgProducts;

		/*
		*	@method: Site.isResidential
		*	returns true if the site is a residential dropoff location
		*/
		this.isResidential = function()
		{
			if(this.R_Battery  == "Y" || this.R_Battery  == "F") return 1;
			if(this.R_Lamps  == "Y" || this.R_Lamps  == "F") return 1;
			if(this.R_Thermostats  == "Y" || this.R_Thermostats  == "F") return 1;
			if(this.R_ElectronicDevs  == "Y" || this.R_ElectronicDevs  == "F") return 1;
			if(this.R_OtherHgProducts  == "Y" || this.R_OtherHgProducts  == "F") return 1;
			return 0;
		};
		
		/*
		*	@method: Site.isCommercial
		*	returns true if the site is a commercial dropoff location
		*/
		this.isCommercial = function()
		{
			if(this.B_Battery == "Y" || this.B_Battery == "F") return 1;
			if(this.B_Lamps == "Y" || this.B_Lamps == "F") return 1;
			if(this.B_Thermostats == "Y" ||  this.B_Thermostats == "F") return 1;
			if(this.B_ElectronicDevs == "Y" || this.B_ElectronicDevs == "F") return 1;
			if(this.B_OtherHgProducts == "Y" || this.B_OtherHgProducts == "F") return 1;
			return 0;	
		};
	}//end class Site
		
	/*
	*	@method: Model.getAllSites
	*	returns a collection of all Site objects
	*/
	this.getAllSites = function()
	{
		return this.sites;
	}	
	
	/*
	*	@method: Model.getCoordinatesForTown
	*	returns the coordinates of the geographical center of the given town
	*/
	this.getCoordinatesForTown = function(townname)
	{
		var coordinates = { lat : '0', lng : '0', LONG_MIN_X: '', LAT_MIN_Y : '', LONG_MAX_X : '', LAT_MAX_Y: ''};		
	
		for(var i in this.towns)		
			if(this.towns[i].name == townname)
			{	
				coordinates.lat = this.towns[i].lat;
				coordinates.lng = this.towns[i].lng;
				coordinates.LONG_MIN_X= this.towns[i].LONG_MIN_X;
				coordinates.LAT_MIN_Y= this.towns[i].LAT_MIN_Y;
				coordinates.LONG_MAX_X= this.towns[i].LONG_MAX_X;
				coordinates.LAT_MAX_Y= this.towns[i].LAT_MAX_Y;
			}			
			
		return coordinates;
	}
	
	/*
	*	@method: Model.getAllTowns
	*	returns an array of all towns in this format {name, lat, lng}
	*/
	this.getAllTowns = function()
	{
		return this.towns;
	}
	
	/*
	*	@method Model.getLongestTownLengthInLetters
	*	return the string length of the longest town
	*/
	this.getLongestTownLengthInLetters =function()
	{
		var longest = 0;
		for(var i in this.towns)					
			if(this.towns[i].name.length > longest) longest = this.towns[i].name.length;
		
		return longest;		
	}

}//end class Model
