var typeListData = [];
var elemKeyword = null;
var elemMainType = null;
var elemSubType = null;
var elemTypeSelect = null;

function startSearch()
{
    var keyword = elemKeyword.value.trim();
    var mainTypeId = elemMainType.value;
    var mainTypeText = elemMainType.$("option[value=" + mainTypeId + "]").text;
    var subTypeId = elemSubType.value;
    var subTypeText = elemSubType ? elemSubType.$("option[value=" + subTypeId + "]").text : "";
	
    view.doAdvancedSearch(keyword,mainTypeId,mainTypeText,subTypeId,subTypeText);
//    view.doAdvancedSearch(
//    {
//        "keyword": keyword,
//        "mainTypeId": mainTypeId,
//        "mainTypeText": mainTypeText,
//        "subTypeId": subTypeId,
//        "subTypeText": subTypeText
//    });
}

function mainTypeChange()
{
    var subTypeList = [];
    var mainType = elemMainType.value;
    for (var i = 0; i < typeListData.length; i++) 
    {
        if (typeListData[i]["id"] == mainType) 
        {
            if (typeListData[i]["element"] && typeListData[i]["element"].length) 
                subTypeList = typeListData[i]["element"];
            break;
        }
    }
    
    elemSubType && elemSubType.remove();
    
    var html = "";
    if (mainType == "0") 
    {
        html += gameListPanel.getHTML(subTypeList, "gSubType", 0);
    }
    else 
    {
        if (subTypeList.length && subTypeList[0]["name"]) 
        {
            html += "<select class='sel' id='gSubType'>";
            for (var a = 0; a < subTypeList.length; a++) 
                html += "<option value='" + subTypeList[a]["id"] + "'>" + subTypeList[a]["name"] + "</option>";
            html += "</select>";
        }
    }
    
    elemTypeSelect.append(html);
    elemSubType = self.$("#gSubType");
}

//for c++
function initGameList(listData)
{
    typeListData = listData;
    var defaultSubData = [];
    
    var htmla = "<select class='sel' id='gMainType'>";
    for (var i = 0; i < typeListData.length; i++) 
    {
        htmla += "<option value='" + typeListData[i]["id"] + "'>" + typeListData[i]["type"] + "</option>";
        if (typeListData[i]["id"] == "0" && typeListData[i]["element"] && typeListData[i]["element"].length) 
            defaultSubData = typeListData[i]["element"];
    }
    htmla += "</select>";
    
    var htmlb = gameListPanel.getHTML(defaultSubData, "gSubType", 0);
    
    elemTypeSelect.html = htmla + htmlb;
    
    elemMainType = self.$("#gMainType");
    elemSubType = self.$("#gSubType");
    
    elemMainType.onValueChanged = mainTypeChange;
}

self.ready = function()
{
    elemKeyword = self.$("#keyword");
    elemTypeSelect = self.$("#typeSelect");
    elemKeyword.onKey = function(e)
    {
        if (e.type == Event.KEY_DOWN && e.keyCode == 13) 
            startSearch();
    };
    
    //initGameList(typeListTestData);
    view.onLoadhtmlComplete();
};