﻿var documents = null, categoryElement = null, productElement = null, loadingElement = null;

function initialize(category, context) {
    $(document).ready(function() {
        categoryElement = document.getElementById("categoryList");
        productElement = document.getElementById("productList");
        loadingElement = document.getElementById("loading");

        if (category)
            loadProducts(category, context);
        else
            categoryElement.disabled = false;

        // add MNP Hack
        autoNavigatorLength();
    });
}

function onSuccess(result, context) {
    documents = eval(result);
    var length = (documents) ? documents.length : 0;

    for (var i = 0; i < length; i++) {
        productElement.options[i + 1] = new Option(documents[i].Product, i);

        // if pass specified Product.Id, the product will be selected in dropdown list 
        if (context && documents[i].Id == context) {
            productElement.options[i + 1].selected = "selected";
            loadDocuments(i + 1);
        }
    }

    processing(false);
}
function onFailed() {
    //TODO:
    alert("failed to invoke WebService");

    processing(false);
}

// category: Mouse, Keyboard, Sets, Gaming ...
// context: here indicates Product.Id, its value = SKU
function loadProducts(category, context) {
    processing(true);
    // invoke Documentation WebService    
    Microsoft.Hardware.Services.Documentation.GetDocumentationForRetail(category, onSuccess, onFailed, context);
}

function loadDocuments(index) {
    var cellArray = [
                ["QSG", document.getElementById("QSG_Cell"), ""],
                ["PG", document.getElementById("PG_Cell"), ""],
                ["TDS", document.getElementById("TDS_Cell"), ""],
                ["SS", document.getElementById("SS_Cell"), ""]
            ];

    if (index < 1) {
        for (var j = 0; j < cellArray.length; j++) {
            if (cellArray[j][1]) cellArray[j][1].innerHTML = "";
        }
        return;
    }

    if (documents && documents.length >= index) {
        var files = documents[index - 1].Files;
        for (var i = 0; i < files.length; i++) {
            for (var j = 0; j < cellArray.length; j++) {
                if (files[i].Type == cellArray[j][0])
                    cellArray[j][2] += buildXMLForFile(files[i]);
            }
        }

        for (var i = 0; i < cellArray.length; i++) {
            if (cellArray[i][1]) cellArray[i][1].innerHTML = cellArray[i][2];
        }
    }
}

function buildXMLForFile(file) {
    return String.format("<div><a href='{0}' target='_blank'>{1} file{2}</a></div>", file.Url, file.Format, (file.Size == null ? "" : " (" + file.Size + ")"));
}
function processing(inProcessing) {
    if (inProcessing) {
        loadingElement.style.display = "block";
        categoryElement.disabled = true;
        productElement.disabled = true;
        productElement.length = 1;
        loadDocuments(0);
    }
    else {
        loadingElement.style.display = "none";
        categoryElement.disabled = false;
        productElement.disabled = (productElement.options.length < 2) ? true : false;
    }
}
