function GoogleMap()
{
	this.map='';
	this.markers=[];
	this.infowindow='';
	this.initArray=[];
	this.currentID='';
	this.nativeWidth=0;
	this.browserSupportFlag=false;
}
GoogleMap.prototype.Register = function(efunc)
{
	this.efunc=efunc;

	efunc();
};
GoogleMap.prototype.Init = function(__initArray)
{
	var me = this;
	initArray = __initArray;

	me.directionsEnabled = (__initArray.Directions != undefined) ? __initArray.Directions : true;
	me.InfoWindow = (__initArray.InfoWindow != undefined) ? __initArray.InfoWindow : true;

	me.checkIsLocatable();

	var coords = __initArray.LatLng.split(',');
	coords[0] = parseFloat(coords[0]);
	coords[1] = parseFloat(coords[1]);
	var latlng = new google.maps.LatLng(coords[0], coords[1]);
	var myOptions = {
		zoom: __initArray.Zoom,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	me.map = new google.maps.Map(document.getElementById("googleMap"), myOptions);

	if(me.InfoWindow == true)
		me.map.panBy( 0, -120 );

	me.addMarker({
		initArray: __initArray,
		Coords: coords,
		ID: me.Encode(__initArray.LatLng),
		LatLng: latlng
	});

	me.nativeWidth = $('#googleMap').width();
}
GoogleMap.prototype.addMarker = function(__markerArray)
{
	var me = this;

	var tID = __markerArray.ID;
	var marker = new google.maps.Marker({
		position: __markerArray.LatLng,
		map: me.map,
		id: tID
	});

	if(me.InfoWindow == true)
	{
		var directionsDOM = (me.browserSupportFlag == true) ? '<button class="sButton" style="width: 100%;" id="dirBtnAuto">Get Directions to Me</button><br />' : '';

		var directions = (me.directionsEnabled == true) ? '<strong>Directions:</strong><br />'+directionsDOM+'<input type="text" style="width: 166px;margin-right: 5px;" id="gmap_destination" /><button class="sButton" style="width: 50px;" id="dirBtn">GO</button>' : '';

		var contentString = [
			'<div style="height: 225px;width: 250px;">',
				'<img src="'+__markerArray.initArray.Logo+'" style="max-width: 180px;max-height: 90px;" /><br />',
				'<p>'+__markerArray.initArray.Address+'<br />',
				__markerArray.initArray.City+', '+__markerArray.initArray.Province+'<br />',
				__markerArray.initArray.PostalCode+'</p>',
				directions,
			'</div>'
		].join('');
		infowindow = new google.maps.InfoWindow({
			content: contentString
		});

		marker.infoWindow = infowindow;
	}

	me.markers[tID] = marker;

	if(me.InfoWindow == true)
	{
		me.markers[tID].infoWindow.open(me.map, me.markers[tID]);
		google.maps.event.addListener(me.markers[tID], 'click', function()
		{
			me.ShowInfo(me.markers[tID].id);
		});
	}

	me.currentID = tID;

	if(me.directionsEnabled == true)
	{
		google.maps.event.addListener(me.markers[tID].infoWindow, 'domready', function()
		{
			$('#dirBtn').unbind('click').click(function()
			{
				if($('#gmap_destination').val() != '')
					me.getDirections(__markerArray.Coords[0],__markerArray.Coords[1],$('#gmap_destination').val());
				else
					alert('You must enter your location before we can direct you to the dealership.');
			});
			$('#dirBtnAuto').unbind('click').click(function()
			{
				me.getDirections(__markerArray.Coords[0],__markerArray.Coords[1],'auto');
			});
		});
	}
}
GoogleMap.prototype.ShowInfo = function(__id)
{
	var me = this;
	me.markers[me.currentID].infoWindow.close();
	me.markers[__id].infoWindow.open(me.map, me.markers[__id]);

	me.map.setCenter(me.markers[__id].position);
	me.map.panBy( 0, -120 );

	me.currentID = __id;
}
GoogleMap.prototype.reInit = function()
{
	var me = this;
	$('#googleMap').html('');
	$('#directionsWrapper').hide();
	me.efunc();
}
GoogleMap.prototype.getDirections = function(__lat, __lng, __destination)
{
	var me = this;

	if(me.initialLocation != undefined || me.browserSupportFlag == false)
	{
		$('#directions').html('<h3>Directions:</h3>');
		$('#directionsWrapper').show();
		if($('#googleMap').width() >= me.nativeWidth)
			$('#googleMap').width( $('#googleMap').width() - 170 );
		
		//google.maps.event.trigger(me.map, 'resize');

		var directionsService = new google.maps.DirectionsService();
		var directionsDisplay = new google.maps.DirectionsRenderer();

		directionsDisplay.setMap(me.map);

		var xy=[];
		for(x in me.initialLocation)
		{
			xy.push(me.initialLocation[x]);
		}

		var destination = (__destination == "auto") ? new google.maps.LatLng(xy[0], xy[1]) : __destination;
		var request = {
			origin: destination,
			destination: new google.maps.LatLng(__lat, __lng),
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		}

		directionsService.route(request, function(result, status)
		{
			if(status == google.maps.DirectionsStatus.OK)
			{
				directionsDisplay.setDirections(result);
				me.markers[me.currentID].infoWindow.close();

				me.map.fitBounds(result.routes[0].bounds);

				me.showDirections(result);
			}
		});
	} else {
		if(me.browserSupportFlag == true)
			alert('You need to allow us to detect your location first.\nPlease click "Allow" or "Share Location" at the top of the page if you wish to let us automatically fill in your location.')
	}
}
GoogleMap.prototype.showDirections = function(__resObj)
{
	var me = this;
	var route = __resObj.routes[0].legs[0];

	$('#directions').append('<p>Distance: <strong>'+route.distance.text+'</strong><br />Duration: <strong>'+route.duration.text+'</strong></p>');

	for(i=0;i < route.steps.length;i++)
	{
		$('#directions').append('<p'+( (i%2==0)?' style="background: #EEE;"':'' )+'>'+route.steps[i].instructions+'</p>');
	}

	var padding = {
		'top': $('#directions').css('padding-top'),
		'bottom': $('#directions').css('padding-bottom')
	};
	for(x in padding){ padding[x] = parseInt(padding[x].replace('px', '')); }

	$('#directionsWrapper').append('<button id="close_directions" class="sButton">Close Directions</button>');
	$('#close_directions').css({ 'width': $('#directions').outerWidth()+'px', 'display': 'block', 'margin': '0px auto' });
	$('#directions').css({
		'height': (($('#googleMap').height()-$('#close_directions').outerHeight())-(padding.top+padding.bottom))+'px'
	});

	$('#close_directions').unbind('click').click(function()
	{
		me.closeDirections();
	});
}
GoogleMap.prototype.closeDirections = function()
{
	var me = this;
	$('#googleMap').html('').width( me.nativeWidth );
	$('#directionsWrapper').hide();
	$('#close_directions').remove();
	me.efunc();
}
GoogleMap.prototype.Encode = function(str)
{
	var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
	var encoded = [];
	var c = 0;
	while (c < str.length)
	{
		var b0 = str.charCodeAt(c++);
		var b1 = str.charCodeAt(c++);
		var b2 = str.charCodeAt(c++);
		var buf = (b0 << 16) + ((b1 || 0) << 8) + (b2 || 0);
		var i0 = (buf & (63 << 18)) >> 18;
		var i1 = (buf & (63 << 12)) >> 12;
		var i2 = isNaN(b1) ? 64 : (buf & (63 << 6)) >> 6;
		var i3 = isNaN(b2) ? 64 : (buf & 63);
		encoded[encoded.length] = chars.charAt(i0);
		encoded[encoded.length] = chars.charAt(i1);
		encoded[encoded.length] = chars.charAt(i2);
		encoded[encoded.length] = chars.charAt(i3);
	}
	return encoded.join('');
}
GoogleMap.prototype.checkIsLocatable = function()
{
	var me = this;

	if(navigator.geolocation)
	{
		me.browserSupportFlag = true;
		navigator.geolocation.getCurrentPosition(function(position)
		{
			me.initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
		}, function()
		{
			me.browserSupportFlag = false;
		});
	}
	else if(google.gears)
	{
		me.browserSupportFlag = true;
		var geo = google.gears.factory.create('beta.geolocation');
		geo.getCurrentPosition(function(position)
		{
			me.initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
		}, function()
		{
			me.browserSupportFlag = false;
		});
	} else {
		me.browserSupportFlag = false;
	}
}
