/**
* @author Raymond Chau
*
*/
SideBar=function(element){
this.element=element;
this.panels=new Array();
this.currentpanel=null;
} 
SideBar.prototype={
addPanel : function(panel){
this.panels.push(panel);
},
removePanel : function(panel){
this.panels.pop(panel);
panel.parent=null;
},
getPanels : function(){
return this.panels;
},
setCurrentPanel : function(panel){
this.currentpanel=panel;
},
getCurrentPanel : function(){
return this.currentpanel;
}  
} 
/**
* @classDescription Collapsible panel on the right hand side column of the page 
* 
*/
Panel=function(element,visible){
var panel=element; 
this.element=element;
this.id=element.id;
this.parent=null;
var divs=panel.getElementsByTagName("div");
for(var i=0;i<divs.length;i++){
if(divs[i].className=="panelcontent")
this.panelcontent=divs[i];
else if(divs[i].className.indexOf("widget")>-1){
var img=divs[i].getElementsByTagName("img");
if((img.length>0)&&(img[0].getAttribute("src").indexOf("minus.gif")!=-1)){
/*
this.minimizeLink=divs[i].getElementsByTagName("a")[0];
this.minimizeLink.panel=this;*/
this.minusButton=divs[i].getElementsByTagName("img")[0];
this.minusButton.panel=this;
this.paneltitle=divs[i].getElementsByTagName("span")[0];
}
}
}
if(this.minusButton) {this.minusButton.onclick=this.minusHandler;this.minusButton.className="active";}
if(!visible)this.collapse();
},
Panel.prototype={
setParent : function(parent){
this.parent=parent;
},
getParent : function(){
return this.parent;
},
disable : function(){
this.minusButton.onclick=null;
this.minusButton.className = "disabled";
},
/**
* Handler when the minimize button is clicked 
* @param {Object} e
*/
minusHandler : function(e){
var panel=this.panel;
var minusButton=panel.minusButton;
var panelcontent=panel.panelcontent;
if(minusButton.getAttribute("src").indexOf("minus")!=-1)
panel.collapse();		
else
panel.expand();
if(!e) var e=window.event;
e.cancelBubble=true;
if(e.stopPropagation)e.stopPropagation();
return false;
},	
/**
* Expand the panel
*/
expand : function(){
var panel=this;
var minusButton=panel.minusButton;
var panelcontent=panel.panelcontent;
var paneltitle=panel.paneltitle;	
var sidebar=panel.getParent();
var currentpanel=null;
if(sidebar){
currentpanel=sidebar.getCurrentPanel(); 
if(currentpanel && currentpanel!=panel){   
currentpanel.collapse();
sidebar.setCurrentPanel(panel);
}
}
minusButton.src=minusButton.getAttribute("src").replace("plus","minus");   
panelcontent.style.display="block";
paneltitle.className="on";
return false;
},
/**
* Collapse the panel
*/
collapse : function(){
var panel=this;
var minusButton=panel.minusButton;
var panelcontent=panel.panelcontent;
var paneltitle=panel.paneltitle;		
minusButton.src=minusButton.getAttribute("src").replace("minus","plus");		
panelcontent.style.display="none";
paneltitle.className="off";
return false;
}
}
