/*
filename:  SpryTabbedPanels.js
description:  needed for tabbed panels
last revision: may-06-2010
dependencies:  none
*/

var Spry;
if(!Spry){
Spry={};
}
if(!Spry.Widget){
Spry.Widget={};
}
Spry.Widget.TabbedPanels=function(_1,_2){
this.element=this.getElement(_1);
this.defaultTab=0;
this.tabSelectedClass="TabbedPanelsTabSelected";
this.tabHoverClass="TabbedPanelsTabHover";
this.tabFocusedClass="TabbedPanelsTabFocused";
this.panelVisibleClass="TabbedPanelsContentVisible";
this.focusElement=null;
this.hasFocus=false;
this.currentTabIndex=0;
this.enableKeyboardNavigation=true;
this.nextPanelKeyCode=Spry.Widget.TabbedPanels.KEY_RIGHT;
this.previousPanelKeyCode=Spry.Widget.TabbedPanels.KEY_LEFT;
Spry.Widget.TabbedPanels.setOptions(this,_2);
if(typeof (this.defaultTab)=="number"){
if(this.defaultTab<0){
this.defaultTab=0;
}else{
var _3=this.getTabbedPanelCount();
if(this.defaultTab>=_3){
this.defaultTab=(_3>1)?(_3-1):0;
}
}
this.defaultTab=this.getTabs()[this.defaultTab];
}
if(this.defaultTab){
this.defaultTab=this.getElement(this.defaultTab);
}
this.attachBehaviors();
};
Spry.Widget.TabbedPanels.prototype.getElement=function(_4){
if(_4&&typeof _4=="string"){
return document.getElementById(_4);
}
return _4;
};
Spry.Widget.TabbedPanels.prototype.getElementChildren=function(_5){
var _6=[];
var _7=_5.firstChild;
while(_7){
if(_7.nodeType==1){
_6.push(_7);
}
_7=_7.nextSibling;
}
return _6;
};
Spry.Widget.TabbedPanels.prototype.addClassName=function(_8,_9){
if(!_8||!_9||(_8.className&&_8.className.search(new RegExp("\\b"+_9+"\\b"))!=-1)){
return;
}
_8.className+=(_8.className?" ":"")+_9;
};
Spry.Widget.TabbedPanels.prototype.removeClassName=function(_a,_b){
if(!_a||!_b||(_a.className&&_a.className.search(new RegExp("\\b"+_b+"\\b"))==-1)){
return;
}
_a.className=_a.className.replace(new RegExp("\\s*\\b"+_b+"\\b","g"),"");
};
Spry.Widget.TabbedPanels.setOptions=function(_c,_d,_e){
if(!_d){
return;
}
for(var _f in _d){
if(_e&&_d[_f]==undefined){
continue;
}
_c[_f]=_d[_f];
}
};
Spry.Widget.TabbedPanels.prototype.getTabGroup=function(){
if(this.element){
var _10=this.getElementChildren(this.element);
if(_10.length){
return _10[0];
}
}
return null;
};
Spry.Widget.TabbedPanels.prototype.getTabs=function(){
var _11=[];
var tg=this.getTabGroup();
if(tg){
_11=this.getElementChildren(tg);
}
return _11;
};
Spry.Widget.TabbedPanels.prototype.getContentPanelGroup=function(){
if(this.element){
var _12=this.getElementChildren(this.element);
if(_12.length>1){
return _12[1];
}
}
return null;
};
Spry.Widget.TabbedPanels.prototype.getContentPanels=function(){
var _13=[];
var pg=this.getContentPanelGroup();
if(pg){
_13=this.getElementChildren(pg);
}
return _13;
};
Spry.Widget.TabbedPanels.prototype.getIndex=function(ele,arr){
ele=this.getElement(ele);
if(ele&&arr&&arr.length){
for(var i=0;i<arr.length;i++){
if(ele==arr[i]){
return i;
}
}
}
return -1;
};
Spry.Widget.TabbedPanels.prototype.getTabIndex=function(ele){
var i=this.getIndex(ele,this.getTabs());
if(i<0){
i=this.getIndex(ele,this.getContentPanels());
}
return i;
};
Spry.Widget.TabbedPanels.prototype.getCurrentTabIndex=function(){
return this.currentTabIndex;
};
Spry.Widget.TabbedPanels.prototype.getTabbedPanelCount=function(ele){
return Math.min(this.getTabs().length,this.getContentPanels().length);
};
Spry.Widget.TabbedPanels.addEventListener=function(_14,_15,_16,_17){
try{
if(_14.addEventListener){
_14.addEventListener(_15,_16,_17);
}else{
if(_14.attachEvent){
_14.attachEvent("on"+_15,_16);
}
}
}
catch(e){
}
};
Spry.Widget.TabbedPanels.prototype.cancelEvent=function(e){
if(e.preventDefault){
e.preventDefault();
}else{
e.returnValue=false;
}
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble=true;
}
return false;
};
Spry.Widget.TabbedPanels.prototype.onTabClick=function(e,tab){
this.showPanel(tab);
return this.cancelEvent(e);
};
Spry.Widget.TabbedPanels.prototype.onTabMouseOver=function(e,tab){
this.addClassName(tab,this.tabHoverClass);
return false;
};
Spry.Widget.TabbedPanels.prototype.onTabMouseOut=function(e,tab){
this.removeClassName(tab,this.tabHoverClass);
return false;
};
Spry.Widget.TabbedPanels.prototype.onTabFocus=function(e,tab){
this.hasFocus=true;
this.addClassName(tab,this.tabFocusedClass);
return false;
};
Spry.Widget.TabbedPanels.prototype.onTabBlur=function(e,tab){
this.hasFocus=false;
this.removeClassName(tab,this.tabFocusedClass);
return false;
};
Spry.Widget.TabbedPanels.KEY_UP=38;
Spry.Widget.TabbedPanels.KEY_DOWN=40;
Spry.Widget.TabbedPanels.KEY_LEFT=37;
Spry.Widget.TabbedPanels.KEY_RIGHT=39;
Spry.Widget.TabbedPanels.prototype.onTabKeyDown=function(e,tab){
var key=e.keyCode;
if(!this.hasFocus||(key!=this.previousPanelKeyCode&&key!=this.nextPanelKeyCode)){
return true;
}
var _18=this.getTabs();
for(var i=0;i<_18.length;i++){
if(_18[i]==tab){
var el=false;
if(key==this.previousPanelKeyCode&&i>0){
el=_18[i-1];
}else{
if(key==this.nextPanelKeyCode&&i<_18.length-1){
el=_18[i+1];
}
}
if(el){
this.showPanel(el);
el.focus();
break;
}
}
}
return this.cancelEvent(e);
};
Spry.Widget.TabbedPanels.prototype.preorderTraversal=function(_19,_1a){
var _1b=false;
if(_19){
_1b=_1a(_19);
if(_19.hasChildNodes()){
var _1c=_19.firstChild;
while(!_1b&&_1c){
_1b=this.preorderTraversal(_1c,_1a);
try{
_1c=_1c.nextSibling;
}
catch(e){
_1c=null;
}
}
}
}
return _1b;
};
Spry.Widget.TabbedPanels.prototype.addPanelEventListeners=function(tab,_1d){
var _1e=this;
Spry.Widget.TabbedPanels.addEventListener(tab,"click",function(e){
return _1e.onTabClick(e,tab);
},false);
Spry.Widget.TabbedPanels.addEventListener(tab,"mouseover",function(e){
return _1e.onTabMouseOver(e,tab);
},false);
Spry.Widget.TabbedPanels.addEventListener(tab,"mouseout",function(e){
return _1e.onTabMouseOut(e,tab);
},false);
if(this.enableKeyboardNavigation){
var _1f=null;
var _20=null;
this.preorderTraversal(tab,function(_21){
if(_21.nodeType==1){
var _22=tab.attributes.getNamedItem("tabindex");
if(_22){
_1f=_21;
return true;
}
if(!_20&&_21.nodeName.toLowerCase()=="a"){
_20=_21;
}
}
return false;
});
if(_1f){
this.focusElement=_1f;
}else{
if(_20){
this.focusElement=_20;
}
}
if(this.focusElement){
Spry.Widget.TabbedPanels.addEventListener(this.focusElement,"focus",function(e){
return _1e.onTabFocus(e,tab);
},false);
Spry.Widget.TabbedPanels.addEventListener(this.focusElement,"blur",function(e){
return _1e.onTabBlur(e,tab);
},false);
Spry.Widget.TabbedPanels.addEventListener(this.focusElement,"keydown",function(e){
return _1e.onTabKeyDown(e,tab);
},false);
}
}
};
Spry.Widget.TabbedPanels.prototype.showPanel=function(_23){
var _24=-1;
if(typeof _23=="number"){
_24=_23;
}else{
_24=this.getTabIndex(_23);
}
if(!_24<0||_24>=this.getTabbedPanelCount()){
return;
}
var _25=this.getTabs();
var _26=this.getContentPanels();
var _27=Math.max(_25.length,_26.length);
for(var i=0;i<_27;i++){
if(i!=_24){
if(_25[i]){
this.removeClassName(_25[i],this.tabSelectedClass);
}
if(_26[i]){
this.removeClassName(_26[i],this.panelVisibleClass);
_26[i].style.display="none";
}
}
}
this.addClassName(_25[_24],this.tabSelectedClass);
this.addClassName(_26[_24],this.panelVisibleClass);
_26[_24].style.display="block";
this.currentTabIndex=_24;
};
Spry.Widget.TabbedPanels.prototype.attachBehaviors=function(_28){
var _29=this.getTabs();
var _2a=this.getContentPanels();
var _2b=this.getTabbedPanelCount();
for(var i=0;i<_2b;i++){
this.addPanelEventListeners(_29[i],_2a[i]);
}
this.showPanel(this.defaultTab);
};


