// Define the DMD global namespace object
var DMD = window.DMD || {};

// Setup global vars
if (typeof $D == "undefined") {$D = YAHOO.util.Dom;}
if (typeof $E == "undefined") {$E = YAHOO.util.Event;}

/*****************************************************
 Class: Page
******************************************************/
DMD.Page = function()
{
	return {
		//--------------------------------------------------
		init : function(e)
		{
			var list = [];

			// Add 'expand' button to elements with associated class
			list = $D.getElementsByClassName('expand');
			for (var i=0, len=list.length; i<len; ++i) {
				DMD.TextareaExpander.setup(list[i]);
			}

			// Add 'target=_blank' attribute to elements with associated class
			list = $D.getElementsByClassName('targetBlank');
			for (var i=0, len=list.length; i<len; ++i) {
				if ( list[i].nodeName == 'A' ) {
					list[i].target = "_blank";
				}
			}
		}
	}
}();

/*****************************************************
 Class: Image
******************************************************/
DMD.Image = function()
{
	var _preloadList = null;

	return {
		rootUrl : '/library/img',

		//--------------------------------------------------
		preload : function(file)
		{
			var path = DMD.Image.rootUrl + file;

			if (_preloadList == null) {
				_preloadList = new Array();
			}

			var index = _preloadList.length;
			_preloadList[index] = new Image();
			_preloadList[index].src = path;
		}
	}
}();

/*****************************************************
 Class: Utility
******************************************************/
DMD.Util = function()
{
	return {
		//--------------------------------------------------
		hasAttribute : function(el, attr) {
			return el.getAttribute(attr) && el.getAttribute(attr) != '' && el.getAttribute(attr) != null;
		},

		//--------------------------------------------------
		trim : function(value) {
			return DMD.Util.lTrim(DMD.Util.rTrim(value));
		},
		lTrim : function(value) {
			return value.replace(/^\s+/, '');
		},
		rTrim : function(value) {
			return value.replace(/\s+$/, '');
		},

		//--------------------------------------------------
		setVisible : function(el, isVisible) {
			var value = isVisible ? '' : 'none';
			$D.setStyle($D.get(el), 'display', value);
		},

		//--------------------------------------------------
		getParentElement : function(el, tag)
		{
			var parentEl = el.parentNode;
			while (parentEl.nodeName != tag.toUpperCase() && parentEl.parentNode != null) {
				parentEl = parentEl.parentNode;
			}
			return parentEl.nodeName == tag.toUpperCase() ? parentEl : null;
		},

		//--------------------------------------------------
		debug : function(el)
		{
			var m = "";
			for (var p in el) {
				m += p + ": '" + el[p] + "'\n";
			}

			// Write to Firebug console (Firefox only)
			if (YAHOO.ext.util.Browser.isGecko) {console.info(m);}
			else {alert(m);}
		}
	}
}();
