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.Series - A datastructure designed to contain a series of Garmin.Sample.
 18  * 
 19  * @author Bobby Yang bobby.yang.at.garmin.com
 20  * @version 1.0
 21  */
 22 /**Contains a series of samples.  Could represent tracks, routes, waypoints, and
 23  * many other types of data.
 24  * @class Garmin.Series
 25  * @constructor 
 26  * @param (String) type - the type of data this series contain.  Should be determined by what
 27  * 							information is recorded by the samples this series contain.
 28  */
 29 Garmin.Series = function(type){};
 30 Garmin.Series = Class.create();
 31 Garmin.Series.prototype = {
 32 
 33 	initialize: function(seriesType) {
 34 		this.seriesType = seriesType;
 35 		this.samples = new Array();
 36 	},
 37 
 38 	getSeriesType: function() {
 39 		return this.seriesType;
 40 	},
 41 
 42 	setSeriesType: function(seriesType) {
 43 		this.seriesType = seriesType;
 44 	},
 45 
 46 	getSamples: function() {
 47 		return this.samples;		
 48 	},
 49 	
 50 	getSample: function(index) {
 51 		var targetSample = null;
 52 		if (index >= 0 && index < this.samples.length) {
 53 			targetSample = this.samples[index];
 54 		}
 55 		return targetSample;
 56 	},
 57 	
 58 	addSample: function(sample) {
 59 		if (sample != null) {
 60 			this.samples.push(sample);			
 61 		}
 62 	},
 63 	
 64 	getSamplesLength: function() {
 65 		return this.samples.length;
 66 	},
 67 
 68 	getFirstValidLocationSample: function() {
 69 		return this.findNearestValidLocationSample(0, 1);
 70 	},
 71 	
 72 	getLastValidLocationSample: function() {
 73 		return this.findNearestValidLocationSample(this.getSamplesLength()-1, -1);
 74 	},
 75     
 76     /** Find the nearest valid location point to the index given
 77      * 
 78      * @param index is the index
 79      * @param incDirection is an int in the direction we'd like to look positive 
 80      * 	nums are forward, negative nums are backwards
 81      * 
 82      * @type Garmin.Sample 
 83      * @return The nearest point (possibly the index) that has a valid latitude and longitude
 84      */ 
 85     findNearestValidLocationSample: function(index, incDirection) {
 86 		return this._findNearestValidLocationSampleInternal(index, incDirection, 0);
 87     },
 88 	
 89 	_findNearestValidLocationSampleInternal: function(index, incDirection, count) {
 90 		// make sure we haven't looped through every element already
 91 		if (this.getSamplesLength() > 0 && count < this.getSamplesLength()) {
 92 			// make sure index requested is within bounds
 93 			if (index >= 0 && index < this.getSamplesLength()) {
 94 				var sample = this.getSample(index);
 95 				if (sample.isValidLocation()) {
 96 					return sample;	
 97 				} else {
 98 					return this._findNearestValidLocationSampleInternal(index + incDirection, incDirection, ++count);
 99 				}
100 			} else if (index > this.getSamplesLength()) {
101 				return this._findNearestValidLocationSampleInternal(this.getSamplesLength()-1, -1, count);
102 			} else {
103 				return this._findNearestValidLocationSampleInternal(0, 1, count);
104 			}	
105 		} else {
106 			return null;
107 		}		
108 	},
109 	
110 	printMe: function(tabs) {
111 		var output = tabs + "  [Series]\n";
112 		output += tabs + "    seriesType: " + this.seriesType + "\n";	
113 		output += tabs + "    samples:\n";
114 		for (var i = 0; i < this.samples.length; i++) {
115 			output += this.samples[i].printMe(tabs + "    ");
116 		}
117 		return output;
118 	},
119 	
120 	toString: function() {
121 		return "[Series]";
122 	}
123 };
124 
125 Garmin.Series.TYPES = {
126 	history:		"history",
127 	route:			"route",
128 	waypoint:		"waypoint",
129 	course:			"course"
130 };
131 /*
132 // Dynamic include of required libraries and check for Prototype
133 // Code taken from scriptaculous
134 // TODO: put this code in a library and reuse is instead of copying it to new files
135 var GarminSeries = {
136 	require: function(libraryName) {
137 	  // inserting via DOM fails in Safari 2.0, so brute force approach
138 	  document.write('<script type="text/javascript" src="'+libraryName+'"></script>');
139 	},
140 
141 	load: function() {
142 		if((typeof Prototype=='undefined') || 
143 			(typeof Element == 'undefined') || 
144 			(typeof Element.Methods=='undefined') ||
145 			parseFloat(Prototype.Version.split(".")[0] + "." +
146 			Prototype.Version.split(".")[1]) < 1.5) {
147 			throw("GarminSeries requires the Prototype JavaScript framework >= 1.5.0");
148 		}
149 
150 		$A(document.getElementsByTagName("script"))
151 		.findAll(
152 			function(s) {
153 				return (s.src && s.src.match(/GarminSeries\.js(\?.*)?$/))
154 			}
155 		)
156 		.each(
157 			function(s) {
158 				var path = s.src.replace(/GarminSeries\.js(\?.*)?$/,'../../');
159 				var includes = s.src.match(/\?.*load=([a-z,]*)/);
160 				var dependencies = 'garmin/activity/GarminMeasurement' +
161 									',garmin/activity/GarminSample';
162 			    (includes ? includes[1] : dependencies).split(',').each(
163 					function(include) {
164 						GarminSeries.require(path+include+'.js') 
165 					}
166 				);
167 			}
168 		);
169 	}
170 }
171 
172 GarminSeries.load();*/