﻿// JScript File

// arrays to hold copies of the markers and html used by the side_bar
// because the function closure trick doesnt work there
var gmarkers = [];
var i = 0;
var linkid = "activite" + i;
var lastlinkid = "";
var bounds = new GLatLngBounds();
var map;

var htmls = [];
var to_htmls = [];
var from_htmls = [];

function CreateMarkerAndPoint2(Lat, Lng, name, URL, html, languageView) {
    // add the points  
    var LinkIDCreate = "activite" + i;
    var LinkIDCreate2 = i;
    var point = new GLatLng(Lat, Lng);
    var marker = createMarker(point, name, html, languageView);
    map.addOverlay(marker);
    bounds.extend(point);
}


// A function to create the marker and set up the event window
function createMarker(point, name, html, languageView) {
    // use a custom icon with letter A - Z
    //var letter = String.fromCharCode("A".charCodeAt(0) + i);
    var myIcon = new GIcon(G_DEFAULT_ICON, "/medias/communs/map/default.png");
    myIcon.printImage = "/medias/communs/map/default.png";
    myIcon.mozPrintImage = "/medias/communs/map/default.png";
    myIcon.iconSize = new GSize(21, 21);
    myIcon.iconAnchor = new GPoint(0, 21);
    myIcon.shadow = "/medias/communs/map/ombre.png";
    myIcon.shadowSize = new GSize(27, 28);

    var marker = new GMarker(point, { icon: myIcon, title: name });

    // The info window version with the "Arriv&eacute;e" form open
    html = name + '<br>' + html
    if (languageView == 'fr') {
        to_htmls[i] = html + '<br>Directions: <b>Arriv&eacute;e</b> - <a href="javascript:fromhere(' + i + ')">D&eacute;part</a>' +
           '<br>Adresse d&eacute;part:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=200 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Pour s\'y rendre" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
        // "(" + name + ")" + 
           '"/>';
        // The info window version with the "Arriv&eacute;e" form open
        from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">Arriv&eacute;e</a> - <b>D&eacute;part</b>' +
           '<br>Adresse arriv&eacute;e:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=200 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Pour s\'y rendre" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
        // "(" + name + ")" + 
           '"/>';
        // The inactive version of the direction info
        html = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">Arriv&eacute;e</a> - <a href="javascript:fromhere(' + i + ')">D&eacute;part</a>';
    } else {
        to_htmls[i] = html + '<br>Directions: <b>End</b> - <a href="javascript:fromhere(' + i + ')">Start</a>' +
           '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=200 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
        // "(" + name + ")" + 
           '"/>';
        // The info window version with the "Arriv&eacute;e" form open
        from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">End</a> - <b>Start</b>' +
           '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=200 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
        // "(" + name + ")" + 
           '"/>';
        // The inactive version of the direction info
        html = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">End</a> - <a href="javascript:fromhere(' + i + ')">Start</a>';
    }


    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });

    // save the info we need to use later for the side_bar
    gmarkers[i] = marker;
    htmls[i] = html;
    i++;
    return marker;
}

function SetInitLayoutCarte() {
    var strUrl = window.location.href;
    var intPosition = strUrl.indexOf('#');
    if (intPosition > 0) {
        lastlinkid = strUrl.substring(intPosition + 1);
        var aHighlight = getElementsByName_iefix(lastlinkid);
        for (i = 0; i < aHighlight.length; i++)
            aHighlight[i].firstChild.className = "innerItem listHighlight";
    }
}


function getElementsByName_iefix(name) {
    var rootList = document.getElementById("escListeAct");
    var elem = rootList.getElementsByTagName("li");
    var arr = new Array();
    for (i = 0, iarr = 0; i < elem.length; i++) {
        //att = elem[i].getAttribute("name");
        att = elem[i].className;
        if (att == name) {
            arr[iarr] = elem[i];
            iarr++;
        }
    }
    return arr;
}


// This function picks up the click and opens the corresponding info window
//function myclick(i) {
//	linkid="activite"+i;
//	GEvent.trigger(gmarkers[i], "click");
//}


// This function picks up the click and opens the corresponding info window
function myclick(i) {
    gmarkers[i].openInfoWindowHtml(htmls[i]);
    location.href = "#anchorCarte";
}

// functions that open the directions forms
function tohere(i) {
    gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
    gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

function initialize() {
    if (GBrowserIsCompatible()) {
        var mapDiv = document.getElementById("googleCarte");
        // create the map
        map = new GMap2(mapDiv);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new  GScaleControl());
        map.setCenter(new GLatLng(45.629405, -73.487549), 5);
        var CopyrightDiv = mapDiv.firstChild.nextSibling;
        CopyrightDiv.className = "googleCarte2";
    } else {
        alert("Désolé, Google Maps n'est pas compatible avec ce navigateur \n Sorry, Google Maps is not compatible with this browser");
    }
}