
WeGluCollapsablePane = function() {

var paneGroups;
var collapsingTime;
var collapsingSeq;

this.paneGroups = new Array();
this.collapsingTime = 800;  // Duration in milliseconds of colapsing animation
this.collapsingSeq = [0, 2, 4, 8, 16, 32, 50, 68, 84, 92, 96, 98, 100];
}

WeGluCollapsablePane.prototype =
{

createPane:function (paneTarget, paneContent, collapsed, group, onPaneCollapsed) {

	if(typeof(paneTarget) == "string")
	  paneTarget = document.getElementById(paneTarget);
	if(typeof(paneContent) == "string")
	  paneContent = document.getElementById(paneContent);
	  
	paneContent.style.overflow = "hidden";
	paneContent.style.display = "block";
	paneTarget.contentObj = paneContent;
	if(collapsed != true)  collapsed = false;
	if(collapsed) {
	  paneTarget.keepHeight = paneTarget.contentObj.offsetHeight;
	  paneTarget.contentObj.style.height = "0px";
	  paneTarget.contentObj.style.display = "none";
	}
	paneTarget.collapsed = collapsed;
	paneTarget.collapsing = false;
	paneTarget.targetObj = this;
	paneTarget.onclick = this.paneClick;
	paneTarget.onPaneCollapsed = onPaneCollapsed;

	if(group != undefined  &&  group != "") {
	  this.addPaneToGroup(group, paneTarget);
	}
	else
	  paneTarget.group = false;
},

getPaneGroup:function (name) {
	var grp = false;
	for(var n = 0; n < this.paneGroups.length; n++) {
	  if(this.paneGroups[n].name == name) {
	    grp = this.paneGroups[n];
		break;
	  }
	}
	return grp;
},

// Adds a pane to a pane group
addPaneToGroup:function (group, pane) {

	var grp = this.getPaneGroup(group);
	if(!grp) {
	  grp = new Object();
	  grp.name = group;
	  grp.items = new Array();
	  this.paneGroups[this.paneGroups.length] = grp;
	}
	var exists = false;
	for(var n = 0; n < grp.items.length; n++) {
	  if(grp.items[n].id == pane.id) {
	    exists = true;
		break;
	  }
	}
	if(!exists) {
	  grp.items[grp.items.length] = pane;
	}
	pane.group = group;
},

paneClick:function () {

	this.targetObj.doCollapsingPane(this, !this.collapsed);
},

//  Collapses/uncollapses a pane
//  In the case of uncollpsing, returns the pane in the same group that had to be collapsed
doCollapsingPane:function (pane, collapse) {

	var res = undefined;
	if(pane.collapsing)
	  return res;
	if(pane.collapsed == collapse)
	  return res;
	pane.collapsing = true;
	pane.collapsed = collapse;
	
	var d = new Date();
	var t = d.getTime();
	pane.startTime = t;
	
	if(collapse) {
	  pane.keepHeight = pane.contentObj.offsetHeight;
	  window.collapsePaneRef = this;
	  setTimeout("window.collapsePaneRef.collapsePaneAnim('"+ pane.id +"')", 5);
	}
	else {
	
	  var keepH = pane.contentObj.style.height;
	  pane.contentObj.style.display = "block";
	  pane.contentObj.style.height = "auto";
	  pane.keepHeight = pane.contentObj.offsetHeight;
	  pane.contentObj.style.height = keepH;
	  window.collapsePaneRef = this;
	  setTimeout("window.collapsePaneRef.uncollapsePaneAnim('"+ pane.id +"')", 5);
	  // Collapses the other panes in the group
	  if(pane.group) {
	    res = this.collapseGroup(pane);
	  }
	}
	return res;
},

// Collapses all the other panes in a given pane's group
collapseGroup:function (pane) {

	var res = undefined;
	var grp = this.getPaneGroup(pane.group);
	if(grp) {
	  var items = grp.items;
	  var p;
	  for(var n = 0; n < items.length; n++) {
	    p = items[n];
		if(p.id != pane.id  &&  !p.collapsed) {
		  this.doCollapsingPane(p, true);
		  res = p;
		}
	  }
	}
	return res;
},

collapsePaneAnim:function (sobj) {
	var obj = document.getElementById(sobj);
	if(!obj)
	  return;

	var d = new Date();
	var t = d.getTime() - obj.startTime;
	var ind = Math.round((t * 12) / this.collapsingTime);
	if(ind > 12)
	  ind = 12;
	ind = 12 - ind;
	var h = Math.floor((this.collapsingSeq[ind] * obj.keepHeight) / 100);
	obj.contentObj.style.height = h + "px";
	if(ind <= 0) {
	  obj.contentObj.style.display = "none";
	  obj.collapsing = false;
	  if(obj.onPaneCollapsed)
	    obj.onPaneCollapsed(obj);
	}
	else {
	  window.collapsePaneRef = this;
	  setTimeout("window.collapsePaneRef.collapsePaneAnim('"+ sobj +"')", 40);
	}
	
/*
	var dd = Math.floor(obj.contentObj.offsetHeight / 3);
	if(dd < 8)  dd = 8;
	var h = obj.contentObj.offsetHeight - dd;
	if(h < 0)
	  h = 0;
	obj.contentObj.style.height = h + "px";
	if(h == 0) {
	  obj.collapsing = false;
	  if(obj.onPaneCollapsed)
	    obj.onPaneCollapsed(obj);
	}
	else {
	  window.collapsePaneRef = this;
	  setTimeout("window.collapsePaneRef.collapsePaneAnim('"+ sobj +"')", 40);
	}
*/
},

uncollapsePaneAnim:function (sobj) {
	var obj = document.getElementById(sobj);
	if(!obj)
	  return;
	var d = new Date();
	var t = d.getTime() - obj.startTime;
	var ind = Math.round((t * 12) / this.collapsingTime);
	if(ind > 12)
	  ind = 12;
	var h = Math.floor((this.collapsingSeq[ind] * obj.keepHeight) / 100);
	obj.contentObj.style.height = h + "px";
	
	if(ind >= 12) {
	  obj.collapsing = false;
	  obj.contentObj.style.height = "auto";
	  if(obj.onPaneCollapsed)
	    obj.onPaneCollapsed(obj);
	}
	else {
	  window.collapsePaneRef = this;
	  setTimeout("window.collapsePaneRef.uncollapsePaneAnim('"+ sobj +"')", 40);
	}
	
/*
	var dd = Math.floor(obj.contentObj.offsetHeight / 2);
	if(dd < 8)  dd = 8;
	var h = obj.contentObj.offsetHeight + dd;
	if(h > obj.keepHeight)
	  h = obj.keepHeight;
	obj.contentObj.style.height = h + "px";
	if(h == obj.keepHeight) {
	  obj.collapsing = false;
	  obj.contentObj.style.height = "auto";
	  if(obj.onPaneCollapsed)
	    obj.onPaneCollapsed(obj);
	}
	else {
	  window.collapsePaneRef = this;
	  setTimeout("window.collapsePaneRef.uncollapsePaneAnim('"+ sobj +"')", 40);
	}
*/
},

// Expands a child block inside a pane (that must be uncollapsed)
expandChild:function (pane, child) {

	if(typeof(pane) == "string")
	  pane = document.getElementById(pane);
	if(typeof(child) == "string")
	  child = document.getElementById(child);
	  
	if(pane.collapsing  ||  pane.collapsed)
	  return;
	  
	child.style.overflow = "hidden";
	if(child.style.display == "block") {
	  this.doExpandChid(child, true);
	}
	else {
	  this.doExpandChid(child, false);
	}
},

doExpandChid:function (child, collapse) {

	if(child.collapsing)
	  return;
	child.collapsing = true;
	if(collapse) {
	  window.collapsePaneRef = this;
	  setTimeout("window.collapsePaneRef.collapseChildAnim('"+ child.id +"')", 5);
	}
	else {
	  child.style.display = "block";
	  child.style.height = "auto";
	  child.keepHeight = child.offsetHeight;
	  child.style.height = "0px";
	  window.collapsePaneRef = this;
	  setTimeout("window.collapsePaneRef.uncollapseChildAnim('"+ child.id +"')", 5);
	}
},

collapseChildAnim:function (sobj) {
	var obj = document.getElementById(sobj);
	if(!obj)
	  return;
	var dd = Math.floor(obj.offsetHeight / 3);
	if(dd < 8)  dd = 8;
	var h = obj.offsetHeight - dd;
	if(h < 0)
	  h = 0;
	obj.style.height = h + "px";
	if(h == 0) {
	  obj.collapsing = false;
	  obj.style.display = "none";
	}
	else {
	  window.collapsePaneRef = this;
	  setTimeout("window.collapsePaneRef.collapseChildAnim('"+ sobj +"')", 5);
	}
},

uncollapseChildAnim:function (sobj) {
	var obj = document.getElementById(sobj);
	if(!obj)
	  return;
	var dd = Math.floor(obj.offsetHeight / 2);
	if(dd < 8)  dd = 8;
	var h = obj.offsetHeight + dd;
	if(h > obj.keepHeight)
	  h = obj.keepHeight;
	obj.style.height = h + "px";
	if(h == obj.keepHeight) {
	  obj.collapsing = false;
	  obj.style.height = "auto";
	}
	else {
	  window.collapsePaneRef = this;
	  setTimeout("window.collapsePaneRef.uncollapseChildAnim('"+ sobj +"')", 5);
	}
}


}

