// Part.js
// Javascript Behaviour for the Part 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
// -----
// 09.03.2006 created by Matthias Hertel
// 16.09.2006 context on event-methods is now set to the bound object.
// 23.09.2006 using images instead of text for the window frame buttons
// 05.05.2007 HideContent, ShowContent added, some simplifications.

var PartBehaviour = {
   /// <summary>Implementation of a JavaScript Behavior
   /// for a composite html control with a title and content area.</summary>

  state: "anystate", /// <summary>hello</summary>

  onclick: function (evt) {
    /// <summary>Handle the onclick event for the elements in the title area.</summary>
    evt = evt || window.event;
    if (evt.srcElement.className == "VEFull")
      this.ShowContent();
    else if (evt.srcElement.className == "VEMini")
      this.HideContent();
    else if (evt.srcElement.className == "VEClose")
      this.parentNode.removeChild(this);
  },

  onresize: function (evt) {
    /// <summary>Handle the onresize event. This event occurs when a window is resized and the part
    /// has no fixed width.</summary>
    this.resize();
  },


  // --- public methods ---
  
  init: function () {
    /// <summary>Initialze the part by showing the content and adjusting the size of the shadow.</summary>
    this.ShowContent();
    this.resize();
  }, // init


  ToggleContent: function () {
    /// <summary>Toggle the visibility of the content.</summary>
    var objs;
    
    objs = jcl.getElementsByClassName(this, "VEContent");
    if ((objs != null) && (objs[0] != null)) {
      if (objs[0].style.display == "none")
        this.ShowContent();
      else
        this.HideContent();
    } // if
    this.resize();
  },


  HideContent: function () {
    /// <summary>Hide the content and adjust the visibility of the optional titlebar buttons.</summary>
    var objs;

    objs = jcl.getElementsByClassName(this, "VEContent");
    if ((objs != null) && (objs[0] != null))
      objs[0].style.display = "none";

    objs = jcl.getElementsByClassName(this, "VEFull");
    if ((objs != null) && (objs[0] != null))
      objs[0].style.display = "";

    objs = jcl.getElementsByClassName(this, "VEMini");
    if ((objs != null) && (objs[0] != null))
      objs[0].style.display = "none";
    this.resize();
  },


  ShowContent: function () {
    /// <summary>Show the content and adjust the visibility of the optional titlebar buttons.</summary>
    var objs;

    objs = jcl.getElementsByClassName(this, "VEContent");
    if ((objs != null) && (objs[0] != null))
      objs[0].style.display = "";

    objs = jcl.getElementsByClassName(this, "VEFull");
    if ((objs != null) && (objs[0] != null))
      objs[0].style.display = "none";

    objs = jcl.getElementsByClassName(this, "VEMini");
    if ((objs != null) && (objs[0] != null))
      objs[0].style.display = "";
    this.resize();
  },


  resize: function () {
    /// <summary>Resize the shadow of the part.</summary>
    var o;
    var h = 0;
    var w = 0;
    o = jcl.getElementsByClassName(this, "VEMover");
    if (o.length > 0) { h += o[0].offsetHeight; w = Math.max(w, o[0].offsetWidth); }
    o = jcl.getElementsByClassName(this, "VETitle");
    if (o.length > 0) { h += o[0].offsetHeight; w = Math.max(w, o[0].offsetWidth); }
    o = jcl.getElementsByClassName(this, "VEContent");
    if (o.length > 0) { h += o[0].offsetHeight; w = Math.max(w, o[0].offsetWidth); }
    o = jcl.getElementsByClassName(this, "VEShadow");
    if (o.length > 0) {
      this.style.height = o[0].style.height = h + "px";
      o[0].style.width = w + "px";
    }
  } // resize


  // --- private methods ---

} // PartBehaviour
