﻿function togglePanel(togglePanelId, stateFieldId) {
    $('#' + togglePanelId).slideToggle('slow', function () {
        if ($(this).is(':hidden')) {
            $('#' + stateFieldId).val(0);
        }
        else {
            $('#' + stateFieldId).val(1);
        }
    });
}
var indesignparams;
function InDesignParams(expandParam, expandDisplayParam, groupParam, rotateParam, featuresParam, staticFeaturesParam, mergingParam, groupingMethod) {
    this.extractParam = function (selector, type, print) {
        var v = $('#' + selector + '_tblContainer td.detailfieldValue ' + type).val();
        if (v == undefined || v == null) { v = ""; }
        var i = v.indexOf(".");
        if (!isNaN(i) && i >= 0) { v = v.substring(i + 1); }
        return v;
    };
    this.extractBool = function (selector) { return $('#' + selector + '_tblContainer ' + 'input[type="checkbox"]').is(':checked'); };
    this.extractSelect = function (selector) { return this.extractParam(selector, 'select', false); };
    this.toString = function () {
        var out = "";
        var temp = this.extractSelect(this.expandParam);
        out += "Expand(" + temp + ");";
        if (out !== "") {
            temp = this.extractSelect(this.expandDisplayParam);
            out += "ExpandDisplay(" + temp + ");";
        }
        temp = this.extractSelect(this.groupParam);
        out += "Group(" + temp + ");";
        temp = this.extractSelect(this.rotateParam);
        out += "Rotate(" + temp + ");";
        var features = [];
        var mergeOptions = [];
        for (var i in this.featuresParam) {
            temp = this.extractSelect(this.featuresParam[i]);
            if (temp != "") {
                features.push(temp);
                var temp2 = this.extractBool(this.mergingParam[i]);
                if (temp2 != "") { mergeOptions.push(temp + '=' + String(temp2)); }
            }
        }
        var staticFeatures = [];
        for (var i in this.staticFeaturesParam) {
            temp = this.extractSelect(this.staticFeaturesParam[i]);
            if (temp != "") { staticFeatures.push(temp); }
        }
        out += "Features(" + features.join() + ");";
        out += "StaticFeatures(" + staticFeatures.join() + ");";
        out += "Merging(" + mergeOptions.join() + ");";
        return out;
    };
    this.expandParam = expandParam;
    this.expandDisplayParam = expandDisplayParam;
    this.groupParam = groupParam;
    this.rotateParam = rotateParam;
    this.featuresParam = featuresParam;
    this.staticFeaturesParam = staticFeaturesParam;
    this.mergingParam = mergingParam;
    this.groupingMethod = groupingMethod;
}

function getDialog() {
    var $dialog = jQuery('<div/>', {
            id: '_dialog',
            style: 'display: block; border: 1px solid black; padding: 2px; border-radius: 5px 5px 5px 5px;' + 
                ' background: none repeat scroll 0px 0px rgb(201, 201, 201); /*width: 400px; height: 200px;*/ overflow: auto;',
            click: function() { jQuery(this).hide(); }
        })
        .append(jQuery('<span style="text-align: center; border: 1px solid black; display: block; padding: 2px; margin: 2px; border-radius: 5px 5px 5px 5px;">Preview</span>'))
        .append(jQuery('<div id="_previewDiv"></div>'));
    var $table = jQuery('<table style="margin: auto; height: 100%;"><tr><td valign="middle" align="center"></td></tr></table>');
    var $dialogBg = jQuery('<div/>', {
            id: '_dialogBg',
            style: 'display: none; top: 0; bottom: 0; left: 0; right: 0; position: absolute; z-index: 1000; background: rgba(0, 0, 0, 0.6);',
            click: function() { jQuery(this).hide().remove(); }
        }).append($table).appendTo("body").find('td').append($dialog);
    return $dialog.find('#_previewDiv');
}

function preview(articlesAsmxUrl, catId, suppAid, indesignparams) {
    //alert(indesignparams.toString());
    try {
        var para = indesignparams.toString();
        $('img#loadingIndicator').show();
        //$('div#previewDiv')
        getDialog().load(
            url = articlesAsmxUrl + '/GetArticleXML',
            data = {
                catalogID: catId,
                articleID: suppAid,
                columnName: '',
                variantField: (indesignparams.groupingMethod === undefined) ? '' : indesignparams.groupingMethod,
                //'ART,ARTICLE_GROUP_MAP', //"<%= /*Datastore.AppSettings.SystemConfig.VariantBlockOriginalName + "." + */Datastore.AppSettings.SystemConfig.VariantBlockParameter %>", 
                p1: '', v1: '', p2: '', v2: '',
                p3: '', v3: '', p4: '', v4: '',
                p5: '', v5: '', p6: '', v6: '',
                p7: '', v7: '', p8: '', v8: '',
                p9: '', v9: '', p10: '', v10: /*$('input#previewArgs').val() + ';'*/para + 'TableOnly();'// + 'ShowStaticInfo();'
            },
            complete = function (responseText, textStatus, xhr) {
                $("div#_previewDiv table[fname]").each(function () {
                    $(this).prepend('<tr><td colspan=' + $(this).attr('cols') + '>' + $(this).attr('fname') + '=' + $(this).attr('fvalue') + '</td></tr>');
                });
                $("div#_previewDiv table").attr({ cellspacing: '1', cellpadding: '1' }).css('background', 'black').css('margin-top', '2px')
                    .find("td").css('background', 'none repeat scroll 0 0 #C9C9C9');
                    //.css('border', '1px solid black').css('padding', '1px');
                $("div#_previewDiv supplier_aids").attr('style', 'display: none;');
                $('img#loadingIndicator').hide();

                var _win = jQuery(window); //jQuery('#subContent').first();
                var _dia = jQuery('div#_dialog div#_previewDiv').first();
                    

                $('div#_dialogBg').show();
                _dia.css('overflow', 'auto');
                if (_dia.width() >= (_win.width())) { _dia.width(_win.width() - 1); }
                if (_dia.height() >= (_win.height())) { _dia.height(_win.height() - 1); }
                return true;
            });
//              complete = function (responseText, textStatus, xhr) {
//              $("div#previewDiv table[fname]").each(function () {
//                  $(this).prepend('<tr><td colspan=' + $(this).attr('cols') + '>' + $(this).attr('fname') + '=' + $(this).attr('fvalue') + '</td></tr>');
//              });
//              $("div#previewDiv table").attr({ cellspacing: '0', cellpadding: '0' }).find("td").css('border', '1px solid black').css('padding', '1px');
//              $("div#previewDiv supplier_aids").attr('style', 'display: none;');
//              $('img#loadingIndicator').hide();

//              var min = function (x, y) { return x < y ? x : y; }
//              var win_width = jQuery('#subContent').first().width();
//              var div_width = jQuery('div#dialog div').first().width();
//              jQuery('div#dialog div').first().css('width', win_width - 10);
//              $('div#dialog').show();
//              return true;
//          });
    }
    catch (err) {
        alert('Error. Cannot load preview.');
    }
    return false;
}