function appendAfter(nodeid, newnode) {
	var node = document.getElementById(nodeid);

	if (node.parentNode) { 
		if (node.nextSibling) {
			node.parentNode.insertBefore(newnode, node.nextSibling);
		}
		else {
			node.parentNode.appendChild(newnode);
		}
	}
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
		return true;
	}
}

var gmaps = [];
addEvent(gmaps, 'unload', function() { gmaps = [] });
_mMapMode = "Karta";
_mMapModeShort = "Karta";
_mStreetMapAlt = "Vägkarta";
_mMapError = "Zooma ut";

_mSatelliteMode = "Satellit";
_mSatelliteModeShort = "Sat";
_mSatelliteMapAlt = "Satellitbild";
_mSatelliteError = "Zooma ut";

_mHybridMode = "Mixad";
_mHybridModeShort = "Mix";
_mHybridMapAlt = "Vägkarta på satellitbild";

_mPanNorth = "Norrut";
_mPanWest = "Västerut";
_mPanEast = "Österut";
_mPanSouth = "Söderut";
_mLastResult = "Återställ";
_mZoomIn = "Zooma in";
_mZoomOut = "Zooma ut";
//GLoadMapsScript();

_mPreferMetric=true;

var _blue = '#0000FF';
var _paleblue = '#6666DD';
var _grayblue = '#5656BB';
var _green = '#00FF00';
var _red = '#FF0000';
var _black = '#000000';
var _white = '#FFFFFF';

function map_init_and_toggle_day(date, tourname, ferries) {
	map_init_and_toggle('map_' + date, 'gmap_' + date, 'day_' + date, tourname, ferries);
}

function map_init_and_toggle(id_to_toggle, gmapid, firstlinename, secondarylinename, tertiarylinename) {
	togel = document.getElementById(id_to_toggle);
	if(togel.style.display == '' || togel.style.display == 'none') {
		togel.style.display = 'block';
	}
	else {
		togel.style.display = 'none';
	}
	if(gmaps[ gmapid ] == null) {
		new ctmap(gmapid).init_map();
		set_center_from_line(gmapid, firstlinename);
		gmaps[gmapid].checkResize();
		
		if(secondarylinename.length > 0) {
			draw(gmapid, secondarylinename, _grayblue, 5, 0.6);
		}

		if(firstlinename.length > 0) {
			draw(gmapid, firstlinename, _red, 1, 0.8);
		}
		if(tertiarylinename.length > 0) {
			draw(gmapid, tertiarylinename, _white, 3, 0.5);
		}

		
	}
	
	
}

/*function init_map(idsuffix, firstlinename, secondarylinename) {
//	alert('suffix:' + idsuffix + ' first:' + firstlinename + ' second:' + secondarylinename);
	new ctmap(idsuffix, firstlinename, secondarylinename).make_map_container();
}

function mapmaker(idsuffix, firstlinename, secondarylinename) {
	new ctmap(idsuffix, firstlinename, secondarylinename).make_map_container();
}*/


function ctmap(gmapid) {

	
	this.init_map = function() {
		if (GBrowserIsCompatible()) {
			map = this.make_gmap();
			//draw_line(1, gmapid, firstlinename, secondarylinename);
		}
	};
/*
	this.make_map_container = function() {
	
		if(document.getElementById(mapid) != null) {
			if(document.getElementById(mapid).style.display == 'none') {
				document.getElementById(mapid).style.display = '';
			}
			else {
				document.getElementById(mapid).style.display = 'none';
			}
		}
		else {
			
			//if(make_container) {
				var container = document.createElement('div');
				container.setAttribute('id', mapid);
				container.setAttribute('class', 'map_container');
				container.className = 'map_container';
				var mapdiv = document.createElement('div');
				mapdiv.setAttribute('id', gmapid);
				mapdiv.setAttribute('class', 'gmapdiv');
				mapdiv.className = 'gmapdiv';

				container.appendChild(mapdiv);

				appendAfter('date_' + idsuffix, container);
			//}

			if (GBrowserIsCompatible()) {
				map = this.make_gmap();
				
				draw_line(1, gmapid, firstlinename, secondarylinename);
			}
			
		}

	};
*/
	this.make_gmap = function() {



		var map = new GMap2(document.getElementById(gmapid));
		gmaps[gmapid] = map;

		map.addControl(new GLargeMapControl());
		map.addControl(new GScaleControl());
		map.addControl(new GMapTypeControl());
		map.enableScrollWheelZoom();
		
		map.hideControls();

		var normalize_text = document.createTextNode('Återställ');
		var normalize_a = document.createElement('a');
		normalize_a.setAttribute('class', 'gmap_control gmap_normalizer');
		normalize_a.setAttribute('href', "javascript:normalize('" + gmapid + "');");
		normalize_a.className = 'gmap_control gmap_normalizer';
		normalize_a.setAttribute('id', 'normalizer_' + gmapid);
		normalize_a.appendChild(normalize_text);
		document.getElementById(gmapid).appendChild(normalize_a);

		var maximize_text = document.createTextNode('Maximera');
		var maximize_a = document.createElement('a');
		maximize_a.setAttribute('class', 'gmap_control gmap_maximizer');
		maximize_a.setAttribute('href', "javascript:maximize('" + gmapid + "');");
		maximize_a.className = 'gmap_control gmap_maximizer';
		maximize_a.setAttribute('id', 'maximizer_' + gmapid);
		maximize_a.appendChild(maximize_text);
		document.getElementById(gmapid).appendChild(maximize_a);

		GEvent.addListener(map, "mouseover", function(){
			map.showControls();
			if(document.getElementById(gmapid).style.width == '100%') {
				document.getElementById('normalizer_' + gmapid).style.display = 'inline';
			}
			else {
				document.getElementById('maximizer_' + gmapid).style.display = 'inline';
			}
		});
		GEvent.addListener(map, "mouseout", function(){
			map.hideControls();
			document.getElementById('normalizer_' + gmapid).style.display = 'none';
			document.getElementById('maximizer_' + gmapid).style.display = 'none';
		});
		GEvent.addDomListener(map.getContainer(), "DOMMouseScroll",
			function(oEvent) { 
				if (oEvent.preventDefault) {
					oEvent.preventDefault();
				}
				else {
					oEvent.returnValue(true);
					oEvent.preventBubble(true);
				}
			}
		);
	
	};

	this.make_map = function(message) {
		alert('yeah: ' + message);
	};

	
}

function maximize(gmapid) {
	document.getElementsByTagName("html")[0].style.overflow = 'hidden';
	/*var body = document.getElementsByTagName('body')[0];
	body.style.overflow = 'hidden';
	body.style.padding = 0;
	body.style.margin = 0;
	body.style.width = '100%';
	body.style.height = '100%';*/
	
	var mapel = document.getElementById(gmapid);

/*	mapel.style.top = getScrollTop() + 'px';
	change_class(gmapid, 'gmapdiv_large');*/

	mapel.style.position = 'absolute';
	mapel.style.padding = '0px';
	mapel.style.margin = '0px';
	mapel.style.width = '100%';
	mapel.style.height = '100%';
	mapel.style.top = getScrollTop() + 'px';
	mapel.style.left = '0px';
	mapel.style.right = '0px';
	mapel.style.bottom = '20px';
	mapel.style.zIndex = 100;

	gmaps[gmapid].checkResize();

	var result = gmapid.split("_");

	document.getElementById('maximizer_' + gmapid).style.display = 'none';
	document.getElementById('normalizer_' + gmapid).style.display = 'inline';

}

function normalize(gmapid) {
	document.getElementsByTagName("html")[0].style.overflow = '';
	var mapel = document.getElementById(gmapid);
	mapel.style.position = 'relative';
	mapel.style.top = '';
	mapel.style.right = '';
	mapel.style.left = '';
	mapel.style.bottom = '';
	mapel.style.width = '';
	mapel.style.height = '';
	mapel.style.zIndex = 1;
	mapel.style.margin = '5px';
	//change_class(gmapid, 'gmapdiv');
	gmaps[gmapid].checkResize();

	var result = gmapid.split("_");
	document.getElementById('maximizer_' + gmapid).style.display = 'inline';
	document.getElementById('normalizer_' + gmapid).style.display = 'none';
	window.location = '#map_' + result[1];
}

function set_center_from_line(gmapid, linename) {
	var line = all_lines[ linename ];
	var center_lat = line[0][0];
	var center_lon = line[0][1];
	var center_zoom = line[1];
	var map_type = line[2];
	map_type	=	line[2] == 'mixed'	?	G_HYBRID_MAP	
				:	line[2] == 'sat'	?	G_SATELLITE_MAP
				:	line[2] == 'map'	?	G_NORMAL_MAP
				:							G_SATELLITE_MAP;
	gmaps[gmapid].setCenter(new GLatLng(center_lat, center_lon), center_zoom, map_type);
}

function draw(gmapid, linename, color, width, opacity) {
	var result = linename.split('_');
	var type = result[0];

	var map = gmaps[gmapid];
	var line = all_lines[ linename ];

	var baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
	
	// Om det är en dag eller resa, markera start och slut
	if(type == 'day' || type == 'tour') {
		var _firstline = line[3][0];
		var _lastline  = line[3][ line[3].length - 1 ];
		var first_lat = _firstline[0][0];
		var first_lon = _firstline[0][1];
		var last_lat  = _lastline[ _lastline.length - 1 ][0];
		var last_lon  = _lastline[ _lastline.length - 1 ][1];

		var icon = new GIcon(baseIcon);
		icon.image = "http://www.google.com/mapfiles/dd-start.png";
		map.addOverlay(new GMarker(new GLatLng(first_lat, first_lon), icon));
		icon.image = "http://www.google.com/mapfiles/dd-end.png";
		map.addOverlay(new GMarker(new GLatLng(last_lat, last_lon), icon));
	}

	color	= (color == null || color.length != 7)	?	'#FF0000'	:	color;
	width	= width == null							?	3			:	width;
	opacity = opacity == null						?	0.7			:	opacity;

	var lines = line[3];
	var distance = 0;

	for(var i = 0; i < lines.length; i++) {
		var current_line = lines[i];
		var latlongs = [];

		for(var j = 0; j < current_line.length; j++) {
			latlongs.push(new GLatLng(current_line[j][0], current_line[j][1]));
		}
			
		//new GPolyline(latlongs, "#FF0000", 3, 0.7);
		var polyline = new GPolyline(latlongs, color, width, opacity);
		
		//distance = distance + polyline.getLength();
		//alert('polylength: ' + latlongs.length);
		map.addOverlay(polyline);
		
	}


}
/*
function draw_line(isfirst, gmapid, linename, secondarylinename) {
	var result = linename.split('_');
	var type = result[0];

	var map = gmaps[gmapid];
	var line = all_lines[ linename ];
	var secondary_line = all_lines[ secondarylinename ];

	if(isfirst) {
		var center_lat = line[0][0];
		var center_lon = line[0][1];
		var center_zoom = line[1];
		var map_type = line[2];
		map_type	=	line[2] == 'mixed'	?	G_HYBRID_MAP	
					:	line[2] == 'sat'	?	G_SATELLITE_MAP
					:	line[2] == 'map'	?	G_NORMAL_MAP
					:							G_SATELLITE_MAP;
		map.setCenter(new GLatLng(center_lat, center_lon), center_zoom, map_type);
	}
	
	

	var baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
	
	// Om det är en dag eller resa, markera start och slut
	if(type == 'day' || type == 'tour') {
		var _firstline = line[3][0];
		var _lastline  = line[3][ line[3].length - 1 ];
		var first_lat = _firstline[0][0];
		var first_lon = _firstline[0][1];
		var last_lat  = _lastline[ _lastline.length - 1 ][0];
		var last_lon  = _lastline[ _lastline.length - 1 ][1];

		var icon = new GIcon(baseIcon);
		icon.image = "http://www.google.com/mapfiles/dd-start.png";
		map.addOverlay(new GMarker(new GLatLng(first_lat, first_lon), icon));
		icon.image = "http://www.google.com/mapfiles/dd-end.png";
		map.addOverlay(new GMarker(new GLatLng(last_lat, last_lon), icon));
	}
	
	if(secondary_line) {
		var lines = secondary_line[3];
		for(var i = 0; i < lines.length; i++) {
			var current_line = lines[i];
			var latlongs = [];

			for(var j = 0; j < current_line.length; j++) {
				//alert("lat:" + current_line[j][0] + " lon:" + current_line[j][1]);
				latlongs.push(new GLatLng(current_line[j][0], current_line[j][1]));
			}
			
			//new GPolyline(latlongs, "#FF0000", 3, 0.7);
			map.addOverlay(new GPolyline(latlongs, "#6666DD", 5, 0.5));
		}
	}
	
	var lines = line[3];
	var distance = 0;
	for(var i = 0; i < lines.length; i++) {
		var current_line = lines[i];
		var latlongs = [];

		for(var j = 0; j < current_line.length; j++) {
			//alert("lat:" + current_line[j][0] + " lon:" + current_line[j][1]);
			latlongs.push(new GLatLng(current_line[j][0], current_line[j][1]));
		}
			
		//new GPolyline(latlongs, "#FF0000", 3, 0.7);
		var polyline = new GPolyline(latlongs, "#FF0000", 3, 0.7);
		//distance = distance + polyline.getLength();
		map.addOverlay(polyline);
		
	}
	//alert(Math.round(distance / 100) / 10);
}
*/
function getScrollTop() {

	if (document.documentElement && document.documentElement.scrollTop) {

		return(document.documentElement.scrollTop);

	}

	else if (document.body) {

		return(document.body.scrollTop);

	}

	else {

		return(window.pageYOffset);

	}

}