﻿//jQuery(document).ready(function()
//{
//    var f = function() { addMarkers; };
//    setTimeout(f, 3000);
//});

function addMarkers()
{
    if (GBrowserIsCompatible())
    {
        attachToSmartMap(false);
    
        eval("var lats = new Array(" + jQuery("#" + latsId).val() + ");");
        eval("var lngs = new Array(" + jQuery("#" + lngsId).val() + ");");
        eval("var locs = new Array(" + jQuery("#" + locsId).val() + ");");

        for (var i = 0; i < locs.length; i++)
        {
            var icon = new GIcon();
            icon.iconAnchor = new GPoint(16, 32);
            icon.iconSize = new GSize(39, 34);
            icon.image = jetkeyIconPath + "arrow.png";
            icon.infoWindowAnchor = new GPoint(10, 3); // new GPoint(5, 1);
            icon.shadow = jetkeyIconPath + "arrowshadow.png";
            icon.shadowSize = new GSize(39, 34);

            var name = "marker" + i;
            var info = locs[i].replace(/&#34;\[\[/g, "\"");
            info = info.replace(/\]\]&#34;/g, "\"");
            var ci = "<div class=\"misinfo\">" + info + "</div>";
            var point = new GLatLng(lats[i], lngs[i]);
            var marker = createMarker(point, name, ci, i, icon);

            map.addOverlay(marker);
        }
    }
};


//function selfHtml(id)
//{
//    return jQuery("<div>").append(jQuery("#" + id).clone()).remove().html()
//}


var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];

// A function to create the marker and set up the event window
function createMarker(point, name, html, i, customIcon)
{
    var marker = new GMarker(point, customIcon);

    // The info window version with the "to here" form open
    to_htmls[i] = html +
           '<br />' +
           '<b>Directions:</b> <b>To here</b> - <a href="javascript:void(0);" onclick="fromhere(' + i + ');">From here</a>' +
           '<div style="padding-top: 3px;">' +
           '    Start address:<br />' +
           '    <form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '        <div style="padding-left: 20px;">' +
           '            <input type="text" size="40" maxlength="48" name="saddr" id="saddr" value="" class="textbox" /><br />' +
           '            <div style="padding-top: 3px; padding-bottom: 3px; text-align: right;">' +
           '                <input value="Get Directions" type="submit" class="button" />' +
           '            </div>' +
           '            <input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + '"/>' +
           '        </div>' +
           '    </form>' +
           '</div>';

    // The info window version with the "from here" form open
    from_htmls[i] = html +
           '<br />' +
           '<b>Directions:</b> <a href="javascript:void(0);" onclick="tohere(' + i + ');">To here</a> - <b>From here</b>' +
           '<div style="padding-top: 3px;">' +
           '    End address:<br />' +
           '    <form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '        <div style="padding-left: 20px;">' +
           '            <input type="text" size="40" maxlength="48" name="daddr" id="daddr" value="" class="textbox" /><br />' +
           '            <div style="padding-top: 3px; padding-bottom: 3px; text-align: right;">' +
           '                <input value="Get Directions" type="submit" class="button" />' +
           '            </div>' +
           '            <input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() + '"/>' +
           '        </div>' +
           '    </form>' +
           '</div>';

    // The inactive version of the direction info
    html = html +
           '<br />' +
           '<b>Directions:</b> <a href="javascript:void(0);" onclick="tohere(' + i + ');">To here</a> - <a href="javascript:void(0);" onclick="fromhere(' + i + ');">From here</a>';

    //marker.openInfoWindowHtml("<div class=\"markerwindow\">" + html + "</div>");

    GEvent.addListener(marker, "click", function()
    {
        marker.openInfoWindowHtml("<div class=\"markerwindow\">" + html + "</div>");
    });

    gmarkers[i] = marker;
    htmls[i] = html;

    return marker;
}

// functions that open the directions forms
function tohere(i)
{
    gmarkers[i].openInfoWindowHtml("<div class=\"markerwindow\">" + to_htmls[i] + "</div>");
}
function fromhere(i)
{
    gmarkers[i].openInfoWindowHtml("<div class=\"markerwindow\">" + from_htmls[i] + "</div>");
}