var activeClustering = false;
var activePersona='';
var usePersona = '';
function initialize(pushCoordinate) 
{
	if (document.getElementById("map_canvas") == null)
		return;

	if (!activeClustering)
		activeClustering = false;

	if (GBrowserIsCompatible()) 
	{
		var map = new GMap2(document.getElementById("map_canvas"));

		//set center on the given country

		// Add 10 markers to the map at random locations
		var bounds = new GLatLngBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var lngSpan = northEast.lng() - southWest.lng();
		var latSpan = northEast.lat() - southWest.lat();
		var polyline;

		//get elements from input type hidden element storing google coordinates
		var all_obj = $("gmap_coords").value.split('&^&');
		if (all_obj.length == 0)
			return "";

		var markers = [];
		for (var i = 0; i < all_obj.length; i++) 
		{
			var coords = all_obj[i].split("|");
			var coordX, coordY;
			
			coordX = coords[0];
			coordY = coords[1];
			var iconType = coords[2];
			var HTMLtext = coords[3];

			if (coordX != undefined && coordY != undefined)
			{
				var point = new GLatLng(coordX, coordY);
				var current_icon = createNumberedIcon(iconType);
				var options = {icon: current_icon};

				oneMarker = createMarkerIcon(point, options, HTMLtext);
				markers.push(oneMarker);
				if (!activeClustering)
					map.addOverlay(oneMarker);

				if (activeDistance)
				{
					var spl = activePersona.split("|");
					usePersona = new GLatLng(spl[0], spl[1]);

					//curve!
					var polyOptions = {geodesic:true};
					polyline = new GPolyline([usePersona , point], "#FF5500", 1, 1, polyOptions);
					map.addOverlay(polyline);
				}
				bounds.extend(point);
			}
		}

		if (activeClustering)
			var markerCluster = new MarkerClusterer(map, markers);

		var useZoom = map.getBoundsZoomLevel(bounds);
		var origZoom = useZoom;

		if (pushCoordinate == undefined)
		{
			//if (useZoom > 8)
			//	useZoom = 8;
		}


		if (useZoom > 15)
			useZoom = 15;

		if (pushCoordinate == 'auto')
		{
			useZoom = origZoom;
			if (useZoom > 16)
				useZoom = 16;

			if (useZoom < 6)
				useZoom = 6;
		}

		//override all zoom calculations if customGmapZoom exists
		if (document.getElementById('customGmapZoom') != null)
			useZoom = parseInt(document.getElementById('customGmapZoom').value);

		if (useZoom > 8)
			useZoom--;

		map.setCenter(bounds.getCenter(), useZoom);
		map.setUIToDefault();
		map.disableScrollWheelZoom();
		map.enableContinuousZoom();
		map.disableGoogleBar();
		//var customUI = map.getDefaultUI();
		//customUI.controls.scalecontrol = false;
		//map.setUI(customUI);
      }
}


function createNumberedIcon(number)
{
	var icon = new GIcon();
	icon.image = STATIC_IMGS_PATH+"/icon"+number+".png";

	var useW=18, useH=24;

	if (document.getElementById('customGmapW') != null)
		useW = document.getElementById('customGmapW').value;

	if (document.getElementById('customGmapH') != null)
		useH = document.getElementById('customGmapH').value;

	icon.iconSize = new GSize(useW, useH);
	icon.iconAnchor = new GPoint(10, 30);
	icon.infoWindowAnchor = new GPoint(10, 1);
	return icon;
}


function createMarkerIcon(point, options, html_popup)
	{
		var marker = new GMarker(point, options);
		if (html_popup != undefined)
			if (html_popup.length > 20)
				GEvent.addListener(marker, "click", function()
				{
					marker.openInfoWindowHtml(html_popup);
					if ($("map_canvas").style.height == '350px')
						$("map_canvas").style.height = '450px';

				});
		return marker;
	}


function prepMapSht()
{
	prepMap('home');
}

function prepMap(mapType, countryOnly, posType, persona, fi_1, fi_2)
{

	if (mapType == 'img')
	{
		$("map_canvas").innerHTML = "<img onClick='prepMapSht()' src='/STATIC_IMGS/__home/gmap_main.png'>";
		return false;
	}

	var usePos, usePosY;

	usePosY = 110;
	if (posType == 2)
		usePos = 430;
	else
		usePos = 440;

	//if maptype is hotel use another usePos
	if (mapType == 'hotel')
		usePos = 200;

	if (mapType == 'home' || mapType == 'region')
	{
		usePos = 200;
		usePosY = 70;
	}

	if (fi_1 == -1)
		mapType = 'home';

	if (mapType == 'country')
	{
		usePos = 200;
	}

	if (persona)
		activePersona = persona;
		
	if (document.getElementById("map_canvas")!=null)
	{
		$("map_canvas").innerHTML = "<div style='float:left; width: 100px; margin-left: "+usePos+"px; margin-top: "+usePosY+"px'><img src='/STATIC_IMGS/map_loader.gif'></div>";
	}

	var attr= "mapType="+mapType;
	if (countryOnly != '' && countryOnly != undefined)
		attr += "&countryId="+countryOnly;

	if (fi_1 != '' && fi_1 != undefined)
		attr += "&fi_1="+fi_1;

	if (fi_2 != '' && fi_2 != undefined)
		attr += "&fi_2="+fi_2;



	//change picture sizes, if required
	if (mapType == 'byUser')
	{
		if (document.getElementById('customGmapW') != null)
			document.getElementById('customGmapW').value = "13";

		if (document.getElementById('customGmapH') != null)
			document.getElementById('customGmapH').value = "31";
	}

	var myAjax = new Ajax.Request(
                    '/proj/PHP/ajax/dispatch.maps.php',
                    {
                        method: 'post',
                        parameters: attr,
                        asynchronous: true,
                        onComplete: showResponse,
                        contentType:'application/x-www-form-urlencoded',
                        encoding: 'UTF-8'
                    });

        function showResponse(originalRequest)
        { 
		var results = originalRequest.responseText.split("&|^");
		var mapInitType;
	
		if (results[0] == undefined)
			return;

		// 4 possible containers:
		// gmap_coords: current container
		// ds_all : gr 0
		// group1
		// group2
		document.getElementById("gmap_coords").value = results[0];

		if (results[1] != '')
			$("ds_all").value = results[1];
		else
			$("ds_all").value = results[0];

		if (results[2] != '')
			$("ds_group1").value = results[2];
		else
			$("ds_group1").value = results[0];

		if (results[3] != '')
			$("ds_group2").value = results[3];
		else
			$("ds_group2").value = results[0];

		if (mapType == 'home' || mapType == 'gal' || mapType == 'search')
			activeClustering = true;
		else
			activeClustering = false;

		if (mapType == 'hotel' || mapType == 'home')
			mapInitType = 'auto';
		else
			mapInityType = false;

		initialize(mapInitType);
        }
}

