var restaurantList=new Array;
var restaurantBMList = new Array;

var restaurantIdInBM={};//ブックマークにあるすべてのお店ID
var restaurantSelected={};

function gridClickStart(){
    var wra = new ObjWrapper(grdList);
    var restaurantId = wra.getValueFromMultiData(wra.getSelectedIndex(),5);
    for (var idx in restaurantList){
         if (restaurantList[idx].id==restaurantId){
             restaurantSelected = restaurantList[idx];
             restaurantSelected.text = restaurantSelected.name;
             txtDetailedInfo.setValue(restaurantSelected.info);
             break;
         }
     }
}

function searchStart(){
	grdList.initTab();
    gMap.map.clearOverlays();
}

function addToBMStart(){
    var wra = new ObjWrapper(grdList);
    if (wra.getSelectedIndex()<0)
        return;

    var restaurantId = wra.getValueFromMultiData(wra.getSelectedIndex(),5);
    //すでに選択されたかどうかチェック
    if (typeof restaurantIdInBM[restaurantId]!="undefined"){
        alert("すでにブックマークに存在します。");
        return;//すでに選択された
    }
    var restaurantToAdd = grdList.tabData[wra.getSelectedIndex()];
    grdList_BM.tabData.push(restaurantToAdd);
    var wraBM = new ObjWrapper(grdList_BM);
    wraBM.setMultiValue(grdList_BM.tabData);

    for (var idx in restaurantList){
         if (restaurantList[idx].id==restaurantId){
             restaurantBMList.push(restaurantList[idx]);
         }
    }
    restaurantIdInBM[restaurantId]="1";

	alert("ブックマークに追加しました。");
}

function gridBMClickStart(){
    var wra = new ObjWrapper(grdList_BM);
    var restaurantId = wra.getValueFromMultiData(wra.getSelectedIndex(),5);
    for (var idx in restaurantBMList){
         if (restaurantBMList[idx].id==restaurantId){
             restaurantSelected = restaurantBMList[idx];
             restaurantSelected.text = restaurantSelected.name;
             txtDetailedInfo_BM.setValue(restaurantSelected.info);
             break;
         }
     }
}

/**
 * ブックマークから店情報を削除
 */
function deleteFromBMStart(){
    var wra = new ObjWrapper(grdList_BM);
    var selectedIndices = wra.getSelectedIndexes();
    var gridData = grdList_BM.tabData;
    for (var i=selectedIndices.length-1;i>=0;i--){
        var index = selectedIndices[i];
        var restaurantId = gridData[index][5];//5番目データはID
        var idxInList = null;
        for (var idx in restaurantBMList){
            if (restaurantBMList[idx].id==restaurantId){
                idxInList = idx;
            }
        }
        delete restaurantIdInBM[restaurantId]
        restaurantBMList.splice(idxInList,1);
        gridData.splice(index,1);
    }
    wra.setMultiValue(gridData);

    gMap_BM.map.clearOverlays();
    txtDetailedInfo_BM.setValue("");
}
