/* Welcome to Emotify!  Copyright 2009 Emotify, Inc.  */

/*
	Creates the EMOTIFY object, if it doesn't already exist.

*/
if (typeof EMOTIFY == "undefined" || !EMOTIFY) {
    var EMOTIFY = {};
 }

/*

EMOTIFY.namespace is used to add members to the EMOTIFY object. 
ex. EMOTIFY.namespace('mySubObj')  creates EMOTIFY.mySubObj
	EMOTIFY.namespace('EMOTIFY.mySubObj') creates EMOTIFY.mySubObj
	EMOTIFY.namespace('mySubObj.specializedObj') creates EMOTIFY.mySubObj.specializedObj
	etc.
	
Based on YUI YAHOO.namespace

*/

EMOTIFY.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=(""+a[i]).split(".");
        o=EMOTIFY;

        // EMOTIFY is implied, so it is ignored if it is included
        for (j=(d[0] == "EMOTIFY") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};


/*  
	set up an error logger.
	Use EMOTIFY.log instead of console.log, this will prevent errors if console.log is not present.
	
*/
EMOTIFY.namespace('log');
if (typeof console != 'undefined' && console !== null) {
    if ((typeof console.log == 'function' || typeof console.log == 'object') && console.log !== null) {
	    EMOTIFY.log = function(tolog) { console.log(tolog); };
	}
}
if (typeof EMOTIFY.log != 'function') {
    EMOTIFY.log = function(tolog) {};
}



/*
EMOTIFY.loaded(check);
Checks to see if the EMOTIFY or jQuery method or object "check" has been loaded

For examples:
Check for EMOTIFY.ui:
EMOTIFY.loaded('ui'); or EMOTIFY.loaded('EMOTIFY.ui');

Check for EMOTIFY.ui.dialog:
EMOTIFY.loaded('ui.dialog'); or EMOTIFY.loaded('EMOTIFY.ui.dialog');

Check for jQuery.click();
EMOTIFY.loaded('jQuery.click');
** Note:  for jQuery you must start the check variable with 'jQuery', otherwise it will look in the EMOTIFY object.


*/


EMOTIFY.namespace('loaded');
EMOTIFY.loaded = function(toCheck) {
	var parts = toCheck.split('.');

	if (toCheck.indexOf('jQuery') > -1) {
		return jQuery()[parts[1]] ? true : false;
	} else {
		var start = (parts[0] == 'EMOTIFY') ? 1 : 0;
		var test = EMOTIFY;
		for (var i = start; i < parts.length; i++) {
			if (typeof test[parts[i]] != 'undefined') {
				test = test[parts[i]];
			} else {
				return false;
			}
		}
		return test;
		return true;
	}
}



/*
Translates an emotion ID into the emotion name  
*/
EMOTIFY.namespace('getName');
EMOTIFY.getName = function(id) {

    if (typeof EMOTIFY.emotions != 'undefined' && EMOTIFY.emotions !== null) {
        if (typeof EMOTIFY.emotions[id] != 'undefined' && EMOTIFY.emotions[id] !== null) {
            return EMOTIFY.emotions[id];
        } else {
            return false;
        }
    }
}

/*
set up a ui object to handle our User Interface interactions
	
*/
EMOTIFY.namespace('ui');

/*
Tries to execute a function.  Fails gracefuly if the function does not exist.

*/
EMOTIFY.namespace('attempt');
EMOTIFY.attempt = function(func) {
    try {
        func();
    } catch (e) {

    }
}

/* Used for tracking actions in our Analytics suite */
EMOTIFY.namespace('track');
EMOTIFY.track = function(action) {
	var url = "/a/" + action;
	if (typeof pageTracker != 'undefined' && pageTracker !== null) {
		pageTracker._trackPageview(url);
	}

	if (typeof clicky != 'undefined' && clicky !== null) {
		clicky.log(url);
	}
	EMOTIFY.log(url);
}

