function $() {
	if ( arguments.length == 1 )
		return ( typeof arguments[0] == "string" ? document.getElementById(arguments[0]) : arguments[0] );
	
	var elements = new Array();
	for ( var i = 0; i < arguments.length; i++ )
		elements.push( typeof arguments[i] == "string" ? document.getElementById(arguments[i]) : arguments[i] )
	return elements;
}

/*
function addEvent( obj, evType, fn ) {
	if ( obj.addEventListener ) {
		obj.addEventListener( evType, fn, true );
		return true;
	}
	else if ( obj.attachEvent ) {
		return obj.attachEvent( "on"+evType, fn );
	}
	return false;
}*/

function trapBubble(e) {
	if ( e.stopPropagation )
		e.stopPropagation();
	else if ( e.cancelBubble )
		e.cancelBubble = true;
	else
		window.event.cancelBubble = true;
}

/* ------------------------------------------------------------ */

function newWindow( url ) {
	window.open( url, '_blank' );
}
function openWindow( url, winName, dialog, width, height, left, top, scrollbars, resizable, menubar, location, toolbar, status )
{
	if ( dialog && window.showModalDialog ) {
		var opts = "help: no; dialogWidth: " + (width+8) + "px; dialogHeight: " + (height+48) + "px";
		
		if ( left )  opts += "; center: no; dialogLeft: " + left + "; dialogTop: " + top;
		
		opts += "; scroll: " + ( scrollbars ? "yes" : "no" );
		opts += "; resizable: " + ( resizable ? "yes" : "no" );
		opts += "; status: " + ( status ? "yes" : "no" );
		
		return window.showModalDialog( url, this.window, opts );
	}
	
	
	if ( left == null )  left = ( screen.availWidth - width ) / 2
	if ( top == null )  top = ( screen.availHeight - height ) / 3
	
	var opts = "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top;
	
	if ( dialog )  opts += ",dialog=yes,modal=yes";
	
	opts += ",scrollbars=" + ( scrollbars ? "yes" : "no" );
	opts += ",resizable=" + ( resizable ? "yes" : "no" );
	opts += ",menubar=" + ( menubar ? "yes" : "no" );
	opts += ",location=" + ( location ? "yes" : "no" );
	opts += ",toolbar=" + ( toolbar ? "yes" : "no" );
	opts += ",status=" + ( status ? "yes" : "no" );
	
	w = window.open( url, winName, opts );
	w.focus();
}

/* ------------------------------------------------------------ */

function createCookie( 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 readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split( /\s*;\s*/ );
	for ( var i = 0; i < ca.length; i++ ) {
		var c = ca[i];
		if ( c.indexOf(nameEQ) == 0 )
			return c.substring( nameEQ.length, c.length );
	}
	return null;
}

function eraseCookie(name)
{
	createCookie( name, "", -1 );
}

/* ------------------------------------------------------------ */

// helper functions for working with css classes
function hasClass( obj, c ) {
	return obj.className.match( new RegExp( "\\b" + c + "\\b" ) );
}
function addClass( obj, c ) {
	if ( hasClass( obj, c ) )  return;
	
	if ( obj.className.length == 0 )
		obj.className = c;
	else
		obj.className += " " + c;
}
function removeClass( obj, c ) {
	obj.className = obj.className.replace( new RegExp( "\\s*\\b" + c + "\\b\\s*" ), "" );
}

/* ------------------------------------------------------------ */

// helper functions for working with lists
function ListAdd( lst, val ) {
	if ( lst.length == 0 )
		return val;
	else
		return lst + "," + val;
}
function ListRemove( lst, val ) {
	if ( lst == val )
		return "";
	else if ( lst.indexOf(",") < 0 )
		return lst;
	
	var items = lst.split(",");
	
	for ( var i = 0; i < items.length; ) {
		if ( items[i] == val )
			items.splice(i,1);
		else
			++i;
	}
	
	return items.join(",");
}

/* ------------------------------------------------------------ */

function findAncestor( obj, t, c ) {
	var p = obj.parentNode;
	if ( typeof p.tagName == "undefined" )
		return null;
	else if ( ( !t || p.tagName.toUpperCase() == t.toUpperCase() ) && ( !c || hasClass( p, c ) ) )
		return p;
	else
		return findAncestor( p, t, c );
}

function getTrueOffset( obj, parent ) {
	var offset = new Object();
	offset.top = 0;
	offset.left = 0;
	
	while ( obj && obj.offsetParent && obj != parent ) {
		offset.top += obj.offsetTop;
		offset.left += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	
	return offset;
}

function parseRecord(obj) {
	var _tokens = obj.id.split('_');
	
	var record = new Object();
	record.id = obj.id;
	record.container = _tokens[0];
	
	for ( var i = 2; i < _tokens.length; i++ ) {
		var n = _tokens[i].indexOf(':');
		record[ _tokens[i].substring(0,n) ] = _tokens[i].substring(n+1);
	}
	return record;
}
