var map;
var gdir;
var gl = new Array();

function mapProductList () {
	map = new GMap2(document.getElementById("map_canvas"));
	if (GBrowserIsCompatible()) {
		map.addControl(new GSmallMapControl());
		map.setCenter(new GLatLng(60.243235,9.89668));
		map.setZoom(7);
		var bounds = new google.maps.LatLngBounds();
		var w = window.location;
		var point;
		
		for (var f = 0; f < gl.length; f++) {
			point = new GLatLng(gl[f].lat, gl[f].lng);
			marker = new GMarker(point);
			if (gl[f].lbl) {
				marker.bindInfoWindowHtml('<h3>'+gl[f].lbl+'</h3><p><a href="'+w.protocol+'//'+w.hostname+w.pathname+'?id=4'+w.search.substring(5)+'&pid='+gl[f].uri+'">Les mer...</a></p>');
			}
			bounds.extend(point);
			map.addOverlay(marker);
		}
		
		var pointsZoomHer = map.getBoundsZoomLevel( bounds );
		var min = 6;
		var max = 10;
		if (pointsZoomHer < min) pointsZoomHer = min;
		if (pointsZoomHer > max) pointsZoomHer = max;
		map.setZoom(pointsZoomHer);

		var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
		var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
		map.setCenter(new GLatLng(clat,clng));
	}
}

function mapProduct () {
	map = new GMap2(document.getElementById("map_canvas"));
	gdir = new GDirections(map, document.getElementById("route"));
	if (GBrowserIsCompatible()) {
		var point = new GLatLng(gl[0].lat, gl[0].lng);
		map.addControl(new GSmallMapControl());
		map.setCenter(point);
		map.setZoom(8);
		marker = new GMarker(point);
		map.addOverlay(marker);
	}
}

function mapTopmenu () {
	map = new GMap2(document.getElementById("map_canvas"));
	gdir = new GDirections(map, document.getElementById("route"));
	if (GBrowserIsCompatible()) {
		var point = new GLatLng(60.243235,9.89668);
		map.addControl(new GSmallMapControl());
		map.setCenter(point);
		map.setZoom(7);
		//map.addOverlay(new GMarker(point));
	}
}

function mapRoute () {
	from = document.frmRoute.txtRoute.value;
	//to = document.getElementById('here').innerHTML;
	to = document.frmRoute.txtTo.value;
	if (from != '' && to != '') {
		map.clearOverlays();
		gdir.load("from: " + from + ", Norway to: " + to);
	}
}

function mapDirections () {
	var root = document.frmDirections;
	var from = root.txtDirections.value;
	var to = root.selDirections.options[root.selDirections.selectedIndex].value;
	
	
	
	if (from != '') {
		map.clearOverlays();
                gdir.load("from: " + from + ", Norge to: " + to + ", Norge");
	}
//alert(from + ' - ' + to);
}

function mapDistance () {
	var root = document.frmDistance;
	var from = root.selDirections.options[root.selDirections.selectedIndex].value;
	
	gdir = new GDirections();
	if (from != '') {
		gdir.load("from: " + from + ", Norge to: " + document.frmRoute.txtTo.value);
		document.getElementById('distance').innerHTML = 'Beregner avstand...';
		setTimeout("showDistance()", 2000);
	}
}
function showDistance () {
	var dist = Math.round(gdir.getDistance().meters / 1000) / 10;
	document.getElementById('distance').innerHTML = 'Avstand: ' + dist + ' km.';
}

function showhide (current) {
	var num = current.id.substring(2);
	var tag = document.getElementById('ul' + num);
	if (tag.style.display == '' || tag.style.display == 'none') {
		tag.style.display = 'block';
	} else if (tag.style.display == 'block') {
		tag.style.display = 'none';
	}
}
function swapImage (url) {
	document.getElementById('image_canvas').src = url;
}
function emptyField (current) {
	current.value = '';
}
function toggleCheckAll (current) {
	var root = document.frmChoices;
	var total = root[current.name + 'Num'].value;
	
	for (var num = 0; num < total; num++) {
		var currentName = current.name + num;
		root[currentName].checked = false;
	}
}
function toggleCheckOne (current) {
	document.frmChoices[current.name.substring(0,3)].checked = false;
}
