/*
###############################################################################
## Product Description: Web Mapping and Matching System                      ##
##                                                                           ## 
## Copyright (c) ifPeople (www.ifpeople.net), 2005-2006. All rights          ## 
## reserved.                                                                 ## 
##                                                                           ##
###############################################################################
*/
// -*- java -*-

var welcomeHelpText = ''
+'<div class="info">'
+'This is your current location. You can use this interface to change or '
+'correct this location. If you need help, see the instructions to the '
+'top.'
+'</div>';

var noUrlText = ''
+'<div class="info">'
+'The system has not enough paremeters to relocate you, please '
+'contact the system administrator.'
+'</div>';

var helpText = ''
+'<p>'
+'To change the location, click on the map to move to the '
+'place where you want to show the location. You can zoom '
+'in and out, and also drag the map wherever you want. '
+'Double-click to select the point. Finally, click on the '
+'submit button.'
+'</p>'
+'<p>'
+'After submission, you\'ll be redirected to the page '
+'where you came from.'
+'</p>'
+'<input id="submitit" type="submit"'
+'disabled="true" value="submit location"'
+'onclick="sendWhereAmI()">'

function urlToSend(){
    return getQueryVariable("geocodeurl")[0];
}

function parametersOk(){
    return getQueryVariable("geocodeurl").length > 0;
}

function getFromURL(){
    var result = '';
    var url = getQueryVariable('fromURL');
    if (url.length > 0){
        result = '&fromURL='+url;
    }
    return result;
}

function locatingInit(point,firstTime){
    //get the center point of the map, and add a marker showing the help text
    var map = document.theMap;
    var selectedPoint = map.getCenterLatLng();
    var initMarker = new GMarker(selectedPoint);
    map.addOverlay(initMarker);
    document.selectedPoint = point;
    document.clicked = false;
    var helpDiv = document.getElementById("help");
    helpDiv.className = "shown";
    helpDiv.innerHTML = helpText;
    if (parametersOk()) {
        if(firstTime){
            welcomeHelpText = welcomeHelpText.replace('your current','a default');
        }
        initMarker.openInfoWindowHtml(welcomeHelpText);
        //launch the event listener
        GEvent.addListener(map, 'click', function(overlay, point) {
                if (overlay) {
                    document.theMap.clearOverlays();
                    disableSubmitButton();
                } else if (point) {
                    document.theMap.clearOverlays();
                    document.selectedPoint = point;
                    document.theMap.addOverlay(new GMarker(point));
                    enableSubmitButton();
                }
            });
        enableSubmitButton();
    } else {
        document.clicked = false;
        initMarker.openInfoWindowHtml(noUrlText);
    }
    return;
}

function sendWhereAmI(){
    if (parametersOk()) {
        if(document.clicked){
            var lat = document.selectedPoint.y;
            var lng = document.selectedPoint.x;
            var url = urlToSend();
            var args = 'relocate=True&lat='+lat+'&lng='+lng+getFromURL();
            joiner = '?';
            if(url.indexOf("?") != -1){ joiner = '&'; }
            window.top.location = url+joiner+args;
        }else{
            alert('Click where you live first');
        }
    } else {
        alert("You can't be relocated. Go back and try again");
    }
    return;
}

function enableSubmitButton() {
    document.clicked = true;
    var submit = document.getElementById("submitit");
    submit.disabled = false;
    return;
}

function disableSubmitButton() {
    document.clicked = false;
    var submit = document.getElementById("submitit");
    submit.disabled = true;
    return;
}

