// QContent map objects and functions
// Copyright 2008-2058 QScend Technologies Inc.

var oMap = null;
var MAP_POI = 'QCfinderMap';

function clientBodyOnLoad() {
    if (document.getElementById(MAP_POI)) {
        oMap = new CoreMap(MAP_POI, QContentMaps.defaultLocation, 16, 'normal', false);
        oMap.init(true); 
        oMap.addPopupMenu('popupmenu', QContentMaps.latId, QContentMaps.longId, QContentMaps.handleMouseClick);
        oMap.attachEvent('onchangemapstyle', QContentMaps.hidePopupMenu);
        var lat = QContentMaps.$(QContentMaps.latId);
        var lng = QContentMaps.$(QContentMaps.longId);
        if (lat && lng) {
            QContentMaps.addPoint(lat.value, lng.value);
			if (lng.value == '' && lat.value == '') {
				oMap.setDefaultLocation();
			}
        } else {
			oMap.setDefaultLocation();
		}
    }
}

function QContentMaps() {

    QContentMaps.defaultLocation = '';
    QContentMaps.latId = '';
    QContentMaps.longId = '';
    QContentMaps.latLabelId = '';
    QContentMaps.longLabelId = '';
    
    QContentMaps.streetNumberId = '';
    QContentMaps.streetId = '';
    QContentMaps.cityId = '';
    QContentMaps.stateId = '';
    QContentMaps.zipId = '';
    QContentMaps.tabStripId = '';
    
    QContentMaps.$ = function(id) {
        return document.getElementById(id);
    }

    QContentMaps.addPoint = function(lat,lng) {
        if (!oMap) return;
        oMap.addPushpin('Selected Point', 'Latitude: ' + lat + '<br>Longitude: ' + lng, lat, lng, null, true, true);
    }
    
    QContentMaps.findAddress = function() {
        if (!oMap) return;
        
        var streetNumber = QContentMaps.$(QContentMaps.streetNumberId).value;
        var street = QContentMaps.$(QContentMaps.streetId).value;
        var city = QContentMaps.$(QContentMaps.cityId).value;
        var state = QContentMaps.$(QContentMaps.stateId);
        state = state.options[state.selectedIndex].value;
        var zip = QContentMaps.$(QContentMaps.zipId).value;
        
        oMap.clear();
        oMap.callBack = QContentMaps.showMapTab;
        oMap.findAddress(streetNumber, street, '', city, state, zip);
                
    }
        
    QContentMaps.handleMouseClick = function(e) {
        if (e.rightMouseButton) {
            oMap.showPopupMenu(e);
        } else {
            oMap.hidePopupMenu();
        }
	return false;
    }

    QContentMaps.hidePopupMenu = function() {
        if (!oMap) return;
        oMap.hidePopupMenu();
    }
    
    QContentMaps.loadPoint = function() {
        var lat = QContentMaps.$(QContentMaps.latId);
        var lng = QContentMaps.$(QContentMaps.longId);
        if (lat && lng) {
            QContentMaps.addPoint(lat.value, lng.value);
        }
    }
    
    QContentMaps.savePoint = function() {
        try {
            QContentMaps.$(QContentMaps.latId).value = oMap.clickPoint.Latitude;
            QContentMaps.$(QContentMaps.longId).value = oMap.clickPoint.Longitude;
            QContentMaps.$(QContentMaps.latLabelId).innerHTML = oMap.clickPoint.Latitude;
            QContentMaps.$(QContentMaps.longLabelId).innerHTML = oMap.clickPoint.Longitude;
        } catch(e) {
            alert('Unable to determine coordinates.\n\n' + e.description);
        }
        oMap.hidePopupMenu();
        QContentMaps.addPoint(oMap.clickPoint.Latitude, oMap.clickPoint.Longitude);
        //oMap.addPushpin('Selected Point', 'Latitude: ' + oMap.clickPoint.Latitude + '<br>Longitude: ' + oMap.clickPoint.Longitude, oMap.clickPoint.Latitude, oMap.clickPoint.Longitude, null, true, true);
    }
    
    QContentMaps.showMapTab = function() {
        var tabStrip = $find(QContentMaps.tabStripId);
        if (!tabStrip) return;
        var tab = tabStrip.findTabByText('Find By Map');
        if (!tab) return;
        tab.set_selected(true);
    }
    
    QContentMaps.tabStripClick = function(sender,eventArgs) {
        //corrects an issue where the map is zoomed very far out when the page reloads
        if (!oMap) return;
        oMap.resetZoom();
    }
}

QContentMaps();  //invoke namespace
