// class definition

function XMLUtilConvertXML2String(parentNode)
{
    //console.log('XMLUtilConvertXML2String > nodeName: ' + parentNode.nodeName + ' / nodeType: ' + parentNode.nodeType);
    var content = '';
    if(parentNode.nodeType == 4)
    { 
        // CDATA node
        return parentNode.nodeValue;
    }
    else if(parentNode.nodeType == 3)
    {
        // text node
        return parentNode.data;
    }
    else if(parentNode.nodeType == 1)
    {
        // element node
        content += '<' + parentNode.nodeName;
        for(var j = 0; j < parentNode.attributes.length; j++)
        {
            content += ' ' + parentNode.attributes[j].nodeName + '="' + parentNode.attributes[j].nodeValue + '"';
        }
        content += '>';
        for(var i = 0; i < parentNode.childNodes.length; i++)
        {
            content += this.convertXML2String(parentNode.childNodes[i]);
        }
        content += '</' + parentNode.nodeName + '>';
    }
    return content;
}

function XMLUtil()
{
    this.convertXML2String = XMLUtilConvertXML2String;
}

function SearchSnippetGetContentAsString(parentNode)
{
    if(parentNode.xml != undefined)
    {
        return SearchSnippetGetContentAsStringIE(parentNode);
    }
    else
    {
        return SearchSnippetGetContentAsStringFirefox(parentNode);
    }
}

function SearchSnippetGetContentAsStringIE(parentNode)
{
    var content = '';
    for(var i=0 ; i<parentNode.childNodes.length ; i++ )
    {
        var n = parentNode.childNodes[i];
        if (n.nodeType == 4)
        {
            // CDATA node
            content += n.nodeValue;
        }
        else
        {
          content += n.xml;
        }
    }
    return content;
}

function SearchSnippetGetContentAsStringFirefox(parentNode)
{
    var content = '';
    var xmlSerializer = new XMLUtil();
    
    for(var i=0 ; i<parentNode.childNodes.length ; i++ )
    {
        content += xmlSerializer.convertXML2String(parentNode.childNodes[i]);
    }

    return content;
}

function SearchSnippetUpdateSearchParameterList()
{
    for(var i = 0; i < searchParameterList.length; i++)
    {
        if(document.getElementById(searchParameterList[i].elementID))
        {
            searchParameterList[i].value = document.getElementById(searchParameterList[i].elementID).value;
        }
    }
}

function SearchSnippetRequestSnippet(addLink)
{
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    httpRequest.onreadystatechange = SearchSnippetProcessSnippet;

    SearchSnippetUpdateSearchParameterList();
    
    var searchParameter = '?mode=snippet';
    for(var i = 0; i < searchParameterList.length; i++)
    {
        searchParameter += '&' + searchParameterList[i].name + '=' + searchParameterList[i].value;
    }

	if (addLink && addLink != "") {
		searchParameter += '&' + addLink;
	}

    if(document.getElementById(loadingImageContainer))
    {
        document.getElementById(loadingImageContainer).innerHTML = '<div style="text-align: center;"><img src="images/DE_loco/Loco_dranbleiben.gif" alt="Bitte warten..." /></div>';
    }

    httpRequest.open('GET', baseURL + searchParameter, true);
    httpRequest.send(null);
}

function SearchSnippetProcessSnippet()
{
    if((httpRequest.readyState == 4) && (httpRequest.status == 200))
    {
        var xml = httpRequest.responseXML;
        var snippetBox = xml.getElementsByTagName('snippetBox').item(0);
        if(snippetBox)
        {
            var snippet = snippetBox.firstChild;
            while(snippet)
            {
                if(snippet.nodeName == 'snippet')
                {
                    var type = snippet.getAttribute('type');
                    var id = snippet.getAttribute('id');
                    if(type == 'element')
                    {
                        if(document.getElementById(id))
                        {
                            var content = SearchSnippetGetContentAsString(snippet);
                            if(content == '')
                            {
                                document.getElementById(id).style.display = 'none';
                            }
                            else if (document.getElementById(id).style.display == 'none')
                            {
                                document.getElementById(id).style.display = 'block';
                            }
                            document.getElementById(id).innerHTML = content;
                        }
                    }
                }
                else if(snippet.nodeName == 'hyperlink')
                {
                    if(snippet.firstChild)
                    {
                        document.location.href = snippet.firstChild.data;
                    }
                }
                snippet = snippet.nextSibling;
            }
        }
    }
    else
    {
    }
}

var baseURL = '';
var searchParameterList = Array();
var loadingImageContainer = '';
var httpRequest = null;

