// some shorthand functions
function createEl(tag)
{
    return document.createElement(tag);
}

function createText(text)
{
	return document.createTextNode(text);
}

function getEl(el_name)
{
    var el = document.getElementById(el_name);
    if (!el) {
        return false
    } else {
        return el;
    }
}

function toggleDisplay(id)
{
    el = getEl(id);

    el.style.display = el.style.display == 'none'
        ? 'block'
        : 'none';

    return el.style.display;
}

function toggleVisibility(id)
{
    el = getEl(id);

    el.style.visibility = el.style.visibility == 'hidden'
        ? 'visible'
        : 'hidden';

    return el.style.visibility;
}

// cookies
// deprecated. use snow.Cookie
function setCookie(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 getCookie(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 clearCookie(name)
{
    createCookie(name,"",-1);
}

// toggle search form
function toggleSearchForm()
{
    var display = toggleDisplay('search_form');
    setCookie('search_form_display', display);
}

// form handling
function select_all_checkboxes(pref, num)
{
	for (var i=0; i < num; i++)
	{
		var checkbox = document.getElementById(pref + i);
		checkbox.checked = true;
	}
}

/* a few handy event functions */
function getEvent(evt)
{
    return evt ? evt : (event ? event : null);
}

function killEvent(evt)
{
    // DOM2
    try
    {
        evt.stopPropagation();
    }
    catch (e)
    {
        // IE
        evt.cancelBubble = true;
    }
}

function getEventTarget(evt)
{
    return (evt.target)
        ? evt.target
        : (evt.srcElement)
            ? evt.srcElement
            : null;
}

// other normalizations...

// get reference to document object for a window
// takes reference to element (eg., iframe) document is in
function getDoc(el)
{
    try
	{
        // DOM2
        if (el.contentDocument)
		{
            return el.contentDocument;
        // IE
        }
		else if (el.Document)
		{
            return el.Document;

        }
		else
		{
            var msg = 'Unable to find iframe content document';
            var e = new Error(msg);
            throw(e);
        }
    }
	catch (e)
	{
        var eh = new ErrorHandler();
        eh.send(e, E_FATAL);
    }
}

/* inheritance sugar */

// ExtObj is our new base object. just a couple of convenient bits
function ExtObj() { } 

ExtObj.prototype = new Object;
ExtObj.prototype.constructor = Object;
ExtObj.superclass = Object.prototype;

ExtObj.prototype.toString = function()
{
    return '[ ExtObj ]';
}

ExtObj.prototype._isPure = function(args)
{
    return args && (args[0] == PURE) ? true : false;
}

// if initializations to the object need to be
// avoided when it's being inherited from, put
// those in init(). It's only called by the default
// constructor if _isPure() returns true
ExtObj.prototype.init = function() { }

Function.prototype.inherits = function(parent)
{
    try
	{
        this.prototype = new parent(PURE);
        this.prototype.constructor = parent;
        this.superclass = parent.prototype;
    }
	catch(e)
	{
        var eh = new ErrorHandler();
        eh.send(e, E_FATAL);
    }

    return this;
}

// default constructors
function newClass()
{
    var type = arguments.length ? arguments[0] : 'default';

    var f = false;

    switch(type)
    {
        case 'default':
            f = function()
            {
                this._isPure(arguments) ? '' : this.init();
            }
        break;

        case 'JaComponent':
            f = function()
            {
                if (this._isPure(arguments))
                {
                    // just inheriting
                    return this;
                }
                else
                {
                    if (arguments.length)
                    {
                        // manipulating previously created node
                        this.id = arguments[0];
                    }
                    else
                    {
                        // brand new
                    }
                    this.init();
                }
            }
        break;
    }

    return f;
}

/* end inheritance sugar */

function message(msg)
{
    var d = getEl('debug_out');
    d.innerHTML = d.innerHTML + '<p>' + msg + '</p>';
}

function var_dump(thing)
{
    var pad = ' ';
	var depth;
	var dump;
	if (arguments[1])
	{
		depth = arguments[1];
	}
	else
	{
		depth = 1;
	}
	for (i = 0; i < depth; i++)
	{
		pad += ' ';
	}

	var type = typeof(thing);
	if (type == 'object' && thing instanceof Array)
	{
		type = 'array';
	}

	dump = pad + type;
	if (type == 'object' || type == 'array')
	{
		depth++;
		dump += "\n";
		for (i in thing)
		{
			dump = dump + pad + pad + i + ':' + var_dump(thing[i], depth) + "\n";
		}
	}
	else
	{
		dump += pad + thing + "\n";
	}
	return dump;
}

function out(msg)
{
	var out = getEl('debug_out');
	if (!out)
	{
		var el = createEl('pre');
		el.setAttribute('id', 'debug_out');
		var body = document.lastChild;
		body.appendChild(el);
		out = getEl('debug_out');
	}
	out.innerHTML += msg + "\n";
}
