﻿// identisch mit Accordion.js
// Javascript Behaviour for the AccordionBehaviour Control
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
// ----- 
// 06.12.2010 created by MFO
var UC_SitemapBehaviour = {
  timer: null, // reference to the timer used for the transitions
  
  // ----- Events -----
  
  onmouseover: function (evt) {
    var src = evt.srcElement;
    while ((src != null) && (src != this) && (src.className != "HL" && src.className != "HLACCORDIONHEADERACTIVE"))
      src = src.parentNode;

//    if ((src != null) && (src != this) && src.className.indexOf("HL") == 0) {
//      src.style.backgroundColor="#f1f1f1";
//    } else if (src != null) {
//      src.style.backgroundColor = "Transparent";
//    }
  }, // onmouseover
  
  onclick: function (evt) {
  //alert(document.getElementById("portalnav").innerHTML);
    var src = evt.srcElement;
    while ((src != null) && (src != this) && (src.className.indexOf("ACCORDIONHEADER") < 0)) {
      src.style.backgroundColor = "Transparent";
      src = src.parentNode;
    }
//    
//    if ((src != null) && (src != this) && src.className.indexOf("HL") == 0) {
//      src.style.backgroundColor="#1B528C";
//    }
    evt = evt || window.event;
    if (this.timer == null && src.className.indexOf("ACCORDIONHEADER") > 0)
      this.SlideOpen(evt);
  }, // onclick  
  
  
  onmouseout: function (evt) {
    var src = evt.srcElement;
//    while ((src != null) && (src != this) && (src.className != "HL"))
//      src = src.parentNode;
//    if (src) {
//      src.style.backgroundColor = "Transparent";
//    }
  }, // onclick
  

  // ----- Methods -----
  
  // setup the timer and start the size transitions
  SlideOpen: function (evt) {
    evt = evt || window.event;
    var h, c, obj = evt.srcElement;

    // search the HEADER
    while ((obj != null) && (obj != this) && (obj.className != "VEACCORDIONHEADER"))
      obj = obj.parentNode;
    if ((obj != null) && (obj != this) && (obj.className == "VEACCORDIONHEADER"))
      obj.className = "HLACCORDIONHEADERACTIVE";
    h = obj;
    
    // search the next CONTENT
    while ((obj != null) && (obj != this) && (obj.className == null || obj.className != "VEACCORDIONCONTENT"))
      obj = obj.nextSibling;

    if ((obj != null) && (obj != this) && (obj.className != null && obj.className == "VEACCORDIONCONTENT")) {
      c = obj;
      c.style.height = "0px";
      c.className = "VEACCORDIONCONTENTACTIVE";
      
      // adjustClassNames
      var allElements = this.getElementsByTagName("div");
      for (var n = 0; n < allElements.length; n++) {
        var obj = allElements[n];
        if ((obj.className == "HLACCORDIONHEADERACTIVE") && (obj != h))
          obj.className = "VEACCORDIONHEADER";
        if ((obj.className == "VEACCORDIONCONTENTACTIVE") && (obj != c))
          obj.className = "VEACCORDIONCONTENT";
      } // for
      
      // start sliding...
      this.timer = window.setTimeout(this._resizeItem.bind(this), 5);
    } // if
  }, // SlideOpen
  

  _resizeItem: function (obj) {
    var allElements = this.getElementsByTagName("div");
    var isFinished = true;
    var delta;

    this.timer = null;

    for (var n = 0; n < allElements.length; n++) {
      var obj = allElements[n];
      if (obj.className == "VEACCORDIONCONTENTACTIVE") {
        // enlarge
        delta = obj.scrollHeight - obj.offsetHeight;
        if (delta <= 0) {
          // nothing.
        } else if ((delta <= 2) && (delta > 0)) {
          // snap exactly
          obj.style.height = obj.offsetHeight + 1 + "px";
        } else {
          obj.style.height = Math.round(obj.offsetHeight + Math.max(2, Math.min(12, delta/3))) + "px";
          isFinished = false;
        } // if
          
      } else if (obj.className == "VEACCORDIONCONTENT") {
        // shrink
        delta = obj.offsetHeight;
        if (delta <= 0) {
          // nothing.
        } else if (delta <= 2) {
          // snap exactly
          obj.style.height = "0px";
        } else {
          obj.style.height = Math.round(obj.offsetHeight - Math.max(2, Math.min(12, delta/3))) + "px";
          isFinished = false;
        } // if
      }
    } // for
    
    if (! isFinished) {
      this.timer = window.setTimeout(this._resizeItem.bind(this), 20);
    } else {
      allElements = this.getElementsByTagName("a");
      
      for (var n = 0; n < allElements.length; n++) {
        var obj = allElements[n];
        var head = obj;
        while ((head != null) && (head != this) && (head.className.indexOf("ACCORDIONHEADER") < 0))
          head = head.parentNode;
          
        var dynhref = obj.getAttribute("ohref");
        if (dynhref && head.className == "HLACCORDIONHEADERACTIVE") {
          obj.href = dynhref;
          obj.getAttribute("ohref").value = "none";
        } else if (dynhref && head.className == "VEACCORDIONHEADER") {
          if (dynhref == "none") {
            obj.getAttribute("ohref").value = obj.href;
            obj.removeAttribute("href");
          }
        }
      } // for
    }
  } // _resizeItem
  
} // UC_SitemapBehaviour
// End

