﻿if (typeof (jsModweb) === "undefined") jsModweb = {};
if (typeof (jsModweb.admin) === "undefined") jsModweb.admin = {};
if (typeof (jsModweb.admin.editing) === "undefined") jsModweb.admin.editing = {};
if (typeof (jsModweb.admin.ajax) === "undefined") jsModweb.admin.ajax = {};
//if (typeof (jsModweb.admin.users) === "undefined") jsModweb.admin.users = {};
jsModweb = {
    debug:
    {
        write: function (o) {
            if (!jQuery.jqModweb.debug._consoleLog('DEBUG: ' + o)) {
                jQuery.jqModweb.debug._messageBox('DEBUG: ' + o);
            }
        },
        log: function (o) {
            jQuery.jqModweb.debug._consoleLog('LOG: ' + o);
        },
        warning: function (o) {
            jQuery.jqModweb.debug._consoleLog('WARNING: ' + o);
        },
        validation: function (o) {
            jQuery.jqModweb.debug._consoleLog(o);
            jQuery.jqModweb.debug._messageBox(o);
        },
        error: function (o) {
            jQuery.jqModweb.debug._consoleLog('ERROR: ' + o);
            jQuery.jqModweb.debug._messageBox('ERROR: ' + o);
        },
        _consoleLog: function (o) {
            var hasConsole = typeof console !== "undefined";
            if (hasConsole == true) {
                // write to firebug.
                console.log(o);
            }
            return hasConsole;
        },
        _messageBox: function (o) {
            alert(o);
        }
    },
    random: function (o) {
        var defaults = {
            min: 0,
            max: 9999999
        }
        var o = jQuery.fn.extend({}, defaults, o);
        var min = o.min;
        var max = o.max;
        return min + Math.floor((max - min + 1) * (Math.random() % 1));
    },
    loader: {
        show: function (o) {
            var overlayContainer = jQuery('<div class="modweb-admin-loaderOverlay"></div>').css({
                'width': '100%'
            , 'height': '100%'
            , 'position': 'fixed'
            , 'left': '0'
            , 'top': '0'
            , 'opacity': '0.4'
            , 'background-color': '#888888'
            , 'z-index': '3001'
            });
            if (typeof (o) !== 'undefined' && typeof (o.id) !== 'undefined') {
                overlayContainer.attr('id', o.id);
            }
            /*var text = jQuery('<div></div>').text('Loading...').css({
            backgroundColor: '#ffffff'
            , 'left': '50%'
            , 'top': '50%'
            , width: '100px'
            , height: '40px'
            , 'z-index': '3001'
            ,'fontSize':'16px'
            , 'position': 'fixed'
            , 'opacity': '1.0'
            ,'color':'#000000'

            });
            overlayContainer.append(text);
            */
            jQuery('body').append(overlayContainer);
        },
        hide: function (o) {
            if (typeof (o) !== 'undefined' && typeof (o.id) !== 'undefined') {
                jQuery('div#' + o.id + '.modweb-admin-loaderOverlay').remove();
            }
            else {
                jQuery('div.modweb-admin-loaderOverlay').remove();
            }
        }
    },
    users: {
        getCurrentUserGuid: function () {
            var guid = jQuery.cookie('dotnet_loginsessionguid');
            if (jQuery.jqModweb.isNullOrEmpty(guid))
                jQuery.jqModweb.debug.error("No user guid!");
            //alert('guid='+guid);
            return guid;
        }
    }/*,
    session: {
        getNodeId: function () {
            debugger;
        }
    }*/





    , AdvancedModalWindow: function (o) {
        var defaults = {
            url: 'http://www.example.com/'
            , ascxPath: null // overrides 'url'. NOTE: The ascx file you call should inherit from ModWindowUserControl to gain access to method to properly manipulate the window.
            , content: null // overrides both ascxPath and url parameters.
            , width: null
            , height: null // NOTE: setting this to 'auto' will set a fixed width based on the current browser height. this has nothing to do with the AutoResizing that takes place after an iframe's .ascx content is loaded. to control this you want to modify the server-side properties of the .ascx template itself.
            , onClose: null // TODO: what's this for?
            , onClosing: null
            , onClosed: null
            , onContentReady: null
            , params: null
            , backgroundColor: '#eeeeee'
            , modal: false
            , animate: true
            , animationSpeed: null
            , overflow: 'auto'
            , title: 'Advanced Modal Window'
            , scrolling: true
            //, allowContentCopying: false
        }
        var o = jQuery.fn.extend({}, defaults, o);
        if (!o.width || o.width === 0)
            o.width = 600;
        if (!o.height || o.height === 0)
            o.height = 400;
        this._o = o;
        this._container = null;
        this._overlayContainer = null;
        this._id = ('window_' + Math.random()).replace(/[.]/g, ''); // used internally to keep track of window. allows opening iframe to have control to close/resize.
        var self = this;
        this.open = function () {
            if (this._o.modal) {
                this._overlayContainer = jQuery('<div id="modweb-jsmodwebadvancedmodalwindow-modalWindow-overlay"></div>').css({
                    'width': '100%'
            , 'height': '100%'
            , 'position': 'fixed'
            , 'left': '0'
            , 'top': '0'
            , 'opacity': '0.25'
            , 'background-color': '#000000'
            , 'z-index': '2999'
                });
                jQuery('body').append(this._overlayContainer);
            }

            if (this._o.ascxPath) {
                this._o.url = "/_framework/modules/website/ModWindow-AscxLoader.aspx?windowid=" + this._id + "&templatepath=" + this._o.ascxPath;

                if (this._o.params) {
                    for (var p in this._o.params) {
                        this._o.url += "&" + p + "=" + this._o.params[p];
                    }
                }
            }

            // auto width if required.
            if (this._o.width === 'auto')
                this._o.width = window.innerWidth - 220;
            if (this._o.height === 'auto')
                this._o.height = window.innerHeight - 200;

            if (this._o.height < 50 && this._o.ascxPath) {
                // set a min height otherwise the ajax loader will cut off.
                this._o.height = 50;
            }
            //alert(this._o.height);

            // main modal window container.
            this._container = jQuery('<div x-modweb-jsmodwebadvancedmodalwindow-modalwindow="yes" id="' + this._id + '"></div>');
            this._container.css({
                'position': 'fixed'
             , 'display': 'none'
             , 'left': '50%'
             , 'top': '50%'
             , 'z-index': '3000'
             , 'margin-left': '-' + (this._o.width / 2 + 32 / 2) + 'px' // the 32 is the estimated caption title bar height.
             , 'margin-top': '-' + (this._o.height / 2 + 32 / 2) + 'px' // the 32 is the estimated caption title bar height.
            })
            .bind('close', function () {
                if (self._o.onClosing !== null) {
                    self._o.onClosing();
                }

                jQuery(self._container).trigger('beforeClose');
                self._container.remove();
                if (self._overlayContainer)
                    self._overlayContainer.remove();

                if (self._o.onClosed !== null) {
                    self._o.onClosed();
                }
            })
            .bind('beforeClose', function () {
                if (self._o.onClose !== null) {
                    self._o.onClose();
                }
            }).bind('modweb_modalwindow_oncontentready', function () {
                if (self._o.onContentReady !== null) {
                    self._o.onContentReady();
                }
            });

            if (typeof (jQuery.fn.draggable) !== 'undefined')
                this._container.draggable({ cursor: 'default' });

            // caption pane.
            var captionPane = jQuery('<div></div>').addClass('draghandle');
            captionPane.width(this._o.width + 12); // plus the border on the contentPane.
            captionPane.height(32);
            captionPane.css({
                backgroundColor: '#5A6973'
                //backgroundColor: '#00f'
                 , '-moz-border-radius-topleft': '0.75em'
                 , 'border-top-left-radius': '0.75em'
                 , '-moz-border-radius-topright': '0.75em'
                 , 'border-top-right-radius': '0.75em'
            });
            captionPane.append(
             jQuery('<span></span>').text(this._o.title).css({
                 color: 'white'
                 , position: 'relative'
                 , top: '8px'
                 , left: '10px'
                 , 'font-size': '16px'
                 , 'font-weight': 'bold'
                 , 'font-family': 'sans-serif'
                 , 'text-shadow': '0px 0px 2px #aaaaaa'
                 , 'cursor': 'default'
             }));
            captionPane.append(
                jQuery('<span></span>')
                .click(function () {
                    self.close();
                })
                .css({
                    float: 'right'
                    , width: '21px'
                    , height: '18px'
                    , 'margin-top': '8px'
                    , 'margin-right': '10px'
                    , '-moz-border-radius': '0.25em'
                    , 'border-radius': '0.25em'
                    , 'cursor': 'pointer'
                })
                .append(
                    jQuery('<img src="/_framework/admin/images/advancedmodalwindow_close.gif" />').css({
                        float: 'left'
                        , 'margin-top': '4px'
                        , 'margin-left': '4px'
                    })
                 )
             );
            this._container.append(captionPane);


            // content pane.
            var contentPane = jQuery('<div></div>');
            if (this._o.content) {
                // there was content sent into the class.
                if (typeof this._o.content.show !== 'undefined')
                    this._o.content.show(); // incase it was hidden.
                contentPane.append(this._o.content);
            }
            else {
                // there was no content sent into the class, create a content container using an iframe.
                var h = '';
                var overflowStyle = this._o.overflow === null ? '' : ('overflow:' + this._o.overflow + ';');
                var scrollingAttribute = ' scrolling="' + (this._o.scrolling ? 'yes' : 'no') + '"';
                scrollingAttribute = '';
                h += '<iframe src="' + this._o.url + '" width="' + this._o.width + '" height="' + this._o.height + '" style="border-style:none;' + overflowStyle + ';"' + scrollingAttribute + '>';
                h += '</iframe>';
                contentPane.append(h);
            }
            contentPane.addClass('content-pane').css({
                'background-color': this._o.backgroundColor
             , 'border-bottom': '6px solid #5A6973'
             , 'border-left': '6px solid #5A6973'
             , 'border-right': '6px solid #5A6973'
            });


            // render ajax loader.
            if (this._o.ascxPath) {
                contentPane.children('iframe').css({
                    'background': "url(/_framework/images/modalwindow-ajax-loader.gif) no-repeat"
                    , 'background-position': 'center'
                    , 'background-color': '#eeeeee'
                });
            }

            this._container.append(contentPane);
            jQuery('body').append(this._container);
            if (this._o.animate) {
                this._container.fadeIn(this._o.animationSpeed);
            }
            else
                this._container.show();

            //jQuery(h).ready(function () { alert('iframe ready!'); });
        };
        this.close = function () {
            this._container.trigger('close');
        };
        // reload() will reload the window if needing to find it by the id.
        this.reload = function (id) {
            this._container = jQuery('#' + id);
            if (this._container.length === 0)
                jsModweb.debug.error("Could not load window with ID '" + id + "' does it exist?.");
        };
        // onContentReady() will be called by the loading .aspx page after the iframe is fully loaded. it is called by the iframe's content to the parent window.
        this.onContentReady = function () {
            this._container.trigger('modweb_modalwindow_oncontentready');
            var iframeContainer = this._container.find('iframe:first');
            iframeContainer.css({ 'background': 'none' });
        };
        this.resize = function () {
            var iframeContainer = this._container.find('iframe:first');
            //iframeContainer.css({ 'background': 'none' });

            var contentHeight = iframeContainer.get(0).contentDocument.body.clientHeight;
            var newContentHeight = contentHeight + 32;
            var browserHeight = document.documentElement.clientHeight;
            var maxHeight = browserHeight - 80;

            if (newContentHeight > maxHeight)
                newContentHeight = maxHeight;

            this._container.animate({
                'margin-top': '-' + (newContentHeight / 2 + 32 / 2) + 'px' // the 32 is the estimated caption title bar height.
            });
            iframeContainer.css({ 'overflow-x': 'hidden' });

            iframeContainer.animate({ height: newContentHeight });
            //iframeContainer.height(newContentHeight);
        }
    }


};

