var search_string = null;

function isDefined(type) {
    return type != 'undefined' && type != 'unknown';
}

function getDOMElementById(id) {
    if (isDefined(typeof document.getElementById)) {
        return document.getElementById(id);
    } else if (isDefined(typeof document.all)) {
        return document.all[id];
    } else {
        return null;
    }
}

function clearElement(_id){
    var e = getDOMElementById(_id);
    e.innerHTML = "";
}

function get_search_items(data, words){
    var count = 0;
    var found = new Array();
    var match = null;
    for (var r = 0, len = data['ids'].length; r < len; r++){
        tosearch = data['shorttxts'][r] + " " + data['longtxts'][r] + " "  + data['ids'][r];
        tosearch = tosearch.toLowerCase();
        match = false;
        for (var i = 0, wlen = words.length; i < wlen; i++){
            if (tosearch.indexOf(words[i])!=-1){
                match = true;
            }else{
                match = false;
                break;
            }
        }
        if(match){
            found[count] = r;
            count++;
        }
    }

    return found;
}

function get_expand_items(data, words){
    var count = 0;
    var found = new Array();
    for (var r = 0, len = data['ids'].length; r < len; r++){
        tosearch = data['ids'][r].toLowerCase();
        if (tosearch.indexOf(words)===0){
            found[count] = r;
            count++;
        }
    }

    return found;
}

function generate_html(found, data, type){
    var index = null;
    var html = null;

    html = "<table id=\"items\">";
    for (var k = 0, len = found.length; k < len; k++){
        index = found[k];
        html += "<tr>";
        // first column
        html += "<td>";
        //onclick=\"tv('"+ type + data['ids'][index] + "');\" href=\"#" + data['ids'][index] + "\"
        //href=\"javascript:fetchStatus('call.php?" + data['ids'][index] +"');\"
        //html += "<a id=\"" + data['ids'][index] + "\" class=\"id\" href=\"javascript:fetchStatus('" + data['ids'][index] + "');\">" + data['ids'][index] + "</a>";
        html += "<a id=\"" + data['ids'][index] + "\" class=\"id\" href=\"javascript:tv('"+ type + data['ids'][index] + "');\">" + data['ids'][index] + "</a>";
        html += "</td>";
        // last column
        html += "<td>";
        htmlstr = "<a href=\"javascript:tv('"+ type + data['ids'][index] + "');\">" + "<span class=\"shorttext\"\">" + data['shorttxts'][index] + "</span><span class=\"price\"\">" + data['itemprice'][index] + "</span></a>";
        htmlstr += "<div class=\"detail\"><span class=\"left\">" + data['quantity'][index] + "</span><span class=\"right\">" + data['unitprice'][index] + "</span></div>";
        var img_str = '';
        if(data['images'][index] !== 0){
            img_str = '<div class="images" id="images'+ index +'"><a href="javascript:showImages('+ index +');">Bilder laden</a></div>';
        }
        htmlstr += "<div class=\"longtext\" id=\""+ type + data['ids'][index] + "\" style=\"display: none;\">" + data['longtxts'][index] + img_str + "</div>";
        html += htmlstr;
        html += "</td>";
        html += "</tr>";
    }
    html += "</table>";

    return html;
}

function filter2(phrase, _id, type){
    var words = null;
    var data = get_array();
    var matching_items = new Array();

    if(type == 'search'){
        words = phrase.toLowerCase().split(" ");
        matching_items = get_search_items(data, words);
    }else{
        words = phrase.toLowerCase();
        matching_items = get_expand_items(data, words);
    }

    var div = getDOMElementById(_id);
    var items_length = matching_items.length;
    if(type == 'search' && items_length === 0){
        div.innerHTML = "<div id=\"message\">Ihre Suche ergab keinen Treffer</div>";
    }else if(type == 'search' && items_length > 50){
        div.innerHTML = "<div id=\"message\">Ihre Suche ergab mehr als 50 Treffer.<br/>Bitte verfeinern Sie Ihre Suchanfrage.</div>";
    }else{
        div.innerHTML = generate_html(matching_items, data, type);
    }
}

function trigger_search(_phrase, _id){
    if(search_string != _phrase){
        search_string = _phrase;
        clearElement(_id);
        filter2(_phrase, _id, 'search');
    }
}

function myKeyDown(event, _phrase, _id){
    if(event.keyCode == 13) {
        trigger_search(_phrase, _id);
    }
}

function tv(id){
    var e = getDOMElementById(id);
    if(e.style.display == 'block'){
        e.style.display = 'none';
    }else{
        e.style.display = 'block';
    }
}

function showItemsTable(id, oz){
    var e = getDOMElementById(id);
    clearElement(id);
    if(e.style.display == 'block'){
        e.style.display = 'none';
    }else{
        filter2(oz, id, 'expand');
        e.style.display = 'block';
    }
}

function showImages(index){
    var data = get_array();
    var htmlstr = '';
    htmlstr += '<img src="' + get_url() + '/oz:' + data['ids'][index] + '"/>';
    var ele = getDOMElementById('images' +index);
    ele.innerHTML = htmlstr;
}

function showTab(id){
    var e = getDOMElementById('info');
    e.style.display = 'none';
    e = getDOMElementById('search');
    e.style.display = 'none';
    e = getDOMElementById('expand');
    e.style.display = 'none';

    var es = getDOMElementById(id);
    es.style.display = 'block';
}

function fontsizer(dir){
    if(typeof(fontsize) == "undefined"){
        fontsize = 100;
    }
    if(dir == "inc"){
        fontsize += 10;
    }else{
        fontsize -= 10;
    }
    getDOMElementById("content").style.fontSize = fontsize + "%";
}
