﻿/* Newsroom Object 
*   This is where everything is built from.
*   Administrative functionality is added to this via `attachComponent`, otherwise we only house our most basic required prototypes
*/
function Newsroom(){
    // if we didn't execute `attachAdmin`, we want to instantiate only `Search`
    if(typeof(attachAdmin) != 'function'){
        var that = this;
        $(function(){
            that.Search = new Search(that);
        });
    }
}
/**
* callService - a wrapper for calls to our webservices to allow for multiple calls and clean loading / error handling
*   Usage: pass objects like this
*   {
*       name    : string    -   the service name
*       post    : object
*       loader  : boolean
*       success : function
*       error   : int
*   }
*/
Newsroom.prototype.callService = function () {
    var that = this;
    for (var i = 0; i < arguments.length; i++) {
        var args = arguments[i];
        this.Loading(args.loader || args.loading ? true : false);
        $.ajaxDotNet(wsl + '/' + args.name, {
            verb: 'POST',
            data: args.post,
            success: function () {
                that.Loading(false);
                args.success.apply(this, arguments);
            },
            error: function (obj) {
                that.Loading(false);
                if (obj.status == "12030" || obj.status == "401") {
                    // if we get an IE or FF unauthorized response, throw code 100 ( lost session )
                    that.handleError(100);
                } else {
                    that.handleError(args.error);
                }
            }
        });
    }
};
/**
* Modal - **BETA** - We want to wrap our usage of modal windows to provide a consistent point of styling and options
*/
Newsroom.prototype.Modal = function (content, opts) {
    $('#edit-content-area').html('').append(content);
    var modal_opts = {
        close: true,
        overlayId: 'editwidget-overlay',
        containerId: 'edit-container'
    };
    for (attr in opts) { modal_opts[attr] = opts[attr]; }
    $('#edit-table').modal(modal_opts);
};
/**
* Loading - **BETA** - we want to display some form of loading
*   Pass boolean to show/hide a dark overlay.
*/
Newsroom.prototype.Loading = function (state) {
    if (state) {
        $.modal.close();
        var modal_opts = {
            close: true,
            overlayId: 'error-overlay',
            containerId: 'error-container'
        };
        $('<div/>').modal(modal_opts);
    } else {
        $.modal.close();
    }
};
/**
* handleError - central display handler for error messages
*/
Newsroom.prototype.handleError = function (e) {
    var errorModal = function (name, msg) {
        $($('<div/>', {
            id: 'error-modal'
        }).append($('<h1/>', {
            html: e == 100 ? "Session Expired" : "Something went wrong...",
            className: "error-title"
        })).append($('<p/>', {
            html: "<strong>" + name ? name : '' + "</strong><br/>" + msg ? msg : '',
            className: "error-desc"
        })).append($('<button/>', {
            text: "Ok",
            className: "error-ack",
            click: function () {
                $.modal.close();
                window.onbeforeunload = null;
                // by default, 100 code is dropped session, take us to the login
                e == 100 ? window.location = "/Auth/Default.aspx" : false;
            }
        }))).modal({
            close: false,
            overlayId: 'error-overlay',
            containerId: 'error-container'
        });
    } ();
    $.ajax({
        type: "GET",
        url: "/error.xml",
        dataType: "xml",
        success: function (xml) {
            $(xml).find('def').each(function () {
                if ($(this).find('code').text() == e) {
                    $('.error-desc').html("<strong>" + $(this).find('name').text() + "</strong><br/>" + $(this).find('msg').text());
                }
            });
        },
        error: function () {
            $('.error-desc').html("<strong>This is embarrassing!</strong><br/>We can't seem to access our error definition file to help explain the issue, but the error code was #" + e + ".");
        }
    });
};
var N = new Newsroom();
/*Plates.synchronize();*/

