function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function insertElement( container, element ) {
   if(getRef(container).insertAdjacentElement == null)
      getRef(container).appendChild( element )
   else
      getRef(container).insertAdjacentElement("beforeEnd", element); //Internet Explorer
}

function setStyle( obj, property, value ) {
	var element = getRef( obj );
	if( element ) {
		try
		{
			if( element.style ) element.style[property] = value;
			else element[property] = value;		
		}
		catch (err)
		{
			element[property] = value;
		}
	}
}

function getStyle( obj, property ) {
	var element = getRef( obj );
	if( element.style ) return element.style[property];
	else return element[property];
}

function is_function(obj) {
	if( !obj ) return false;
    return (obj.constructor.toString().indexOf("Function")!= -1);
}

function getCompStyle( obj, cAttribute ){
	var element = getRef( obj );

	//if IE
	if ( element.currentStyle ) {
		var curVal = eval( 'element.currentStyle.' + cAttribute );
	}
	else {
		//if Mozilla/FF
		var curVal = eval( 'document.defaultView.getComputedStyle( element, null ).' + cAttribute );
	}

	return curVal;
}

function setClass( obj, classname ) {
	getRef(obj).className = classname;
}

function getRef(obj){
	return (typeof obj == "string") ?
	document.getElementById(obj) : obj;
}

function objectExists( obj ) {
	return (document.getElementById(obj) != null);;
}

function addEvent(elm, evType, fn, useCapture) {
	elm = getRef( elm );

	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}

function setListeners(){
//---------------------------------------------------------------------------------
//Google Toolbar stuff
//---------------------------------------------------------------------------------
  inputList = document.getElementsByTagName("INPUT");
  for(i=0;i<inputList.length;i++){
	addEvent( inputList[i], "onpropertychange", restoreStyles, false );
	inputList[i].style.backgroundColor = "";
  }
  selectList = document.getElementsByTagName("SELECT");
  for(i=0;i<selectList.length;i++){
	addEvent( selectList[i], "onpropertychange", restoreStyles, false );
    selectList[i].style.backgroundColor = "";
  }

	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = getElementsByClass( 'hover' );

	for (var i = 0; i < aImages.length; i++) {
		var src = aImages[i].getAttribute('src');
		var ftype = src.substring(src.lastIndexOf('.'), src.length);
		var hsrc = src.replace(ftype, '_hover'+ftype);

		aImages[i].setAttribute('hsrc', hsrc);
		
		aPreLoad[i] = new Image();
		aPreLoad[i].src = hsrc;
		
		aImages[i].onmouseover = function() {
			sTempSrc = this.getAttribute('src');
			this.setAttribute('src', this.getAttribute('hsrc'));
		}	
		
		aImages[i].onmouseout = function() {
			if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_hover'+ftype, ftype);
			this.setAttribute('src', sTempSrc);
		}
	}
}

function restoreStyles(){
	if(event.srcElement.style.backgroundColor != "") {
		event.srcElement.style.backgroundColor = "";
	}
}

//---------------------------------------------------------------------------------

function checkIE() {

}

function pageHeight() {
	var myWidth = 0, myHeight = 0, iterate = true;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	return myHeight;
}

function pageWidth() {
	var myWidth = 0, myHeight = 0, iterate = true;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	return myWidth;
}

function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return -1;
}

function UIDialogsActivate() {
	setStyle( 'dialogs', 'display', 'block' );
	getRef( 'page' ).className = 'faded';
}

function UIDialogsDeActivate() {
	setStyle( 'dialogs', 'display', 'none' );
	getRef( 'page' ).className = '';
}

function createDialog( panel_id, url, style, callback ) {
	var panel = document.createElement('div');
	panel.id = panel_id;
	panel.setAttribute( "id", panel_id );
	panel.className = style;

	AJAXQueue.addGetInstruction( url, panel, function() { UIDialogsActivate(); callback(); } );

	insertElement( getRef( 'dialogs' ), panel );
}

function hideDialog( dialog ) {
	var panel = getRef( dialog );
	panel.parentNode.removeChild( panel );
	panel.id = '';
	panel.setAttribute("id","");
	panel.innerHTML = "";

	UIDialogsDeActivate();
}

function hideDialogNoDeactivate( dialog ) {
	var panel = getRef( dialog );
	panel.parentNode.removeChild( panel );
	panel.id = '';
	panel.setAttribute("id","");
	panel.innerHTML = "";
}



// ----------------------------------------------------------------------------------------
// Code from other places: ----------------------------------------------------------------
// ----------------------------------------------------------------------------------------

/**
 * Retrieve the absolute coordinates of an element.
 *
 * @param element
 *   A DOM element.
 * @return
 *   A hash containing keys 'x' and 'y'.
 */
function getAbsolutePosition(element) {
  var r = { x: element.offsetLeft, y: element.offsetTop };
  if (element.offsetParent) {
    var tmp = getAbsolutePosition(element.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
};

/**
 * Retrieve the coordinates of the given event relative to the center
 * of the widget.
 *
 * @param event
 *   A mouse-related DOM event.
 * @param reference
 *   A DOM element whose position we want to transform the mouse coordinates to.
 * @return
 *    A hash containing keys 'x' and 'y'.
 */
function getRelativeCoordinates(event, reference) {
  var x, y;
  event = event || window.event;
  var el = event.target || event.srcElement;

  if (!window.opera && typeof event.offsetX != 'undefined') {
    // Use offset coordinates and find common offsetParent
    var pos = { x: event.offsetX, y: event.offsetY };

    // Send the coordinates upwards through the offsetParent chain.
    var e = el;
    while (e) {
      e.mouseX = pos.x;
      e.mouseY = pos.y;
      pos.x += e.offsetLeft;
      pos.y += e.offsetTop;
      e = e.offsetParent;
    }

    // Look for the coordinates starting from the reference element.
    var e = reference;
    var offset = { x: 0, y: 0 }
    while (e) {
      if (typeof e.mouseX != 'undefined') {
        x = e.mouseX - offset.x;
        y = e.mouseY - offset.y;
        break;
      }
      offset.x += e.offsetLeft;
      offset.y += e.offsetTop;
      e = e.offsetParent;
    }

    // Reset stored coordinates
    e = el;
    while (e) {
      e.mouseX = undefined;
      e.mouseY = undefined;
      e = e.offsetParent;
    }
  }
  else {
    // Use absolute coordinates
    var pos = getAbsolutePosition(reference);
    x = event.pageX  - pos.x + reference.scrollLeft;
    y = event.pageY - pos.y + reference.scrollTop;
  }
  // Subtract distance to middle
  return { x: x, y: y };
}

function getTotalHeight( obj ) {
	var height = getHeight( obj ) + getExtraHeight( obj );
	return height;
}

function getTotalWidth( obj ) {
	var width = getWidth( obj ) + getExtraWidth( obj );
	return width;
}

function getHeight( obj ) {
	var height = parseInt( getCompStyle( obj, "height" ) );
	if( isNaN( height ) ) height = getRef(obj).offsetHeight;

	return height;
}

function getWidth( obj ) {
	var width = parseInt( getCompStyle( obj, "width" ) );
	if( isNaN( width ) ) width = getRef(obj).offsetWidth;

	return width;
}

function getExtraHeight( obj ) {
	var borderTopWidth = parseInt( getCompStyle( obj, "borderTopWidth" ) );
	if( isNaN( borderTopWidth ) ) {
		borderTopWidth = 1;
	}

	var borderBottomWidth = parseInt( getCompStyle( obj, "borderBottomWidth" ) );
	if( isNaN( borderBottomWidth ) ) {
		borderBottomWidth = 1;
	}

	var height = borderTopWidth + borderBottomWidth + ( isNaN( parseInt( getCompStyle( obj, "marginTop" ) ) ) ? 0 : parseInt( getCompStyle( obj, "marginTop" ) ) ) + ( isNaN( parseInt( getCompStyle( obj, "marginBottom" ) ) ) ? 0 : parseInt( getCompStyle( obj, "marginBottom" ) ) ) + ( isNaN( parseInt( getCompStyle( obj, "paddingTop" ) ) ) ? 0 : parseInt( getCompStyle( obj, "paddingTop" ) ) ) + ( isNaN( parseInt( getCompStyle( obj, "paddingBottom" ) ) ) ? 0 : parseInt( getCompStyle( obj, "paddingBottom" ) ) );

	return height;
}

function getExtraWidth( obj ) {
	var borderLeftWidth = parseInt( getCompStyle( obj, "borderLeftWidth" ) );
	if( isNaN( borderLeftWidth ) ) {
		borderLeftWidth = 1;
	}

	var borderRightWidth = parseInt( getCompStyle( obj, "borderRightWidth" ) );
	if( isNaN( borderRightWidth ) ) {
		borderRightWidth = 1;
	}

	var width = borderLeftWidth + borderRightWidth + ( isNaN( parseInt( getCompStyle( obj, "marginLeft" ) ) ) ? 0 : parseInt( getCompStyle( obj, "marginLeft" ) ) ) + ( isNaN( parseInt( getCompStyle( obj, "marginRight" ) ) ) ? 0 : parseInt( getCompStyle( obj, "marginRight" ) ) ) + ( isNaN( parseInt( getCompStyle( obj, "paddingLeft" ) ) ) ? 0 : parseInt( getCompStyle( obj, "paddingLeft" ) ) ) + ( isNaN( parseInt( getCompStyle( obj, "paddingRight" ) ) ) ? 0 : parseInt( getCompStyle( obj, "paddingRight" ) ) );
	return width;
}

function fitWidth( me, parent, other ) {
	if( !other ) {
		setStyle( me, "width", ( getWidth( parent ) - getExtraWidth( me ) ) + "px" );
	}
	else {
		setStyle( me, "width", ( getWidth( parent ) - getTotalWidth( other ) - getExtraWidth( me ) ) + "px" );
	}
}

function fitHeight( me, parent, other ) {
	if( !other ) {
		setStyle( me, "height", ( getHeight( parent ) - getExtraHeight( me ) ) + "px" );
	}
	else {
		setStyle( me, "height", ( getHeight( parent ) - getTotalHeight( other ) - getExtraHeight( me ) ) + "px" );
	}
}

function getMousePosition() {
	var pos = Array();
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

	pos["x"] = posx;
	pos["y"] = posy;

	return pos;
}

