var categoryHash = {},
    categoryList = [],
    manualData;

function cssInsert () {
    var head = document.getElementsByTagName('head')[0],
        css = document.createElement('link');
    css.type = 'text/css';
    css.rel = 'stylesheet';
    css.href = '/templates/we_hobby/css/manualPick.css';
    css.media = 'screen';
    head.appendChild(css);
}

function tableParse() {
    var el,
        table = document.getElementById('manuals'),
        td,
        tdCount,
        tdIndex,
        tr,
        trCount = 0,
        trIndex,
        row,
        rows = [],
        value;

    // find rows in table
    for (trIndex = 0; trIndex < table.childNodes.length; trIndex++) {
        tr = table.childNodes[trIndex];
        if (tr.nodeName.toLowerCase() == 'tr') {
            row = {
                category: 'unknown',
                model: 'unknown',
                name: 'unknown',
                url: 'unknown'
            };
            tdCount = 0;

            // find cells in row
            for (tdIndex = 0; tdIndex < tr.childNodes.length; tdIndex++) {
                td = tr.childNodes[tdIndex];
                if (td.nodeName.toLowerCase() == 'td') {
                    // parse data from cells
                    switch (tdCount) {
                    case 0:
                        row.category = td.innerHTML.replace(/&amp;/, '&');
                        break;
                    case 1:
                        row.model = trim(td.innerHTML.toLowerCase());
                        if (row.model.length === 0) {
                            row.model = 'unknown';
                        }
                        break;
                    case 2:
                        el = getDescendentByTag(td, 'a');
                        if (el !== null) {
                            row.url = '' + el.getAttribute('href');
                            row.name = el.childNodes[0].nodeValue;
                        }
                        break;
                    }
                    tdCount++;
                }
            }

            row.namePlus = row.model + ' -- ' + row.name;
            row.index = trCount;
            rows.push(row);
            trCount++;
        }
    }
    manualData = rows;
}

function categoriesList() {
    var categoryLast = '',
        i,
        j = 0,
        r;
    for (i = 0; i < manualData.length; i++) {
        r = manualData[i];
        if (r.category != categoryLast) {
            categoryList[j] = r.category;
            categoryHash[r.category] = j;
            categoryLast = r.category;
            j++;
        }
    }
}

function manualNameCompare(a, b) {
    if (manualData[a].namePlus > manualData[b].namePlus) {
        return 1;
    } else if (manualData[a].namePlus == manualData[b].namePlus) {
        return 0;
    } else {
        return -1;
    }
    // not reached
}

function formUpdate() {
    var category = document.forms.manualPick.category,
        catSel,
        i,
        j,
        manual = document.forms.manualPick.manual,
        manualList = [],
        r;

    // read selection
    if (category.value.length === 0 || category.value == 'placeholder') {
        catSel = 0;
    } else {
        catSel = parseInt(category.value);
    }

    // clear form
    for (i = category.options.length - 1; i >= 0; i--) {
        category.remove(i);
    }
    for (i = manual.options.length - 1; i >= 0; i--) {
        manual.remove(i);
    }

    // update form based on selection
    if (document.forms.manualPick.top.value == 'model') {
        category.className = 'invisible';
        for (i = 0; i < manualData.length; i++) {
            manualList.push(i);
        }
    } else {
        category.className = 'visible';
        category.options[0] = new Option(categoryList[catSel], catSel, true);
        j = 1;
        for (i = 0; i < categoryList.length; i++) {
            if (i != catSel) {
                category.options[j] = new Option(categoryList[i], i, false);
                j++;
            }
        }
        for (i = 0; i < manualData.length; i++) {
            r = manualData[i];
            if (categoryHash[r.category] == catSel) {
                manualList.push(i);
            }
        }
    }
    manualList = manualList.sort(manualNameCompare);
    for (i = 0; i < manualList.length; i++) {
        r = manualData[manualList[i]];
        manual.options[i] = new Option(r.namePlus, r.index);
    }
}

function manualLoad() {
    var i,
        index = document.forms.manualPick.manual.value,
        url = null;

    for (i = 0; i < manualData.length; i++) {
        if (manualData[i].index == index) {
            url = manualData[i].url;
            break;
        }
    }
    if (url !== null) {
        window.location = url;
    }
}

function quickJump() {
    var i,
        matches,
        model,
        r = null;
    matches = window.location.toString().match(/model=(.*)/);
    if (matches && matches.length > 0) {
        model = matches[1];
        if (model.length > 0) {
            for (i = 0; i < manualData.length; i++) {
                r = manualData[i];
                if (r.model == model) {
                    break;
                }
            }
        }
    }
    if (r !== null && r.model == model) {
        document.forms.manualPick.top.value = 'name';
        document.forms.manualPick.category.value = categoryHash[r.category];
        document.forms.manualPick.manual.value = r.index;
        formUpdate();
        window.location = r.url;
    }
}

function manualPickInitialize() {
    cssInsert();
    tableParse();
    categoriesList();
    formUpdate();
    quickJump();
}

window.addEvent('domready', function() {
    manualPickInitialize();
});

