﻿// Helpers
function getMap() {
	return findSWF("flashmap");
}

function findSWF(movieName) {
    if (navigator.appName.indexOf("Microsoft")!= -1) {
        return document.getElementById(movieName);
    } else {
        return document.getElementsByName(movieName)[0];
    }
}

function setLocation(x, y, level) {
	map = getMap();
	if (map != null) map.setLocation(x, y, level);
}

function hookEvent(element, eventName, callback) {
    if (element.addEventListener) {
        element.addEventListener(eventName, callback, false);
    }
    else if (element.attachEvent) {
        element.attachEvent("on" + eventName, callback);
    }
}

function unhookEvent(element, eventName, callback) {
    if (element.removeEventListener) {
        element.removeEventListener(eventName, callback, false);
    }
    else if (element.detachEvent) {
        element.detachEvent("on" + eventName, callback);
    }
}

function cancelEvent(e) {
    e = e ? e : window.event;
    if (e.stopPropagation) e.stopPropagation();
    if (e.preventDefault) e.preventDefault();
    e.cancelBubble = true;
    e.cancel = true;
    e.returnValue = false;
    return false;
}

function browserIsFirefox() {
    return (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent));
}

function browserIsChrome() {
    return (/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent));
}

function bodyLoad() {
    addMouseWheelHandler(document, handleMouseWheel);
}

function addMouseWheelHandler(elmt, handler) {
    if (browserIsFirefox()) {
	    hookEvent(elmt, "DOMMouseScroll", handler);
    }
    else {
	    hookEvent(elmt, "mousewheel", handler);  
    }
}

// Pass the mouse wheel action down to the map 
// (mousewheel events cannot be picked up by Flash in Firefox when wmode=transparent
function handleMouseWheel(e) {
    // standardise between browsers
    e = e ? e : window.event;
    var wheelData = e.detail ? e.detail * -1 : e.wheelDelta / 40;
    // work out what scroll increment to pass
    var scrollInc = 1;
    if (wheelData < 0) scrollInc = -1;
    var map = getMap();
    map.doMouseWheelAction(scrollInc);
    cancelEvent(e); // stop the event being processed by the browser window.
}

// A mouse up out of the Flash area cannot be detected by Flash when wmode=transparent, so send the event to the flash map
function handleMouseUp(e) {
    var map = getMap();
    map.doMouseUpAction();
    cancelEvent(e);
}

// Functions
function setMapRequestParameters(name, value) {
	map = getMap();
	if (map != null) map.setMapRequestParameters(name, value);
}

function setCurrentLayerName(name) {    
	map = getMap();
	if (map != null) map.setCurrentLayerName(name);
}

function getCurrentLayerName() {
	map = getMap();
	if (map != null) return map.getCurrentLayerName();
	else return "";
}

function setZoomValue(value) {
	map = getMap();
	if (map != null) map.setZoomValue(value);
}

function getZoomValue() {
	map = getMap();
	if (map != null) return map.getZoomValue();
	else return "5"; // return a default value?
}

function setCenter(x, y) {
	map = getMap();
	if (map != null) map.setCenter(x, y);
}

function refresh() {
	map = getMap();
	if (map != null) map.refresh();
}

function zoomMap(level) {
	map = getMap();
	if (map != null) map.zoomMap(level);
}

function doButtonAction(btnName) {
	map = getMap();
	if (map != null) map.doButtonAction(btnName);
}

function getCenter() {
	map = getMap();
	if (map != null) return map.getCenter(); // returns point with x and y properties
}

function getCenterIX() {
	map = getMap();
	if (map != null) return map.getCenterIX();
}

function getCenterIY() {
	map = getMap();
	if (map != null) return map.getCenterIY();
}

function getResolution() {
	map = getMap();
	if (map != null) return map.getResolution(); // returns resolution in m/px
}

function setResolution(value) {
	map = getMap();
	if (map != null) map.setResolution(value);
}

// Callbacks
function GetMapParameter(name) {
	switch(name) {
		// URL of the config file to use. URL's base must be one of the currently allowed ones
		case "ConfigFileURL":
			return "http://peoplesmap.com/flashmapconfig.ashx";
		case "scaleLevel":  // initial zoom level (0 = zoomed in)
			return "5";
		default:
		    return null;
	}
}

function ShowCenter(){
	var pt = getCenter();
}

function ShowResolution(){
	var res = getResolution();
}
