var wmap = null;
var pinDefault = 'http://www.waterlootv.be/Images/pins/pin_default.png';
var pinSelected = 'http://www.waterlootv.be/Images/pins/pin_selected.png';
var oldPinId = null;
var oldVideoId = null;
var oldLatLong = null;
var defaultZoomLevel = 12;
var videoZoomLevel = 16;
var g_spotSomewhere = false;

function wLoadMap() {
	if (null == wmap) {
		wmap = new VEMap('waterlooMap');
		wmap.SetDashboardSize(VEDashboardSize.Small);
		//wmap.SetDashboardSize(VEDashboardSize.Tiny);
		//wmap.LoadMap(new VELatLong(50.718001, 4.394703), defaultZoomLevel, 'r', false);
		wmap.LoadMap(null, defaultZoomLevel, 'h', false);
		wmap.AttachEvent('onclick', wShapeEventHandler);
		//Sys.Application.add_unload(wUnloadMap);
	}
}

function wUnloadMap() {
    if (null != wmap) {
    	wClearMap();
	    wmap.Dispose();
	    wmap = null;
	}
}

function wClearMap() {
	wmap.DeleteAllShapes();
	oldPinId = null;
	oldLatLong = null;
}

function swapToMap() {
	wSetMapVisible(true, true);
}
function swapToMap2(spotSomewhere) {
	wSetMapVisible(true, spotSomewhere);
}

function swapToList() {
	wSetMapVisible(false, false);
}

function wSetMapVisible(visible, spotSomewhere) {
	document.getElementById('divContentList').style.display = (visible ? 'none' : 'block');
	document.getElementById('divContentMap').style.display = (visible ? 'block' : 'none');
	if (visible) {
		wLoadMap();
        //wClearMap();
	}
	isMapShown = visible;
	setPersistentCookie(visible);
	g_spotSomewhere = spotSomewhere;
	reShowVideos();
	g_spotSomewhere = true;
	if (!visible) {
		wClearMap();
		//wUnloadMap();
	}
}

function swapToMapVideo(vId) {
    oldVideoId = vId;
    swapToMap2(false);
    wReCenterMap(true);
}

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 lnk = document.getElementById(linkId);
	eval(lnk.attributes['href'].nodeValue);
}

function wAddPushpin(latLong, title, descr, linkId, videoId) {
	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;
    }
	return pin;
}

function wReCenterMap(spotOnVideo) {
	wmap.SetCenterAndZoom(((spotOnVideo && (null != oldLatLong)) ? oldLatLong : new VELatLong(50.718001, 4.394703)), (spotOnVideo ? videoZoomLevel : defaultZoomLevel));
}

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 : '');
}

