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();
    GUnload();
};
var showInfo = function(marker, acceptText, denyText) {
    marker.openExtInfoWindow(map, "location_information",
            getInfoWindowContent(marker, acceptText, denyText),
    {beakOffset: 3});
};
var getInfoWindowContent = function(marker, acceptText, denyText) {
    return marker.getLatLng().toUrlValue() + "<br />" +
           "<a href='javascript:acceptPosition(\"" + marker.getLatLng().toUrlValue() + "\")'>" + acceptText + "</a>&nbsp;&nbsp;" +
            "<a href='javascript:void(0);' onclick='$(\"layer_container\").toggle();'>" + denyText + "</a>"
       ;
};

function showMap(zoom, acceptText, denyText) {
    $('layer_container').toggle();
    map = new GMap2($('layer_content'));
    var center = new GLatLng(48.137232,11.575481);
    if ($('position').value.length > 3 && $('position').value.indexOf(",") > 1) {
        var items = $('position').value.split(',');
        var lat = parseFloat(items[0]);
        var lon = parseFloat(items[1]);
        if (lat != NaN && lon != NaN) {
            center = new GLatLng(lat, lon);
        }
    }

    map.setCenter(center, zoom);
    map.addControl(new GLargeMapControl3D());
    map.addControl(new GMapTypeControl());
    this.map.addMapType(G_PHYSICAL_MAP);
    var marker = new GMarker(center, {draggable: true});

    GEvent.addListener(marker, "dragstart", function() {
        map.closeExtInfoWindow();
    });

    GEvent.addListener(marker, "dragend", function() {
        map.panTo(marker.getLatLng());
        showInfo(marker, acceptText, denyText);
    });

    GEvent.addListener(map, "click", function(overlay, latlng) {
        if (overlay == null && latlng) {
            GEvent.trigger(marker, 'dragstart');
            marker.setLatLng(latlng);
            GEvent.trigger(marker, 'dragend');
        }
    });

    map.addOverlay(marker);
    showInfo(marker, acceptText, denyText);
}