// SENDZA GLOBAL OBJECT
var SENDZA = {};

(function () {
    // Private Members
    var _debugMode = false,
        _googleApiKey = 'ABQIAAAAEjERsriR1DX2vseEZOOGpBTI3jlv1Rv7x5Uokxt5V-63fMV5wxSr5_390bxUdoLdG13svB_RVlXpbA',
        _suiDirectory = 'sui',
        _workingDirectory = '';

    // Accessors
    SENDZA.getDebugMode = function () {
        return _debugMode;
    };

    SENDZA.getGoogleApiKey = function () {
        return _googleApiKey;
    };

    SENDZA.getSuiDirectory = function () {
        return _suiDirectory;
    };

    SENDZA.getWorkingDirectory = function () {
        return _workingDirectory;
    };

    SENDZA.setDebugMode = function (debugMode) {
        if (debugMode) {
            _debugMode = true;
        } else {
            _debugMode = false;
        }
    };

    SENDZA.setGoogleApiKey = function (googleApiKey) {
        _googleApiKey = googleApiKey;
    };

    SENDZA.setSuiDirectory = function (suiDirectory) {
        _suiDirectory = suiDirectory;
    };

    SENDZA.setWorkingDirectory = function (workingDirectory) {
        _workingDirectory = workingDirectory;
    };
}());

// Namespace: SENDZA.util
(function () {
    // Private Members
    var _dialogCount = 0,
    
        util;
    
    util = {
        // Public Methods
        alert: function (message, onClick) {
            try {
                _dialogCount += 1;
                new SENDZA.widget.Dialog('snzAlert' + _dialogCount, {
                    audio: SENDZA.util.includeUrl('widget', 'Dialog', 'alert.mp3'),
                    body: message,
                    buttons: [{
                        handler: function () {
                            this.hide();
                            this.destroy();
                            if (typeof onClick === 'function') {
                                onClick();
                            }
                        },
                        text: 'Okay'
                    }]
                });
            } catch (e) {
                alert(message);
                if (typeof onClick === 'function') {
                    onClick();
                }
            }
        },
        confirm: function (message, onClick) {
            try {
                _dialogCount += 1;
                new SENDZA.widget.Dialog('snzConfirm' + _dialogCount, {
                    audio: SENDZA.util.includeUrl('widget', 'Dialog', 'alert.mp3'),
                    body: message,
                    buttons: [{
                        handler: function () {
                            this.hide();
                            this.destroy();
                            if (typeof onClick === 'function') {
                                onClick(true);
                            }
                        },
                        text: 'Yes'
                    }, {
                        handler: function () {
                            this.hide();
                            this.destroy();
                            if (typeof onClick === 'function') {
                                onClick(false);
                            }
                        },
                        text: 'No'
                    }]
                });
            } catch (e) {
                if (confirm(message)) {
                    if (typeof onClick === 'function') {
                        onClick(true);
                    }
                } else if (typeof onClick === 'function') {
                    onClick(false);
                }
            }
        },
        copy: function (object) {
            if (typeof object !== 'object') {
                return false;
            }
            var copy = SENDZA.util.isArray(object) ? [] : {},
                i;
            for (i in object) {
                if (object[i] && typeof object[i] === 'object') {
                    copy[i] = SENDZA.util.copy(object[i]);
                } else {
                    copy[i] = object[i];
                }
            }
            return copy;
        },
        cssIncludeUrl: function (base, container, name) {
            if (typeof name === 'undefined') {
                name = container;
            }
            return this.includeUrl(base, container, name + '.css');
        },
        debugMessage: function (message) {
            if (SENDZA.getDebugMode()) {
                alert(message);
            }
        },
        getHost: function () {
            var urlparts = window.location.href.split('/'),
                host = urlparts[0] + '//' + urlparts[2],
                workingDirectory = SENDZA.getWorkingDirectory();
            if (workingDirectory) {
                return host + '/' + workingDirectory;
            }
            return host;
        },
        getProperty: function (element, property, defaultValue) {
            if (typeof defaultValue === 'undefined') {
                defaultValue = null;
            }
            return YAHOO.util.Dom.batch(element, function (element, property) {
                if (element && element[property]) {
                    return element[property];
                }
                return defaultValue;
            }, property);
        },
        includeUrl: function (base, container, file) {
            if (base && base.length > 0) {
                base = base + '/';
            } else {
                base = '';
            }
            if (container && container.length > 0) {
                container = container + '/';
            } else {
                container = '';
            }
            if (!file || file.length <= 0) {
                file = '';
            }
            return this.getHost() + '/' + SENDZA.getSuiDirectory() + '/' + base + container + file;
        },
        isArray: function (object) {
            return typeof object === 'object' && object.constructor === Array;
        },
        jsIncludeUrl: function (base, container, name) {
            if (typeof name === 'undefined') {
                name = container;
            }
            return this.includeUrl(base, container, name + '.js');
        },
        lTrim: function (str) {
            while (str.substring(0, 1) === ' ') {
                str = str.substring(1, str.length);
            }
            return str;
        },
        prompt: function (message, onClick) {
            try {
                _dialogCount += 1;
                new SENDZA.widget.Dialog('snzPrompt' + _dialogCount, {
                    audio: SENDZA.util.includeUrl('widget', 'Dialog', 'alert.mp3'),
                    body: message + '<br /><br /><form action="" method="POST" onsubmit="return false;"><input class="snz-wide" name="value" type="text" /></form>',
                    buttons: [{
                        handler: function () {
                            this.hide();
                            if (typeof onClick === 'function') {
                                onClick(this.getData().value);
                            }
                            this.destroy();
                        },
                        text: 'Accept'
                    }, {
                        handler: function () {
                            this.hide();
                            this.destroy();
                            if (typeof onClick === 'function') {
                                onClick(null);
                            }
                        },
                        text: 'Cancel'
                    }]
                });
            } catch (e) {
                var value = prompt(message);
                if (typeof onClick === 'function') {
                    onClick(value);
                }
            }
        },
        removeNaughtyCharacters: function (str) {
            return str.replace(/</g, '').replace(/>/g, '');
        },
        removeFromArray: function (array, start, end) {
            if (typeof end === 'undefined') {
                end = start;
            }
            var after = array.slice((start || end) + 1 || array.length);
            array.length = end < 0 ? array.length + end : end;
            array.push.apply(array, after);
            return array;
        },
        setProperty: function (element, property, value) {
            YAHOO.util.Dom.batch(element, function (element, args) {
                if (element) {
                    element[args.property] = args.value;
                }
            }, {
                property: property,
                value: value
            });
        }
    };

    SENDZA.util = util;
}());

// Namespace: SENDZA.util.SendzaLoader
(function () {
    // Private Members
    var _loader = new YAHOO.util.YUILoader({
            base: 'https://ajax.googleapis.com/ajax/libs/yui/2.9.0/build/',
            loadOptional: false,
            combine: false,
            filter: SENDZA.getDebugMode() ? 'DEBUG' : 'MIN',
            allowRollup: true,
            timeout: 15000
        }),
        SendzaLoader = {
            // Public Functions
            moduleAdd: function (args) {
                return _loader.addModule(args);
            },
            moduleInsert: function (args) {
                _loader.insert(args);
            },
            moduleRequire: function (require) {
                _loader.require(require);
            }
        };

    SENDZA.util.SendzaLoader = SendzaLoader;
}());

// Namespace: SENDZA.widget
(function () {
    // Private Members
    var _tooltip = null,
        _tooltipElement = null,
        _tooltipStatus = 'hidden',
    
        widget;
    
    widget = {
        // Public Methods
        drawButtons: function () {
            var button,
                classes,
                i,
                inputs = document.getElementsByTagName('input'),
                j,
                length;
            for (i = inputs.length - 1; i >= 0; i -= 1) {
                if (SENDZA.util.getProperty(inputs[i], 'type') === 'button') {
                    classes = SENDZA.util.getProperty(inputs[i], 'className', '').split(' ');
                    classes.push('snz-button');
                    button = new YAHOO.widget.Button(inputs[i], {
                        onclick: {
                            fn: SENDZA.util.getProperty(inputs[i], 'onclick'),
                            scope: inputs[i]
                        }
                    });
                    button.set('label', '<span class="snz-buttonlabel">' + button.get('label') + '</span>');
                    for (j = 0, length = classes.length; j < length; j += 1) {
                        YAHOO.util.Dom.addClass(button, classes[j]);
                    }
                }
            }
        },
        registerTooltips: function () {
            _tooltip = new YAHOO.widget.Tooltip('tooltip');
            var i,
                length,
                tooltipElements = YAHOO.util.Dom.getElementsByClassName('snz-tooltip'),
                
                onMouseOut,
                onMouseOver;
                
            onMouseOut = function (event, element) {
                if (element === _tooltipElement) {
                    _tooltipElement = null;
                    _tooltip.hide();
                    _tooltipStatus = 'hidden';
                }
            };
            
            onMouseOver = function (event, element) {
                _tooltipElement = element;
                _tooltip.moveTo(YAHOO.util.Event.getPageX(event), YAHOO.util.Event.getPageY(event) + 21);
                _tooltipStatus = 'ready';
            };
            
            for (i = 0, length = tooltipElements.length; i < length; i += 1) {
                YAHOO.util.Event.on(tooltipElements[i], 'mouseover', onMouseOver, tooltipElements[i]);
                YAHOO.util.Event.on(tooltipElements[i], 'mouseout', onMouseOut, tooltipElements[i]);
            }
        },
        showTooltip: function (text) {
            _tooltip.cfg.setProperty('text', text);
            _tooltip.show();
            _tooltipStatus = 'visible';
        },
        hideTooltip: function () {
            _tooltip.hide();
            _tooltipStatus = 'hidden';
        },
        getTooltipStatus: function () {
            return _tooltipStatus;
        }
    };

    SENDZA.widget = widget;
}());

// Sendza Modules
(function () {
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'Ajaxslt', 'Xslt'),
        name: 'Ajaxslt',
        requires: [
            'Ajaxslt-xpath'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'Ajaxslt', 'Dom'),
        name: 'Ajaxslt-dom',
        requires: [
            'Ajaxslt-xmltoken'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'Ajaxslt', 'Util'),
        name: 'Ajaxslt-util',
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'Ajaxslt', 'Xmltoken'),
        name: 'Ajaxslt-xmltoken',
        requires: [
            'Ajaxslt-util'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'Ajaxslt', 'Xpath'),
        name: 'Ajaxslt-xpath',
        requires: [
            'Ajaxslt-dom'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'Animation'),
        name: 'Animation',
        requires: [
            'animation'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('widget', 'AudioIcon'),
        name: 'AudioIcon',
        requires: [
            'AudioPlayback',
            'Icon'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'AudioPlayback'),
        name: 'AudioPlayback',
        requires: [
            'cookie'
        ], // Requires SoundManager
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('widget', 'Calendar'),
        name: 'Calendar',
        requires: [
            'calendar',
            'Calendar-css'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.cssIncludeUrl('widget', 'Calendar'),
        name: 'Calendar-css',
        type: 'css'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'ControlPanel'),
        name: 'ControlPanel',
        requires: [
            'Animation',
            'Error',
            'Marco'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('widget', 'Counter'),
        name: 'Counter',
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        after: ['AudioPlayback', 'button'],
        fullpath: SENDZA.util.jsIncludeUrl('widget', 'Dialog'),
        name: 'Dialog',
        requires: [
            'container',
            'dragdrop',
            'Icon'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('widget', 'DisplayTable'),
        name: 'DisplayTable',
        requires: [
            'datasource',
            'datatable'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('widget', 'DynamicImage'),
        name: 'DynamicImage',
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'Element'),
        name: 'Element',
        requires: [
            'event'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'Encode'),
        name: 'Encode',
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'Error'),
        name: 'Error',
        requires: [
            'json'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'Event'),
        name: 'Event',
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: 'http' + ('https:' === document.location.protocol ? 's' : '') + '://s3.amazonaws.com/getsatisfaction.com/javascripts/feedback-v2.js',
        name: 'GetSatisfaction',
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: 'https://www.google.com/jsapi?key=' + SENDZA.getGoogleApiKey(),
        name: 'GoogleAjaxApis',
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'GoogleMaps'),
        name: 'GoogleMaps',
        requires: [
            'GoogleAjaxApis'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: 'http://maps.google.com/maps/api/js?sensor=false&callback=SENDZA.util.GoogleMaps3.callback',
        name: 'GoogleMaps3',
        requires: [
            'GoogleMaps3Callback'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'GoogleMaps3'),
        name: 'GoogleMaps3Callback',
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('widget', 'Map'),
        name: 'Map',
        requires: [
            'GoogleMaps'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('widget', 'Icon'),
        name: 'Icon',
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('util', 'Marco'),
        name: 'Marco',
        requires: [
            'connection',
            'json'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('widget', 'RichTextEditor'),
        name: 'RichTextEditor',
        requires: [
            'editor',
            'menu',
            'RichTextEditor-css',
            'slider'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.cssIncludeUrl('widget', 'RichTextEditor'),
        name: 'RichTextEditor-css',
        type: 'css'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('widget', 'SelectList'),
        name: 'SelectList',
        requires: [
            'button',
            'SelectList-css'
        ],
        type: 'js'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.cssIncludeUrl('widget', 'SelectList'),
        name: 'SelectList-css',
        type: 'css'
    });
    SENDZA.util.SendzaLoader.moduleAdd({
        fullpath: SENDZA.util.jsIncludeUrl('widget', 'UploadForm'),
        name: 'UploadForm',
        requires: [
            'progressbar',
            'uploader'
        ],
        type: 'js'
    });
}());
