var pluginContainer, templateRow;
var dropPlugins, selectedNameCtl, configDataCtl;
var InputType = { "TextBox": 0, "TextArea": 1, "DropDownList": 2, "CheckBox": 3, "Password": 5 };

function SelectPlugin(pluginType) {
    ResetContainer($(dropPlugins).val(), pluginType);
}

function ResetContainer(name, pluginType) {
    if (pluginContainer.length == 0)
        return;

    $.each($(pluginContainer).find("[rowType=attributeContent]"), function(i, item) {
        $(item).remove();
    });

    if (name.length == 0)
        return;

    // 如果选择了插件，则根据选择的插件获取对应的配置信息
    $.ajax({
        url: "PluginHandler.aspx?type=" + pluginType + "&name=" + name + "&action=getmetadata",
        type: 'GET',
        dataType: 'xml',
        timeout: 10000,
        success: function(xml) {
            CreateContainer(xml);
        }
    });
}

function SelectPluginMessage(pluginType) {
    ResetContainerMessage($(dropPlugins).val(), pluginType);
}
function ResetContainerMessage(name, pluginType) {
    if (pluginContainer.length == 0)
        return;

    $.each($(pluginContainer).find("[rowType=attributeContent]"), function(i, item) {
        $(item).remove();
    });

    if (name.length == 0)
        return;

    // 如果选择了插件，则根据选择的插件获取对应的配置信息
    $.ajax({
        url: "PluginMessageHandler.aspx?type=" + pluginType + "&name=" + name + "&action=getmetadata",
        type: 'GET',
        dataType: 'xml',
        timeout: 10000,
        success: function(xml) {
            CreateContainer(xml);
        }
    });
}


function CreateContainer(meta) {
    var dataXml;
    var hasValue = false;

    if ($(configDataCtl).val().length > 0) {
        var s = $(configDataCtl).val().replace("<xml>", "<root>");
        s = s.replace("</xml>", "</root>");
        dataXml = $("<xml>" + s + "</xml>");
        hasValue = true;
    }

    $.each($(meta).find("att"), function(i, att) {
        var attributeRow = templateRow.clone();
        var t = attributeRow.html();
        t = t.replace("$Name$", $(att).attr("Name"));
        t = t.replace("$Description$", $(att).attr("Description"));

        var strInputInnerHtml = "";
        var strItem = "";
        var strData = "";

        if (hasValue) {
            strData = $(dataXml).find($(att).attr("Property")).text();
        }

        switch (Number($(att).attr("InputType"))) {
            case InputType.TextBox:
                strInputInnerHtml = "<input name='" + $(att).attr("Property") + "' id='" + $(att).attr("Property") + "' value='" + strData + "'\/>";
                break;

            case InputType.Password:
                strInputInnerHtml = "<input name='" + $(att).attr("Property") + "' type='password' id='" + $(att).attr("Property") + "' value='" + strData + "'\/>";
                break;

            case InputType.TextArea:
                strInputInnerHtml = "<textarea name='" + $(att).attr("Property") + "' id='" + $(att).attr("Property") + "'>" + strData + "<\/textarea>";
                break;

            case InputType.CheckBox:
                var check = strData;
                if (strData.toLowerCase() == "true") {
                    strData = "checked=checked";
                }

                strInputInnerHtml = "<input type='checkbox' onclick='this.value=this.checked;' name='" + $(att).attr("Property") + "' id='" + $(att).attr("Property") + "' " + strData + " value='" + check + "'\/>";
                break;

            case InputType.DropDownList:
                strInputInnerHtml = "<select name='" + $(att).attr("Property") + "' id='" + $(att).attr("Property") + "'>";
                $.each($(att).find("Options").find("Item"), function(j, item) {
                    if (strData == $(item).text())
                        strItem = "<Option selected='selected' value='" + $(item).text() + "'>" + $(item).text() + "<\/Option>";
                    else
                        strItem = "<Option value='" + $(item).text() + "'>" + $(item).text() + "<\/Option>";
                    strInputInnerHtml += strItem;
                });
                strInputInnerHtml += "<\/select>";
                break;

            default:
                break;
        }

        t = t.replace("$Input$", strInputInnerHtml);
        attributeRow.html(t);
        attributeRow.attr("rowType", "attributeContent");
        attributeRow.css('display', '');
        attributeRow.appendTo(pluginContainer);
    });
}