var bbmapdirection = function (id, lat, lng, zoom, region) {
	this.id = id;
	this.lat = lat;
	this.lng = lng;
	this.zoom = zoom;
	this.region = region;

	this.getDirection = function () {
		var request = {
			origin: this.addressPosition, 
			destination: this.firmPosition,
			region: this.region,
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};
		
		var object = this;
		this.directionsService.route (request, function (response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				object.directionsDisplay.setDirections(response);
			}
		});
	}

	this.init = function () {
		var center = new google.maps.LatLng(this.lat, this.lng);
		this.map = new google.maps.Map(document.getElementById(this.id), {
			zoom: this.zoom,
			scrollwheel: false,
			center: center,
			mapTypeControl: true,
			mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR},
			navigationControl: true,
			navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
			scaleControl: true,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		});

		//geocoder
		this.geocoder = new google.maps.Geocoder();

		//directions
		var rendererOptions = {
		  draggable: true
		};
		this.directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
		this.directionsDisplay.setMap(this.map);
		this.directionsDisplay.setPanel(this.elementDirectionsPanel);
		
		this.directionsService = new google.maps.DirectionsService();
		
		//address
		var object = this;

		google.maps.event.addListener (this.directionsDisplay, 'directions_changed', function() {
			var directionsResult = object.directionsDisplay.getDirections();
			var route = directionsResult.routes[0];
			var leg = route.legs[0];
			object.addressPosition = leg.start_location;
			
			object.setAddress();
		});

		$(this.elementAddress)
			.focus (function(){
				$(this).data('oldValue', $(this).val());
				$(this).val('');
				//$(this).autocomplete("search");
			})
			.blur (function(){
				$(this).val($(this).data('oldValue'));
			})
			.autocomplete ({
				minLength: 3,
				opened: false,
				selectedItem: false,
				source: function (request, response) {
					object.getAddress(request.term, response);
				},
				open: function () {
					$(this).autocomplete.opened = true;
				},
				close: function () {
					$(this).autocomplete.opened = false;
				},
				select: function (event, ui) {
					if (ui.item) {
						$(this).data('oldValue', ui.item.value);
						$(this).autocomplete.selectedItem = true;

						object.addressPosition = new google.maps.LatLng (ui.item.lat, ui.item.lng);
						object.getDirection();
					}
					else
						$(this).autocomplete.selectedItem = false;
				}
			})
			.keyup (function (event){
				if (event.keyCode == $.ui.keyCode.ENTER)
					if ($(this).autocomplete.selectedItem != true) {
						if ($(this).autocomplete.opened != true)
							$(this).effect('pulsate', {}, 200);
						else
							$(this).autocomplete("widget").effect('shake', {}, 200);
					}
					else {
						$(this).autocomplete.selectedItem = false;
					}
			})

		//prvotni nacteni cesty
		this.getDirection ();
	}

	this.getCurrentAddress = function () {
		var object = this;
		if(navigator.geolocation) {
			// Try W3C Geolocation method (Preferred)
			this.browserSupportFlag = true;
			navigator.geolocation.getCurrentPosition(function(position) {
				object.addressPosition = new google.maps.LatLng (position.coords.latitude, position.coords.longitude);
				//object.setAddress();
				object.getDirection();
			}, function() {
				object.handleNoGeolocation(object.browserSupportFlag);
			});
		} else if (google.gears) {
			// Try Google Gears Geolocation
			this.browserSupportFlag = true;
			var geo = google.gears.factory.create('beta.geolocation');
			geo.getCurrentPosition(function(position) {
				object.addressPosition = new google.maps.LatLng (position.latitude, position.longitude);
				//object.setAddress();
				object.getDirection();
			}, function() {
				object.handleNoGeolocation(object.browserSupportFlag);
			});
		} else {
			// Browser doesn't support Geolocation
			this.browserSupportFlag = false;
			object.handleNoGeolocation(object.browserSupportFlag);
		}
	}

	this.handleNoGeolocation = function (errorFlag) {
		this.errorPositionText;
		if (errorFlag == true) {
		} else {
		}
	}
	
	this.setAddress = function () {
		var geocoderRequest = {'latLng': this.addressPosition};
		var object = this;
		this.geocoder.geocode (geocoderRequest, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				if (results[0]) {
					object.elementAddress.value = results[0].formatted_address;
				}
			}
		});
	}

	this.getAddress = function (address, response) {
		var geocoderRequest = {'address': address, 'region': this.region};
		var object = this;
		this.geocoder.geocode(geocoderRequest, function (results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				response ($.map(results, function (item) {
					return {
						label: item.formatted_address,
						value: item.formatted_address,
						lat: item.geometry.location.lat(),
						lng: item.geometry.location.lng(),
						viewport: item.geometry.viewport,
						bounds: item.geometry.bounds
					}
				}));
			}
		});
	}
	
	//firm position
	this.setFirmPosition = function (lat, lng) {
		this.firmPosition = new google.maps.LatLng (lat, lng);
	}

	//address position
	this.setAddressPosition = function (lat, lng) {
		this.addressPosition = new google.maps.LatLng (lat, lng);
	}
};

