Function.prototype.bind = function(object) {
    var __method = this;
    return function() {
        return __method.apply(object, arguments);
    }
}
//_________________________________________________________________________________________________
function initBoxes() {
    this.oBoxes = new Object();
    this.oBoxesHbw = new Object();
    var aktNode;
    var i = 0;
    var j = 0;
    while(aktNode = document.getElementById("scrollTitle_" + (i))) {
         this.oBoxes[i] = new Box(aktNode, detail, false);
         i++;
    }
    if (detail == 'hotel') {
        for (j = 0; j <= 10; j++) {
            if (document.getElementById("scrollTitleHbw_" + (j))) {
                aktNode = document.getElementById("scrollTitleHbw_" + (j));
                this.oBoxesHbw[j] = new Box(aktNode, detail, true);
            }
        }
    }
    if (detail == 'termine') {
        var terminBox = document.getElementById("scrollTitle_99");
        this.oBoxes[99] = new Box(terminBox, detail, false);
        if (document.getElementById("scrollTitle_98")) {
            this.oBoxesHbw[98] = new Box(document.getElementById("scrollTitle_98"), detail, true);
        }
        if (engine == 'fewo') {
            this.oBoxes[97] = new Box(document.getElementById("scrollTitle_97"), detail, false);
        }

        // geprüfte Angebote
        if (document.getElementById("scrollTitle_90")) {
            this.oBoxes[90] = new Box(document.getElementById("scrollTitle_90"), detail, false);
        }
        if (document.getElementById("scrollTitle_91")) {
            this.oBoxes[91] = new Box(document.getElementById("scrollTitle_91"), detail, false);
        }
        if (document.getElementById("scrollTitle_92")) {
            this.oBoxes[92] = new Box(document.getElementById("scrollTitle_92"), detail, false);
        }

    }
}
var aktOpenBoxId = -1;
var openedIsHbw = false;
//_________________________________________________________________________________________________
function closeOtherAndShow(Box) {
    if(Box.stepper){
        window.clearInterval(Box.stepper);
        Box.stepper = null;
    }
    if (aktOpenBoxId >= 0) {
        if (openedIsHbw) {
            this.oBoxesHbw[aktOpenBoxId].hide();
            this.oBoxesHbw[aktOpenBoxId].setOtherBoxTitleOnHide();
        } else {
            this.oBoxes[aktOpenBoxId].hide();
            this.oBoxes[aktOpenBoxId].setOtherBoxTitleOnHide();
        }
    }
    if ((aktOpenBoxId != Box.boxId) || (openedIsHbw != Box.isHbw)) {
        aktOpenBoxId = Box.boxId;
        openedIsHbw = Box.isHbw;
        window.setTimeout(function(){Box.show()}.bind(this),100);
        Box.setThisBoxTitleOnShow(Box.isAlt);

    } else{
        //Box.hide();
        //Box.setThisBoxTitleOnHide();
        aktOpenBoxId = -1;
    }
}
//_________________________________________________________________________________________________
function Box(BoxTitle, detail, isHbw){
    this.title  = BoxTitle;
    this.detail = detail;
    this.showEffect = "1";
    this.hideEffect = "1";
    var aktNode=this.title;
    var tmpId = aktNode.getAttribute("id").split('_');
    this.boxId = tmpId[1];
    if (this.boxId >= '90') { // Alle zusätzlichen Boxen auf der Termin-Seite
        this.hasScrollLine = "0";
    } else {
        this.hasScrollLine = "1";
    }
    this.isHbw = isHbw;

    /* Setzen der ursprünglichen Klassen-Eigenschaften der geklickten Zeile */
    if (this.hasScrollLine == "1") {
        this.setOldClassesOfBox();
    }

    if (isHbw) {
        this.box = document.getElementById('scrollContentHbw_' + this.boxId);
        this.boxBottom = document.getElementById('scrollContentBottomHbw_' + this.boxId);
        this.closeLink = document.getElementById('scrollCloseLinkHbw_' + this.boxId);
    } else {
        this.box = document.getElementById('scrollContent_' + this.boxId);
        this.boxBottom = document.getElementById('scrollContentBottom_' + this.boxId);
        this.closeLink = document.getElementById('scrollCloseLink_' + this.boxId);
    }

    this.title.onclick=function(){
            closeOtherAndShow(this);
        }.bind(this);

    this.closeLink.onclick=function(){
            closeOtherAndShow(this);
        }.bind(this);
}
//_________________________________________________________________________________________________
Box.prototype.hide = function(){
    if(this.hideEffect!=""){
        this.transform(this.box.offsetHeight,0);
        //this.transform(Box.BoxDefaultHeight,0);
    }else{
        this.box.style.height="0px";
    }
    this.boxBottom.style.display = 'none';
}
//_________________________________________________________________________________________________
/*
    Funktion ermittelt die Position eines Elements anhand seiner Elternelement
    Parameter: ein Element (z.B. ermittelt durch getElementById
    Rückgabewert: ein Positionselement
*/
function getPosition(element)
{
  var elem=element,tagname="",x=0,y=0;

  while ((typeof(elem)=="object")&&(typeof(elem.tagName)!="undefined"))
  {
    y+=elem.offsetTop;
    x+=elem.offsetLeft;
    tagname=elem.tagName.toUpperCase();

    if (tagname=="BODY" || tagname == 'HTML')
      elem=0;

    if (typeof(elem)=="object")
      if (typeof(elem.offsetParent)=="object")
        elem=elem.offsetParent;
  }

  position=new Object();
  position.x=x;
  position.y=y;
  return position;
}
//_________________________________________________________________________________________________
Box.prototype.show = function(){
    if(this.showEffect!=""){
        //this.transform(0,this.box.scrollHeight);
        //this.transform(0,400);
        this.transform(0,Box.BoxDefaultHeight);
    }else{
       //this.box.style.height = this.box.scrollHeight+"px";
        //this.box.style.height = 400+"px";
        this.box.style.height = Box.BoxDefaultHeight+"px";
    }
    this.boxBottom.style.display = 'block';

    //window.scrollTo(0, this.boxPosition-50);
}
//_________________________________________________________________________________________________
Box.prototype.transform = function(from,to){
    this.posStart = from;
    this.posEnd = to;
    this.startTime = (new Date()).getTime();
    this.duration = 400;
    this.stepper = window.setInterval(this.step.bind(this),13);
}
//_________________________________________________________________________________________________
Box.prototype.step = function(){
    var time = (new Date()).getTime();
    if (time >= this.duration+this.startTime) {
        this.box.style.height =  this.posEnd+"px";
        window.clearInterval(this.stepper);
        this.stepper=null;
    }else {
        var pos = (time - this.startTime) / (this.duration);
        this.box.style.height = String(this.effect(pos)*(this.posEnd-this.posStart)+this.posStart)+"px";
    }
}
//_________________________________________________________________________________________________
Box.prototype.effect = function(pos){
    return ((-Math.cos(pos*Math.PI)/2) + 0.5);
}
//_________________________________________________________________________________________________
Box.prototype.setOtherBoxTitleOnHide = function(){
    var linkStr = '';

    switch (this.detail) {
        case 'zielgebiet':
            linkStr = 'Regionsinfo';
            document.getElementById("scrollLine_" + this.boxId).className = this.oldClassName + this.boxColor;
            break;
        case 'hotel':
            if (this.isHbw) {
                linkStr = 'Bewertungen lesen';
            } else {
                if (engine == 'fewo') {
                    linkStr = 'Objektinfos';
                } else {
                    linkStr = 'Hotelinfos';
                }
            }
            document.getElementById("scrollLine_" + this.boxId).className = this.oldClassName;
            document.getElementById("scrollLine_" + this.boxId + '_2').className = this.oldClassName;
            break;
        case 'termine':
            if (this.boxId == 99) {
                if (engine == 'fewo') {
                    linkStr = 'Länderinfos';
                } else {
                    linkStr = 'Umgebungsinfos';
                }
            } else if(this.boxId == 98) {
                linkStr = 'Bewertung lesen';
            }
            else {
                if (this.isHbw) {
                    linkStr = 'Bewertung lesen';
                } else {
                    if (engine == 'fewo') {
                        linkStr = 'Objektinfos';
                    } else {
                        linkStr = 'Hotelinfos';
                    }
                }
            }
            if (this.hasScrollLine == "1") {
                document.getElementById("scrollLine_" + this.boxId).className = this.oldClassName + this.boxColor;
            }
            break;
    }

    if ((detail == 'hotel' || detail == 'termine') && this.isHbw) {
        this.title.innerHTML = '<span onclick="refreshContent(' + this.boxId + ', \'hbw\');" class="arrowDownLink">' + linkStr + '</span>';
    } else {
        this.title.innerHTML = '<span onclick="refreshContent(' + this.boxId + ', \'hi\');" class="arrowDownLink">' + linkStr + '</span>';
    }
}
//_________________________________________________________________________________________________
// Funktion erstmal (scheinbar) überflüssig
/*
Box.prototype.setThisBoxTitleOnHide = function(){
    switch (this.detail) {
        case 'zielgebiet':
            this.title.innerHTML = '<span class="arrowDownLink">Regionsinfo</span>';
            document.getElementById("scrollLine_" + this.boxId).className = this.oldClassName + this.boxColor;
            break;
        case 'hotel':
            this.title.innerHTML = '<span class="arrowDownLink">Hotelinfos</span>';
            document.getElementById("scrollLine_" + this.boxId).className = this.oldClassName;
            document.getElementById("scrollLine_" + this.boxId + '_2').className = this.oldClassName;
            break;
        case 'termine':
            this.title.innerHTML = '<span class="arrowDownLink">Hotelinfos</span>';
            if (this.hasScrollLine == "1") {
                document.getElementById("scrollLine_" + this.boxId).className = this.oldClassName + this.boxColor;
            }
            break;
    }
}
*/
//_________________________________________________________________________________________________
Box.prototype.setThisBoxTitleOnShow = function(isAlt){
    var linkStr = '';

    switch (this.detail) {
        case 'zielgebiet':
            if (isAlt == '1'){
                 document.getElementById("scrollLine_" + this.boxId).className = 'regtabtrzielALT_open';
            } else {
                 document.getElementById("scrollLine_" + this.boxId).className = 'regtabtrziel_open';
            }
            break;
        case 'hotel':
            if (isAlt == '1'){
                 document.getElementById("scrollLine_" + this.boxId).className = 'hottrsubALT_open';
                 document.getElementById("scrollLine_" + this.boxId + '_2').className = 'hottrsubALT_open';
            } else {
                 document.getElementById("scrollLine_" + this.boxId).className = 'hottrsub_open';
                 document.getElementById("scrollLine_" + this.boxId + '_2').className = 'hottrsub_open';
            }
            break;
        case 'termine':
            if (this.hasScrollLine == "1") {
                if (isAlt == '1'){
                     document.getElementById("scrollLine_" + this.boxId).className = 'tertab5rowALT_open';
                } else {
                     document.getElementById("scrollLine_" + this.boxId).className = 'tertab5row_open';
                }
            }
            break;
    }
    if (this.isHbw) {
        linkStr = 'Bewertungen schließen';
    } else {
        if (this.boxId == '98') {
            linkStr = 'Bewertung schließen';
        } else {
            linkStr = 'Info schließen';
        }
    }
    this.title.innerHTML = '<span class="arrowUpLink">' + linkStr + '</span>';

    //this.boxPosition = getPosition(this.title).y;
}
//_________________________________________________________________________________________________
Box.prototype.setOldClassesOfBox = function(){
    switch (this.detail) {
        case 'zielgebiet':
            var scrollLine = document.getElementById('scrollLine_' + this.boxId);
            var boxClass = scrollLine.className;
            if (boxClass.indexOf('ALT') != '-1') {
                this.oldClassName = 'regtabtrzielALT';
                this.isAlt = '1';
            } else {
                this.oldClassName = 'regtabtrziel';
                this.isAlt = '0';
            }
            if (boxClass.indexOf('_dark') != '-1') {
                this.boxColor = '_dark';
            } else {
                this.boxColor = '_light';
            }
            break;
        case 'hotel':
            var scrollLine = document.getElementById('scrollLine_' + this.boxId);
            var boxClass = scrollLine.className;
            if (boxClass.indexOf('ALT') != '-1') {
                this.oldClassName = 'hottrsubALT';
                this.isAlt = '1';
            } else {
                this.oldClassName = 'hottrsub';
                this.isAlt = '0';
            }
            break;
        case 'termine':
            if (document.getElementById('scrollLine_' + this.boxId)) {
                var scrollLine = document.getElementById('scrollLine_' + this.boxId);
                var boxClass = scrollLine.className;
            } else {
                var boxClass = '';
            }
            if (boxClass.indexOf('ALT') != '-1') {
                this.oldClassName = 'tertab5rowALT';
                this.isAlt = '1';
            } else {
                this.oldClassName = 'tertab5row';
                this.isAlt = '0';
            }
            if (boxClass.indexOf('_dark') != '-1') {
                this.boxColor = '_dark';
            } else {
                this.boxColor = '_light';
            }
            break;
    }
}
