var undefined;

//*****************************************************

function ContentRotator(idx) {
  this.id = undefined;  
  this._direction = 'horizontal'; // horizontal,vertical 
  this._mode = 'standard'; // standard,fade
  this._speed = 10; // '1 - n' timeout-speed msec
  this._speedStep =  2; // '1 - n' Pixelschritte px  
  this._activeElem = undefined;  
  this._locked = false;    
  this._timeout = undefined;  
  this.contentElementList = [];  
  
  this._setId(idx);
}

//*****************************************************

ContentRotator.prototype._setId = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("ContentRotator->_setId: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("ContentRotator->_setId: Argument str ist nicht vom Typ String!");
  }
  this.id = str;
}        

//*****************************************************

ContentRotator.prototype._setActiveElem = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("ContentRotator->_setActiveElem: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("ContentRotator->_setActiveElem: Argument str ist nicht vom Typ String!");
  }
  var elem = document.getElementById(str);
  if (! elem) {
    focus();
    throw new Error("ContentRotator->_setActiveElem: Es existiert kein HTML-Element mit ID="+ str +"!");
  }
  this._activeElem = elem;
}       

//*****************************************************

ContentRotator.prototype._getActiveElem = function () {
  if (this._activeElem == undefined) {
    if (this.contentElementList.length) {
      this._setActiveElem(this.contentElementList[0].id);
    }
  }
  return  this._activeElem;
}   

//*****************************************************

ContentRotator.prototype._getRootElem = function () {
  var root = document.getElementById(this.id);
  if (! parent) {
    focus();
    throw new Error('ContentRotator->_getRootElem: Es existiert kein HTML-Element mit ID='+ this.id +'!');
  }
  return root;
}  

//*****************************************************

ContentRotator.prototype._getMoveElem = function () {
  var moveElem = document.getElementById('_ContentRotator_inner_'+ this.id);
  if (! parent) {
    focus();
    throw new Error('ContentRotator->_getMoveElem: Es existiert kein HTML-Element mit ID=_ContentRotator_inner_'+ this.id +'!');
  }
  return moveElem;
}

//*****************************************************

ContentRotator.prototype.setDirection = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("ContentRotator->setDirection: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("ContentRotator->setDirection: Argument str ist nicht vom Typ String!");
  }
  this._direction = str;
}

//*****************************************************

ContentRotator.prototype.setMode = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("ContentRotator->setMode: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("ContentRotator->setMode: Argument str ist nicht vom Typ String!");
  }
  this._mode = str;
}

//*****************************************************

ContentRotator.prototype.setSpeed = function (n){
  if (arguments.length!=1) {
    focus();
    throw new Error('ContentRotator->setSpeed: Falsche Anzahl von Argumenten!');
  }
  if (typeof n != 'number') {
    focus();
    throw new Error('ContentRotator->setSpeed: Argument n ist nicht vom Typ number!');
  }
  this._speed = n;
}

//*****************************************************

ContentRotator.prototype.setSpeedStep = function (n){
  if (arguments.length!=1) {
    focus();
    throw new Error('ContentRotator->setSpeedStep: Falsche Anzahl von Argumenten!');
  }
  if (typeof n != 'number') {
    focus();
    throw new Error('ContentRotator->setSpeedStep: Argument n ist nicht vom Typ number!');
  }
  this._speedStep = n;
}

//*****************************************************

ContentRotator.prototype.addContentElement = function (obj){
  if (arguments.length!=1) {
    focus();
    throw new Error('ContentRotator->addContentElement: Falsche Anzahl von Argumenten!');
  }
  if (! (obj instanceof ContentRotatorElement)) {
    focus();
    throw new Error('ContentRotator->addContentElement: Argument obj ist keine Instance von ContentRotatorElement!');
  }
  this.contentElementList.push(obj);
}

//*****************************************************

ContentRotator.prototype.draw = function (){
  var html = this._getContentHTML();
  var root = this._getRootElem();
  root.innerHTML = html;
}

//*****************************************************

ContentRotator.prototype.rotate = function (id){
  if (arguments.length!=1) {
    focus();
    throw new Error('ContentRotator->rotate: Falsche Anzahl von Argumenten!');
  }
  if (this._locked) {
    return;
  }
  this._locked = true;
  if (this._mode == 'fade') {
    return this._fade(id);
  }
  return this._move(id);
}

//*****************************************************

ContentRotator.prototype._move = function (id){
  if (arguments.length!=1) {
    focus();
    throw new Error('ContentRotator->_move: Falsche Anzahl von Argumenten!');
  }
  if (this._direction == 'vertical') {
    return this._moveVertical(id);
  }
   return this._moveHorizontal(id);
}

//*****************************************************

ContentRotator.prototype._moveHorizontal = function (id){
  if (this._timeout) {
    window.clearTimeout(this._timeout);
  }
  if (arguments.length!=1) {
    focus();
    throw new Error('ContentRotator->_moveHorizontal: Falsche Anzahl von Argumenten!');
  }
  var root = this._getRootElem();
  var moveElem = this._getMoveElem();
  var toRotate = document.getElementById(id);
  var posRoot = ContentRotator.getLeftPosition(root);
  var posToRotate = ContentRotator.getLeftPosition(toRotate);
  if (posRoot == posToRotate)  {
    this._setActiveElem(id);
    this._locked = false;
    return;
  }
  var moveStep = this._speedStep;
  if (Math.abs(posRoot - posToRotate) < moveStep) {
    moveStep = Math.abs(posRoot - posToRotate);
  }
  if (posToRotate > posRoot) {
    moveStep *= -1;
  }
  var _left = moveElem.style.left;
  if (_left == '') {
    //_left = ContentRotator.getLeftPosition(moveElem);
    _left = 0;
  }
  moveElem.style.left = parseFloat(parseFloat(_left) + moveStep) + 'px';
  var instance = ContentRotator.getInstance(this.id);
  var func = function () {
    return instance._moveHorizontal(id);
  }
  this._timeout = window.setTimeout(func, this._speed);
}

//*****************************************************

ContentRotator.prototype._moveVertical = function (id){
  if (this._timeout) {
    window.clearTimeout(this._timeout);
  }
  if (arguments.length!=1) {
    focus();
    throw new Error('ContentRotator->_moveVertical: Falsche Anzahl von Argumenten!');
  }
  var root = this._getRootElem();
  var moveElem = this._getMoveElem();
  var toRotate = document.getElementById(id);
  var posRoot = ContentRotator.getTopPosition(root);
  var posToRotate = ContentRotator.getTopPosition(toRotate);
  if (posRoot == posToRotate)  {
    this._setActiveElem(id);
    this._locked = false;
    return;
  }
  var moveStep = this._speedStep;
  if (Math.abs(posRoot - posToRotate) < moveStep) {
    moveStep = Math.abs(posRoot - posToRotate);
  }
  if (posToRotate > posRoot) {
    moveStep *= -1;
  }
  var _top = moveElem.style.top;
  if (_top == '') {
    //_top = ContentRotator.getTopPosition(moveElem);
    _top = 0;
  }
  moveElem.style.top = parseFloat(parseFloat(_top) + moveStep) + 'px';
  var instance = ContentRotator.getInstance(this.id);
  var func = function () {
    return instance._moveHorizontal(id);
  }
  this._timeout = window.setTimeout(func, this._speed);
}

//*****************************************************

ContentRotator.prototype._fade = function (id){
  if (arguments.length!=1) {
    focus();
    throw new Error('ContentRotator->_fade: Falsche Anzahl von Argumenten!');
  }
  var toRotate = document.getElementById(id);
  var active = this._getActiveElem();
  var instance = ContentRotator.getInstance(this.id);
  var endFuncFadeIn = function () {
    instance._setActiveElem(id);
    instance._locked = false;
  }
  var endFuncFadeOut = function () {
    instance._fadeMoveToPosition(id);
    instance._resetOpacity(active);
    instance._fadeIn(toRotate, endFuncFadeIn);
    //toRotate.focus();
    return;
  }
  this._fadeOut(active, endFuncFadeOut);
}

//*****************************************************

ContentRotator.prototype._fadeOut = function (elem, endFunc){
  if (this._timeout) {
    window.clearTimeout(this._timeout);
  }
  if (arguments.length<1) {
    focus();
    throw new Error('ContentRotator->_fadeOut: Falsche Anzahl von Argumenten!');
  }
  if (elem.filters != undefined){
    // IE
    var opValue = elem.filters['Alpha']['opacity'];
    if (opValue == undefined) {
      opValue = 100;
      elem.filters['Alpha']['opacity'] = opValue;
      elem.filters['Alpha']['finishopacity'] = opValue;
    }
    opValue = parseFloat(opValue);
    opValue -= 5;
    elem.filters['Alpha']['opacity'] = opValue;
    elem.filters['Alpha']['finishopacity'] = opValue;
  } else {
    // Other
    var opValue = elem.style.opacity;
    if (opValue == '') {
      opValue = 1;
      elem.style.opacity = opValue;
    }
    opValue = parseFloat(opValue);
    opValue -= 0.05;
    elem.style.opacity = opValue;
  }
  if (opValue > 0) {
    var instance = ContentRotator.getInstance(this.id);
    var func = function () {
      instance._fadeOut(elem, endFunc);
    }
    this._timeout = window.setTimeout(func, this._speed);
    return;
  }
  if (endFunc) {
    endFunc();
  }
}

//*****************************************************

ContentRotator.prototype._fadeIn = function (elem, endFunc){
  if (this._timeout) {
    window.clearTimeout(this._timeout);
  }
  if (arguments.length<1) {
    focus();
    throw new Error('ContentRotator->_fadeIn: Falsche Anzahl von Argumenten!');
  }
  var max = 1;
  if (elem.filters != undefined){
    // IE
    max = 100;
    var opValue = elem.filters['Alpha']['opacity'];
    if (opValue == undefined) {
      opValue = 0;
      elem.filters['Alpha']['opacity'] = opValue;
      elem.filters['Alpha']['finishopacity'] = opValue;
    }
    opValue = parseFloat(opValue);
    opValue += 5;
    elem.filters['Alpha']['opacity'] = opValue;
    elem.filters['Alpha']['finishopacity'] = opValue;
  } else {
    // Other
    var opValue = elem.style.opacity;
    if (opValue == '') {
      opValue = 0;
      elem.style.opacity = opValue;
    }   
    opValue = parseFloat(opValue);
    opValue += 0.05;
    elem.style.opacity = opValue;
  }
  if (opValue < max) {
    var instance = ContentRotator.getInstance(this.id);
    var func = function () {
      instance._fadeIn(elem, endFunc);
    }
    this._timeout = window.setTimeout(func, this._speed);
    return;
  }
  if (endFunc) {
    endFunc();
  }
}

//*****************************************************

ContentRotator.prototype._fadeMoveToPosition = function (id){
  if (arguments.length!=1) {
    focus();
    throw new Error('ContentRotator->_fadeMoveToPosition: Falsche Anzahl von Argumenten!');
  }
  var root = this._getRootElem();
  var toRotate = document.getElementById(id);
  var moveElem = this._getMoveElem();
  if (this._direction=='vertical') {
    var posRoot = ContentRotator.getTopPosition(root);
    var posToRotate = ContentRotator.getTopPosition(toRotate);
    var diff = parseFloat(posRoot - posToRotate);
    var _top = moveElem.style.top;
    if (_top == '') {
      _top = 0;
    }
    diff += parseFloat(_top);
    moveElem.style.top = diff + 'px';
    return;
  }
  var posRoot = ContentRotator.getLeftPosition(root);
  var posToRotate = ContentRotator.getLeftPosition(toRotate);
  var diff = parseFloat(posRoot - posToRotate);
  var _left = moveElem.style.left;
  if (_left == '') {
    _left = 0;
  }
  diff += parseFloat(_left);
  moveElem.style.left = diff + 'px';
}

//*****************************************************

ContentRotator.prototype._resetOpacity = function (elem){
  if (arguments.length!=1) {
    focus();
    throw new Error('ContentRotator->_resetOpacity: Falsche Anzahl von Argumenten!');
  }
  if (elem.filters != undefined){
    // IE
    elem.filters['Alpha']['opacity'] = 100;
    elem.filters['Alpha']['finishopacity'] = 100;
  } else {
    // Other
    elem.style.opacity = 1;
  }
}

//*****************************************************

ContentRotator.prototype._getContentHTML = function (){
   var html = '';
   html += '<div class="content-rotator-inner" id="_ContentRotator_inner_'+ this.id +'">\n'; 
     for (var i=0; i<this.contentElementList.length; i++) {
       var ce = this.contentElementList[i];
       html += '<div class="content-rotator-element" id="'+ ce.id +'">\n'; 
       html += ce.content;
       html += '<div class="float-aufheben"><br /></div>\n';
       var le = undefined;
       var ne = undefined;
       if (i>0 || i<parseFloat(this.contentElementList.length-1)) {
         html += '<p class="content-rotator-switch">\n'; 
         if (i>0) {
            le = this.contentElementList[i - 1];
            html += '<a href="javascript:void(0);" onclick="ContentRotator.getInstance(\''+ this.id +'\').rotate(\''+ le.id +'\');" class="content-rotator-switch">&#171;</a>'; 
         }          
         html += ' Säit '+ (i + 1) +' vun ' + this.contentElementList.length + ' '; 
         if (i<parseFloat(this.contentElementList.length-1)) {
           ne = this.contentElementList[i +1]; 
           html += '<a href="javascript:void(0);" onclick="ContentRotator.getInstance(\''+ this.id +'\').rotate(\''+ ne.id +'\');" class="content-rotator-switch">&#187;</a>'; 
         }          
         html += '</p>\n'; 
       }
       html += '<div class="float-aufheben"><br /></div>\n';
       html += '</div>\n'; 
     }
   html += '<div class="float-aufheben"><br /></div>\n'; 
   html += '</div>\n'; 
   return html;
}

//*****************************************************

ContentRotator._registerInstance = {};
ContentRotator._registerInstanceLength = [];
ContentRotator._increment = [];

//*****************************************************

ContentRotator.getTopPosition = function(elem) {
  if (arguments.length!=1) {
    focus();
    throw new Error("ContentRotator.getTopPosition: Falsche Anzahl von Argumenten!");
  }  
  var pos = 0;
  if (elem.nodeName.toLowerCase() == 'area'){
    elem = elem.parentNode;
  }
  if (elem.nodeName.toLowerCase() == 'map'){
    elem = elem.parentNode;
  }
  var nextElem = elem;
  pos += nextElem.offsetTop;
  while (nextElem.offsetParent) {
    pos += nextElem.offsetParent.offsetTop;
    nextElem = nextElem.offsetParent;
  }
  return parseFloat(pos);
}

//*****************************************************

ContentRotator.getLeftPosition = function(elem) {
  if (arguments.length!=1) {
    focus();
    throw new Error("ContentRotator.getLeftPosition: Falsche Anzahl von Argumenten!");
  }
  var pos = 0;
  if (elem.nodeName.toLowerCase() == 'area'){
    elem = elem.parentNode;
  }
  if (elem.nodeName.toLowerCase() == 'map'){
    elem = elem.parentNode;
  }
  var nextElem = elem;
  pos += nextElem.offsetLeft;
  while (nextElem.offsetParent) {
    pos += nextElem.offsetParent.offsetLeft;
    nextElem = nextElem.offsetParent;
  }
  return parseFloat(pos);
}

//*****************************************************

ContentRotator.getInstance = function(id) {
  if (arguments.length!=1) {
    throw new Error("Falsche Anzahl von Argumenten!");
  }
  if (! (ContentRotator._registerInstance[id])){
    focus();
    throw new Error("Es ist keine ContentRotator.Instance mit id=" + id + " registriert!");
  }
  return ContentRotator._registerInstance[id];
}

//*****************************************************

ContentRotator.createInstance = function(id) {
  if (!arguments.length) {
    id = '_ContentRotator_' + ContentRotator._increment.length;
    ContentRotator._increment.push(1);
  }
  if (! (ContentRotator._registerInstance[id])){
    ContentRotator._registerInstance[id] = new ContentRotator(id);
    ContentRotator._registerInstanceLength.push(id);
  } else {
    focus();
    throw new Error("ContentRotator.createInstance: id schon vorhanden!");
  }
  return ContentRotator.getInstance(id);
}



//*****************************************************
//*****************************************************
//*****************************************************
//*****************************************************

function ContentRotatorElement(idx) {
  this.id = undefined;
  this.content = '';
  
  this._setId(idx);
}

//*****************************************************

ContentRotatorElement.prototype._setId = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("ContentRotatorElement->_setId: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("ContentRotatorElement->_setId: Argument str ist nicht vom Typ String!");
  }
  this.id = str;
}                    

//*****************************************************

ContentRotatorElement.prototype.setContent = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("ContentRotatorElement->setContent: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("ContentRotatorElement->setContent: Argument str ist nicht vom Typ String!");
  }
  this.content = str;
}

//*****************************************************

ContentRotatorElement._registerInstance = {};
ContentRotatorElement._registerInstanceLength = [];
ContentRotatorElement._increment = [];

//*****************************************************

ContentRotatorElement.getInstance = function(id) {
  if (arguments.length!=1) {
    throw new Error("Falsche Anzahl von Argumenten!");
  }
  if (! (ContentRotatorElement._registerInstance[id])){
    focus();
    throw new Error("Es ist keine ContentRotatorElement.Instance mit id=" + id + " registriert!");
  }
  return ContentRotatorElement._registerInstance[id];
}

//*****************************************************

ContentRotatorElement.createInstance = function(id) {
  if (!arguments.length) {
    id = '_ContentRotatorElement_' + ContentRotatorElement._increment.length;
    ContentRotatorElement._increment.push(1);
  }
  if (! (ContentRotatorElement._registerInstance[id])){
    ContentRotatorElement._registerInstance[id] = new ContentRotatorElement(id);
    ContentRotatorElement._registerInstanceLength.push(id);
  } else {
    focus();
    throw new Error("ContentRotatorElement.createInstance: id schon vorhanden!");
  }
  return ContentRotatorElement.getInstance(id);
}

//*****************************************************

