/**
	* Set up the global BBX object
*/
if(!BBX) {
	var BBX = {'user':{}};
};




/**
 * Returns the namespace specified and creates it if it doesn't exist
 * Lifted directly from YUI, just changed the main object name. (May 12 2009)
 * <pre>
 * BBX.namespace("property.package");
 * BBX.namespace("BBX.property.package");
 * </pre>
 * Either of the above would create BBX.property, then
 * BBX.property.package
 *
 * Be careful when naming packages. Reserved words may work in some browsers
 * and not others. For instance, the following will fail in Safari:
 * <pre>
 * BBX.namespace("really.long.nested.namespace");
 * </pre>
 * This fails because "long" is a future reserved word in ECMAScript
 *
 *
 * @method namespace
 * @static
 * @param  {String} arguments 1-n namespaces to create 
 * @return {Object}  A reference to the last namespace object created
 */


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

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

    return o;
};

/**
 * For now, just records that a module has been loaded.  
 * When the new loader is written it will also remove the module from the "loading" array and execute any onload events
 *
 */
BBX.namespace('status.registered');
BBX.status.registered = [];
BBX.register = function(module) {
	if(!BBX.status.registered.in_array(module)) {
		BBX.status.registered.push(module);
	}
};
BBX.isRegistered = function(key) {
    if(BBX.status.registered.in_array(key)) {
	return true;
    }
    return false;
}



BBX.namespace('welcome');
BBX.welcome = {
	setFTCookie:function() {
		BBX.cookie.create('firstTime', 'false', 365);
	}
};


/*
Tells us if there is a thirdparty login provider present.
@return : true if there is a provider, false if not

** the @return will change to an array of present thirdparty providers if there are any, false if there are not, soon.
*/
BBX.user.isThirdPartyLoggedIn = function() {
	if(BBX.user.thirdPartyProviders) {
		return BBX.user.thirdPartyProviders;
	} else {
		return false;
	}
};


/**
 * @return bool True if we are logged in, false otherwise.
 *
 * Checks to see if a user is logged in, (client-side check only)
 */

BBX.user.isLoggedIn = function() {
  if($('#dname').length > 0) {
      return true;
  }
  return false;
}


/*
BBX.traverse contains several methods for traversing the DOM.
*/
BBX.namespace('traverse');

/**
getAncestorByClass looks for an ancestor of node with the class className.  When it finds it it returns that element.
*/

BBX.traverse.getAncestorByClass = function(node,className) {
	
	do {
		if($(node).hasClass(className)) {
			return node;
		}
	} while(node.parentNode && (node = node.parentNode));
};

/**
getAncestorByTag looks for an ancestor with the tag tagName.  When it finds it it returns that element.
*/
BBX.traverse.getAncestorByTag = function(node,tagName) {
	
	do {
		if($(node).nodeName.toLowerCase() == tagName.toLowerCase()) {
			return node;
		}
	} while(node.parentNode && (node = node.parentNode));
};


/**
BBX.bind binds a function to an object, setting the value of "this" in the function to the object.

*/
BBX.namespace('bind');
BBX.bind =  function(func, obj) { 
  var method = func, 
   temp = function() { 
    return method.apply(obj, arguments); 
   }; 
  
  return temp; 
}


/*
BBX.measure contains several methods for measuring DOM items.
*/
BBX.namespace('measure');
BBX.measure = function() {

	var o = {
	
		viewport:function() {
			var x,y;
			if (self.innerHeight) // all except Explorer
			{
				x = self.innerWidth;
				y = self.innerHeight;
			}
			else if (document.documentElement && document.documentElement.clientHeight)
				// Explorer 6 Strict Mode
			{
				x = document.documentElement.clientWidth;
				y = document.documentElement.clientHeight;
			}
			else if (document.body) // other Explorers
			{
				x = document.body.clientWidth;
				y = document.body.clientHeight;
			}
			return [x,y];
		},
	
	
		pagesize:function() {
			var x,y,y1,y2,y3;
			y1 = document.body.scrollHeight;
			y2 = document.body.offsetHeight;
	        var b = $(document.getElementsByTagName('body')[0]);
	        y3 = parseInt((b.getStyle('height')),10);
	        y = Math.max(y1,y2);
	        if(!isNaN(y3)) {
	        	y = Math.max(y,y3);
	        }
			if (y1 >= y2) // all but Explorer Mac
			{
				x = document.body.scrollWidth;
			}
			else // Explorer Mac;
				 //would also work in Explorer 6 Strict, Mozilla and Safari
			{
				x = document.body.offsetWidth;
			}
			return [x,y];
		},
	
		scrolled:function() {
			var x,y;
			if (window.pageYOffset) // all except Explorer
			{
				x = window.pageXOffset;
				y = window.pageYOffset;
			}
			else if (document.documentElement && document.documentElement.scrollTop)
				// Explorer 6 Strict
			{
				x = document.documentElement.scrollLeft;
				y = document.documentElement.scrollTop;
			}
			else if (document.body) // all other Explorers
			{
				x = document.body.scrollLeft;
				y = document.body.scrollTop;
			}
			return [x,y];
		},
	
		dist4center:function(tgt) {
			var th=parseInt($(tgt).getStyle('height'),10);
			var tw=parseInt(tgt.offsetWidth,10);
			var top=Math.floor(o.scrolled()[1]+(o.viewport()[1]/2)-(th/2));
			var left=Math.floor(o.scrolled()[0]+(o.viewport()[0]/2)-(tw/2));
			return [left,top];
		},
	

    
	    positionOnPage:function(obj) {
			var curleft,curtop;
	        curleft=0;
	        curtop=0;
			if (obj.offsetParent) {
				do {
					curleft += obj.offsetLeft;
					curtop += obj.offsetTop;
				} while (obj = obj.offsetParent);
			}
			return [curleft,curtop];
		}
	
	
	
	}
	
	return o;


}();


/**
BBX.utils holds various utility functions

*/
BBX.namespace('utils.addid');
BBX.utils.addid = function (obj,start) {
	if(!obj || typeof obj == 'string') { return false; }
	if(typeof start == 'undefined' || start === null) {
		start = '';
	}
	
	var ntime,newid;
	ntime = new Date();
	newid = start+(Math.round(Math.random()*ntime.getTime()));
	obj.setAttribute('id',newid);
	obj.id = newid;
	return newid;
}
BBX.utils.getHref = function(dom) {

	var hr = dom.getAttribute('href');
	if(hr.indexOf('http://') === 0) {
		hr = dom.getAttribute('href',2);	
	}
	return hr;
}

/**
 *  BBX.utils.redirect(url)
 *
 *  Redirects the browser to the URL specified by url
 *  This way we don't have to remember the window.location syntax.
 *
 *  @param string url The URL to redirect to
 *  @return void
 *
 *
 *
 */
BBX.utils.redirect = function(url) {
    window.location = url;
}


/**
 *
 *  BBX.utils.findPositionInSequence(fnd, seq)
 *  Finds the item represented by find in the sequence sequence.
 *  Returns the ordinal of find, or boolean false if it is not found.
 *
 *  @param DOMReference fnd  The item to find in the sequence
 *  @param array seq (An array of DOM references) The sequence to look in for find.
 *
 *  @return mixed Returns the ordinal of the found item if found, or a bool false otherwise
 */

BBX.utils.findPositionInSequence = function(fnd, seq) {
	for(var i=0;i<seq.length;i++) {
		if(seq[i] == fnd) {
			return i;
		}
	}
	return false;
}


/**
 * Cookie handling utilities.
 */
 BBX.namespace('cookie');
 BBX.cookie = (function() {
   
    function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else { var expires = ""; }
	document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
	    var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0;i < ca.length;i++) {
		    var c = ca[i];
		    while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	    return null;
    }

    function eraseCookie(name) {
	    createCookie(name,"",-1);
    }

    var ext = {
	create: createCookie,
	read: readCookie,
	erase: eraseCookie
    };

    return ext;

})();


BBX.configdata = (function() {

    var data = [];
    data['gridsize.widescreen'] = 20;
    data['gridsize.normal'] = 15;
    data['gridrowlength.widescreen'] = 4;
    data['gridrowlength.normal'] = 3;
    data['fbperms'] = "publish_stream,offline_access,email,user_about_me,user_birthday";
    data['domain'] = 'bonzobox.com';
    data['animation_duration'] = 200;
    data['adrefreshinterval.leader'] = 30000;
    data['recaptcha_public_key'] = '6Le1ywQAAAAAABUIAssSYxP91x7DrULnIw-z1gFd ';

    var o = {

	getVal:function(k) {
	    return data[k];

	}

    }

    return o;

})();

(function() {
    
    var track = function(eventname, data) {
	
	if(!data) {
	    data = {};
	}

	if(_kmq) {
	    _kmq.push(['record', eventname, data]);
	}
	
	
    };
    
    
    
})();



