// JavaScript Document
map = Array();
map['current_travel_active'] = 0;
map['data_obj'] = false;
map['tmp_marker'] = false;
function init_google_maps(x,y,z) {
	if (GBrowserIsCompatible()) {
		map['data_obj'] = document.getElementById('map_value_data');
		map['map'] = new GMap2(document.getElementById("google_maps_canvas"));
		map_load_values();
		map['map'].setCenter(new GLatLng(x, y), z);
		map['map'].setUIToDefault();
		map['map'].disableScrollWheelZoom();

		// Create our "tiny" marker icon
		var Icon = new GIcon(G_DEFAULT_ICON);
		Icon.image = "http://www.greencoast.se/fileadmin/images/red_spot3.png";
		Icon.iconSize = new GSize(11, 11);
		Icon.shadowSize = new GSize(11, 11);
		Icon.iconAnchor = new GPoint(6, 6);
						
		// Set up our GMarkerOptions object
		markerOptions = { icon:Icon };
		markers = Array();
		markers_num = map['data_obj'].children.length;
		if(map['encodedPolyline']) {
			map['map'].addOverlay(map['encodedPolyline']);
		}
		c = 0;
		while(c < markers_num && gm_x[c]) {
		/* for (var c = 0; c < markers_num; c++) { */
			data_row_obj = map['data_obj'].children[c];
			if(map['use_iframes']) {
				var point = new GLatLng(html_value(data_row_obj.children[0]), html_value(data_row_obj.children[1]));
			}
			else {
				var point = new GLatLng(gm_x[c],gm_y[c]);
			}
			map['map'].addOverlay(createMarker(point, c, markerOptions));
			c++;
		}
	}
}
function createMarker(point, number, markerOptions) {
	var marker = new GMarker(point, markerOptions);
	marker.value = number;
	map['tmp_marker'] = marker;	
	GEvent.addListener(marker, "mouseover", function() {
		var myHtml = (map_use_iframes) ? 
			'<a href="javascript:load_location_info('+html_value(map['data_obj'].children[number].children[3])+')">'+html_value(map['data_obj'].children[number].children[2])+'</a>' :
			'<a href="javascript:load_location_info('+gm_id[number]+')">'+html_value(document.getElementById('map_id_'+gm_id[number]).getElementsByTagName('h2')[0])+'</a>';
		// gm_title[number]
		// map.openInfoWindowHtml(point, myHtml);
		my_InfoWindowHtml(marker, myHtml);
	});
	GEvent.addListener(marker, "click", function() {
		id = (map['use_iframes']) ? html_value(map['data_obj'].children[number].children[3]) : gm_id[number];
		load_location_info(id);
	});
	return marker;
}
function my_InfoWindowHtml(marker, html) {
	img_dom = 'bt';
	obj_name = 'map_InfoWindowHtml';
	if(!document.getElementById(obj_name)) {
		embryo = document.createElement('div');
		embryo.id = obj_name;
		embryo.style.position = 'absolute';
		obj = marker[img_dom].parentNode.parentNode.appendChild(embryo);
		embryo = document.createElement('div');
		child = obj.appendChild(embryo);
	}
	else {
		obj = document.getElementById(obj_name)
		child = obj.children[0];
	}
	obj.style.left = (marker[img_dom].offsetLeft + map['title_offset_x']) + 'px';
	obj.style.top = (marker[img_dom].offsetTop + map['title_offset_y']) + 'px';
	child.innerHTML = html;
}
function load_location_info(id) {
	if(map['use_iframes']) {
		obj_id = 'map_location_info';
		if(!document.getElementById(obj_id)) {
			embryo = document.createElement('iframe');
			embryo.id = obj_id;
			document.getElementById('global_wrap').appendChild(embryo);
		}
		stuff = '/conf/ext/map/show_location.php?id='+id;
		document.getElementById(obj_id).src = (document.domain == 'translate.googleusercontent.com') ? 
		document.URL.split(document.baseURI)[0]+'http://'+document.baseURI.split('/')[2]+stuff+document.URL.split(document.baseURI)[1]: stuff;
	}
	else {
		if(map['current_active']) {
			document.getElementById('map_id_'+map['current_active']).className = 'inactive';
		}
		obj = document.getElementById('map_id_'+id);
		obj.className = 'active';
		if(obj.getAttribute('widget') != '') {
			source = document.getElementById('map_widget_'+obj.getAttribute('widget')).getElementsByTagName('div')[0];
			divs = obj.getElementsByTagName('div').length; found = false; i = 0;
			while(!found && i < divs) {
				if(obj.getElementsByTagName('div')[i].className == 'map_widget') {
					found = obj.getElementsByTagName('div')[i];
				}
				i++;
			}
			embryo = source.cloneNode(true);
			found.appendChild(embryo);
			obj.setAttribute('widget','');
		}
		map['current_active'] = id;
	}
	if(document.getElementById('map_InfoWindowHtml')) {
		adulterine = document.getElementById('map_InfoWindowHtml');
		adulterine.parentNode.removeChild(adulterine);
	}
}
function load_travel_info(obj) {
	if(map['current_travel_active']) {
		document.getElementById(map['current_travel_active']).className = 'inactive';
	}
	pobj = obj;
	while(pobj.nodeName != 'DT') {
		pobj = pobj.parentNode;
	}
	while(pobj.nodeName != 'DD') {
		pobj = pobj.nextSibling;
	}
	pobj.className = 'active';
	map['current_travel_active'] = pobj.id
}

function html_value(obj) {
	var text;
	if(obj.children.length == 0) {
		text = (obj.textContent) ? obj.textContent : obj.innerHTML;
	}
	else {
		new_obj = obj.children[0].childNodes[1]
		text = (new_obj.textContent) ? new_obj.textContent : new_obj.innerHTML;
	}
	if(!text) { 
		text = obj.childNodes[0].innerHTML;
	}
	return text;
}
function debug(obj) {
	nodes = Array();
	nodes.push(obj);
	out = '';
	limit = 20;
	path = '';
	while((limit > 0) && (node = nodes.shift())) {
		out += path + ' ' + node.nodeName + ':'+node.innerHTML+'<br>';
		if(node.childNodes.length > 0) {
			path = node.nodeName;
			for(i=0;i<node.childNodes.length;i++) {
				nodes.push(node.childNodes[i]);
			}
		}
		limit--;
	}
	return out;
}
function suicide(obj) {
	if(map_use_iframes) {
		child = parent.document.getElementById("map_location_info");
		child.parentNode.removeChild(child);
	}
	else {
		obj.parentNode.className = 'inactive';
	}
}
