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

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(typeof 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);
			if(locationArray.length>1){
				GEvent.addListener(marker, "click", function() { 
					var offsetTop = $('#rowItem'+this.name).offset().top;
					if(typeof offsetTop != 'undefined' && offsetTop>0){
						offsetTop = parseInt(offsetTop);
						$('html,body').animate({scrollTop: offsetTop}, 500);
						setTimeout("$('#rowItem"+this.name+"').effect('pulsate', { times: 1 }, 400);",510);
					}
				});
			}
			that.gMap.addOverlay(marker);
			
		}
		
		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));
			if (zoomLevel>zoom){
				zoomLevel = zoom;
			}
			else {
				zoomLevel = zoomLevel - 1;
			}
			
			this.centerPoint = bounds.getCenter();
			this.centerZoom = parseInt(zoomLevel);
			
		}else{
		
			if(locationArray.length>0){
				
				var zoomLevel = parseInt(zoom);
				if (zoomLevel > 13){
					zoomLevel = 13;
				}				
				this.centerPoint = point;
				this.centerZoom = zoomLevel;
			}
			
		}
		
		this.gMap.setCenter(this.centerPoint, this.centerZoom);
		
		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');
						$('#googleRoute').html('');
						directions = new GDirections(that.gMap, document.getElementById('googleRoute'));
						var waypointsArray = new Array();
						waypointsArray[0] = point;
						waypointsArray[1] = pointTo;
						directions.loadFromWaypoints(waypointsArray);
					}
				});
			}
		}
	}

}