var F = new Object();
var colors = ["#e44046", "#53b6f1","#b61ca8", "#e0a033",  "#0e718a", "#cd591f","#43bfa5", "#bcc931",  "#734bf7", "#8a603a"]
F.defaultColor = "#333333";
F.colors = colors;

F.icons = {
	start: {
		url: '../images/map_icons/start_icon.png',
		size: [45,37],
		anchor: [0,37],
		infoWindowAnchor: [5,1],
		shadow: {
			url: '../images/map_icons/shadow.png',
			size: [53, 45]
		}
	},
	live: {
		url: '../images/map_icons/live_icon.gif',
  		size: [24,24],
  		anchor: [12,12],
  		shadow: {
  			url: '../images/map_icons/live_icon_shadow.png',
  			size: [28,28]
  		}
  	},
  	lapPoint: {
  		url: '../images/map_icons/lap.png',
  		size: [17,9],
  		anchor: [8,9],
  		infoWindowAnchor: [0,0],
  		shadow: {
  			url: '../images/map_icons/lap_shadow.png',
  			size: [17,9]
  		}
  	},
  	fastestMarker: {
  		url: '../images/map_icons/fast_icon.png',
  		size: [35,27],
  		anchor: [0,27],
  		infoWindowAnchor: [0,0],
  		shadow: {
  			url: '../images/map_icons/flag_shadow.png',
  			size: [43,35]
  		}
  	},
	slowestMarker: {
		url: '../images/map_icons/slow_icon.png',
		size: [35,27],
		anchor: [0,27],
		infoWindowAnchor: [0,0],
		shadow: {
			url: '../images/map_icons/flag_shadow.png',
			size: [43,35]
		}
  	},
	highestMarker: {
  		url: '../images/map_icons/high_icon.png',
  		size: [35,27],
  		anchor: [0,27],
  		infoWindowAnchor: [0,0],
  		shadow: {
  			url: '../images/map_icons/flag_shadow.png',
  			size: [43,35]
  		}
  	},
	lowestMarker: {
  		url: '../images/map_icons/low_icon.png',
  		size: [35,27],
  		anchor: [0,27],
  		infoWindowAnchor: [0,0],
  		shadow: {
  			url: '../images/map_icons/flag_shadow.png',
  			size: [43,35]
  		}
  	},
  	camera: {
  		url: '../images/map_icons/camera.png',
  		size: [46,29],
  		anchor: [23,24],
  		infoWindowAnchor: [0,0],
  		shadow: {
  			url: '../images/map_icons/camera_shadow.png',
  			size: [46,29]
  		}
  	},
  	video: {
  		url: '../images/map_icons/video.png',
  		size: [46,29],
  		anchor: [23,24],
  		infoWindowAnchor: [0,0],
  		shadow: {
  			url: '../images/map_icons/video_shadow.png',
  			size: [46,29]
  		}
  	},
  	man: {
  		url: '../images/map_icons/man_icon.png',
  		size: [21,27],
  		anchor: [1,27],
  		infoWindowAnchor: [0,0],
  		shadow: {
  			url: '../images/map_icons/man_icon_shadow.png',
  			size: [29,35]
  		}
  	},
	small_circle: {
  		url: '../images/map_icons/black_circle_small.png',
  		size: [5,5],
  		anchor: [2,2],
  		infoWindowAnchor: [2,2]
  	},
  	zoom_in_button: {
  		url: '../images/map_icons/zoom_in_button.png',
  		size: [41,41],
  		anchor: [1,1]
  	},
  	zoom_out_button: {
  		url: '../images/map_icons/zoom_out_button.png',
  		size: [41,41],
  		anchor: [1,1]
  	}
};

F.getColor = function(index) {
	if( index >= this.colors.length ) {
		return this.defaultColor;
	} else {
		return this.colors[index];
	}
}

function localize(locale, array) {
	var result = locale;
	for( var i = 0; i < array.length; i++ ) {
		result = result.replace('{' + i + '}', array[i]);
	}
	return result;
}

function Unit() {
	this.id;
	this.text;
	this.factor = 1;
	
	this.convert = function(value) {
		if ( this.factor * value < 1000 ) 
			return Math.round(this.factor * value/10)/100;
		if ( this.factor * value > 50000)
			return Math.round(this.factor * value/1000);
		else
			return Math.round(this.factor * value/100)/10;
	}
	
	this.round = function(value) {
		if( value < 1000 )
			return Math.round(value/10)/100;
		if( value > 50000 )
			return Math.round(value/1000); 
		else
			return Math.round(value/100)/10; 
	}
	
	this.next = function() {
		return UNITS[this.id%3+1];
	}
}

var SPEED = new Unit();
SPEED.id = -2;
SPEED.factor = 3600;
SPEED.text = 'km/h';

var IMPERICALSPEED = new Unit();
IMPERICALSPEED.id = -1;
IMPERICALSPEED.factor = 2236.9356;	//TODO: check desimal places
IMPERICALSPEED.text = 'mi/h';

var NAUTICALSPEED = new Unit();
NAUTICALSPEED.id = 0;
NAUTICALSPEED.factor = 1943.64;
NAUTICALSPEED.text = 'nm/h';

var METRIC = new Unit();
METRIC.id = 1;
METRIC.factor = 1;
METRIC.text = 'km';
METRIC.speed = SPEED;

var IMPERICAL = new Unit();
IMPERICAL.id = 2;
IMPERICAL.factor = 0.621371;
IMPERICAL.text = 'mi';
IMPERICAL.speed = IMPERICALSPEED;

var NAUTICAL = new Unit();
NAUTICAL.id = 3;
NAUTICAL.factor = 0.5399;
NAUTICAL.text = 'nm';
NAUTICAL.speed = NAUTICALSPEED;

var TIME = new Unit();
TIME.id = 0;
TIME.factor = 0.2777;
TIME.text = 'h';

var UNITS = new Array();
UNITS[1] = METRIC;
UNITS[2] = IMPERICAL;
UNITS[3] = NAUTICAL;

F.oldMsie = function() {
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	return IE6;
}

F.displayDistance = function(meter, withUnit) {
	return sport.unit.convert(meter) + (withUnit?" " + sport.unit.text:"");
}

/**
 * Converts time in seconds to hours, minutes and seconds
 */
F.displayTime = function(seconds) {
	if( seconds == 0)
		return "0s";
	var hours = Math.floor(seconds / 3600);
	var minutes = Math.floor(seconds % 3600 / 60);
	var seconds = Math.floor(seconds % 60);
	var result = ((hours>0?hours + "h ":"") + (minutes>0?minutes + "min ":"") + (seconds > 0?seconds + "s":""));
	return result;
}

F.displayPace = function(seconds, meters) {
	if( meters == 0 )
		return NaN;
	var local = sport.unit.convert(meters);
	return F.displayTime(seconds / local) + ' per ' + sport.unit.text;
}

/**
 * Displays maximum pace based on maximum speed.
 */
F.displayMaxPace = function(maxSpeed) {
	if( maxSpeed == 0 )
		return NaN;
	return F.displayTime(1000 / maxSpeed) + " per " + sport.unit.text;
}

F.createPosition = function(x,y) {
	if(x.constructor == GLatLng)
		return x;
	
	if(x.lat) {
		return new GLatLng(x.lat, x.lon);
	}
	
	if( y != undefined)
		return new GLatLng(x,y);
	else
		return new GLatLng(x[0],x[1]); 
}

F.getWorkoutImageUrl = function(workout, image, tn) {
	return "/nts/workoutdetail/image.do?ph=false&tn=" + tn + "&u=" + workout.owner.id + "&w=" + workout.id + "&i=" + image.id;
}

function MinMaxObject() {
	this.max_lat = -1000;
	this.max_long = -1000;
	this.min_lat = 1000;
	this.min_long = 1000;

	this.update = function(coords) {
		var me = this;
		$.each(coords, function(key, coord) {
			coord = F.createPosition(coord);
			if (coord.lat() > me.max_lat) {
				me.max_lat = coord.lat();
			}
			
			if (coord.lat() < me.min_lat) {
	      		me.min_lat = coord.lat();
			}
	
	  	    if (coord.lng() > me.max_long) {
				me.max_long = coord.lng();
	  	    }
	  	    
	    	if (coord.lng() < me.min_long) {
	      		me.min_long = coord.lng();
			}
		});
	}
	
	this.getBoundingBox = function() {
		return new GLatLngBounds(
			F.createPosition(this.min_lat, this.min_long),
			F.createPosition(this.max_lat, this.max_long));
	}
	this.getBounds = this.getBoundingBox;
}

$(document).ready(function() {
	/**
	 * Oerride icon newMarker method.
	 */
	if( window.GIcon ) {
		GIcon.prototype.newMarker = function(coord, name, trackId) {
			var marker = new GMarker(F.createPosition(coord), this);
			marker.id = name + (trackId!=undefined?"_" + trackId:""); 
			return marker;
		}
	}
	
	/**
	 * Create a method for opening an info window.
	 */
	if(window.GMarker ) {
		GMarker.prototype.infoWindow = function(html) {
			GEvent.addListener(this, "click", function() {
				this.openInfoWindowHtml(html);
			});
			
		}
		GMarker.prototype.tooltip = function(text) {
			if( text.constructor == String ) {
				this.select().attr('title', text).Tooltip();
			} else {
				this.select().Tooltip(text);
			}
		}
		GMarker.prototype.select = function() {		
			return $('#mtgt_' + this.id);
		}
	}
	
	/**
	 * Create an icon for a specified name.
	 */
	if(window.GIcon) {
		F.createIcon = function(name) {
			var iconInfo = F.icons[name];
			var icon = new GIcon();
			icon.image = iconInfo.url;
			icon.iconSize = new GSize(iconInfo.size[0],iconInfo.size[1]);
			icon.iconAnchor = new GPoint(iconInfo.anchor[0], iconInfo.anchor[1]);
		
			if( iconInfo.infoWindowAnchor ) {				
				icon.infoWindowAnchor = new GPoint(iconInfo.infoWindowAnchor[0], iconInfo.infoWindowAnchor[1]);
			}
			
			if( iconInfo.shadow ) {
				icon.shadow = iconInfo.shadow.url;
				icon.shadowSize = new GSize(iconInfo.shadow.size[0],iconInfo.shadow.size[1]);
			}
		
			return icon;
		}
	}
});

function debug(object) {
	var str = "";
	$.each(object, function(key, value) {
		str += "(" + key + " -> " + value + ")";
	});
	alert(str);
}
