function changeMapType(mapTypeInteger)
{
	switch (mapTypeInteger)
	{
		case 1:
			map.setMapType(G_NORMAL_MAP);
			break;
		case 2:
			map.setMapType(G_SATELLITE_MAP);
			break;
		case 3:
			map.setMapType(G_HYBRID_MAP);
			break;
		case 4:
			map.setMapType(G_PHYSICAL_MAP);
			break;
		default :
			map.setMapType(G_NORMAL_MAP);
			break;
	}
}

function pageLoadFunctions()
{  
	if (!GBrowserIsCompatible()) return;

	addShortcut();
	
	var sURL = new String(window.location);
	var queryStr = new QuerystringObject(sURL);
	var showHelpOnStart = true;
	
	map = new GMap2(document.getElementById("map"),{mapTypes : [
	G_NORMAL_MAP,
	G_SATELLITE_MAP, 
	G_HYBRID_MAP, 
	G_PHYSICAL_MAP
	] });
	if (doesRememberWhereIWasLastTime())
	{
		gotoWhereIWasLastTime();
	}else{
	    var targetPoint = new GLatLng(13.239945,-12.65625);
	    var zoom = 2;
	    map.setCenter(targetPoint);	
	    map.setZoom(zoom);
	}	
	map.addControl(new GMapTypeControl());
	map.enableContinuousZoom();
	
	//google analytics
	_uacct = "UA-217190-1";
	urchinTracker();
	if (showHelpOnStart == true){
		toggleHelp();      
	}	
	
}

function doOnFocus(keyChar) {

    try {
        // This switch determines which button on the remote is pressed.
        switch (keyChar) {
            case 38:  // UP button was selected.
                moveMap("up");
                break;

            case 40:  // DOWN button was selected.
                moveMap("down");
                break;

            case 37:  // LEFT button was selected.
                moveMap("left");
                break;

            case 39:  // RIGHT button was selected.
                moveMap("right");
                break;
            case 33:               // PAGE UP (remote control Plus button)
                zoomIn();  // selected; page-up scrolling menu 
                return true;       // (currently works for keyboard Page 
                break;             // Up, but not for the remote button).

            case 34:                // PAGE DOWN (remote control Minus
                zoomOut(); //  button) selected; page-down 
                return true;        // scrolling menu (currently works for 
                break;              // keyboard Page Down, but not for the 
            // remote button). 
            case 48:
            case 49:
            case 50:
            case 51:
            case 52:
            case 53:
            case 54:
            case 55:
            case 56:
            case 57:
                changeMapType(keyChar - 48);
                break;
            case 13: //Enter / OK
                cycleMapFwd();
                break;
            case 73: //i
            case -100: //Info button
                toggleHelp();
                break;
            default:
                return false;
                // Ignore all other clicks.
        }
    }
    catch (ex) {
        // Ignore error.
    }
}

function getCurrentMapType() {
    var lastMap = map.getCurrentMapType();
    if (lastMap == G_SATELLITE_MAP) return 2;
    if (lastMap == G_HYBRID_MAP) return 3;
    if (lastMap == G_PHYSICAL_MAP) return 4;
    return 1;
}

function cycleMapFwd() {

    if (helpIsDisplayed) {
        toggleHelp();
    } else {
        var currentType = getCurrentMapType();
        currentType++;
        if (currentType == 5) {
            currentType = 1;
            toggleHelp();
        }
        changeMapType(currentType);
    }
}

function rememberWhereIWasLastTime()
{
    var currentPoint = map.getCenter();
	setCookie("lastlat",currentPoint.lat(),30);
	setCookie("lastlng",currentPoint.lng(),30);
	setCookie("lastzoom",map.getZoom(),30);
	var lastMap = getCurrentMapType();	
	setCookie("lastType",lastMap,30);
}

function doesRememberWhereIWasLastTime()
{
	var lastlng = getCookie("lastlng");
	return (lastlng && lastlng.length > 0);	
}

function gotoWhereIWasLastTime()
{
	var lastlat = getCookie("lastlat");
	var lastlng = getCookie("lastlng");
	var lastzoom = getCookie("lastzoom");
	var gotoPoint = new GLatLng(parseFloat(lastlat),parseFloat(lastlng));
	map.setCenter(gotoPoint);
	map.setZoom(parseInt(lastzoom,10));
	var lastType = parseInt(getCookie("lastType"),10);
	changeMapType(lastType);
}

function forgetWhereIWasLastTime()
{
	delCookie("lastlat");
	delCookie("lastlng");
	delCookie("lastzoom");
	delCookie("lastType");	
}


