var teams =$H();
var lastTeamOver = null;

function Team(id, name, colour, markerNo, normalMarker, lateMarker, tags, url, model, flag, owner, captain) {

	this.id = id;
	this.name = name;
	this.colour = colour;
	this.markerNo = markerNo;
	this.normalMarker = normalMarker;
	this.lateMarker = lateMarker;
	this.tags = tags;
	this.url = url;
	this.model = model;
	this.flag=flag;
	this.owner = owner;
	this.captain = captain;
	
	this.trackVisible = false;
	this.clinkWaiting = false;
	
	
	this.marker = null;
	this.track = new Array();
	this.position = null;
	this.history = null;

	this.latest = team_latest;
	this.moveHistory = team_moveHistory;
	this.setHistory = team_setHistory;
	this.setPosition = team_setPosition;
	this.showTrack = team_showTrack;
	this.hideTrack = team_hideTrack;
	this.clunk = team_clunk;
	this.clink = team_clink;
	this.over = team_over;
	this.aged = team_aged;
	this.checkTags = team_checkTags;
}


/*=======Private methods for internal use only========*/

function team_aged() {
	//this.marker.setImage("img/markers/" + this.lateMarker + "/" + this.markerNo + ".png");
}

function team_hideTrack() {
	for (var index = 0; index < this.track.length; ++index) {
		if (this.track[index] != null) this.track[index].setMap(null);
	}
	
	this.track = new Array();
	this.trackVisible = false;
	this.clinkWaiting = false;		
}

function team_latest() {
	if (this.position) {
		this.marker.setPosition(new google.maps.LatLng(this.position.latitude, this.position.longitude));
	}
}

function team_showTrack(track) {
	for (var index = 0; index < this.track.length; ++index) {
  		if (this.track[index] != null) this.track[index].setMap(null);
  	}
	
	
	for (var index = 0; index < track.length; ++index) {
		
		var llArray = new Array();
		
		for (var posIndex = 0; posIndex < track[index].length; ++posIndex) {
			var trackNode = new google.maps.LatLng(track[index][posIndex].a,track[index][posIndex].o);
			llArray.push(trackNode);
		}
		
		var section = new google.maps.Polyline({
		    path: llArray,
		    strokeColor: "#" + this.colour,
		    strokeOpacity: 0.8,
		    strokeWeight: 3,
		    geodesic: true
		  });

		section.setMap(map);
		this.track[index] = section;
		
	}
	
	this.trackVisible = true;
	this.clinkWaiting = false;
}

function team_setHistory(h) {
	this.history = new Array();
	for (var index = 0; index < h.length; ++index) {
  		var p = h[index];
		this.history[p.m] = p.p;
  	}	 
}

function team_moveHistory(v) {
	if (this.history) {
		while (!this.history[v] && v > 0) {
			v--;
		}
		if (this.history[v]) {
			this.marker.setPosition(new google.maps.LatLng(this.history[v].a, this.history[v].o));
			this.marker.sbdId=this.history[v].i;
		}
	}
}

function team_setPosition(position) {
	this.position = position;

	// remove old map blob
	if (this.marker) this.marker.setMap(null);
	
	var marker = new google.maps.Marker({
	      position: new google.maps.LatLng(position.latitude, position.longitude),
	      map: map,
	      icon: new google.maps.MarkerImage("img/markers/" + this.normalMarker + "/" + this.markerNo + ".png", new google.maps.Size(20, 34)), shadow: new google.maps.MarkerImage('http://maps.gstatic.com/intl/en_us/mapfiles/markers/marker_sprite.png', new google.maps.Size(37,34), new google.maps.Point(20, 0), new google.maps.Point(10, 34))
	  	});
	
	
	marker.team = this;
	
	
    google.maps.event.addListener(marker, 'click', function() {
		marker.team.clink();
	});

	
    google.maps.event.addListener(marker, "mouseover", function() {
		marker.team.over();
	});
	
    google.maps.event.addListener(marker, "mouseout", function() {
		teamOut();
	});
	
    
	
	//marker.setMap(map);
	this.marker = marker;
	
}


function team_clink() {
	if (this.clinkWaiting) return;
	
	//if (this.trackVisible == false) startBigWait();
	this.clinkWaiting = true;
	
	var extra = "";
/*	if (!liveMode) {
		extra = "&d=" + $('daySelect').options[$('daySelect').selectedIndex].value + $('timeSelect').options[$('timeSelect').selectedIndex].value;	
	}
*/	// get route polyline
	var opt = {
	    	method: 'post',
	    	postBody: 'a=v&t=' + this.id + extra,
	    	onSuccess: pollResponse,
	    	onFailure: function(t) {
	        	alert('Error ' + t.status + ' -- ' + t.statusText);
	    	}
	}
	new Ajax.Request('RaceAJAX', opt);
	
	
	if (this.url.length > 5) {
		if ($('teamInfoFrame').src != this.url) $('teamInfoFrame').src=this.url;
		if (!$('teamInfo').visible()) Effect.BlindDown("teamInfo",{ duration: 0.5 });
	} else {
		if ($('teamInfo').visible()) Effect.BlindUp('teamInfo', { duration: 0.5 });	
	}
	
	
}


function team_clunk() {
	dragged = false;
	
	if (this.marker) {
		map.panTo(this.marker.getPosition());
	}
	if (liveMode) {
		// inform server
		var opt = {
		    	method: 'post',
		    	postBody: 'a=t&t=' + this.id,
		    	onSuccess: function(t) {},
		    	onFailure: function(t) {
		        	alert('Error ' + t.status + ' -- ' + t.statusText);
		    	}
		}
		new Ajax.Request('RaceAJAX', opt);
	}
}


function team_over() {	
	
	
	
	//$('tooltip').style.display="";
	
	if (!liveMode && !pCache.containsKey(this.marker.sbdId)) {
		lastTeamOver = this;
		// do AJAX
		var opt = {
		    	method: 'post',
		    	postBody: 'a=o&p=' + this.marker.sbdId,
		    	onSuccess: pollResponse,
		    	onFailure: function(t) {
		        	alert('Error ' + t.status + ' -- ' + t.statusText);
		    	}
		}
		new Ajax.Request('RaceAJAX', opt);
		return;
	} 
	
	// if we get here, we're not in live mode, and we should have a position in the cache
	var pos = this.position;
	if (!liveMode) {
		pos = pCache.get(this.marker.sbdId);
	}

	if (pos) {
		
		var osText="";
		if (os) osText= pos.extra.os + "<br/>";
		
		var info = "<p>" + pos.extra.gpsTime + "<br>" + pos.extra.laf + "&nbsp;" + pos.extra.lof + "<br/>" + osText + pos.course + "&deg; at " + (pos.speed).toFixed(1) + " " + units + "</p>";
		
		new Tip($('map'), info, {
			title: this.name,
			hook: { tip: 'topLeft', mouse: true },
			offset: { x: 14, y: 14 },
			delay: 0,
			style: 'racetip',
			width: 170
		});
 	}
}

function team_checkTags() {
	if (this.tags.length == 0 || this.tags.intersect(tagsOn).length > 0) {
		$('team_' + this.id).style.display="";
		if (this.marker) this.marker.setVisible(true);
		for (var index = 0; index < this.track.length; ++index) {
			if (this.track[index] != null) this.track[index].setMap(map);
		}
		
	} else {
		$('team_' + this.id).style.display="none";
		for (var index = 0; index < this.track.length; ++index) {
			if (this.track[index] != null) this.track[index].setMap(null);
		}
		if (this.marker) this.marker.setVisible(false);
	}
}


function teamOut() {
	if ($('map').prototip) $('map').prototip.remove();
}

function ntos(n){
    n=n.toString(16);
    if (n.length == 1) n="0"+n;
    n="%"+n;
    return unescape(n);
}

