/*
	GoogleMap PHP class' javascript tool,
	2008, flashpistols webdesign, Berlik Levente
	----------------------------------------------------
	NOTE: in case of using custom tooltips ($map->tooltip_type = _TOOLTIP_CUSTOM),
	don't forget that the map's div needs a position relative container DIV, which is like the map's DIV
	NOTE #2: if you use only this javascript part, and not the class, it's important to place the
	points given with latlng at the beggining of the coords array, and the the ones with addresses
*/

var map;					// this is the map object
var continous_zoom;
var doubleclick_zoom;
var control_type;			// 0: no-control; 1: GSmallMapControl, 2: GLargeMapControl, 3: GSmallZoomControl
var map_div_id = 'map';		// the ID of the div which contains the googlemap
var tooltip_type = 1;	// google's by default
var index1 = 0;
var index2 = 0;
var pina = 0;
var lasticon = null;
var gm_printed = 0;

var geocoder = new GClientGeocoder();
var bounds = new GLatLngBounds();

var points = new Array();	// this holds our coordinates (GMarkers)
var coords = new Array();	// this hold our coordinates/addresses, its content is usually generated by the php class

function map_init() {
	if(GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(map_div_id));

		switch (control_type) {
			case 1: {
				map.addControl(new GSmallMapControl());
				break;
			}
			case 2: {
				map.addControl(new GLargeMapControl());
				break;
			}
			case 3: {
				map.addControl(new GSmallZoomControl());
			}
			default: break;
		}

		for (index1 = 0; index1 < coords.length; index1++) {

			if (typeof coords[index1] == 'undefined') continue;

			if (coords[index1]['lat']) {	// if the coordinate is a lat-long point
				var point = new GLatLng(coords[index1]['lat'], coords[index1]['lng']);
				map.setCenter(point);

				var icon = new GIcon(G_DEFAULT_ICON);
				if (typeof coords[index2]['icon'] == 'object') {

					icon.image = coords[index2]['icon']['url'];
					icon.iconSize = new GSize(coords[index1]['icon']['w'], coords[index2]['icon']['h']);
					if (coords[index2]['icon']['shadow'] != -1) {
						icon.shadow = coords[index2]['icon']['shadow'];
						icon.shadowSize = new GSize(coords[index2]['icon']['sw'], coords[index2]['icon']['sh']);
					}

					if (typeof coords[index2]['icon']['ax'] != 'undefined') {
						icon.iconAnchor = new GPoint(coords[index2]['icon']['ax'], coords[index2]['icon']['ay']);
					}
				}

				var marker = new GMarker(point, icon);
				map.addOverlay(marker);
				bounds.extend(marker.getPoint());
				map.setZoom(map.getBoundsZoomLevel(bounds) - 1);
				map.setCenter(bounds.getCenter());
				points[index2] = marker;
				index2++;

				if (tooltip_type == 1) {	// google's
					if (typeof coords[index2]['tooltip'] != 'undefined') {
						GEvent.addListener(marker, 'mouseover', function() {
							var point = this.getLatLng();
							for (var j = 0; j < points.length; j++) {
								if (typeof points[j] == 'undefined') continue;
								if (points[j].getPoint() == point) break;
							}
							map.openInfoWindowHtml(point, coords[j]['tooltip']);
						});
					}
				} else {
					if (typeof coords[index1]['tooltip'] != 'undefined') {
						GEvent.addListener(marker, 'mouseover', function() {
							var point = this.getLatLng();
							for (var j = 0; j < points.length; j++) {
								if (typeof points[j] == 'undefined') continue;
								if (points[j].getPoint() == point) break;
							}
							map_set_tooltip(j);

							lasticon = this.getIcon().image;
							if (typeof coords[j]['icon_hover'] != 'undefined') {
								this.setImage(coords[j]['icon_hover']);
							}
						});

						GEvent.addListener(marker, 'mouseout', function() {
							map_clr_tooltips();
							if (lasticon != '') this.setImage(lasticon);
						});
						if (typeof coords[index1]['link'] != 'undefined') {
							GEvent.addListener(marker, 'click', function() {
								var point = this.getLatLng();
								for (var j = 0; j < points.length; j++) {
									if (typeof points[j] == 'undefined') continue;
									if (points[j].getPoint() == point) break;
								}
								//alert(coords[j]['link']);
								window.location = coords[j]['link'];
							});
						}
					}
					
				}
			} else {	// if the coordinate is an address
				geocoder.getLatLng(coords[index1]['addr'], set_point);
				break;	// unfortunately needed hax
			} // eof address
		}	// eof for

	}
}

function set_point(point) {

	if (point) {
		map.setCenter(point);

		var icon = new GIcon(G_DEFAULT_ICON);
		if (typeof coords[index2]['icon'] == 'object') {

			icon.image = coords[index2]['icon']['url'];
			icon.iconSize = new GSize(coords[index2]['icon']['w'], coords[index2]['icon']['h']);
			if (coords[index2]['icon']['shadow'] != -1) {
				icon.shadow = coords[index2]['icon']['shadow'];
				icon.shadowSize = new GSize(coords[index2]['icon']['sw'], coords[index2]['icon']['sh']);
			}

			if (typeof coords[index2]['icon']['ax'] != 'undefined') {
				icon.iconAnchor = new GPoint(coords[index2]['icon']['ax'], coords[index2]['icon']['ay']);
			}
		}

		var marker = new GMarker(point, icon);
		map.addOverlay(marker);
		bounds.extend(marker.getPoint());
		map.setZoom(map.getBoundsZoomLevel(bounds) - 1);
		map.setCenter(bounds.getCenter());
		points[index2] = marker;

		if (tooltip_type == 1) {	// google's builtin
			if (typeof coords[index2]['tooltip'] != 'undefined') {
				GEvent.addListener(marker, 'mouseover', function() {
					var point = marker.getLatLng();
					for (var j = 0; j < points.length; j++) {
						if (typeof points[j] == 'undefined') continue;
						if (points[j].getPoint() == point) break;
					}
					map.openInfoWindowHtml(point, coords[j]['tooltip']);
				});
			}
		} else {	// our custom tooltip
			if (typeof coords[index2]['tooltip'] != 'undefined') {
				GEvent.addListener(marker, 'mouseover', function() {
					var point = marker.getLatLng();
					for (var j = 0; j < points.length; j++) {
						if (typeof points[j] == 'undefined') continue;
						if (points[j].getPoint() == point) break;
					}
					map_set_tooltip(j);

					lasticon = marker.getIcon().image;
					if (typeof coords[j]['icon_hover'] != 'undefined') {
						marker.setImage(coords[j]['icon_hover']);
					}
				});

				GEvent.addListener(marker, 'mouseout', function() {
					map_clr_tooltips();
					if (lasticon != '') marker.setImage(lasticon);
				});
				
				if (typeof coords[index1]['link'] != 'undefined') {
					GEvent.addListener(marker, 'click', function() {
						var point = this.getLatLng();
						for (var j = 0; j < points.length; j++) {
							if (typeof points[j] == 'undefined') continue;
							if (points[j].getPoint() == point) break;
						}
						//alert(coords[j]['link']);
						window.location = coords[j]['link'];
					});
				}
			}
		}
	}	// eof if point
	index2++;
	if (typeof coords[index2] != 'undefined') geocoder.getLatLng(coords[index2]['addr'], set_point);
}

function map_set_tooltip(i) {
	if(gm_printed){
		if (!map) alert('map object not found');
		if (!points[i]) alert('points not found');

		var tooltip = document.getElementById('map_tooltip_'+i);
		if (!tooltip) return;

		var map_div = document.getElementById(map_div_id);
		if (!map_div) alert('map container not found');
		var map_div_height = map_div.scrollHeight;
		var map_div_width = map_div.scrollWidth;

		var marker = points[i];
		var cnt = map.getCurrentMapType().getProjection().fromLatLngToPixel( map.getBounds().getSouthWest(), map.getZoom() );
		var mark = map.getCurrentMapType().getProjection().fromLatLngToPixel( marker.getPoint(), map.getZoom() );

		var pixelpos = new GPoint();
		pixelpos.x = mark.x - cnt.x + 15;
		pixelpos.y = map_div_height + (mark.y - cnt.y) + 10;

		//alert ('m: '+mark.x+', '+mark.y+' c:'+cnt.x+', '+cnt.y+' p:'+pixelpos.x+', '+pixelpos.y+' md:'+map_div.scrollHeight);

		if (pixelpos.x > map_div_width || pixelpos.y > map_div_height || pixelpos.x < 0 || pixelpos.y < 0) return;
		
		Number.prototype.NaN0 = function(){ return isNaN(this) ? 0 : this; }
		var mapdivpos = getPosition(map_div);
		var x = pixelpos.x + mapdivpos.x;
		var y = pixelpos.y + mapdivpos.y;

		tooltip.style.display = 'block';

		var tooltip_w = tooltip.scrollWidth;
		var tooltip_h = tooltip.scrollHeight;
		
		//if (x + tooltip_w > document.body.clientWidth) { x -= tooltip_w + 20; }
		//if (y + tooltip_h > document.body.clientHeight + document.body.scrollTop) { y -= tooltip_h + 30; }
		var pageSize = getPageSize();
		var scroll = getPageScroll(0);
		toppos   = scroll.y + (pageSize.wh / 2);
		leftpos	= scroll.x + (pageSize.pw / 2);
		if (x + tooltip_w > leftpos) { x -= tooltip_w + 20; }
		if (y + tooltip_h > toppos) { y -= tooltip_h + 30; }

		tooltip.style.left = x+'px';
		tooltip.style.top = y+'px';
		
		//tooltip.innerHTML = toppos + ', ' + y + ' ' + document.body.offsetHeight + ' ' + document.body.clientHeight + ' ' + document.body.scrollTop;
		//tooltip.innerHTML = document.body.clientWidth + ' ' + document.body.clientHeight + ' ' + document.body.scrollTop;
	}
}

function map_clr_tooltip(i) {
	if(gm_printed){
		var tooltip = document.getElementById('map_tooltip_'+i);
		if (!tooltip) return;
		tooltip.style.display = 'none';
	}
}

function map_clr_tooltips() {
	for (j = 0; j < coords.length; j++) {
		if (typeof coords[j]['tooltip'] != 'undefined')	document.getElementById('map_tooltip_'+j).style.display = 'none';
	}
}
function getPosition(target){
	var left = 0;
	var top  = 0;
	do{
		left += target.offsetLeft + (target.currentStyle ? (parseInt(target.currentStyle.borderLeftWidth)).NaN0() : 0);
		top += target.offsetTop + (target.currentStyle ? (parseInt(target.currentStyle.borderTopWidth)).NaN0() : 0);
	}while(target = target.offsetParent);
	return {x:left, y:top};
}

function getPageSize(){
	var scroll = getPageScroll(1);
	
	var windowWidth, windowHeight;
	if(self.innerHeight) {	// all except
		windowWidth = self.innerWidth; windowHeight = self.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict
		windowWidth = document.documentElement.clientWidth; windowHeight = document.documentElement.clientHeight;
	}else if(document.body) { // other
		windowWidth = document.body.clientWidth; windowHeight = document.body.clientHeight;
	}
	
	pageHeight = (scroll.y < windowHeight) ? windowHeight : scroll.y ;
	pageWidth = (scroll.x < windowWidth) ? windowWidth : scroll.x ;

	return {pw:pageWidth, ph:pageHeight, ww:windowWidth, wh:windowHeight};
}
function getPageScroll(mode){
	var xScroll, yScroll;
	if(mode==1){
		if(window.innerHeight && window.scrollMaxY) {
			xScroll = document.body.scrollWidth; yScroll = window.innerHeight + window.scrollMaxY;
		}else if(document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth; yScroll = document.body.scrollHeight;
		}else{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth; yScroll = document.body.offsetHeight;
		}
	}else{
		if(self.pageYOffset){
			xScroll = self.pageXOffset; yScroll = self.pageYOffset;
		}else if(document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
			xScroll = document.documentElement.scrollLeft; yScroll = document.documentElement.scrollTop;
		}else if(document.body){ // other
			xScroll = document.body.scrollLeft; yScroll = document.body.scrollTop;
		}
	}
	return {x:xScroll,y:yScroll};
}
