﻿var map;
var newpoints = new Array();
var GPSXNode;
var GPSYNode;
var markers = [];

function OnPageLoad() {
    var myOptions = {
        zoom: zoomIndex,
        center: centerPoint,
        navigationControl: true,
        navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
        mapTypeControl: true,
        mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU },
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    map = new google.maps.Map(document.getElementById(controlId), myOptions);

    for (var i = 0; i < Points.length; i++) {
        var point = new google.maps.LatLng(Points[i][0], Points[i][1]);
        var objectName = Points[i][2];
        var popuphtml = Points[i][3];
        var link = Points[i][4];
        var icon = Points[i][5];

        var marker = createMarker(point, objectName, popuphtml, link, icon);
    }
}

function createMarker(point, objectName, popuphtml, link, image) {
    var marker = new google.maps.Marker({
        position: point,
        map: map,
        title: objectName,
        icon: image
    });

    markers.push(marker);
    return marker;
}

function clearMarkers() {
    for (var i = 0; i < markers.length; i++) {
        markers[i].setMap(null);
    }
}

function PointMarker(GPSX, GPSY, ZoomIndex) {
    var XNode = document.getElementById(GPSX);
    var YNode = document.getElementById(GPSY);
    var ZoomIndex = document.getElementById(ZoomIndex);

    XNode.value = XNode.value.replace(',', '.');
    YNode.value = YNode.value.replace(',', '.');
    ZoomIndex.value = map.getZoom();

    if (IsNumeric(XNode.value) && XNode.value != '-' && XNode.value != '' && IsNumeric(YNode.value) && YNode.value != '' && YNode.value != '-') {
        clearMarkers();
        var point = new google.maps.LatLng(parseFloat(YNode.value), parseFloat(XNode.value));

        var objectName = 'MyPinPoint';
        var popuphtml = '';
        var link = '';

        var marker = createMarker(point, objectName, popuphtml, link);
        map.setCenter(point);
    }
}

function ReplaceDecimal(Id) {
    var Node = document.getElementById(Id);
    Node.value = Node.value.replace(',', '.');
}

//----------------------------------------------------------------------------------------------
// Overlabel

function initOverLabels() {
    if (!document.getElementById) return;

    var labels, id, field;

    labels = document.getElementsByTagName('label');
    for (var i = 0; i < labels.length; i++) {

        if (labels[i].className == 'overlabel') {

            // Skip labels that do not have a named association
            // with another field.
            id = labels[i].htmlFor || labels[i].getAttribute('for');
            if (!id || !(field = document.getElementById(id))) {
                continue;
            }

            // Change the applied class to hover the label 
            // over the form field.
            labels[i].className = 'overlabel-apply';

            // Hide any fields having an initial value.
            if (field.value !== '') {
                hideLabel(field.getAttribute('id'), true);
            }

            // Set handlers to show and hide labels.
            field.onfocus = function() {
                hideLabel(this.getAttribute('id'), true);
            };
            field.onblur = function() {
                if (this.value === '') {
                    hideLabel(this.getAttribute('id'), false);
                }
            };

            // Handle clicks to label elements (for Safari).
            labels[i].onclick = function() {
                var id, field;
                id = this.getAttribute('for');
                if (id && (field = document.getElementById(id))) {
                    field.focus();
                }
            };

        }
    }
};

function hideLabel(field_id, hide) {
    var field_for;
    var labels = document.getElementsByTagName('label');
    for (var i = 0; i < labels.length; i++) {
        field_for = labels[i].htmlFor || labels[i].getAttribute('for');
        if (field_for == field_id) {
            labels[i].style.display = (hide) ? 'none' : 'block';
            return true;
        }
    }
};

//----------------------------------------------------------------------------------------------
// Activate menu

function activateMenu(nav) {
    if (document.all && document.getElementById(nav).currentStyle) {
        var navroot = document.getElementById(nav);

        /* Get all the list items within the menu */
        var lis = navroot.getElementsByTagName("LI");
        for (i = 0; i < lis.length; i++) {

            /* If the LI has another menu level */
            if (lis[i].lastChild.tagName == "UL") {

                /* assign the function to the LI */
                lis[i].onmouseover = function() {

                    /* display the inner menu */
                    this.lastChild.style.display = "block";
                }
                lis[i].onmouseout = function() {
                    this.lastChild.style.display = "none";
                }
            }
        }
    }
}
//----------------------------------------------------------------------------------------------

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}

//----------------------------------------------------------------------------------------------

function Init() {
    OnPageLoad();
    setTimeout(initOverLabels, 50);
    activateMenu('sfMenu');
    externalLinks();
}

//window.onload = OnPageLoad;
window.onload = Init;


