// dale_utils.js

// Add array push function
if (typeof Array.prototype.push == 'undefined')
{
	Array.prototype.push = function () {
		var i=0, b=this.length, a=arguments;
		for (i; i<a.length;i++)
		{
			this[b+i]=a[i];
		}
		return this.length;
	};
}

// trim
function trim(str)
{
	return str.replace(/^\s*|\s*$/g,"");
}
// Gets the child elements of a certain tag and type from a dom object.
function getElementsByClassName(dom, tag_name, class_name)
{
	var classRE = new RegExp('\\b'+class_name+'\\b');
	var results = [];
	var temp_tags = dom.getElementsByTagName(tag_name);
	for (var i=0; i < temp_tags.length; i++)
	{
		if (classRE.test(temp_tags[i].className))
		{
			results.push(temp_tags[i]);
		}
	}
	return results;
}

// Creates a javascript object from a <dl>. The <dt>'s function as the
// property name while the <dd>'s the value.
// Exmaple: <dl id="dbml_obj">
//				<dt>Word</dt>
//					<dd>Some value for Word</dd>
//				<dt>some_key</dt>
//					<dd>some value</dd>
//			</dl>
//			This gets turned into { 'Word': 'Some value for Word', 'some_key': 'some value' }
//			So ...
//			var temp = document.getElementById("dbml_obj");
//			var result = createDBObject(temp);
//			alert(result.Word);
//			would popup "Some value for Word"
function createDBObject(dbml_dl)
{
	var dbml_object = new Object();
	dt_list = dbml_dl.getElementsByTagName("dt");
	dd_list = dbml_dl.getElementsByTagName("dd");
	for (var i=0; i<dt_list.length; i++)
	{
		// IE adds a white space for innerHTML. Trim both sides
		var key = trim(dt_list[i].innerHTML);
		var value = dd_list[i].innerHTML;
		// Encode the html entity &amp; into &.
		if (key == 'link')
		{
			value = value.replace(/\&amp;/g,"&");
		}
		// Add property to dbObject
		dbml_object[key] = value;
		// If this key is the image href, create an Image object from it.
		if (key == 'image_href')
		{
			dbml_object["image"] = new Image();
			dbml_object["image"].src = value;
		}
	}
	return dbml_object;
}
// Take in a dom object and a dbObject. Populates the dom object based on the classname of it's children
function render_object(dbObject, node)
{
	var cList = node.childNodes;
	for (var i=0; i < cList.length; i++)
	{
		var oChild = cList[i];
		// Get an array of class names for this child
		var classList = [];
		if (oChild.className)
		{
			classList = oChild.className.split(' ');
		}

		for (var j=0; j < classList.length; j++)
		{
			c_name = classList[j];
			if (dbObject[c_name])
			{
				if (c_name == 'image')
				{
					oChild.src = dbObject[c_name].src;
				}
				else if (c_name == 'link')
				{
					oChild.href = dbObject[c_name];
				}
				else 
				{
					var text_node = document.createTextNode(dbObject[c_name]);
					while (oChild.firstChild)
					{
						oChild.removeChild(oChild.firstChild);
					}
					oChild.appendChild(text_node);
				}
			}
			else if (c_name == 'image_viewer')
			{ // image_viewer is not a property of dbObject
				magic_resize(oChild, dbObject['image']);
			}
		}
		// Recursion is used since childNodes returns only nodes from 1 depth lower.
		render_object(dbObject, oChild);
	}
}

magic_resize =
function (viewer, image)
{
	// the actual image tag inside of the image viewer
	// Will use the first one found, but there should only
	// be one img as it stands.
	var viewer_image = viewer.getElementsByTagName('img')[0];

	// Variables to hold dimensions
	var v_height, v_width;
	var i_height, i_width;

	// Get the Viewer dimensions
	if (viewer.offsetHeight) 
	{
		v_height = viewer.offsetHeight;
		v_width = viewer.offsetWidth;
	}
	else if (viewer.style.pixelHeight)
	{
		v_height = viewer.pixelHeight;
		v_width = viewer.pixelWidth;
	}
	
	// Get image dimensions
	i_height = image.height;
	i_width = image.width;
	
	// Varibles to hold ratios
	var v_ratio = v_width / v_height;
	var i_ratio = i_width / i_height;
	
	if (i_ratio > v_ratio)
	{// Image has a wider aspect

		viewer_image.width = v_width;
		viewer_image.height = v_width / i_ratio;
	}
	else if (i_ratio < v_ratio)
	{// Image has a skinnier aspect

		viewer_image.height = v_height;
		viewer_image.width = v_height * i_ratio;
	}
	else 
	{ // Image has same aspect
		viewer_image.height = v_height;
		viewer_image.width = v_width;
	}
	viewer_image.src = image.src;
};	
