﻿// map jQuery to $j
var $j = jQuery.noConflict();

var Site = function() { };

// Verify an object is not null and is initilized
Site.IsUndefinedOrNull = function(e) {
    try {
        return (e === undefined || e === null)
    } catch (e) {
        return false; 
    }
};

// Setup page load events
Site.OnloadEvents = {
    register: function(f) {
        this.events.push(f);
    },
    init: function() {
        var me = this;
        jQuery.each(me.events, function(i, o) {
            if (typeof me.events[i] == "function") me.events[i]();
        });
    },
    events: []
};

// Send AJAX request to a server endpoint with specified parameters
Site.SendRequest = function(o) {
    if (!Site.IsUndefinedOrNull($j)) {
        $j.ajax
            ({
                type: o.type || "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                url: o.url,
                data: o.data || '{}',
                success: o.success || Site.AjaxSuccess,
                error: o.error || Site.AjaxError
            });
    }
};

// Default AJAX error handler
Site.AjaxError = function(x, s, e) {
    if ($j.browser.msie) {
        alert('Sorry, we are experiencing a problem with one of your requests. Please try again. Exception: ' + e);
    }
    else {
        console.log('Sorry, we are experiencing a problem with one of your requests. Please try again.  Exception: ' + e);
    }
};

// Default AJAX success handler
Site.AjaxSuccess = function(d, s) {

};

Site.SetPage = function(p) {
    document.body.className = p;
    $j('#navigation a.active').removeClass('active');
    $j('#nav-' + p + ' a').addClass('active');
};

jQuery(document).ready(function() {
    var hash = window.location.hash;
    if (hash.length > 0) {
        Site.SetPage(hash.split('#')[1]);
    }

    $j('#navigation a span').click(function(event) {
        Site.SetPage(event.target.parentNode.href.split('#')[1]);
        event.preventDefault();
    });

    Site.OnloadEvents.init(); /* run this last */
});

