/*************************************************
id: dhtml
version: 1.4
modified: 2008.07.10
project: Moon (v. 2.4)
author: Audrius Naslenas, audrius@vpu.lt
*************************************************/

function $() {
  var results = [], element;
  for (var i = 0; i < arguments.length; i++) {
    element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
	  results.push(element);
  }
  return results.length < 2 ? results[0] : results;
}

function getElementsByClassName(className, parentElement)
{
  var children = ($(parentElement) || document.body).getElementsByTagName('*');
  var el = [];
  var child,klass;
  for (var i = 0; i < children.length; i++) {
  	child = children[i];
	if (child.className) {
	  klass = " " + child.className + " ";
	  if (klass.indexOf(" " + className + " ") > -1) el.push(child);
    }
  }
  return el;
}

function getOffset(node) {
	var x=0,y=0;
	var parentNode = node;
	while (parentNode) {
		x += parentNode.offsetLeft;
		y += parentNode.offsetTop;
		parentNode = parentNode.offsetParent;
	}
	return {x:x,y:y};
}

// ********* darbui su klasemis *******
function objClass(obj) {}

objClass.prototype.initialize = function(obj)
{
    this.element=obj;
	var mEl=this.element.className.split(/\s+/);
	this.names=[];
	for (i in mEl) if (mEl[i].length > 0) this.names.push(mEl[i]);
}

objClass.prototype.set = function(className)
{
	this.element.className = className;
}

objClass.prototype.has = function (val)
{
	for (i in this.names)
		if (this.names[i] == val) return true;
	return false;
}

objClass.prototype.add = function(classNameToAdd)
{
	if (this.has(classNameToAdd)) return;
	this.names.push(classNameToAdd);
	this.set(this.names.join(' '));
}

objClass.prototype.remove = function(classNameToRemove)
{
	if (!this.has(classNameToRemove)) return;
	var mas=[];
	for (i in this.names)
		if (this.names[i] != classNameToRemove) mas.push(this.names[i]);
	this.names=mas;
	this.set(mas.join(' '));
}

objClass.prototype.toggle = function(className)
{
	if (this.has(className)) this.remove(className);
	else this.add(className);
    return this.has(className);
}

var $class=function (obj)
{
    if ($class.initObj==null) $class.initObj=new objClass();
	if  (typeof obj == 'string') {
        if ($class.objID!=null && $class.objID==obj) return $class.initObj;
        $class.objID=obj;
        obj= $(obj);
	} else $class.objID=null;
    $class.initObj.initialize(obj);
	return $class.initObj;
}

//************ Events ***************
function eventAdd (obj,type,fn)
{
	if (obj.attachEvent) {
		if (typeof(obj.eIndex) == "undefined") obj.eIndex=0;
		else obj.eIndex++;
		var i=obj.eIndex;
		obj['e'+i] = fn;
		obj[type+fn] = function() { obj['e'+i](window.event); }
		obj.attachEvent('on'+type,obj[type+fn]);
	}else if (obj.addEventListener)
		obj.addEventListener(type,fn,false);
}

function eventRemove (obj,type,fn) {
	if (obj.detachEvent) {
		obj.detachEvent('on'+type,obj[type+fn]);
		obj[type+fn] = null;
	} else
		obj.removeEventListener(type,fn,false);
}

//********** Ajax object ******************

function ajax(resultContentType)
{
	// resultType for firefox 'text/xml' or 'text/html'
	this.resultContentType = resultContentType==null ? 'text/xml':resultContentType;
	var agent = navigator.userAgent.toLowerCase();
	this.firefox=/firefox/.test(agent) ? true:false;
	this.http=null;
	this.stateFunct=null;
}

ajax.prototype.httpInit=function ()
{

	if(this.http && this.http.readyState!=0) {
       this.http.onreadystatechange=function(){};
	   this.http.abort();
    }
	if (this.firefox) this.http =null;
    if (!this.http) {
    	var xhr=null;
        if(window.XMLHttpRequest) {
			xhr = new XMLHttpRequest();// Firefox
			if (xhr.overrideMimeType) xhr.overrideMimeType(this.resultContentType);
		} else if(window.ActiveXObject) { // Internet Explorer
		    try {
		      xhr = new ActiveXObject("Msxml2.XMLHTTP");
		    } catch (e) {
		      try {
		        xhr = new ActiveXObject("Microsoft.XMLHTTP");
		      } catch (e1) {
		        xhr = null;
		      }
		    }
		  }
		this.http=xhr;
    }
    if (this.http) this.http.onreadystatechange=this.stateFunct;
}

ajax.prototype.get=function (uri)
{
    this.httpInit();
	if (!this.http) return;
	this.http.onreadystatechange=this.stateFunct;
	this.http.open("GET",uri,true);
    this.http.send(null);
}

ajax.prototype.post=function (uri,paramString)
{
    this.httpInit();
	if (!this.http) return;
	this.http.onreadystatechange=this.stateFunct;
	this.http.open("POST",uri,true);
    this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    this.http.setRequestHeader("Content-length", paramString.length);
    this.http.setRequestHeader("Connection", "close");
    this.http.send(paramString);
}

ajax.prototype.outputToFunction=function(func)
{
	this.stateFunct=func;
}