var wmap = null;
var pinDefault = '/Images/pins/pin_default.png';
var pinSelected = '/Images/pins/pin_selected.png';
var oldPinId = null;
var oldVideoId = null;
var oldLatLong = null;
var defaultZoomLevel = 6;
var defaultLat = 50.637268; //50.718001
var defaultLong = 3.062439; //4.394703
var g_minDst = Number.MAX_VALUE;

function wLoadMap() {
	if ((null == wmap) && ('undefined' != typeof VEMap)) {
		wmap = new VEMap('LevacommMap');
		wmap.SetDashboardSize(VEDashboardSize.Normal);
		//wmap.LoadMap(new VELatLong(defaultLat, defaultLong), defaultZoomLevel, 'r', false);
		wmap.LoadMap(null, defaultZoomLevel, VEMapStyle.Road, false);
		wmap.AttachEvent('onclick', wShapeEventHandler);
		//Sys.Application.add_unload(wUnloadMap);
	}
}

function wUnloadMap() {
    if (null != wmap) {
    	wClearMap();
	    wmap.Dispose();
	    wmap = null;
	}
}

function wClearMap() {
	if (null != wmap) {
		wmap.DeleteAllShapes();
	}
	oldPinId = null;
	oldLatLong = null;
	g_minDst = Number.MAX_VALUE;
}

function swapToMap() {
	wSetMapVisible(true);
}

function swapToList() {
	wSetMapVisible(false);
}

function wSetMapVisible(visible) {
	document.getElementById('divContentList').style.display = (visible ? 'none' : 'block');
	document.getElementById('divContentMap').style.display = (visible ? 'block' : 'none');
	if (visible) {
		wLoadMap();
        //wClearMap();
	}
	isMapShown = visible;
	setPersistentCookie(visible);
	reShowVideos();
	if (!visible) {
		wClearMap();
		//wUnloadMap();
	}
}

function swapToMapVideo(vId) {
    oldVideoId = vId;
    swapToMap();
    wReCenterMap();
}

function wShapeEventHandler(e) {
	if ((null != e.elementID) && (true == e.leftMouseButton)) {
	    // if the click occured on an element
		wPinClick(e.elementID);
	}
}

function wPinClick(pinID) {
	var pin = wmap.GetShapeByID(pinID);
	wmap.SetCenter(pin.latLong);
	if (null != oldPinId) {
		var oldPin = wmap.GetShapeByID(oldPinId);
		if (null != oldPin) {
			oldPin.SetCustomIcon(pinDefault);
		}
	}
	pin.SetCustomIcon(pinSelected);
	oldPinId = pinID;
	oldVideoId = pin.videoId;
	wVideoClick(pin.linkButtonID);
}

function wVideoClick(linkId) {
	// simulate the click on the link associated to the video...
	var href = document.getElementById(linkId).href;
	if (href.indexOf('javascript') == -1) {
		document.location = href;
	} else {
		eval(href);
	}
}

function wAddPushpin(cLat, cLong, title, descr, linkId, videoId) {
	if (null != wmap) {
		var latLong = new VELatLong(cLat, cLong)
		var pin = new VEShape(VEShapeType.Pushpin, latLong);
		pin.SetTitle(title);
		pin.SetCustomIcon(pinDefault);
		pin.linkButtonID = linkId;
		pin.latLong = latLong;
		pin.videoId = videoId;
		wmap.AddShape(pin);
		pin.SetDescription(descr.replace(/%pinId%/, pin.GetID()));
		if ((null != oldVideoId) && (videoId == oldVideoId)) {
			pin.SetCustomIcon(pinSelected);
			oldPinId = pin.GetID();
    		oldLatLong = latLong;
		} else if (null == oldLatLong) {
    		oldLatLong = latLong;
		}
		if (latLong != oldLatLong) {
			g_minDst = Math.min(g_minDst, CalcDistance(latLong, oldLatLong));
		} else {
			g_minDst = Number.MAX_VALUE;
		}
		return pin;
	} else {
		return null;
	}
}

function CalcDistance(ll1, ll2) {
	return Math.sqrt(Math.pow(ll1.Latitude - ll2.Latitude, 2)
					+ Math.pow(ll1.Longitude - ll2.Longitude, 2));
}

function wReCenterMap() {
	if (null != wmap) {
		g_minDst = g_minDst * 1000;
		var zoom = Math.round( Math.log(15391 / g_minDst) / 0.8 );
		// we readjust the zoom level, for it not to be out of bounds
		if (zoom >= 19) {
			zoom = 18;
		} else if (zoom < 1) {
			zoom = 1;
		}
		var center = ((null != oldLatLong) ? oldLatLong : new VELatLong(defaultLat, defaultLong));
		wmap.SetCenterAndZoom(center, zoom);
    }
}

function setCurrentPlayingVideo(videoId) {
    oldVideoId = videoId;
}


function setPersistentCookie(b) {
	setCookie('WantMap', (b ? 'True' : 'False'), 1, '/');
}

function setCookie(name, value, expires, path) {
	var today = new Date();
	today.setTime(today.getTime());
	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = name+'='+escape(value) +
		((expires) ? ';expires='+expires_date.toGMTString() : '') + //expires.toGMTString()
		((path) ? ';path='+path : '');
}
