/// <reference path="LanxerLibrary.js" />
/// <reference path="Global_Page.js" />
/// <reference path="Global.js" />
/// <reference path="Global_AJAX.js" />

var loc_Profile_CurrentIndex = 0;
var loc_LastQuery = '';
var loc_Interval = false;
var list = null;
var tb = null;
var lastValue = false;

function loc_ProfileFindKeyDown(e)
{
    if(loc_LastQuery != '')
    {
        if(loc_LastQuery == tb.value)
            return;
    }
    
    if(e.keyCode == 13 && lastValue != tb.value)
    {
        list.innerHTML = aj_geo('typeFindLocation=' + tb.value);
        lastValue = tb.value;
        
        loc_Profile_CurrentIndex = 0;
        loc_LastQuery = tb.value;
    
        var toSelect = g('ps_LR' + loc_Profile_CurrentIndex);
        if(toSelect)
            toSelect.style.backgroundColor = '#DEECFA';
        
        list = g('SELECTLOCATION');
        list.style.display = 'block';
        
        return;
    }
    
    // loc_ProfileFindKeyDown_Exe(e);
    // loc_Interval = window.setInterval(loc_ProfileFindKeyDown_Exe , 300);
    pos_SetList();
    list.innerHTML = '<div style=padding:8px;>Type your city, state and press enter when finished...</div>';
}

var pos_SetListC = 0;
function pos_SetList()
{
    if(!list)
    {
        list = g('SELECTLOCATION');
        tb = g('loc_ProfileFindLocTB');
        var position = getElementPosition(tb);
        list.style.width = '400px';
        list.style.display = 'block';
        list.style.backgroundColor = '#f4f4f4';
        list.style.border = '1px solid #bbbbbb';
        list.style.top = position.top + tb.offsetHeight;
        list.style.left = position.left;
        if(IsEmptyText(list.innerHTML))
            list.innerHTML = '<div style=padding:8px;>Type your city and press enter when finished...</div>';
    }
}

function loc_ProfileFindKeyDown_Exe()
{
  //  window.clearInterval(loc_Interval);
    
    if(IsEmptyText(tb.value))
    {   
        list.innerHTML = '<div style=padding:8px;>Type your city followed by country if needed...</div>';
        return;
    }
    
    list.innerHTML = aj_geo('typeFindLocation=' + tb.value);
    loc_Profile_CurrentIndex = 0;
    loc_LastQuery = tb.value;
    
    var toSelect = g('ps_LR' + loc_Profile_CurrentIndex);
    if(toSelect)
        toSelect.style.backgroundColor = '#DEECFA';
}

function loc_ProfileFindArrowListner(e)
{
    var tb = g('loc_ProfileFindLocTB');
    var levent = e ? e : event;
    
    if(levent.keyCode == 38) // up
    {
        if(loc_Profile_CurrentIndex > 0)
            loc_Profile_CurrentIndex -= 1;
        else
        {
            loc_Profile_CurrentIndex = -1;
            pos_Unselect();
            return;
        }
    }
    else if(levent.keyCode == 40) // down
    {
        if(g('ps_LR' + (loc_Profile_CurrentIndex + 1)))
            loc_Profile_CurrentIndex += 1;
        else
        {
            loc_Profile_CurrentIndex += 1;
            pos_Unselect();
            return;
        }
    }
    else if(levent.keyCode == 13) // enter
    {
        pos_SelectItem(loc_Profile_CurrentIndex,true);
    }
    
    pos_Unselect();
    
    var toSelect = g('ps_LR' + loc_Profile_CurrentIndex);
    if(toSelect)
        toSelect.style.backgroundColor = '#DEECFA';
   
}

var pos_SelectItemC = 0;
function pos_SelectItem(index, fromEnter)
{
    if(g('ps_LRHID' + index))
    {
        g('loc_DisplaySelectedLocation').innerHTML =  aj_geo('typeSubmitLocation=' + g('ps_LRHID' + index).value + '&workingItem=' + g('loc_SelectLocationWorkingItem').value);
        visTog('loc_SelectLocationArea' , 'loc_DisplaySelectedLocation');
        
        if(fromEnter)
            pos_OnBlur();
    }
}

var pos_OnBlurInterval = false;
function pos_OnBlur() // must do this on an interval or else it clears before doing firing select item
{
    pos_OnBlurInterval = window.setTimeout(pos_OnBlur_Do , 120);
}

function pos_OnBlur_Do()
{
    if(pos_OnBlurInterval)
    {
        window.clearTimeout(pos_OnBlurInterval);
    }
    
    if(list)
    {
        list.style.display = 'none';
        list = null;
    }   
}

function pos_OnFocus()
{
    pos_SetList();
}

function pos_ItemOver(index)
{
    loc_Profile_CurrentIndex = index;
    var doUnselect=false;
    for(var i = 0; (doUnselect=g('ps_LR' + i)); i++)
        doUnselect.style.backgroundColor = '';
    
    var toSelect = g('ps_LR' + loc_Profile_CurrentIndex);
    if(toSelect)
        toSelect.style.backgroundColor = '#DEECFA';
}

function pos_ItemOut(index)
{
    pos_Unselect();
}

function pos_Unselect()
{
 var doUnselect=false;
    for(var i = 0; (doUnselect=g('ps_LR' + i)); i++)
        doUnselect.style.backgroundColor = '';
}