var accordionSlider =
function(objLayer, strIdentifier, intDefault, intChildDefault, intSpeed, intTimer) {
   this.array = [];
   this.defaultElem = (isNaN(parseInt(intDefault))) ? 0 : parseInt(intDefault);
   this.defaultChildElem = (isNaN(parseInt(intChildDefault))) ? 0 : parseInt(intChildDefault);
   this.speed = (isNaN(parseInt(intSpeed))) ? 10 : parseInt(intSpeed);
   this.timer = (isNaN(parseInt(intTimer))) ? 10 : parseInt(intTimer);
   this.identifier = (typeof (strIdentifier) == "string") ? strIdentifier : "accordion_class";
   this.headerID = "header";
   this.contentID = "content";
   this.startSetShrink = [];  // Array to push objects that need to be hidden on start
   this.lockOpenCurrent = false;
   this.slideHorizontally = false;
   this.isOpening = false;
   this.childSliders = [];    // Array of child accordionSlider objects associated with this object.
   this.totalAccordions = 0;  // Total number of 'accordion' nodes created by this object.

   this.init =
   function() {
      this.layer = (typeof (objLayer) == "string") ? document.getElementById(objLayer) : objLayer;
      if (this.layer == null) return;

      var ds, len

      ds = this.layer.getElementsByTagName("div");
      len = ds.length;

      for (var i = 0; i < len; i++) {
         var div, div_id;

         div = ds[i];
         div_id = div.id;

         // Exit here if the div is already made an accordion
         if (div.isAccordion != null) continue;

         if (div.className != null && div.className.indexOf(this.identifier) != -1) {
            var tmpSlider = new accordionSlider(div, this.identifier, this.defaultChildElem, 0, this.speed, this.timer);
            tmpSlider.lockOpenCurrent = this.lockOpenCurrent;
            tmpSlider.slideHorizontally = this.slideHorizontally;
            tmpSlider.init();
            if (tmpSlider.totalAccordions > 0) this.childSliders.push(tmpSlider);
         }
         else if (div_id.indexOf(this.headerID) != -1) {
            var sldr = this;
            div.oldClick = div.onclick;
            // Setup onclick event
            div.onclick = function() { if (this.oldClick) this.oldClick(); sldr.process(this); };
            // Setup mouse-over/out events
            div.defaultCSS = (div.className) ? div.className : "";
            div.onmouseover = function() { this.className = this.defaultCSS + " on"; };
            div.onmouseout = function() { this.className = this.defaultCSS; };
            // Setup link nodes in the headers
            this.handleHeaderLinkClick(div);
         }
         else if (div_id.indexOf(this.contentID) != -1) {
            // Increment the total number of accordions.
            this.totalAccordions++;

            if (!this.slideHorizontally)
               this.getDefaultHeight(div); // Save the div's original height;
            else
               this.getDefaultWidth(div); // Save the div's original width;

            // Find out if the fade filter should be applied to this layer
            div.noFilter = (div.getAttribute) ? div.getAttribute("noFilter") : false;
            div.noFilter = (typeof (div.noFilter) != "string") ? false :
                           (div.noFilter.toLowerCase().indexOf("true") != -1 ||
                            div.noFilter.toLowerCase().indexOf("1") != -1);

            // Push the partial id into the array
            var sharedID = div_id.replace(this.contentID, "");
            this.array.push(sharedID);

            // If the content div has a hideOnLoad attribute then find out
            //    its current value, if true then hide the content.
            var hideOnLoad = (div.getAttribute) ? div.getAttribute("hideOnLoad") : false;
            hideOnLoad = (typeof (hideOnLoad) != "string") ? false :
                           (hideOnLoad.toLowerCase().indexOf("true") != -1 ||
                            hideOnLoad.toLowerCase().indexOf("1") != -1);

            if (this.array.length != this.defaultElem || hideOnLoad)
               this.startSetShrink.push(div);
            else {
               var lastHeader = document.getElementById(sharedID + this.headerID);
               this.setHeaderCSS(lastHeader);

               // Set the divs default height or width.
               if (!this.slideHorizontally)
                  div.style.height = div.defaultHeight;
               else
                  div.style.width = div.defaultWidth;

               div.style.display = "block";
            }
         }
         // Indicate that the div was processed
         div.isAccordion = true;
      }

      // Go through the shrink array and shrink any div's in there
      for (var i = 0; i < this.startSetShrink.length; i++) {
         var sDiv = this.startSetShrink[i];
         // Save the div's original width/height;
         if (!this.slideHorizontally)
            sDiv.style.height = "0px";
         else
            sDiv.style.width = "0px";
         sDiv.style.display = "none";
      }
      // Delete the shrink array
      this.startSetShrink = null;
   };

   // Gets the content div's default width
   this.getDefaultWidth =
   function(div) {
      // Save the current offsetheight of the div
      var tmpW = div.offsetWidth;
      div.style.width = "auto";    // Change height to auto to get true height of div
      // Get the new offsetheight
      var newW = div.offsetWidth;

      // Test between the saved width and the new width.
      // If they are different then the width has been set through stylesheets
      if (newW != tmpW) {
         // Set the height to whatever is the lowest number
         if (newW > tmpW)
            div.defaultWidth = newW + "px";
         else
            div.defaultWidth = tmpW + "px";
      }
      else  // Else, the height is set by auto
         div.defaultWidth = "auto";

      div.maxw = tmpW;
   };

   // Gets the content div's default height
   this.getDefaultHeight =
   function(div) {
      // Save the current offsetheight of the div
      var tmpH = div.offsetHeight;
      div.style.height = "auto";    // Change height to auto to get true height of div
      // Get the new offsetheight
      var newH = div.offsetHeight;

      // Test between the saved height and the new height.
      // If they are different then the height has been set through stylesheets
      if (newH != tmpH) {
         // Set the height to whatever is the lowest number
         if (newH > tmpH)
            div.defaultHeight = newH + "px";
         else
            div.defaultHeight = tmpH + "px";
      }
      else  // Else, the height is set by auto
         div.defaultHeight = "auto";

      div.maxh = tmpH;
   };

   // Changes header nodes css classname's to the on or off position
   this.setHeaderCSS =
   function(div, on) {
      var on = (on == null) ? true : false;

      if (div == null) return;

      if (on) {
         div.oldCSS = div.defaultCSS;
         div.defaultCSS += " on down";
      }
      else
         div.defaultCSS = div.oldCSS;

      div.className = div.defaultCSS;
   };

   // Handles click events for link nodes located in the header
   this.handleHeaderLinkClick =
   function(div) {
      var lnks = div.getElementsByTagName("a");

      for (var i = 0; i < lnks.length; i++) {
         lnks[i].openLink = this.openHeaderLink;
         lnks[i].onclick = function(e) { killEvent(e); this.openLink(); };
      }
   };

   this.openHeaderLink =
   function() {
      if (this.href == null) return true;
      var lnkURL = this.href;
      var features = "resizable=1,width=980,height=550,toolbar=1," +
                     "status=1,titlebar=1,location=1,left=20,top=20";
      window.open(lnkURL, "_blank", features);
   };

   this.process =
   function(d) {
      if (this.lockOpenCurrent && this.isOpening) return;

      for (var i = 0; i < this.array.length; i++) {
         var s, h, c, cd;

         s = this.array[i];
         h = document.getElementById(s + this.headerID);

         // If the header is no longer present, remove it from the array
         if (h == null) { this.array.splice(i, 0); continue; }
         c = s + this.contentID;
         cd = document.getElementById(c);
         clearInterval(cd.timer);

         if (h == d && cd.style.display == 'none') {
            cd.style.display = "block";

            // Determine div's new width/height if it's default is set to auto
            this.processNodeWidthOrHeight(cd);

            this.setHeaderCSS(h);
            this.islide(c, 1);
            this.isOpening = true;
         }
         else if (cd.style.display == 'block') {
            if (h == d && this.lockOpenCurrent) continue;

            // Determine a new max width/height
            if (this.slideHorizontally)
               if (c.defaultWidth == "auto") {
               var newW = cd.offsetWidth;
               if (cd.maxw != newW) cd.maxw = newW;
            }
            else
               if (c.defaultHeight == "auto") {
               var newH = cd.offsetHeight;
               if (cd.maxh != newH) cd.maxh = newH;
            }

            this.setHeaderCSS(h, false);
            this.islide(c, -1)
         }
      }
   };

   this.processNodeWidthOrHeight =
   function(cd) {
      var checkWidth = (this.slideHorizontally && cd.defaultWidth == "auto");
      var checkHeight = (!this.slideHorizontally && cd.defaultHeight == "auto");
      // Determine div's new height if it's default height is set to auto
      if (checkWidth || checkHeight) {
         cd.style.visibility = "hidden";
         cd.style.position = "absolute";

         if (checkWidth) {
            // Try to get new element width
            cd.style.width = "auto";
            var newW = cd.offsetWidth;
            if (cd.maxw != newW) cd.maxw = newW;
            cd.style.width = "0px";
         }
         else {
            // Try to get new element height
            cd.style.height = "auto";
            var newH = cd.offsetHeight;
            if (cd.maxh != newH) cd.maxh = newH;
            cd.style.height = "0px";
         }
         cd.style.visibility = "visible";
         cd.style.position = "relative";
      }
   };

   this.islide =
   function(i, d) {
      var c;

      c = document.getElementById(i);
      if (c == null) return false;
      c.direction = d;
      var sldr = this;
      var tmpTimer;
      var tmpFunc = function() { sldr.slide(i, tmpTimer); };
      c.timer = setInterval(tmpFunc, this.timer);
      tmpTimer = c.timer;
   };

   this.slide =
   function(i, timer) {
      var c, m, n, dist;

      c = document.getElementById(i);
      if (c == null) {
         clearInterval(timer);
         return false;
      }

      m = this.slideHorizontally ? c.maxw : c.maxh;
      n = this.slideHorizontally ? c.offsetWidth : c.offsetHeight;

      if (n <= 0) n = 4;

      dist = (c.direction == 1) ? Math.round((m - n) / this.speed) : Math.round(n / this.speed);

      if (dist <= 1) { dist = 1 }

      if (this.slideHorizontally)
         c.style.width = n + (dist * c.direction) + 'px';
      else
         c.style.height = n + (dist * c.direction) + 'px';

      if (!c.noFilter) {
         c.style.opacity = n / m;
         c.style.filter = 'alpha(opacity=' + (n * 100 / m) + ')';
      }

      if ((n < 2) && (c.direction != 1)) {
         c.style.display = "none";
         clearInterval(c.timer);
         this.isOpening = false;
      }
      else if (n > (m - 2) && c.direction == 1) {
         clearInterval(c.timer)

         if (this.slideHorizontally)
            c.style.width = c.defaultWidth;
         else
            c.style.height = c.defaultHeight;

         if (!c.noFilter) {
            c.style.opacity = null;
            c.style.filter = null;
         }
         this.isOpening = false;
      }
   }
};


// Kill an event function
// Stop all page events
if (killEvent == null) {
   var killEvent =
      function(e) {
         // Stops the submit action
         if (window.event) // Try IE Method first
         {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
         }
         if (e && e.preventDefault && e.stopPropagation) // Next try DOM method
         {
            e.preventDefault();
            e.stopPropagation();
         }
         return false;  // Finally, just return false
      };
}


