// -----------------------
// --+ Acolyte-Objekt +---
// -----------------------

// Vorbereitung
var AcIE=window.attachEvent && !window.opera;
var AcOpera=!!window.opera;
var AcIsStrict=document.compatMode && (document.compatMode.indexOf('CSS')>-1);

// Shortcuts
function get(node) { if (typeof node=='string') node=document.getElementById(node); return(Acolyte.augment(node)); }
function create(nodename) { return(get(document.createElement(nodename))); }
function isint(value) { return(value.toString().match(/^[0-9]+$/g)); }
function getint(value) { if (!value) return(0); return(parseInt(value)); }

// Instanz bilden und Browsererkennung speichern
var Acolyte=
 {
   Browser:
    {
      IE:             AcIE,
      Gecko:          (navigator.userAgent.indexOf('Gecko')>-1) && (navigator.userAgent.indexOf('KHTML')==-1),
      Opera:          AcOpera,
      Safari:         navigator.vendor && (navigator.vendor.indexOf('Apple')>-1),
      isStrict:       AcIsStrict,
      hasBoxModelBug: !AcIsStrict && (AcIE  || (AcOpera && parseInt(navigator.appVersion)<9))
    }
 };

// Abfragen zur Breite und Hoehe des Displays, des sichtbaren Bereichs und der Scrollkoordinaten
Acolyte.getScreenWidth=function() { return(screen.availWidth); }
Acolyte.getScreenHeight=function() { return(screen.availHeight); }
Acolyte.getVisibleWidth=function()
 {
   return(Math.min
    (
     (document.documentElement && document.documentElement.clientWidth)?document.documentElement.clientWidth:screen.availWidth,
     (document.body && document.body.clientWidth)?document.body.clientWidth:screen.availWidth,
     window.innerWidth?window.innerWidth:screen.availWidth
    ));
 }
Acolyte.getVisibleHeight=function()
 {
   return(Math.min
    (
     (document.documentElement && document.documentElement.clientHeight)?document.documentElement.clientHeight:screen.availHeight,
     (document.body && document.body.clientHeight)?document.body.clientHeight:screen.availHeight,
     window.innerWidth?window.innerHeight:screen.availHeight
    ));
 }
Acolyte.getScrollWidth=function()
 {
   if (typeof window.pageXOffset=='number') return(window.pageXOffset);
   if ((document.body) && ((document.body.scrollLeft) || (document.body.scrollTop))) return(document.body.scrollLeft);
   return(document.documentElement.scrollLeft);
 }
Acolyte.getScrollHeight=function()
 {
   if (typeof window.pageYOffset=='number') return(window.pageYOffset);
   if ((document.body) && ((document.body.scrollLeft) || (document.body.scrollTop))) return(document.body.scrollTop);
   return(document.documentElement.scrollTop);
 }
Acolyte.getCenterX=function(width) { if (!width) width=0; return((this.getVisibleWidth()-width)/2+this.getScrollWidth()); }
Acolyte.getCenterY=function(height) { if (!height) height=0; return((this.getVisibleHeight()-height)/2+this.getScrollHeight()); }

// Events registrieren
Acolyte.addEvent=function(obj,type,fn)
 {
   if (obj.addEventListener) obj.addEventListener(type,fn,false); else
     if (obj.attachEvent) { obj['e'+type+fn]=fn; obj[type+fn]=function() { obj['e'+type+fn](window.event); }; obj.attachEvent('on'+type,obj[type+fn]); }
 }
Acolyte.removeEvent=function(obj,type,fn)
 {
   if (obj.removeEventListener) obj.removeEventListener(type,fn,false); else
     if (obj.detachEvent) { obj.detachEvent('on'+type,obj[type+fn]); obj[type+fn]=null; obj['e'+type+fn]=null; }
 }

// Ajax-Request
Acolyte.ajaxLoad=function(request,callback)
 {
   // Mozilla, Opera, Safari, Internet Explorer 7
   var xmlhttp=false;
   if (typeof(XMLHttpRequest)!='undefined') xmlhttp=new XMLHttpRequest();
   // Internet Explorer 6 und aelter
   if (!xmlhttp) try { xmlhttp=new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) { try { xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) { xmlhttp=false; } }
   // Anfrage abschicken
   if (xmlhttp)
    {
      if (callback) xmlhttp.onreadystatechange=callback;
      xmlhttp.open('GET',request,callback?true:false);
      xmlhttp.send(null);
      if (callback) return(xmlhttp);
      return(xmlhttp.responseText);
    }
   return(false);
 }

// Element augmentieren
Acolyte.augment=function(element)
 {
   if ((element) && (typeof element=='object') && (!element.setSize))
     for (var i in Acolyte.augmentation) element[i]=Acolyte.augmentation[i];
   return(element);
 }

// -------------------------------------------------
// --+ Erweiterte Element-Funktionen definieren +---
// -------------------------------------------------

// Augmentation-Objekt erstellen
Acolyte.augmentation=new Object();

// TODO: Testen (string "px"), Style-Fallback?
Acolyte.augmentation.getWidth=function() { return(this.offsetWidth); }
Acolyte.augmentation.getHeight=function() { return(this.offsetHeight); }

// Style setzen
Acolyte.augmentation.setStyle=function(style,value) { this.style[style]=value; return(this); }
Acolyte.augmentation.setStyles=function(styles) { for (var i in styles) this.style[i]=styles[i]; return(this); }

// Element verstecken
Acolyte.augmentation.hide=function(visibility) { if (visibility) this.style.visibility='hidden'; else this.style.display='none'; }

// Element sichtbar machen
Acolyte.augmentation.show=function(display)
 {
   if (!display) display='block';
   if ((!this.style.visibility) || (this.style.visibility=='hidden')) this.style.visibility='visible';
   if (this.style.display=='none') this.style.display=display;
 }

// Koordinaten auslesen
Acolyte.augmentation.getX=function()
 {
   if ((this.style.left!='') && (this.style.position=='absolute')) return(parseInt(this.style.left));
   var x=parseInt(this.offsetLeft),element=this;
   while (element.offsetParent) { element=element.offsetParent; if (element.offsetLeft) x+=parseInt(element.offsetLeft); }
   return(x);
 }
Acolyte.augmentation.getY=function()
 {
   if ((this.style.top!='') && (this.style.position=='absolute')) return(parseInt(this.style.top));
   var y=parseInt(this.offsetTop),element=this;
   while (element.offsetParent) { element=element.offsetParent; if (element.offsetTop) y+=parseInt(element.offsetTop); }
   return(y);
 }

// Koordinaten setzen
Acolyte.augmentation.setX=function(x) { if (isint(x)) x+='px'; this.style.left=x; return(this); }
Acolyte.augmentation.setY=function(y) { if (isint(y)) y+='px'; this.style.top=y; return(this); }
Acolyte.augmentation.setXY=function(x,y) { this.setX(x); this.setY(y); return(this); }
Acolyte.augmentation.center=function() { this.setXY(Acolyte.getCenterX(this.getWidth()),Acolyte.getCenterY(this.getHeight())); }

// Breite und Hoehe setzen
Acolyte.augmentation.setSize=function(width,height) { this.setWidth(width); this.setHeight(height); return(this); }
Acolyte.augmentation.setWidth=function(width)
 {
   if (isint(width)) { if (!Acolyte.Browser.hasBoxModelBug) width-=getint(this.style.paddingLeft)+getint(this.style.paddingRight)+getint(this.style.borderLeftWidth)+getint(this.style.borderRightWidth); width+='px'; }
   this.style.width=width;
   return(this);
 }
Acolyte.augmentation.setHeight=function(height)
 {
   if (isint(height)) { if (!Acolyte.Browser.hasBoxModelBug) height-=getint(this.style.paddingTop)+getint(this.style.paddingBottom)+getint(this.style.borderTopWidth)+getint(this.style.borderBottomWidth); height+='px'; }
   this.style.height=height;
   return(this);
 }

// Events
Acolyte.augmentation.addEvent=function(type,fn) { Acolyte.addEvent(this,type,fn); }

// Element verschiebbar machen
Acolyte.augmentation.setDraggable=function(handle)
 {
   if (!handle) handle=this; handle.dragparent=this;
   handle.addEvent('mousedown',this.startDrag);
   handle.addEvent('mouseup',this.endDrag);
   if (!Acolyte.draghandler) document.getElementsByTagName('body')[0].addEvent('mousemove',this.doDrag); Acolyte.draghandler=true;
   this.style.position='absolute';
 }
Acolyte.augmentation.startDrag=function(event) { Acolyte.dragactive=[this.dragparent,this.dragparent.getX(),this.dragparent.getY(),event.clientX,event.clientY]; }
Acolyte.augmentation.doDrag=function(event) { if (Acolyte.dragactive) Acolyte.dragactive[0].setXY(event.clientX-(Acolyte.dragactive[3]-Acolyte.dragactive[1]),event.clientY-(Acolyte.dragactive[4]-Acolyte.dragactive[2])); }
Acolyte.augmentation.endDrag=function(event) { Acolyte.dragactive=false; }