﻿/* *********************************************************************************************************** */
/* NOTE: This Should Always Be Placed in the "HEAD" Section of the Page. (Scripts Used By Us Are Loaded Async) */
/* *********************************************************************************************************** */

// Global Scope
{
    // Google Analytics Tracking Array/Queue
    var _gaq = [];

    function submitFormAjax(formName, onSuccess) {
        $.post($(formName).attr('action'), $(formName).serialize() + "&jsonRequest=true",
                        function (saveData) { onSuccess(saveData); });
        return false;
    }  
    
}

// JustGiving Scope
JustGiving = {
    __namespace: true, 
    WriteConsole: function writeConsole(value) {
                    if (window.console) {
                        window.console.info(value);
                    }
                  }
};

// Analytics
JustGiving.Analytics = function () {
    __namespace = true;

    var spyRunning = false;

    function renderPushArguments(array) {
        try {
            if (spyRunning) {
                var output = 'Analytics Request Sent: _gaq.push([';
                for (i = 0; i < array.length; i++) {
                    var item = array[i];

                        if (i > 0)
                            output += ', ';

                        output += ('\'' + item + '\'');
                };
                output += ']);';
                JustGiving.WriteConsole(output);
            }
        } catch (err) { debugger; };
    };

    function Push(array) {
        if (array.constructor != Array) {
            throw "Only Arrays Should be Pushed to JustGiving.Analytics.Push (See: http://bit.ly/gnonOe)";
        }
        else {
            renderPushArguments(array);
            _gaq.push(array);
        }
    };

    function Spy() {

        function createAnalyticsElement() {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

            // Once The Script is Loaded, We Need to Override "Push" Again
            /* Raised by IE */
            ga.onreadystatechange = function () {
                if (s.readyState == 'complete') {
                    /* Nothing */
                }
            };

            /* Raised by FF/Webkit */
            ga.onload = function () {
                /* Nothing */
            };
        };

        createAnalyticsElement();
        spyRunning = true;
        JustGiving.WriteConsole('JustGiving Google Analytics Spy Running...');
    };

    return {
        spy: Spy,
        push: Push
    };
} ();

JustGiving.Analytics.SocialTracking = function () {
    var settings = {
        network: '',
        socialAction: '',
        target: '',
        pagePath: ''
    };

    function extendSettings(options) {
        settings = $.extend({}, settings, options);
        //JustGiving.WriteConsole(settings);
    }

    function track() {
        if (settings.network !== '' && settings.socialAction != '') {
            if (settings.pagePath !== '') {
                try { JustGiving.Analytics.push(['_trackSocial', settings.network, settings.socialAction, settings.target, settings.pagePath]); } catch (ex) { }
            }
            else {
                try { JustGiving.Analytics.push(['_trackSocial', settings.network, settings.socialAction, settings.target]); } catch (ex) { }
            }
        }
        else {
            JustGiving.WriteConsole('_trackSocial not pushed to Google Analytics as network and action not defined');
        }
    };

    return {
        Track: track,
        ExtendSettings: extendSettings
    };
} ();

// Spin Up Components
JustGiving.Analytics.spy();
