var where_detect_watch_timeout;
var where_detect_previous_keywords;
var where_detect_fail_handle_timeout;

/**
 *  add client side validation to the question's where input
 */
function where_detect_watch(event)
{
    share_plan_form_last_char = event.keyCode;
    if(where_detect_watch_timeout > 0)
    {
        clearTimeout(where_detect_watch_timeout);
    }//if
    if(share_plan_form_last_char == Event.KEY_TAB)
    {
        where_detect();
    }
    else
    {
        if(share_plan_form_last_char != Event.KEY_RETURN)
        {
            where_detect_watch_timeout = setTimeout(function() {
                where_detect();
            }, 800);
        }//if
    }//if/else
}//function

function where_detect()
{
    var field_id = 'question_location';
    var $field = $(field_id);
    var keywords = trim($field.getValue());

    // Close detect area if no keywords
    if(!keywords)
    {
        where_detect_previous_keywords = keywords;
        where_detect_area('off');
        return;
    }

    // Make sure keywords have changed
    if(keywords == where_detect_previous_keywords)
    {
        return;
    }

    where_detect_previous_keywords = keywords;

    // Close detect area, show spinny and disable form while looking up place results

    where_detect_area('off');

    var loading_img = document.getElementById("loading_img");
    loading_img.style.display = "block";

    // Ajax call for place information
    new Ajax.Request('/home/find_location_from_google',
    {
        method:'get',
        parameters:
        {
            keywords: keywords
        },
        onSuccess:function(transport,json)
        {
            var places = transport.responseText.evalJSON(true);
            if(places!='')
            {
                where_detect_area('on');

                document.getElementById("location_title").innerHTML = places.address;
                document.getElementById("location_image").src = "http://maps.google.com/maps/api/staticmap?center=" +
                places.latitude + "," + places.longitude + "&zoom=14&size=100x70&maptype=roadmap&markers=color:red|" +
                places.latitude + "," + places.longitude + "&sensor=false";
                    
            }
        },
        onComplete: function()
        {
            loading_img.style.display = "none";
            clearTimeout(where_detect_fail_handle_timeout);
        }
    });

    showPreview();

    where_detect_fail_handle_timeout = setTimeout(function() {
        detect_pending('off');
    },6000);
}

/**
 *  turn on busy notification for the given id
 *
 *  @param  string  id  the id of the input element to turn on pending notification
 *  @param  boolean status  true to turn notification on, false to turn it off
 */
function detect_pending(id,status)
{
    if(!id)
    {
        return;
    }//if
}

/**
 * toggle the display of the where detect area below the where field
 *
 * @param string status "on" means show area, "off" means hide it
 */
function where_detect_area(status)
{
    if(status == 'on')
    {
        document.getElementById("location_view").style.display = 'block';
    } else {
        document.getElementById("location_view").style.display = 'none';
        where_detect_result = 0;
    }
}

var where_detect_result = 0;

function trim(str)
{
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function selectLocation() {
    document.getElementById("question_location").value = document.getElementById("location_title").innerHTML;
    where_detect_area('off');
    showPreview();
}
