// This is our global EB object.
// It should be used as our root namespace for custom functions.
// When applicable we should have subnamespaces such as EB.Analytics
//
// Example:
// EB.Analytics = (function($, imports){
//     var my_private_variable;
//     return {
//          func: function(){},
//          func2: function(){}
//     };
// }(jQuery, imports));
// 
// For more info read http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
var EB = (function($){
    // private vars go here
    
    return {

        logging: false,

        // Log input to console (cross-browser)   
        log: function(attribute){
            if (EB.logging){
                try {
                  window.console.log.apply( window.console, arguments );
                } catch(e) {
                  try {
                    opera.postError.apply( opera, arguments );
                  } catch(e){
                    alert( Array.prototype.join.call( arguments, " " ) );
                  }
                }        
            }   
        }
    };
}(jQuery));


// For Google Analytics and tracking Actions
EB.Analytics = (function($, EB){
    
    var prefix;
    var pageTracker;
    
    return {
        
        // Initialize with prefix and the Google Analytics pageTracker
        init: function(pre, pt){
            prefix = pre;
            pageTracker = pt;
        },
        
        // Track a page view in Google Analytics
        trackPageView: function(){
        	if(!!pageTracker){
        		pageTracker._trackPageview(arguments);
        	}
        },
        
        // Record an action, and track with Google Analytics.
        // accepts object of form: {name: 'share', category: 'facebook', id: 3}
        // settings is for additional or overriding of $.ajax settings
        // all attributes are optional
        recordAction: function(action, settings){
            // track the action with google analytics
            if (!!prefix && action.name && action.category){
                EB.Analytics.trackPageView('/' + prefix + action.name + '-' + action.category);
            }
            
            var url = window.server + '/recordaction';
            var eid = window.eid;
            var data = {
                uid: window.uid,
                fb_uid: window.fb_uid,
                eid: eid,
                action_category: action.category,
                action: action.name,
                action_id: action.id,        
             url: window.url_escaped
            };
            baseSettings = {
                url: url,
                type: "POST",
                data: data,
                success: function(data) {
                    EB.log('Action ' + action.name + ' tracked.');
                },
                error: function(data) {
                    EB.log('ERROR: Action ' + action.name + ' not tracked.');
                }
            };
            $.ajax($.extend({}, baseSettings, settings));  
        }
    };
}(jQuery, EB)); 

EB.util = (function($, EB){
    return {
        truncate: function(s, limit){
            if (typeof(s) != "string"){
                return "";
            }
            if (limit > 3 && s.length > limit){
                return s.slice(0, limit - 3) + "...";
            }
            return s;
        }
    };
}(jQuery, EB));

