var searchBoxList = new Array();

var simpleFiledsForCombobox = new Array();
var listFiledsForCombobox = new Array();

var alreadyConnected = false;

for (var j = 0; j < fields.simpleFields.length; j++)
{
    simpleFiledsForCombobox.push({"name":fields.simpleFields[j], "value":fields.simpleFields[j]});
}

for (var field in fields.listFields)
{
    listFiledsForCombobox.push({"name":field, "value":field});
}

var simpleStore = new dojo.data.ItemFileReadStore(
{data:{
    identifier:"name",
    items:simpleFiledsForCombobox
}});
var listStore = new dojo.data.ItemFileReadStore(
{data:{
    identifier:"name",
    items:listFiledsForCombobox
}});

function buildState()
{
    for (var x = 0; x < state.length; x++)
    {
        createSearchBox(state[x]);
    }
}

function createSearchBox(boxData, setFocus)
{
    var div = document.createElement("div");
    dojo.byId("searchContainer").appendChild(div);
    var searchBox;
    if (boxData.type == "GLOBAL")
    {
        searchBox = new aw.GlobalSearchBox(boxData, div);
    }
    else if (boxData.type == "FREETEXT")
    {
        searchBox = new aw.FreeTextSearchBox(boxData, div);
    }
    else if (boxData.type == "SIMPLELIST")
    {
        searchBox = new aw.ListSearchBox(dojo.mixin(boxData, {
            "allValues":fields.listFields[boxData.field],
            "changeButtonLabel":"++"
        }), div);
    }
    else if (boxData.type == "MULTILIST")
    {
        searchBox = new aw.ListSearchBox(dojo.mixin(boxData, {
            "allValues":fields.listFields[boxData.field],
            "changeButtonLabel":"++"
        }), div);
    }
    else if (boxData.type == "NAMESPACE")
    {
        searchBox = new aw.NamespaceSearchBox(dojo.mixin(boxData, {
            nameLabel:' név:',
            idLabel:',  id:',
            textLabel:'',
            title: 'névtér elem: '
        }), div);
    }

    dojo.connect(searchBox, "onEnter", "startSearch");

    var index = searchBoxList.length;
    dojo.connect(searchBox, "destroy", function()
    {
        searchBoxList[index] = null;
    });

    searchBoxList.push(searchBox);
    if (setFocus)
    {
        dojo.connect(dijit.byId("searchBoxDialog")._fadeOut, "onEnd", dojo.hitch(this, function()
        {
            searchBoxList[searchBoxList.length - 1].focus();
        }));
    }
}

function addSearchBox(eiszUrl)
{
    if (dojo.byId('searchBoxTypeGlobal').checked)
    {
        createSearchBox({
            "type" : "GLOBAL",
            "field" : "Minden mezőben",
            "value" : ""
        }, true);
    }
    else if (dojo.byId('searchBoxTypeFreeText').checked)
    {
        createSearchBox(
        {
            "type" : "FREETEXT",
            "field" : dijit.byId('searchBoxTypeFreeTextField').getValue(),
            "value" : ""
        }, true);

    }
    else if (dojo.byId('searchBoxTypeList').checked)
    {
        createSearchBox(
        {
            "type" : "SIMPLELIST",
            "field" : dijit.byId('searchBoxTypeListField').getValue(),
            "values" : []
        }, true);

    }
    else if (dojo.byId('searchBoxTypeNamespace').checked)
    {
        if (!alreadyConnected)
        {
            dojo.connect(dijit.byId('searchBoxDialog')._fadeOut, "onEnd", function()
            {
                setTimeout("openNamespaceSearcher('" + eiszUrl + "')", 50);
            });
            alreadyConnected = true;
        }
    }
}

function openNamespaceSearcher(eiszUrl)
{
    window.open('http://ndans1.nda.hu/index.php?eiszsearch=true&url=' + eiszUrl + '/namespaceDetailed.html', 'namespace', 'width=840,height=580,menubar=no,toolbar=no,status=yes,resizable=yes,scrollbars=yes');
}

function selectNamespaceEntityForDetailedSearch(id, name)
{
    createSearchBox(
    {
        "type" : "NAMESPACE",
        "text" : "",
        "name" : name,
        "identifier" : id
    }, true);
}


function startSearch()
{
    dijit.byId("submitButton").focus();
    setTimeout(submitForm, 0);
}

function submitForm()
{
    var ret = new Array();
    for (var i = 0; i < searchBoxList.length; i++)
    {
        var sb = searchBoxList[i];
        if (sb && sb != null)
        {
            ret.push(sb.getState());
        }
    }
//    console.debug(dojo.toJson(ret));
    dojo.byId("searchTerms").value = dojo.toJson(ret);
    dojo.byId("searchForm").submit();

}
dojo.addOnLoad(buildState);

