﻿var _gmap      = null;
var _markerMgr = null;
var _geocoder  = null;
var _gmarkers  = [];
var _startMarker = null;
var _endMarker = null;
var _courseLine = null;
var _mapSearchMarker = null;
var _gmapLoaded = false;

window.onload = function() {
    if (GBrowserIsCompatible()) {
        var gmapElem = document.getElementById("div_gmap");

        _gmap = new GMap2(gmapElem);
        _gmap.setCenter(new GLatLng(26.2418, 127.7234), 13);
        _gmap.addControl(new GLargeMapControl());
        _gmap.addControl(new GScaleControl());
        _gmap.addControl(new GOverviewMapControl(new GSize(200, 160)));
        DownloadMarkersUrl();

        GEvent.addDomListener(gmapElem, "DOMMouseScroll", _gmap.wheelZoom);
        GEvent.addDomListener(gmapElem, "mousewheel", _gmap.wheelZoom);

        resizeApp();

        _gmapLoaded = true;
    }
} 

//
// マウスホイールズーム
//
GMap2.prototype.wheelZoom = function(event) 
{ 
    if ((event.detail || -event.wheelDelta) < 0) 
    { _gmap.zoomIn();}
    else
    { _gmap.zoomOut();} 
  
    return false; 
}


function resizeMap(width, height)
{
    var gmapElem = document.getElementById("div_gmap");
        
    gmapElem.style.width = width;
    gmapElem.style.height = height;
    
    if (_gmap != null)
    {
        _gmap.checkResize();
    }
}

function getStationIcon()
{
    var icon = new GIcon();
    icon.image = "resources/images/station.gif";
    icon.iconSize = new GSize(11, 28);
    icon.shadowSize = new GSize(0, 0);
    icon.iconAnchor = new GPoint(11, 14);
    icon.infoWindowAnchor = new GPoint(5, 28);
    return icon;
}

function getStartMarkIcon()
{
    var icon = new GIcon();
    icon.image = "resources/images/startMark.gif";
    icon.iconSize = new GSize(26, 50);
    icon.shadowSize = new GSize(0, 0);
    icon.iconAnchor = new GPoint(13, 25);
    icon.infoWindowAnchor = new GPoint(13, 0);
    return icon;
}

function getEndMarkIcon()
{
    var icon = new GIcon();
    icon.image = "resources/images/endMark.gif";
    icon.iconSize = new GSize(26, 50);
    icon.shadowSize = new GSize(0, 0);
    icon.iconAnchor = new GPoint(13, 25);
    icon.infoWindowAnchor = new GPoint(13, 0);
    return icon;
}

function getMapSearchMarkIcon()
{
    var icon = new GIcon();
    icon.image = "resources/images/stationSearch.gif";
    icon.iconSize = new GSize(25, 42);
    icon.shadowSize = new GSize(0, 0);
    icon.iconAnchor = new GPoint(12, 21);
    icon.infoWindowAnchor = new GPoint(12, 0);
    return icon;
}

function getBusIcon(direction)
{
    var icon = new GIcon();
    if (direction >= 0 && direction < 25)
    {
        icon.image = "resources/images/bus0.gif";
        icon.iconSize = new GSize(14, 30);
        icon.iconAnchor = new GPoint(12, 15);
        icon.infoWindowAnchor = new GPoint(0, 30);
    }
    else if (direction >= 25 && direction < 65)
    {
        icon.image = "resources/images/bus45.gif";
        icon.iconSize = new GSize(29, 29);
        icon.iconAnchor = new GPoint(14, 14);
        icon.infoWindowAnchor = new GPoint(0, 29);
    }
    else if (direction >= 65 && direction < 115)
    {
        icon.image = "resources/images/bus90.gif";
        icon.iconSize = new GSize(30, 14);
        icon.iconAnchor = new GPoint(15, 7);
        icon.infoWindowAnchor = new GPoint(0, 14);
    }
    else if (direction >= 115 && direction < 155)
    {
        icon.image = "resources/images/bus135.gif";
        icon.iconSize = new GSize(29, 29);
        icon.iconAnchor = new GPoint(14, 14);
        icon.infoWindowAnchor = new GPoint(0, 29);
    }
    else if (direction >= 155 && direction < 200)
    {
        icon.image = "resources/images/bus180.gif";
        icon.iconSize = new GSize(14, 30);
        icon.iconAnchor = new GPoint(7, 15);
        icon.infoWindowAnchor = new GPoint(0, 30);
    }
    else if (direction >= 200 && direction < 245)
    {
        icon.image = "resources/images/bus225.gif";
        icon.iconSize = new GSize(29, 29);
        icon.iconAnchor = new GPoint(14, 14);
        icon.infoWindowAnchor = new GPoint(0, 29);
    }
    else if (direction >= 245 && direction < 290)
    {
        icon.image = "resources/images/bus270.gif";
        icon.iconSize = new GSize(30, 14);
        icon.iconAnchor = new GPoint(15, 7);
        icon.infoWindowAnchor = new GPoint(0, 14);
    }
    else if (direction >= 290 && direction < 335)
    {
        icon.image = "resources/images/bus315.gif";
        icon.iconSize = new GSize(29, 29);
        icon.iconAnchor = new GPoint(14, 14);
        icon.infoWindowAnchor = new GPoint(0, 29);
    }
    else if (direction >= 335 && direction < 360)
    {
        icon.image = "resources/images/bus0.gif";
        icon.iconSize = new GSize(14, 30);
        icon.iconAnchor = new GPoint(7, 15);
        icon.infoWindowAnchor = new GPoint(0, 30);
    }
    
    icon.shadowSize = new GSize(0, 0);
    return icon;
}

function createMarker(id, name, point, ico, onClickHandler, args)
{
    var marker = null;
    
    if (ico != null && ico != '')
    {
        marker = new GMarker(point, {title:name, icon:ico});
    }
    else
    {
        marker = new GMarker(point, {title:name});
    }
        
    marker.name = name;
    
    GEvent.addListener(marker, "click", function() {
       document.getElementsByName(args).item(0).value = id;
        __doPostBack(onClickHandler, '');
    });
    
    return marker;
}

function async(wait) {
    if (arguments.length == 1) return;
    var conn = arguments[arguments.length - 1];
    for (var i = arguments.length - 2; i != 0; i--) {
        conn = (function (f, c) {
            return function () {
                var args = [ f.apply(null, arguments) ];
                setTimeout(
                    function () {
                        c.apply(null, args);
                    },
                wait);
            };
        })(arguments[i], conn);
    }
    conn();
}

