﻿jQuery(function () {
    jQuery('div[data-modweb-designer-window]').each(function () {

        var containerUniqueID = jQuery(this).data('uniqueid');
        var thisWindowContainer = jQuery(this);
        var onClientCloseScript = jQuery(this).data('onclientclose');
        var width = jQuery.jqModweb.toInt(jQuery(this).data('width'));
        var height = jQuery.jqModweb.toInt(jQuery(this).data('height'));
        var parametersJsonString = jQuery(this).data('parameters');
        var title = jQuery(this).data('title');
        var backgroundColor = jQuery(this).css('backgroundColor');
        var borderColor = jQuery(this).css('borderColor');
        var borderWidth = jQuery(this).css('borderWidth');
        var showTitleBar = jQuery.jqModweb.toBool(jQuery(this).data('showtitlebar'));
        var preserveForm = jQuery.jqModweb.toBool(jQuery(this).data('preserveform'));
        var modal = jQuery.jqModweb.toBool(jQuery(this).data('modal'));

        var templatePath = jQuery(this).data('templatepath');
        var navigateUrl = jQuery(this).data('navigateurl');
        var container = jQuery(this);

        var win = null;


        // remove all the styling from the windows container object itself.
        // the styling here was only used to send data down to the client, but the jsModweb.AdvancedModalWindow class will handle all the styling itself.
        jQuery(this).css({
            border: 'none'
        });


        // find all controls that have this uniqueid for this window node and add a click event to them that will open the window.
        jQuery('[data-modweb-designer-window-trigger-open="' + containerUniqueID + '"]').each(function () {
            jQuery(this).click(function () {
                var o = {
                    params: jQuery.parseJSON(parametersJsonString)
                    , content: (typeof navigateUrl === 'undefined' && typeof templatePath === 'undefined') ? container : null
                };
                if (typeof navigateUrl !== 'undefined') o.url = navigateUrl;
                if (!jQuery.jqModweb.isNullOrEmpty(title)) o.title = title;
                if (typeof templatePath !== 'undefined') o.ascxPath = templatePath;
                if (backgroundColor !== '') o.backgroundColor = backgroundColor;
                if (borderColor !== '') o.borderColor = borderColor;
                if (borderWidth !== '') o.borderWidth = borderWidth;
                if (showTitleBar !== '') o.showTitleBar = showTitleBar;
                if (preserveForm !== '') o.preserveForm = preserveForm;
                if (modal !== '') o.modal = modal;
                if (width)
                    o.width = width;
                if (height)
                    o.height = height;
                win = new jsModweb.AdvancedModalWindow(o);
                win.open();
            });
        });


        // now do the same thing for close buttons.
        jQuery('[data-modweb-designer-window-trigger-close="' + containerUniqueID + '"]').each(function () {
            jQuery(this).click(function () {
                if (win) {
                    eval(onClientCloseScript);
                    win.close();
                }
            });
        });
    });
});
