(function($) {
	// $.fn.trackmytour = function(points, colour, width) {
	$.fn.trackmytour = function(points, options) {
		options = $.extend({
			colour: '#555555',
			width: 5,
			showPolylines: true,
			zoom: 8,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			center: new google.maps.LatLng(0, 0),
			
			photoCommentBalloonTemplate: "\
				<div class='#{className}'> \
					<span class='floatRight'>Day #{day}</span> \
					<b>#{timestamp}</b> \
					<hr/> \
					<div class='clearfix'><img class='photo' src='#{img}' height='#{h}' width='#{w}' />#{comment}</div> \
					<hr/> \
					<p class='faint'>Posted: #{timesince} ago</p> \
					<p><img src='#{tour_type_icon}'> <img src='#{type_icon}'> <img src='#{weather_icon}'></p> \
				</div>",

			commentBalloonTemplate: "\
				<div class='#{className}'> \
					<span class='floatRight'>Day #{day}</span> \
					<b>#{timestamp}</b> \
					<hr/> \
					<div class='clearfix'>#{comment}</div> \
					<hr/> \
					<p class='faint'>Posted: #{timesince} ago</p> \
					<p><img src='#{tour_type_icon}'> <img src='#{type_icon}'> <img src='#{weather_icon}'></p> \
				</div>",

			balloonTemplate: "\
				<div class='#{className}'> \
					<span class='floatRight'>Day #{day}</span> \
					<b>#{timestamp}</b> \
					<hr/> \
					<p class='faint'>Posted: #{timesince} ago</p> \
					<p><img src='#{tour_type_icon}'> <img src='#{type_icon}'> <img src='#{weather_icon}'></p> \
				</div>"
		}, options);

		return this.each(function() {
			var gmap = new google.maps.Map(this, options);
			var bounds = new google.maps.LatLngBounds();
			var polyLine = [];
	
			$.each(points, function(i, point) {	
				point.latlng = new google.maps.LatLng(point.lat, point.lon);
	
				$.addMarker(gmap, point, options);
	
				polyLine.push(point.latlng);
				bounds.extend(point.latlng);
			});
			
			if (options.showPolylines) {
				new google.maps.Polyline({
					path: polyLine,
					strokeColor: options.colour,
					strokeOpacity: 1.0,
					strokeWeight: options.width
				}).setMap(gmap);						
			};
			
			google.maps.event.addListener(gmap, 'click', function () {
				if ($.lastInfoWindow) {
					$.lastInfoWindow.close();
				};
			});
	
			gmap.fitBounds(bounds);
		});
	};

	$.getBalloonText = function(point, options) {
		if ((point.comment.stripTags().length == 0) && (point.img.length == 0)) {
			bText = $.template(options.balloonTemplate, point);
			bText = $.template(bText, {
				className: 'balloon commentBalloon'
			});
		} else if (point.img.length > 0) {
			bText = $.template(options.photoCommentBalloonTemplate, point);

			if (point.comment.length > 120) {
				bText = $.template(bText, {
					className: 'balloon photoCommentBalloon'
				});
			} else {
				bText = $.template(bText, {
					className: 'balloon photoSimpleCommentBalloon'
				});
			};
		} else {
			bText = $.template(options.commentBalloonTemplate, point);
			bText = $.template(bText, {
				className: 'balloon commentBalloon'
			});
		};

		return bText;
	};

	$.addMarker = function (gmap, point, options) {
		var marker = new google.maps.Marker({
			position: point.latlng,
			map: gmap
		});

		var infowindow = new google.maps.InfoWindow({
			content: $.getBalloonText(point, options),
			maxWidth: 10000
		});

		var sID = '#wp' + point.id;
 		
		var updateSideArea = function() {
			if ( $('#waypoints').size() ) {
			     	$('#waypoints li.selected').removeClass('selected');
				
				$(sID).addClass('selected');
				
				$('#sidescrollarea').scrollTo(sID, 350, {
					offset: -50
				});
				
				self.location.hash = point.id;
			};
		};
		
		$(sID).click(function (e) {	
			google.maps.event.trigger(marker, 'click');
		});
		
		google.maps.event.addListener(marker, 'click', function () {
			if ($.lastInfoWindow) {
				$.lastInfoWindow.close();
			};
			
			$.lastInfoWindow = infowindow;
			infowindow.open(gmap, marker);

			updateSideArea();
		});
	};

	$.template = function (s, o) {
		return s.replace(/#{([^{}]*)}/g, function (a, b) {
			var r = o[b];
			return typeof r === 'string' || typeof r === 'number' ? r : a;
		});
	};
	 /*
	$.fn.tabs = function () {
		return this.each(function () {
			var tabs = $(this);

			tabs.find('a').click(function (e) {
				tabs.find('a').each(function () {
					$($(this).attr('href')).hide();
				});

				$($(this).blur().attr('href')).show();

				tabs.find('li').removeClass('selectedTab');
				$(this).closest('li').addClass('selectedTab');

				return false;
			});

			tabs.find('a:first').click();
		});
	};
		*/
})(jQuery);

$.extend(String.prototype, {
	stripTags: function () {
		return this.replace(/<\/?[^>]+>/gi, '');
	}
});