












































Module('ca.carleton.gcrc.atlas.widgets.tab','$Revision: 3784 $',function(mod){
var modObservable=imprt('ca.carleton.gcrc.mvc.observable');
var modDisplayWidget=imprt('ca.carleton.gcrc.atlas.displayWidget');
var modDomUtil=imprt('ca.carleton.gcrc.dom.util');





mod.Factory=Class(modDisplayWidget.WidgetFactory,function(publ,priv,supr){




publ.__init__=function(
id_,
tab_
){

supr.__init__.call(this);


this.id=id_;
this.tab=tab_;
this.containedFactories=[];
this.tabName=[];
}




publ.IsHTML=function()
{
return true;
}





publ.Width=function()
{
return 0;
}

publ.GetType=function()
{
return'TabWidgetFactory';
}





publ.Height=function()
{
return 0;
}












publ.Create=function(
svgParent_,
dataModel_,
x_,
y_,
width_,
height_
)
{
return new mod.TabWidget(
this,
svgParent_,
dataModel_,
x_,
y_,
width_,
height_
);
}


publ.AddFactory=function(factory_)
{
if(factory_.IsHTML()==false)
return;

this.containedFactories.push(factory_);

var name=factory_.GetName();
if(name==null)
{
this.tabName.push("Tab"+this.tabName.length);
}
else
{
this.tabName.push(name);
}
}

publ.GetTabId=function(num)
{
return(this.tab+'_tab_'+num);
}

publ.GetTabContentId=function(num)
{
return this.containedFactories[num].GetId();
}

});





mod.TabWidget=Class(modObservable.Observer,function(publ,priv,supr){










publ.__init__=function(
factory_,
htmlParent_,
dataModel_,
x_,
y_,
width_,
height_
){

supr.__init__.call(this);

var myDocument=htmlParent_.ownerDocument;
this.tabContainer=myDocument.getElementById(factory_.id);
this.element=myDocument.getElementById(factory_.tab);
this.factory=factory_;

this.controlFeature=dataModel_.LayerFromName('_global').FeatureFromId('tab');


this.tabContainer.style.display='inline';
this.tabContainer.style.top=''+y_+'px';
this.tabContainer.style.left=''+x_+'px';
this.tabContainer.style.width=''+width_+'px';
this.tabContainer.style.height=''+height_+'px';

this.containedWidgets=[];


this.DrawTabs();


for(var i=0;i<this.factory.containedFactories.length;i++)
{
this.containedWidgets[i]=this.factory.containedFactories[i].Create(document.getElementById(this.factory.GetTabContentId(i)),
dataModel_,
x_,
y_,
width_,
height_);
}


var selectedIndex=0;
var currentName=this.controlFeature.GetVariableValue('current');
if(null!=currentName){
for(var i=0;i<this.factory.containedFactories.length;i++)
{
if(currentName==this.factory.tabName[i]){
selectedIndex=i;
}
}
}
this.SelectTab(selectedIndex);
}

publ.Remove=function()
{
this.tabContainer.style.display='none';

while(this.containedWidgets.length>0){
var widget=this.containedWidgets.pop();
widget.Remove();
}

modDomUtil.RemoveChildren(this.element);
}

publ.Hide=function()
{
this.tabContainer.style.display='none';
}

publ.Show=function()
{
this.tabContainer.style.display='block';
}


publ.GetWidgetName=function(index_)
{
if(index_<this.factory.tabName.length)
return this.factory.tabName[index_];

return null;
}


publ.SelectTab=function(sel)
{
for(var i=0;i<this.factory.containedFactories.length;i++)
{
if(i==sel)
{

document.getElementById(this.factory.GetTabContentId(i)).style.display="inline";
if(this.factory.containedFactories.length>1)
document.getElementById(this.factory.GetTabId(i)).style.backgroundColor="orange";

this.controlFeature.SetVariableValue('current',this.factory.tabName[i]);
}
else
{

document.getElementById(this.factory.GetTabContentId(i)).style.display="none";
if(this.factory.containedFactories.length>1)
document.getElementById(this.factory.GetTabId(i)).style.backgroundColor="grey";
}
}
}


publ.FindWidgetIndex=function(id_)
{
var i;
for(i=0;i<this.factory.containedFactories.length;i++)
{
if(id_==this.factory.containedFactories[i])
break;
}
return i;
}


publ.DrawTabs=function()
{
if(this.factory.containedFactories.length>1)
{

for(i=0;i<this.factory.containedFactories.length;i++)
{
this.CreateTab(i);
}


document.getElementById(this.factory.GetTabContentId(0)).parentNode.style.borderTop='2px solid orange';
}
else
{

}
}


publ.CreateTab=function(index_)
{
var spanElem=document.createElement('span');

spanElem.setAttribute('id',this.factory.GetTabId(i));
var receiver=this;
spanElem.onclick=function(event)
{
receiver.SelectTab(index_);
};

spanElem.innerHTML=this.GetWidgetName(i);
this.element.appendChild(spanElem);
}
});

});