﻿var map;
var newpoints = new Array();
var GPSXNode;
var GPSYNode;
var markers = [];

function OnPageLoad() {
    if (GBrowserIsCompatible()) {   
        map = new GMap2(document.getElementById(controlId));
        
        map.setCenter(centerPoint, zoomIndex);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.enableScrollWheelZoom();

        for (var i = 0; i < Points.length; i++) {
            var point = new GLatLng(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);
            map.addOverlay(marker);
        }

        //map.zoomToMarkers();
    }
    
//    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
//    });

    var marker = new GMarker(point, { icon: image, title: objectName })
    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 = 'Här skedde olyckan!';
//        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(',', '.');
//}

//function externalLinks() {
//    if (document.getElementsByTagName) {
//        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";
//            }
//        }
//    }
//}

//----------------------------------------------------------------------------------------------
// 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;
        }
    }
};

//----------------------------------------------------------------------------------------------

function Init() {
    OnPageLoad();
    setTimeout(initOverLabels, 50);
    externalLinks();
}

//window.onload = OnPageLoad;
//window.onload = Init;


