﻿//init the dialog panel
function initDialog()
{
    $("#viewCodePanel").dialog({
        autoOpen: false,
        modal: true,
        width: 700,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        }
    });
}

$(function() {
    initDialog();
});

function showCode(dictObj) {
   
    $("#urlCode").html($.base64Decode(dictObj.urlCode));
    if (dictObj.categories == "*")
        dictObj.categories = "* (Any)";
    $("#categories").html($.base64Decode(dictObj.categories));
    $("#filterCode").html($.base64Decode(dictObj.filterCode));
    $("#viewCodePanel").dialog('open');
}

function dictInfo(dictName, subCatName, subCatValue, urlCode, filterCode, postData, expertMode,
                            contributor, createSource, createDate, urlRegex) {
    this.dictName = dictName;
    this.subCatName = subCatName;
    this.subCatValue = subCatValue;
    this.urlCode = urlCode;
    this.filterCode = filterCode;
    this.postData = postData;
    this.expertMode = parseInt(expertMode);
    this.contributor = contributor;
    this.createSource = createSource;
    this.createDate = parseInt(createDate);
    this.urlRegex = urlRegex;
}

function installDictionaries(dictJson) {

    var arrDictObj = [];
    for (var i = 0; i < dictJson.length; i++) {
        var dictObj = new dictInfo($.base64Decode(dictJson[i].dictName),
                                    $.base64Decode(dictJson[i].subCatName),
                                    $.base64Decode(dictJson[i].subCatValue),
                                    $.base64Decode(dictJson[i].urlCode),
                                    $.base64Decode(dictJson[i].filterCode),
                                    $.base64Decode(dictJson[i].postData),
                                    $.base64Decode(dictJson[i].expertMode),
                                    $.base64Decode(dictJson[i].contributor),
                                    $.base64Decode(dictJson[i].createSource),
                                    $.base64Decode(dictJson[i].createDate),
                                    $.base64Decode(dictJson[i].urlRegex));
        arrDictObj.push(dictObj);
    }
    var strCode = $.toJSON(arrDictObj);

    InvokePanel(strCode);
}

function InvokePanel(strCode) {
    if ('createEvent' in document) {
        var element = document.createElement('div');
        element.style.display = "none";
        element.innerHTML = strCode;
        document.documentElement.appendChild(element);

        var evt = document.createEvent('Events');
        evt.initEvent('DictionaryTipAddEvent', true, false);
        element.dispatchEvent(evt);
        document.documentElement.removeChild(element);
    }
}
