function getElementPosition(elemID)
{
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined') {
	    offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	return {left:offsetLeft, top:offsetTop};
}
jQuery(document).ready(function() {
    jQuery('#divInfoSucUnder').css('background-color', '#FFFFFF').css('height', '100%').corner('round 8px').parent().css('padding', '4px').corner('round 10px');
	jQuery('#resultDiv').css('height', '80%');
	positionSugDiv();
});
function positionSugDiv()
{
    jQuery('#divInfoSug').css('top',eval(getElementPosition('tx_ltgformsv2_pi1__city').top + 15) + 'px');
	jQuery('#divInfoSug').css('left',eval(getElementPosition('tx_ltgformsv2_pi1__city').left - 110) + 'px');
	setTimeout('positionSugDiv()', 500);
	jQuery('#tx_ltgformsv2_pi1__city').keyup(function() {
	    currentLocation = jQuery(this).val();
    })
}
var currentLocation;
function getLocation(jData) 
{
    if (jData == null) {
        return;
    }
    var html = "<ul class='listSuggestions'>";
    var geonames = jData.geonames;
    for (i=0;i< geonames.length;i++) {
        html += "<li onclick=loadCity('" + geonames[i].name + "','" + geonames[i].countryName + "')>" + geonames[i].name + ', ' + geonames[i].countryName + '</li>';
    }
    html += '</ul>';
    jQuery('#divInfoSucUnder #resultDiv').attr('innerHTML', html);
}
function loadCity(city, country)
{
    jQuery('#tx_ltgformsv2_pi1__city').val(city);
    jQuery('#tx_ltgformsv2_pi1__country').val(country);
    jQuery('#tx_ltgformsv2_pi1__city').change();
    jQuery('#divInfoSug').slideUp('normal');
}
function searchCity() 
{
    request = 'http://ws.geonames.org/searchJSON?q=' +  encodeURIComponent(currentLocation)+ '&maxRows=10&callback=getLocation';
    aObj = new JSONscriptRequest(request);
    aObj.buildScriptTag();
    aObj.addScriptTag();
}
function odump(object, depth, max)
{
    depth = depth || 0;
    max = max || 2;
    if (depth > max)
        return false;
    var indent = "";
    for (var i = 0; i < depth; i++)
        indent += "  ";
    var output = "";  
    for (var key in object){
        output += "\n" + indent + key + ": ";
        switch (typeof object[key]){
            case "object": output += odump(object[key], depth + 1, max); break;
            case "function": output += "function"; break;
            default: output += object[key]; break;        
         }
     }
     return output;
}
