1 if (Garmin == undefined) var Garmin = {};
  2 /**
  3  * Copyright � 2007 Garmin Ltd. or its subsidiaries.
  4  *
  5  * Licensed under the Apache License, Version 2.0 (the 'License')
  6  * you may not use this file except in compliance with the License.
  7  * You may obtain a copy of the License at
  8  *
  9  *    http://www.apache.org/licenses/LICENSE-2.0
 10  *
 11  * Unless required by applicable law or agreed to in writing, software
 12  * distributed under the License is distributed on an 'AS IS' BASIS,
 13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  * See the License for the specific language governing permissions and
 15  * limitations under the License.
 16  * 
 17  * @fileoverview Garmin.Sample - A datastructure designed to contain a number of measurements
 18  * 									recorded at a single point.
 19  * 
 20  * @author Bobby Yang bobby.yang.at.garmin.com
 21  * @version 1.0
 22  */
 23 /**A collection of measurements recorded at a single point.
 24  * @class Garmin.Sample
 25  * @constructor 
 26  */
 27 Garmin.Sample = function(){};
 28 Garmin.Sample = Class.create();
 29 Garmin.Sample.prototype = {
 30 
 31 	initialize: function() {
 32 		// lazy loading related values
 33 		this.isLazyLoaded = false;
 34 		this.factory = null;
 35 		this.dom = null;
 36 		
 37 		// measurements of this sample
 38 		this.measurements = new Hash();
 39 	},
 40 
 41 	setLazyLoading: function(isLazyLoaded, factory, dom) {
 42 		this.isLazyLoaded = isLazyLoaded;
 43 		this.factory = factory;
 44 		this.dom = dom;
 45 	},
 46 
 47 	getMeasurements: function() {
 48 		this.finishLoading();
 49 		return this.measurements;
 50 	},
 51 	
 52 	getMeasurement: function(mKey) {
 53 		this.finishLoading();
 54 		return this.measurements[mKey];
 55 	},
 56 	
 57 	getMeasurementValue: function(mKey) {
 58 		this.finishLoading();
 59 		return (this.measurements[mKey] ? this.measurements[mKey].getValue() : null);
 60 	},
 61 
 62 	getMeasurementContext: function(mKey) {
 63 		this.finishLoading();
 64 		return (this.measurements[mKey] ? this.measurements[mKey].getContext() : null);
 65 	},
 66 	
 67 	getLatitude: function() {
 68 		return this.getMeasurementValue(Garmin.Sample.MEASUREMENT_KEYS.latitude);
 69 	},
 70 	
 71 	getLongitude: function() {
 72 		return this.getMeasurementValue(Garmin.Sample.MEASUREMENT_KEYS.longitude);
 73 	},	
 74 	
 75 	getTime: function() {
 76 		return this.getMeasurementValue(Garmin.Sample.MEASUREMENT_KEYS.time);
 77 	},
 78 	
 79 	setMeasurement: function(mKey, mValue, mContext) {
 80 		// if the key does not exist or is not of type Garmin.Measurement, create a new measurement object
 81 		// else overwrite existing value and context
 82 		if (!this.measurements[mKey] || !(this.measurements[mKey] instanceof Garmin.Measurement)) {
 83 			this.measurements[mKey]= new Garmin.Measurement(mValue, mContext);
 84 		} else {
 85 			this.measurements[mKey].setValue(mValue);
 86 			this.measurements[mKey].setContext(mContext);
 87 		}
 88 	},
 89 	
 90 	/** Determines if this Sample is valid for determing location
 91 	 * @type Boolean
 92 	 * @return True if latitude and longitude exist, false otherwise
 93 	 */
 94     isValidLocation: function() {
 95     	var latitude = this.getMeasurement(Garmin.Sample.MEASUREMENT_KEYS.latitude);
 96     	var longitude = this.getMeasurement(Garmin.Sample.MEASUREMENT_KEYS.latitude);    	
 97         return ((latitude != null && latitude.getValue() != null) && 
 98         		(longitude != null && longitude.getValue() != null));
 99     },	
100 	
101 	/** Finish loading the measurements for this sample if previously lazy-loaded.
102 	 */
103 	finishLoading: function() {
104 		if (this.isLazyLoaded) {
105 			this.factory.finishLoadingSample(this.dom, this);
106 		}	
107 	},	
108 	
109 	printMe: function(tabs) {
110 		var output = ""
111 		output += tabs + "  [Sample]\n";	
112 		
113 		var measKeys = this.measurements.keys();
114 		for (var i = 0; i < measKeys.length; i++) {
115 			output += tabs + "    " + measKeys[i] + ":\n";	
116 			output += this.measurements[measKeys[i]].printMe(tabs + "    "); 
117 		}
118 		
119 		return output;
120 	},
121 	
122 	toString: function() {
123 		return "[Garmin.Sample]"
124 	}
125 };
126 
127 Garmin.Sample.MEASUREMENT_KEYS = {
128 	cadence:			"cadence",
129 	distance:			"distance",
130 	elevation:			"elevation",
131 	heartRate:			"heartRate",
132 	latitude:			"latitude",
133 	longitude:			"longitude",
134 	sensorState:		"sensorState",
135 	time:				"time"
136 };
137 /*
138 // Dynamic include of required libraries and check for Prototype
139 // Code taken from scriptaculous
140 // TODO: put this code in a library and reuse is instead of copying it to new files
141 var GarminSample = {
142 	require: function(libraryName) {
143 	  // inserting via DOM fails in Safari 2.0, so brute force approach
144 	  document.write('<script type="text/javascript" src="'+libraryName+'"></script>');
145 	},
146 
147 	load: function() {
148 		if((typeof Prototype=='undefined') || 
149 			(typeof Element == 'undefined') || 
150 			(typeof Element.Methods=='undefined') ||
151 			parseFloat(Prototype.Version.split(".")[0] + "." +
152 			Prototype.Version.split(".")[1]) < 1.5) {
153 			throw("GarminSample requires the Prototype JavaScript framework >= 1.5.0");
154 		}
155 
156 		$A(document.getElementsByTagName("script"))
157 		.findAll(
158 			function(s) {
159 				return (s.src && s.src.match(/GarminSample\.js(\?.*)?$/))
160 			}
161 		)
162 		.each(
163 			function(s) {
164 				var path = s.src.replace(/GarminSample\.js(\?.*)?$/,'../../');
165 				var includes = s.src.match(/\?.*load=([a-z,]*)/);
166 				(includes ? includes[1] : 'garmin/activity/GarminMeasurement').split(',').each(
167 					function(include) {
168 						GarminSample.require(path+include+'.js') 
169 					}
170 				);
171 			}
172 		);
173 	}
174 }
175 
176 GarminSample.load();*/