/**
*	Utility.js - what do you think it does?
*/

/**
*	straight stolen from prototype
*/
function $(id)
{
	return document.getElementById(id);
}

/**
*	This one is all mine - does not seem to work unless call things after window.onload()
*/
function trace(msg, target)
{
	var target = target || 'tracebox';
	var bars = " -------------------- ";
	var remsg = '<br/>';
	remsg += msg;
	remsg += '<br/>';
	remsg += bars;	
	
	if(!$(target))
	{
	    var div = document.createElement('div');
	    div.id = target;
		if(target == 'tracebox')
		{
		    with(div.style)
		    {
			    position = "absolute";
			    top = "0px"; 
			    right = "0px";
			    width = "170px";
			    height = "500px";
			    border = "1px solid #AAAAAA";
			    background = "#EEEEEE";
			    padding = "5px";
			    overflow = "auto";
			}
		}		
		div.innerHTML = '';
		document.body.appendChild(div);
	}
	
	$(target).innerHTML += remsg;
}

/*
*	Runs a single iteration through an object, then calls trace
*/
function oTrace(object)
{
	var msg = "";
	for(var i in object)
	{
		msg += '<b> ' + i + ':</b> ' + object[i] + '<br/>';
	}
	
	trace(msg);
}

/**
*	EZ remapping (I think that is the term for it ...
*/
function dw(msg)
{
	document.write(msg + '<br/>');
}

/**
*   HTML tag writing function
*   Aaron Lile 2008
*
*   @param element -    string, the element type. ie: 'p'
*   @param content -    the text node value.  ie: 'this is some sample content'
*                           note: other elements can be sent in the content, such as <em> tags
*   @param attributes - object containing any attributes desired in the element.  ie: {class:'leftNav'}
*   @return tmp -       the assembled element
*/
function enTag(element, content, attributes)
{
    element = element || "span";
    var tmp = document.createElement(element);
    tmp.innerHTML = content || "";
    
    if(attributes && typeof(attributes) == "object")
    {
        for(var i in attributes)
        {
            tmp[i] = attributes[i];
        }
    }
    return tmp;
}


/*
*   Takes an object / array, iterates through until down to primitives
*   @ param obj - object or array
*   @ param str - string value - for recursion
*   @ return str - modified string representation of the object
*/
function objectParse(obj, str)
{
    var tab = enTag('span', '&nbsp;', {style:'width:5px'});
    str = str || '';
   // trace('Array: ' + (obj instanceof Array) + "   object: " + (obj instanceof Object));
    if(obj instanceof Object)
    {
        for(var i in obj)
        {
            if(obj[i] instanceof Object)
            { 
                objectParse(obj[i], str);
                continue;
            }
            
            str += tab + i + ": " + obj[i] + "<br/>";            
        }
    }
    else
    {
        str += obj.toString();
    }
    
    return str;
    
}

/*
*   Builds a <select> group
*   @ param data - array containing the option values
*   @ param id - optional id for the <select> group
*   @ param sel - number of the default option, if any
*   @ return select - string containing the <select> group
*/

function enSelect(data, id, sel)
{
    var options = '';
    var sID = id || '';
    var sel = sel || 0;
    
    for(var i = 0; i < data.length; i++)
    {
        data[i] = data[i].toLowerCase();
        var present = capFirst(data[i]);
        
        if(i == sel)
        {
            options += enTag('option', present, {value:data[i], selected:'selected'});
        }
        else
        {
            options += enTag('option', present, {value:data[i]});
        }
       
    }
    var select = enTag('select', options, {id:sID});
    return select;
}

/*
*   Capitilizes the first letter in a word, and makes all others l/c
*   @ param str - string that gets its capital
*   @ return cap - string with a capital
*/
function capFirst(str)
{
    str = str.toLowerCase();
    
    var first = str[0].toUpperCase();
    var rest = str.substring(1);
   
    cap = first + rest;
   
    return cap;
    
}

/* Show / hide elements - turns visibility to hidden, position:absolute, or v/v for showing */

function hide(id)
{
    $(id).style.visibility = 'hidden';
    $(id).style.position = 'absolute';
}

function show(id)
{
    $(id).style.visibility = 'visible';
    $(id).style.position = 'static';
}

