/**
*	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 style = '';
	var bars = " -------------------- ";
	var remsg = '<br/>';
	remsg += msg;
	remsg += '<br/>';
	remsg += bars;
	
	
	if(!$(target))
	{
		if(target == 'tracebox')
		{
			style = "position:absolute;";
			style += "top:0px; right:0px;"
			style += "width:170px;"
			style += "border:1px solid #AAAAAA;";
			style += "background:#EEEEEE;";
			style += "padding:5px;";
		}
		
		document.body.innerHTML += '<div id="' + target + '" style="' + style +'"></div>';
		$(target).innerHTML = '';
	}
	
	$(target).innerHTML += remsg;
}

/**
*	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 tag -       a string with the assembled parameters
*/
function enTag(element, content, attributes)
{
    // check params
    var element = element || 'span';
    var content = content || '';
    var attributes = attributes || false;
    // open the tag
    var tag = "<" + element;
    // set element attributes, if any are given
    if(attributes)
    {
        for(var i in attributes)
        {
            tag += " " + i + "=\"" + attributes[i] + "\"";
        }
    }
    // close the tag
    switch(element)
    {
        case 'img':
        case 'input':
            tag += "/>";
            break;
        default:
            tag += ">" + content + "</" + element + ">";
    }
    // return the string
    return tag;
}


/*
*   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';
}