//Žemelapio pradiniai nustatymai
var center_latitude = 55.698486; //55.698486 - Klaipeda
var center_longtitude = 21.147308; //21.147308 - Klaipeda
var center_zoom = 12;
var obj_info;
var map;

var CREATED_ZOOMS = new Array();
var ALL_OBJECTS;
var ALL_MARKERS = new Array();

for (var i=7; i<=16; i++)
{
	CREATED_ZOOMS[i]=false;
	ALL_MARKERS[i]=new Array();
}

//stiliaus nustatymai
var active_image=HTTP_HEADER+'/_images/active.png';
var inactive_image=HTTP_HEADER+'/_images/inactive.png';
var active_color="#ffffff";
var inactive_color="#534f4e";
var icon_image=HTTP_HEADER+"/_images/gicon.png";
var zoomin_image=HTTP_HEADER+"/_images/plius.png";
var zoomout_image=HTTP_HEADER+"/_images/minus.png";
var level_image=HTTP_HEADER+"/_images/level.png";


//Ajax
function createRequestObject() {
	var req;
 	if(window.XMLHttpRequest){
  		// Firefox, Safari, Opera...
    	req = new XMLHttpRequest();
    }
    else if(window.ActiveXObject) {
    	// Internet Explorer 5+
     	req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
    	// There is an error creating the object,
     	// just as an old browser is being used.
      	alert('There was a problem creating the XMLHttpRequest object');
    }
    return req;
}


function handleDivTag(divTag) {
	var divTag;
	return divTag;
}


var http = createRequestObject();
var divhandler = new handleDivTag(null);


function handleResponse() 
{	
	switch (http.readyState) 
	{
		case 0:	
			break;
		case 1:
			break;
		case 2:
			break;
		case 3:
			break;
		case 4:		
			if(http.status == 200)
			{	
				divhandler.divt = http.responseText;
				if (divhandler.divtag=='v1')
					report_v1(divhandler.divt);	
				if (divhandler.divtag=='v2')
					report_v2(divhandler.divt);	
				if (divhandler.divtag=='v3')
					report_v3(divhandler.divt);	
				if (divhandler.divtag=='v5')
					report_v5(divhandler.divt);	
			}
			break;
	}
	return false;
}


/*function sendRequest(version, string)
{
	divhandler.divtag = version;
	http.abort();	
	http.onreadystatechange = handleResponse;
	http.open('get', HTTP_HEADER+"/admin/modules/nturtas/maps/maps.php?version="+version+'&'+string);
 	http.send(null);
}*/

function sendRequest(version, string)
{
	divhandler.divtag = version;
	var rand=Math.random();
	http.abort();
	http.onreadystatechange = handleResponse;
	http.open('get', HTTP_HEADER+"/admin/modules/nturtas/maps/maps.php?version="+version+'&rand='+rand+'&'+string);
	http.send(null);
}


//Žemelapio kurimas

/*function redraw_zoom()
{

	function TextualZoomControl() {}
	
	TextualZoomControl.prototype = new GControl();
	
	TextualZoomControl.prototype.initialize = function(map)
	{
		var container = document.createElement("div");
	
		var zoomInDiv = document.createElement("div");
		this.setButtonStyle_(zoomInDiv);
		container.appendChild(zoomInDiv);
		zoomInDiv.innerHTML='<img src="'+zoomin_image+'" alt="" width="24" height="23" />';
		GEvent.addDomListener(zoomInDiv, "click", function()
		{
			map.zoomIn();
		});
		
		
		var levelDiv = document.createElement("div");
		this.setButtonStyle_(levelDiv);
		container.appendChild(levelDiv);
		levelDiv.innerHTML='<img src="'+level_image+'" alt="" width="24" height="62" />';
	
		var zoomOutDiv = document.createElement("div");
		this.setButtonStyle_(zoomOutDiv);
		container.appendChild(zoomOutDiv);
		  //zoomOutDiv.appendChild(document.createTextNode("Zoom Out"));
		  
		zoomOutDiv.innerHTML='<img src="'+zoomout_image+'" alt="" width="24" height="23" />';
		GEvent.addDomListener(zoomOutDiv, "click", function() {
			map.zoomOut();
		});
	
		map.getContainer().appendChild(container);
			return container;
	}
	
	// By default, the control will appear in the top left corner of the
	// map with 7 pixels of padding.
	TextualZoomControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
	}
	
	// Sets the proper CSS for the given button element.
	TextualZoomControl.prototype.setButtonStyle_ = function(button) 
	{
		//button.style.textDecoration = "underline";
		//button.style.color = "#0000cc";
		//button.style.backgroundColor = "white";
		//button.style.backgroundImage = "url("+HTTP_HEADER+"/_images/plius.png)";
		//button.style.font = "small Arial";
		//button.style.border = "1px solid black";
		//button.style.padding = "2px";
		//button.style.marginBottom = "0px";
		//button.style.textAlign = "center";
		//button.style.width = "24px";
		button.style.cursor = "pointer";
	}
	
	 map.addControl(new TextualZoomControl());
}



//norma
function redraw_map_control(type, num)
{
	
	
	if (num==1)
	{
		var image=active_image;
		var text_color=active_color;
	}
	else
	{
		var image=inactive_image;
		var text_color=inactive_color;
	}
	var position=5+((3-num)*82);
	
	function mapControl() {}
	
    mapControl.prototype = new GControl();
    mapControl.prototype.initialize = function(map)
	{
		var container = document.createElement("div");

		var zoomInDiv = document.createElement("div");
		this.setButtonStyle_(zoomInDiv);
		container.appendChild(zoomInDiv);
		zoomInDiv.innerHTML='<div id="map_div_'+num+'" style="background:url('+image+') no-repeat center top; width:80px; height:22px; padding-top:4px;">'+type.getName()+'</div>';     
		GEvent.addDomListener(zoomInDiv, "click", function() {
			map.setMapType(type);
			for (i=1; i<=3; i++)
			{
				var id = document.getElementById('map_div_'+i);
				if (id)
				{
					if (num==i)
					{
						id.style.backgroundImage='url('+active_image+')';
						id.style.color=active_color;
					}
					else
					{
						id.style.backgroundImage='url('+inactive_image+')';
						id.style.color=inactive_color;
					}
				}
			}
			
		});
		
		map.getContainer().appendChild(container);
		return container;
	}
	mapControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(position, 7));
	}

    // Sets the proper CSS for the given button element.
	mapControl.prototype.setButtonStyle_ = function(button) {
		//button.style.textDecoration = "underline";
		button.style.color = text_color;
		//button.style.backgroundColor = "white";
		//button.style.backgroundImage = "url(http://localhost/sveikatos_kred/fir.GIF)";
		button.style.font = "small Arial";
		//button.style.border = "1px solid black";
		//button.style.padding = "2px";
		//button.style.marginBottom = "3px";
		button.style.textAlign = "center";
		//button.style.width = "40px";
		button.style.cursor = "pointer";
    }
	
	map.addControl(new mapControl());
}*/


function initialize_map_single()
{
	get_obj_info(obj_id, kalba);
}

function get_obj_info(obj_id, kalba)
{
	var params ="id="+obj_id+"&kalba="+kalba;
	sendRequest('v1',params);
}


function report_v1(response)
{
	//alert(response);
	try 
	{
    	obj_info = eval(response);
	}
	catch(exception) 
	{
    	//alert('Objekto koordinaciu nustatyti nepavyko.');
		hide_map();
	}
	
	if (obj_info)
	{
		if ((obj_info.latitude=='0') || (obj_info.longtitude=='0'))
		{
			//get_coords(obj_info.id, obj_info.address, obj_info.text);
			hide_map();
		}
		else
		{
			var latitude = obj_info.latitude;
			var longtitude = obj_info.longtitude;
			var point = new GLatLng(latitude, longtitude);
			init_map(point);
			place_marker(point, obj_info.address);
		}
	}
}


function report_v2(response)
{
}

function init_map(point)
{	
	map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(point, center_zoom);
    map.setUIToDefault();
	
	//redraw_zoom();
	//redraw_map_control(G_NORMAL_MAP,1);
	//redraw_map_control(G_SATELLITE_MAP,2);
	//redraw_map_control(G_HYBRID_MAP,3);
}


function place_marker(point, address)
{
	var custom_icon = new GIcon(G_DEFAULT_ICON);
	custom_icon.image = icon_image;
	custom_icon.iconSize = new GSize(20, 35);
	markerOptions = { icon:custom_icon };
	var marker=new GMarker(point,markerOptions);
	GEvent.addListener(marker, "click", function() {
    	var myHtml = address;
    	map.openInfoWindowHtml(point, myHtml);
	});
	map.addOverlay(marker);
}

function add_marker(point, address, zoom)
{
	var custom_icon = new GIcon(G_DEFAULT_ICON);
	custom_icon.image = icon_image;
	custom_icon.iconSize = new GSize(20, 35);
	markerOptions = { icon:custom_icon };
	ALL_MARKERS[zoom].push(new GMarker(point,markerOptions));
	var index = (ALL_MARKERS[zoom].length-1);
	GEvent.addListener(ALL_MARKERS[zoom][index], "click", function() {
    	var myHtml = address;
    	map.openInfoWindowHtml(point, myHtml);
	});
	map.addOverlay(ALL_MARKERS[zoom][index]);
}

function get_coords(obj_id, address, text)
{
	var geocoder = new GClientGeocoder();
	if (geocoder) 
	{
		geocoder.getLatLng(address, function(point){
			init_map(point);
			place_marker(point, address);
			update_coords(obj_info.id, point);					  
		})
	}
}

function get_coords_multi(obj_id, address, text)
{
	var geocoder = new GClientGeocoder();
	if (geocoder) 
	{
		geocoder.getLatLng(address, function(point){
			place_marker(point, address);
			//update_coords(obj_id, point);					  
		})
	}
}

function update_coords(obj_id, point)
{
	latitude=point.lat();
	longtitude=point.lng();
	var params ="id="+obj_id+'&lat='+latitude+'&lng='+longtitude;
	//alert(params);
	sendRequest('v2',params);
}

function initialize_map_multi()
{
	var point = new GLatLng(center_latitude, center_longtitude);
	init_map(point);
	get_objects(objects, kalba);
}


function change_zoom(oldzoom, newzoom)
{
	if (oldzoom>newzoom)
	{
		hide_level(oldzoom);
	}
	else
	{
		show_level(newzoom);
	}
}


function get_objects(objects, kalba)
{
	var params="sql_arr="+objects+"&kalba="+kalba+"&domain="+HTTP_HEADER;
	sendRequest('v3',params);
}


function create_zoom(zoom)
{
	var number=zoom-7;
	if (number<0) number=0;
	if (number>9) number=9;
	for (index in ALL_OBJECTS[number])
	{
		var obj=ALL_OBJECTS[number][index];	
		var latitude = obj.latitude;
		var longtitude = obj.longtitude;
		if (latitude && longtitude)
		{
			var point = new GLatLng(latitude, longtitude);
			add_marker(point, obj.address, zoom);
		}
	}
	CREATED_ZOOMS[zoom]=true;
}

function show_level(zoom)
{
	if (CREATED_ZOOMS[zoom])
	{
		for (index in ALL_MARKERS[zoom])
		{
			var marker=ALL_MARKERS[zoom][index];
			marker.show();
			
		}
	}
	else create_zoom(zoom);
}

function hide_level(zoom)
{
	if (zoom>=7)
	{
		for (index in ALL_MARKERS[zoom])
		{
			var marker=ALL_MARKERS[zoom][index];
			marker.hide();
		}
	}
}


function report_v3(response)
{
	try 
	{
    	ALL_OBJECTS = eval(response);
	}
	catch(exception) 
	{
    	hide_map();
	}
	
	if (ALL_OBJECTS)
	{
		var current_zoom=map.getZoom();
		if (current_zoom<7) current_zoom=7;
		if (current_zoom>16) current_zoom=16;
		for (var i=7; i<=current_zoom; i++)
		{
			create_zoom(i);
		}
		
		GEvent.addListener(map, "zoomend", function(oldLevel, newLevel){ change_zoom(oldLevel, newLevel) });
	}
	
	
}

