var WxItemSelectID = 'WxItemSelect';


function WxGMapSelectionBoxHandleChangeID (inID){
    WxItemSelectID = inID;
}

function WxGMapSelectionBoxSelectByItemID (inItemID){
    var itemSelect = document.getElementById(WxItemSelectID);
    if(itemSelect != null){
        for(var j = 0; j < itemSelect.length; ++ j){
            if(itemSelect.options[j].value == WxGMap_selectedID){               
                itemSelect.selectedIndex = j;
            }
        }
    }
}

function WxGMapSelectionBoxRefreshSelections (){


    WxGMapSelectionBoxClear();
    var latLngItems = WxGMap_LastDownload;
    
    // add each station to the itemSelect box using AddStationToSelect to handle the distance calculation and sorting
    for(j = 0; j < latLngItems.length; ++ j){
        var curItem = latLngItems[j];
        WxGMapSelectionBoxAddItem(curItem);           
    }
    
    // s_selectedID keeps track of the selected station in spite of refreshes or if the station can not be seen at the time
    // it is also the stationID because it is the only constant throughout the interface no matter where it is located in the selectBox
    if(WxGMap_selectedID){
        var itemSelect = document.getElementById(WxItemSelectID);
        if(itemSelect != null){
            for(var j = 0; j < itemSelect.length; ++ j){
                if(itemSelect.options[j].value == WxGMap_selectedID){
                    
                    itemSelect.selectedIndex = j;
                }
            }
        }
    }
    
}

// clears the selection box

function WxGMapSelectionBoxClear(){
    var itemSelect = document.getElementById(WxItemSelectID);
    if(itemSelect != null){
        while(itemSelect.length > 0){
            itemSelect.remove(0);
        }
    }
}

function WxGMapSelectionBoxAddItem (inItem){
    var itemSelect = document.getElementById(WxItemSelectID);
    if(itemSelect != null){
        var elItemOption = document.createElement('option');

            inItem.Distance = (WxGMap.getCenter().distanceFrom(new GLatLng(inItem.Lat,inItem.Lng))/1609.344);
        
        WxGMap_items[inItem.ID].Distance = inItem.Distance;
        elItemOption.text = inItem.Title;

        elItemOption.value = inItem.ID;
        if(itemSelect.length == 0){
            try {
                itemSelect.add(elItemOption,null);
            } catch(ex) {
                itemSelect.add(elItemOption);
            }
        } else {
            var x = 0;
            // this is the sort operation, comparing the distances and finding the proper place to insert each station
            while (x < itemSelect.length && WxGMap_items[itemSelect.options[x].value].Distance < inItem.Distance){
                x++;
            }
            try{
                itemSelect.add(elItemOption,itemSelect[x]);
            }
            catch(ex){
                itemSelect.add(elItemOption,x);
            }
        }
        return itemSelect.length-1;
    } else {
        return -1;
    }
    
}

function WxGMapSelectionBoxOnSelectionChanged (){
    
    var itemSelect = document.getElementById(WxItemSelectID);
    if(itemSelect != null){
        var theID = itemSelect.options[itemSelect.selectedIndex].value;
        var curItem = WxGMap_items[theID];

        WxGMap_selectedID = theID;

        WxGMapCreateInfoWindowHTML(curItem,curItem.Marker);         
    }
}
