﻿window.Ditec = {};

Ditec.Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
};

var _TmpMap;
var _TmpHtmlInformation;

Ditec.GoogleMap = Class.create();

Ditec.GoogleMap.prototype = {
	initialize: function(objInit)
	{
		this.MapHolder = objInit.MapHolder,
		this.Map = new GMap2(document.getElementById(this.MapHolder)),
		this.GeoEncoder = new GClientGeocoder(),
		this.DefaultLat = objInit.DefaultLat,
		this.DefaultLng = objInit.DefaultLng,
		this.AllLocations = "",
		this.Address = objInit.Address
		
		
		if(this.Address)
			this.ZoomInAddress(this.Address);
		else
			this.SetNewPosition(new GLatLng(this.DefaultLat,this.DefaultLng),5);
				
		this.AddDefaultMapControls();
		this.SetDefaultMapProperties();
	},
	SetNewPosition: function(MapPosition, intZoom) {
		this.Map.setCenter(MapPosition, intZoom);
	},
	AddDefaultMapControls: function() {
		this.Map.addControl(new GLargeMapControl());
	},
	SetDefaultMapProperties: function() {
		this.Map.enableScrollWheelZoom();
		this.Map.enableInfoWindow();
	},
	MarkAllDealers: function() {
		for (var i = 0; i < this.AllLocations.length; i++) {
        	var arrLocationItem = this.AllLocations[i].split('|');                    
            if(arrLocationItem[1] != "0") {
            	var Marker = this.CreateMark(arrLocationItem[1],arrLocationItem[3]);
            	this.Map.addOverlay(Marker);
			}
        }
	},
	MarkDealer: function(Dealer) {
		//_TmpHtmlInformation = Dealer.HtmlInformation;
		_TmpMap = this.Map;
		this.GeoEncoder.getLatLng(Dealer.Address,
			function(MarkPoint)
			{
				if (!MarkPoint)
				{
				  alert("Tyvärr kunde inte platsen/verkstaden hittas");
				}
				else
				{
					var Marker = new GMarker(MarkPoint);
					//Marker.bindInfoWindowHtml(_TmpHtmlInformation);
					_TmpMap.addOverlay(Marker);
				}
			});
	},
	CreateMark: function(strLatLng, strInfoMessage) {
		var arrLatLng = strLatLng.split(",");
		var MarkPoint = new GLatLng(arrLatLng[0],arrLatLng[1]);
		var Marker = new GMarker(MarkPoint);
		Marker.bindInfoWindowHtml(strInfoMessage);
		
		return Marker;
	},
	ZoomInAddress: function(strAddress) {
		this.GetPositionFromAddress(strAddress)
	},
	GetPositionFromAddress: function(strAddress) {
		//var MapPosition = this.GetDealerPositionFromAddress(strAddress);
		//if(MapPosition)
		//	this.SetNewPosition(MapPosition,9);
		//else {
			_TmpMap = this.Map;
			this.GeoEncoder.getLatLng(strAddress,this.SetZoom);
		//}
	},
	SetZoom: function(MapPosition) {
		if (!MapPosition) {
		  alert('Tyvärr kunde inte platsen hittas');
		  return;
		}
		else {
			_TmpMap.setCenter(MapPosition, 9);
		}
	},
	GetDealerPositionFromAddress: function(strAddress) {
		for (var i = 0; i < this.AllLocations.length; i++) {
        	arrLocationItem = this.AllLocations[i].split('|');                    
            if(arrLocationItem[1] != "0" && arrLocationItem[2].toLowerCase().indexOf(strAddress.toLowerCase()) > -1)
        	{
        		var arrLatLng = arrLocationItem[1].split(",");
				var MapPosition = new GLatLng(arrLatLng[0],arrLatLng[1]);
				return MapPosition;
			}
        }
	}
};

