﻿var _latitude, _longitude;


function initialize() {
	var centerlocation = new google.maps.LatLng(_latitude, _longitude);
	if (google.loader.ClientLocation) {
		centerlocation = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
	}

	/*
	// Try Google Gears Geolocation
	else if (google.gears) {
		var geo = google.gears.factory.create('beta.geolocation');
		geo.getCurrentPosition(function (position) {
			centerLocation = new google.maps.LatLng(position.latitude, position.longitude);
			map.setCenter(centerlocation);
		}, null);
	}
	// Try W3C Geolocation (Preferred)
	else if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(function (position) {
			centerLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
			map.setCenter(centerlocation);
		}, null);
	}*/

	var mapOptions = {
		center: centerlocation,
		zoom: 13,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

	var input = document.getElementById($('.grouplocation').attr('id'));
	var autocomplete = new google.maps.places.Autocomplete(input);

	autocomplete.bindTo('bounds', map);

	google.maps.event.addListener(autocomplete, 'place_changed', function () {
	    var place = autocomplete.getPlace();
		_latitude = place.geometry.location.lat();
		_longitude = place.geometry.location.lng();

		$('.hidLatitude').val(_latitude);
		$('.hidLongitude').val(_longitude);
	});

	// Sets a listener on a radio button to change the filter type on Places
	// Autocomplete.
	function setupClickListener(id, types) {
		var radioButton = document.getElementById(id);
		google.maps.event.addDomListener(radioButton, 'click', function () {
			autocomplete.setTypes(types);
		});
	}

	setupClickListener('changetype-all', []);
	setupClickListener('changetype-establishment', ['establishment']);
	setupClickListener('changetype-geocode', ['geocode']);

	google.maps.event.addDomListener(window, 'load', initialize);
}

$(document).ready(function () {

	//set up event handlers
	_userId = parseInt($('#hidUserId').val(), 10);
	_token = $('#hidToken').val();

	_latitude = $('.hidLatitude').val();
	_longitude = $('.hidLongitude').val();
	
	initialize();
});

function stopRKey(evt) {
	var evt = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type == "text")) { return false; }
}

document.onkeypress = stopRKey;

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function (from, to) {
	var rest = this.slice((to || from) + 1 || this.length);
	this.length = from < 0 ? this.length + from : from;
	return this.push.apply(this, rest);
};
if (typeof (Number.prototype.toRad) === "undefined") {
	Number.prototype.toRad = function () {
		return this * Math.PI / 180;
	}
}

