var Ajax;
var map;
/*
 if (Ajax && (Ajax != null)) {
 Ajax.Responders.register({
 onCreate: function() {
 if($('spinner') && Ajax.activeRequestCount>0)
 Effect.Appear('spinner',{duration:0.5,queue:'end'});
 },
 onComplete: function() {
 if($('spinner') && Ajax.activeRequestCount==0)
 Effect.Fade('spinner',{duration:0.5,queue:'end'});
 }
 });
 }
 */

function toggleDonwload(idx) {
    $('clicker' + idx).toggle();
    $('roller' + idx).toggle();
}

function toggleDonwloadAdvanced(idx, text) {
    $('clicker' + idx).toggle();
    $('roller' + idx).toggle();
    $('result' + idx).fade({delay : 5, afterFinish: function() {
        $('result' + idx).update(text).appear()
    }});
}
var handleKeyPress = function (e, form) {
    var key = e.keyCode || e.which;

    if (key == 13) {
        new Ajax.Updater('popup_content', '/web/j_spring_security_check', {asynchronous:true,evalScripts:true,onComplete:function(e) {
            doLogin(e);
        },parameters:Form.serialize(form)});
        return false;
    }
    if (key == 27) {
        $('popup_container').toggle();
        return false;
    }

};

// select google map location

var acceptPosition = function(latLng) {
    $('position').value = latLng;
    $('layer_container').toggle();
};
var getInfoWindowContent = function(marker, acceptText, denyText) {
    return "<div style='background:white;padding:10px;border:1px solid black;line-height:20px;'>" +
            (Math.round(marker.latitude * 10000) / 10000) + ",&nbsp;" + (Math.round(marker.longitude * 10000) / 10000) + "<br />" +
            "<a href='javascript:acceptPosition(\"" + marker.latitude + "," + marker.longitude + "\")'>" + acceptText + "</a>" +
    "</div>";
};
function lookupAddress(address) {
    var ret;
    new Ajax.Request('../map/reverseGeo',{
        asynchronous:false,
        evalScripts:true,
        method: 'GET',
        parameters:{ search:  encodeURIComponent(address) },
        onSuccess: function (result) {
            ret = result.responseJSON;
        }
    });
    return ret;
}

function showBingMap(zoom, acceptText, denyText, key) {
    $('layer_container').toggle();

    var center = 'undefined';
    var positionStr = $('position').value;
    if (positionStr.length > 3) {
        if (positionStr.indexOf(",") > 1) {
            var items = positionStr.split(',');
            var lat = parseFloat(items[0]);
            var lon = parseFloat(items[1]);
            if (!isNaN(lat) && !isNaN(lon)) {
                center = [[lat, lon]];
            }
        }
    }
    if (center == 'undefined') {
        center = lookupAddress(positionStr);
    }

    var mapOptions = {
        credentials: key,
        zoom: zoom,
        enableClickableLogo: false,
        showDashboard: true,
        showCopyright: false,
        showLogo: false,
        enableSearchLogo: false
    };
    
    if (center.length == 1) {
        mapOptions.center = new Microsoft.Maps.Location(center[0][0], center[0][1]);
    }

    map = new Microsoft.Maps.Map($("layer_content"), mapOptions);

    var points = [];
    for (var i=0;i<center.length;i++) {
        var point = new Microsoft.Maps.Location(center[i][0], center[i][1]);
        var marker = new Microsoft.Maps.Pushpin(point, { draggable: true });
        points.push(point);

        createInfoBox(marker, acceptText, denyText);
        
        map.entities.push(marker);
    }
    this.map.setView({ bounds: Microsoft.Maps.LocationRect.fromLocations(points) });
}

var createInfoBox = function(marker, acceptText, denyText) {

    var infoBox = new Microsoft.Maps.Infobox(marker.getLocation(), {
        htmlContent: getInfoWindowContent(marker.getLocation(), acceptText, denyText),
        visible: true,
        height: 80,
        width: 200,
        showCloseButton: false,
        draggable: true,
        showPointer: true,
        zIndex : 10000,
        offset: new Microsoft.Maps.Point(-80, 100)
    });

    Microsoft.Maps.Events.addHandler(marker, 'mouseup', function(e) {
        createInfoBox(marker, acceptText, denyText);
    });
    Microsoft.Maps.Events.addHandler(marker, 'mousedown', function(e) {
        map.entities.remove(infoBox);
    });

    map.entities.push(infoBox);
};


