/* Google map custom functions */
/* @author Sqills */

pointsArray = new Array();

function showAddress(zoom, gMap, latlng) {
	zoom = gMap.getZoom();
	var point = new GLatLng(latlng.y,latlng.x);
	var address = pointsArray;
	var infoTxt = '';
	
	for(var index = 0; index < address.length; index++){

		if(latlng.x==address[index][5] && latlng.y==address[index][6]) {
			infoTxt = '<p>';
			if(address[index][4]!='' && address[index][4]!=undefined) { infoTxt = infoTxt + '<img src="' + address[index][4] + '"><br />'; }
			if(address[index][0]!='') { infoTxt = infoTxt + address[index][0] + '<br />'; }
			if(address[index][1]!='') { infoTxt = infoTxt + address[index][1] + '<br />'; }
			if(address[index][2]!='') { infoTxt = infoTxt + address[index][2] + '<br />'; }
			if(address[index][3]!='') { infoTxt = infoTxt + address[index][3] + '<br />'; }
			if(address[index][7]!='') { infoTxt = infoTxt + address[index][7] + ''; }
			infoTxt = infoTxt + '</p>'
			
		}
	}
	if(infoTxt!='') {
		gMap.openInfoWindowHtml(point,infoTxt);
		gMap.setCenter(point, zoom);
	}

}

function gMapLoad(divId) {
	
	this.markers = new Array();

	this.gMap = new Object();
	
	var that = this;
	var mapDiv = document.getElementById(divId);
	
	var defaultZoom = 3;
	var lat = 48.000;
	var lng = 5.000;
	
	var centerPoint = null;
	var centerZoom = null;
 	
	// check google map api can be initilized
	if((typeof GMap2 != 'function') || !GBrowserIsCompatible() || !mapDiv) {
		this.gMap = false;
		return this.gMap;
	}
	
	// initilize map
	this.gMap = new GMap2(mapDiv);
	
	//this.gMap.setMapType(G_HYBRID_MAP);
	//this.mapControl = new GSmallMapControl();
	//this.gMap.addControl(this.mapControl);

	this.gMap.setCenter(new GLatLng(lat, lng), defaultZoom);
	
	this.centerMap = function(lat,lng,zoom){
		var point = new GLatLng(lat, lng);
		this.gMap.panTo(point);
		var currentZoom = this.gMap.getZoom();
		var targetZoom = zoom;
		if(targetZoom>currentZoom){
			while(currentZoom<targetZoom){
				this.gMap.zoomIn(point,true,true);
				currentZoom++;
			}
		}else{
			this.gMap.setZoom(targetZoom);
		}
	}
	
	this.resetCenterMap = function(){
		var currentZoom = this.gMap.getZoom();
		var targetZoom = this.centerZoom;
		if(targetZoom<currentZoom){
			while(currentZoom>targetZoom){
				this.gMap.zoomOut(this.centerPoint,true,true);
				currentZoom--;
			}
		}else{
			this.gMap.setZoom(this.centerZoom);
		}
		this.gMap.panTo(this.centerPoint);
	}
	
	this.clearOverlays = function(){
		this.gMap.clearOverlays();
	}
	
	this.checkResize = function(){
		var thisCenter = this.gMap.getCenter();
		var thisZoom = this.gMap.getZoom();
		this.gMap.checkResize();
		this.gMap.setCenter(thisCenter);
		this.gMap.setZoom(thisZoom);
	}
	
	this.showLocations = function(locationArray,fromAddressStr){
	
		// initilize failed
		if(!that.gMap) {
			return false;
		}
		
		this.clearOverlays();
		this.gMap.setCenter(new GLatLng(lat, lng), defaultZoom);
		this.gMap.checkResize();
		
		var minLat = 0;
		var minLng = 0;
		var maxLat = 0;
		var maxLng = 0;
		
		for(var i = 0; i < locationArray.length; i++){
					
			var options = { name: locationArray[i][0] };
			if(locationArray[i][3]!=undefined){
				iconUrl = locationArray[i][3];
				customIcon = new GIcon(G_DEFAULT_ICON);
				customIcon.image = iconUrl;
				customIcon.iconSize = new GSize(20, 34);
				options = ({ name: locationArray[i][0], icon:customIcon });
			}
			
			
			var thisLat = locationArray[i][1];
			var thisLng = locationArray[i][2];
			
			if(thisLat > maxLat) {
	 			maxLat = thisLat;
			}
			if(thisLat < minLat || minLat === 0) {
	 			minLat = thisLat;
			}
			if(thisLng > maxLng) {
	 			maxLng = thisLng;
			}
			if(thisLng < minLng || minLng === 0) {
	 			minLng = thisLng;
			}
			
			var point = new GLatLng(thisLat,thisLng);
			var zoom = locationArray[i][4];
			var marker = new GMarker(point, options);
				GEvent.addListener(marker, "click", function(latlng) { 
					showAddress(zoom, that.gMap, latlng);
					that.gMap.setCenter(point, zoom);
				});
			that.gMap.addOverlay(marker);
	        var customUI = this.gMap.getDefaultUI();
	        customUI.maptypes.hybrid = false;
	        this.gMap.setUI(customUI);

		}
		
		if(locationArray.length>1){
			
			var bounds = new GLatLngBounds();
			bounds.extend(new GLatLng(minLat, minLng));
			bounds.extend(new GLatLng(maxLat, maxLng));
			var zoomLevel = parseInt(that.gMap.getBoundsZoomLevel(bounds));
			zoomLevel = zoomLevel - 1;
			
			this.gMap.setCenter(bounds.getCenter(), parseInt(zoomLevel));
			
			this.centerPoint = bounds.getCenter();
			this.centerZoom = parseInt(zoomLevel);
			
		}else{
		
			if(locationArray.length>0){
		
				this.gMap.setCenter(point, parseInt(zoom));
				
				this.centerPoint = point;
				this.centerZoom = parseInt(zoom);
			
			}
			
		}
		
		if(locationArray.length>0){
			pointTo = point;
			
			if(fromAddressStr!=''){
				var geocode = new GClientGeocoder();
				geocode.getLatLng(fromAddressStr, function(point){
					if(!point){
						$('#googleRoute').html('&nbsp;');
						$('#routefromcity').addClass('colorred');
						showLocations(locationsArray,'');
					}else{
						$('#routefromcity').removeClass('colorred');
						directions = new GDirections(that.gMap, document.getElementById('googleRoute'));
						var waypointsArray = new Array();
						waypointsArray[0] = point;
						waypointsArray[1] = pointTo;
						directions.loadFromWaypoints(waypointsArray);
					}
				});
			}
		}
	}

	this.displayRoute = function(){
		  from=$('#fromLocation').attr('value');
		  to=$('#toLocation').attr('value');
		  if (from!='' && to!='') {
			  directionsPanel = document.getElementById("routeDirections");
			  directions = new GDirections(this.gMap, directionsPanel);
			  directions.load("from: "+from+" to: "+to+"");
			  $('#routeDirectionsHeader').show();
		  } else {
		
		  }
	}	
}


