﻿if (window['console'] === undefined) {
    window['console'] = function() { };
    console.log = function() { };
}

var GoodNites = {};
var GoodNitesFlash = {};
var GoodNitesUserControls = {};

// create a method that will allow us to extend the functionality of an object with that of another object.
// think of this as inheritence through the process of copying properties / methods
GoodNites.Extend = function(destination, source) {
    for (var property in source.prototype) {
        if (destination.prototype[property] === undefined) {
            destination.prototype[property] = source.prototype[property];
        }
    }
    return destination;
};

// define the core object for use in the system.  this defines a basic structure of the javascript framework
// that we'll be using for the site.
GoodNites.Core = function() { };

GoodNites.Core.prototype = {
    InitializeServices: function() { },
    AttachEvents: function() { },
    Display: function() { },
    Initialize: function() {
        this.InitializeServices();
        this.AttachEvents();
        this.Display();
    },
    ServiceProxy: function(serviceUrl) {
        /// <summary>
        /// Generic Service Proxy class that can be used to
        /// call JSON Services using jQuery.
        /// Depends on JSON2.js modified for MS Ajax usage
        /// </summary>
        /// <param name="serviceUrl" type="string">
        /// The Url of the service ready to accept the method name
        /// should contain trailing slash (or other URL separator ?,&)
        /// </param>
        /// <example>
        /// var proxy = new ServiceProxy("JsonStockService.svc/");
        /// proxy.invoke("GetStockQuote",{symbol:"msft"},
        ///              function(quote) { alert(result.LastPrice); },onPageError);
        ///</example>

        var _I = this;

        this.serviceUrl = serviceUrl;

        this.invoke = function(method, params, callback, error) {
            /// <summary>
            /// Calls a WCF/ASMX service and returns the result.
            /// </summary>   
            /// <param name="method" type="string">The method of the service to call</param>
            /// <param name="params" type="object">An object that represents the parameters to pass {symbol:"msft",years:2}      
            /// <param name="callback" type="function">Function called on success.
            /// Receives a single parameter of the parsed result value</parm>
            /// <param name="errorCallback" type="function">Function called on failure.
            /// Receives a single error object with Message property</parm>

            // Convert input data into JSON - REQUIRES modified JSON2.js
            var json = JSON.stringify(params);

            // Service endpoint URL       
            var url = _I.serviceUrl + method;

            $.ajax({
                url: url,
                data: json,
                type: "POST",
                processData: false,
                contentType: "application/json",
                timeout: 10000,
                dataType: "text",  // not "json" we'll parse
                success: function(res) {
                    if (!callback) return;

                    // Use json library so we can fix up MS AJAX dates
                    var result = JSON.parse(res);

                    // Wrapped message contains top level object node
                    // strip it off
                    for (var property in result) {
                        callback(result[property]);
                        break;
                    }
                },
                error: function(xhr) {
                    if (!error) return;
                    var res = xhr.responseText;
                    if (res && res.charAt(0) == '{')
                        var err = JSON.parse(res);
                    if (err)
                        error(err);
                    else
                        if (xhr.status != 200)
                        error({ Message: "Http Error: " + xhr.statusText });
                    else
                        error({ Message: "Unknown Error Response" });
                    return;
                }
            });
        };
    }
};