1 //** Tab Content script v2.0- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
\r 
   2 //** Updated Oct 7th, 07 to version 2.0. Contains numerous improvements:
\r 
   3 //   -Added Auto Mode: Script auto rotates the tabs based on an interval, until a tab is explicitly selected
\r 
   4 //   -Ability to expand/contract arbitrary DIVs on the page as the tabbed content is expanded/ contracted
\r 
   5 //   -Ability to dynamically select a tab either based on its position within its peers, or its ID attribute (give the target tab one 1st)
\r 
   6 //   -Ability to set where the CSS classname "selected" get assigned- either to the target tab's link ("A"), or its parent container
\r 
   7 //** Updated Feb 18th, 08 to version 2.1: Adds a "tabinstance.cycleit(dir)" method to cycle forward or backward between tabs dynamically
\r 
   8 //** Updated April 8th, 08 to version 2.2: Adds support for expanding a tab using a URL parameter (ie: http://mysite.com/tabcontent.htm?tabinterfaceid=0) 
\r 
  10 ////NO NEED TO EDIT BELOW////////////////////////
\r 
  12 function ddtabcontent(tabinterfaceid){
\r 
  13         this.tabinterfaceid=tabinterfaceid //ID of Tab Menu main container
\r 
  14         this.tabs=document.getElementById(tabinterfaceid).getElementsByTagName("a") //Get all tab links within container
\r 
  15         this.enabletabpersistence=true
\r 
  16         this.hottabspositions=[] //Array to store position of tabs that have a "rel" attr defined, relative to all tab links, within container
\r 
  17         this.currentTabIndex=0 //Index of currently selected hot tab (tab with sub content) within hottabspositions[] array
\r 
  18         this.subcontentids=[] //Array to store ids of the sub contents ("rel" attr values)
\r 
  19         this.revcontentids=[] //Array to store ids of arbitrary contents to expand/contact as well ("rev" attr values)
\r 
  20         this.selectedClassTarget="link" //keyword to indicate which target element to assign "selected" CSS class ("linkparent" or "link")
\r 
  23 ddtabcontent.getCookie=function(Name){ 
\r 
  24         var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
\r 
  25         if (document.cookie.match(re)) //if cookie found
\r 
  26                 return document.cookie.match(re)[0].split("=")[1] //return its value
\r 
  30 ddtabcontent.setCookie=function(name, value){
\r 
  31         document.cookie = name+"="+value+";path=/" //cookie value is domain wide (path=/)
\r 
  34 ddtabcontent.prototype={
\r 
  36         expandit:function(tabid_or_position){ //PUBLIC function to select a tab either by its ID or position(int) within its peers
\r 
  37                 this.cancelautorun() //stop auto cycling of tabs (if running)
\r 
  40                         if (typeof tabid_or_position=="string" && document.getElementById(tabid_or_position).getAttribute("rel")) //if specified tab contains "rel" attr
\r 
  41                                 tabref=document.getElementById(tabid_or_position)
\r 
  42                         else if (parseInt(tabid_or_position)!=NaN && this.tabs[tabid_or_position].getAttribute("rel")) //if specified tab contains "rel" attr
\r 
  43                                 tabref=this.tabs[tabid_or_position]
\r 
  45                 catch(err){alert("Invalid Tab ID or position entered!")}
\r 
  46                 if (tabref!="") //if a valid tab is found based on function parameter
\r 
  47                         this.expandtab(tabref) //expand this tab
\r 
  50         cycleit:function(dir, autorun){ //PUBLIC function to move foward or backwards through each hot tab (tabinstance.cycleit('foward/back') )
\r 
  52                         var currentTabIndex=(this.currentTabIndex<this.hottabspositions.length-1)? this.currentTabIndex+1 : 0
\r 
  54                 else if (dir=="prev"){
\r 
  55                         var currentTabIndex=(this.currentTabIndex>0)? this.currentTabIndex-1 : this.hottabspositions.length-1
\r 
  57                 if (typeof autorun=="undefined") //if cycleit() is being called by user, versus autorun() function
\r 
  58                         this.cancelautorun() //stop auto cycling of tabs (if running)
\r 
  59                 this.expandtab(this.tabs[this.hottabspositions[currentTabIndex]])
\r 
  62         setpersist:function(bool){ //PUBLIC function to toggle persistence feature
\r 
  63                         this.enabletabpersistence=bool
\r 
  66         setselectedClassTarget:function(objstr){ //PUBLIC function to set which target element to assign "selected" CSS class ("linkparent" or "link")
\r 
  67                 this.selectedClassTarget=objstr || "link"
\r 
  70         getselectedClassTarget:function(tabref){ //Returns target element to assign "selected" CSS class to
\r 
  71                 return (this.selectedClassTarget==("linkparent".toLowerCase()))? tabref.parentNode : tabref
\r 
  74         urlparamselect:function(tabinterfaceid){
\r 
  75                 var result=window.location.search.match(new RegExp(tabinterfaceid+"=(\\d+)", "i")) //check for "?tabinterfaceid=2" in URL
\r 
  76                 return (result==null)? null : parseInt(RegExp.$1) //returns null or index, where index (int) is the selected tab's index
\r 
  79         expandtab:function(tabref){
\r 
  80                 var subcontentid=tabref.getAttribute("rel") //Get id of subcontent to expand
\r 
  81                 //Get "rev" attr as a string of IDs in the format ",john,george,trey,etc," to easily search through
\r 
  82                 var associatedrevids=(tabref.getAttribute("rev"))? ","+tabref.getAttribute("rev").replace(/\s+/, "")+"," : ""
\r 
  83                 this.expandsubcontent(subcontentid)
\r 
  84                 this.expandrevcontent(associatedrevids)
\r 
  85                 for (var i=0; i<this.tabs.length; i++){ //Loop through all tabs, and assign only the selected tab the CSS class "selected"
\r 
  86                         this.getselectedClassTarget(this.tabs[i]).className=(this.tabs[i].getAttribute("rel")==subcontentid)? "selected" : ""
\r 
  88                 if (this.enabletabpersistence) //if persistence enabled, save selected tab position(int) relative to its peers
\r 
  89                         ddtabcontent.setCookie(this.tabinterfaceid, tabref.tabposition)
\r 
  90                 this.setcurrenttabindex(tabref.tabposition) //remember position of selected tab within hottabspositions[] array
\r 
  93         expandsubcontent:function(subcontentid){
\r 
  94                 for (var i=0; i<this.subcontentids.length; i++){
\r 
  95                         var subcontent=document.getElementById(this.subcontentids[i]) //cache current subcontent obj (in for loop)
\r 
  96                         subcontent.style.display=(subcontent.id==subcontentid)? "block" : "none" //"show" or hide sub content based on matching id attr value
\r 
 100         expandrevcontent:function(associatedrevids){
\r 
 101                 var allrevids=this.revcontentids
\r 
 102                 for (var i=0; i<allrevids.length; i++){ //Loop through rev attributes for all tabs in this tab interface
\r 
 103                         //if any values stored within associatedrevids matches one within allrevids, expand that DIV, otherwise, contract it
\r 
 104                         document.getElementById(allrevids[i]).style.display=(associatedrevids.indexOf(","+allrevids[i]+",")!=-1)? "block" : "none"
\r 
 108         setcurrenttabindex:function(tabposition){ //store current position of tab (within hottabspositions[] array)
\r 
 109                 for (var i=0; i<this.hottabspositions.length; i++){
\r 
 110                         if (tabposition==this.hottabspositions[i]){
\r 
 111                                 this.currentTabIndex=i
\r 
 117         autorun:function(){ //function to auto cycle through and select tabs based on a set interval
\r 
 118                 this.cycleit('next', true)
\r 
 121         cancelautorun:function(){
\r 
 122                 if (typeof this.autoruntimer!="undefined")
\r 
 123                         clearInterval(this.autoruntimer)
\r 
 126         init:function(automodeperiod){
\r 
 127                 var persistedtab=ddtabcontent.getCookie(this.tabinterfaceid) //get position of persisted tab (applicable if persistence is enabled)
\r 
 128                 var selectedtab=-1 //Currently selected tab index (-1 meaning none)
\r 
 129                 var selectedtabfromurl=this.urlparamselect(this.tabinterfaceid) //returns null or index from: tabcontent.htm?tabinterfaceid=index
\r 
 130                 this.automodeperiod=automodeperiod || 0
\r 
 131                 for (var i=0; i<this.tabs.length; i++){
\r 
 132                         this.tabs[i].tabposition=i //remember position of tab relative to its peers
\r 
 133                         if (this.tabs[i].getAttribute("rel")){
\r 
 134                                 var tabinstance=this
\r 
 135                                 this.hottabspositions[this.hottabspositions.length]=i //store position of "hot" tab ("rel" attr defined) relative to its peers
\r 
 136                                 this.subcontentids[this.subcontentids.length]=this.tabs[i].getAttribute("rel") //store id of sub content ("rel" attr value)
\r 
 137                                 this.tabs[i].onclick=function(){
\r 
 138                                         tabinstance.expandtab(this)
\r 
 139                                         tabinstance.cancelautorun() //stop auto cycling of tabs (if running)
\r 
 142                                 if (this.tabs[i].getAttribute("rev")){ //if "rev" attr defined, store each value within "rev" as an array element
\r 
 143                                         this.revcontentids=this.revcontentids.concat(this.tabs[i].getAttribute("rev").split(/\s*,\s*/))
\r 
 145                                 if (selectedtabfromurl==i || this.enabletabpersistence && selectedtab==-1 && parseInt(persistedtab)==i || !this.enabletabpersistence && selectedtab==-1 && this.getselectedClassTarget(this.tabs[i]).className=="selected"){
\r 
 146                                         selectedtab=i //Selected tab index, if found
\r 
 150                 if (selectedtab!=-1) //if a valid default selected tab index is found
\r 
 151                         this.expandtab(this.tabs[selectedtab]) //expand selected tab (either from URL parameter, persistent feature, or class="selected" class)
\r 
 152                 else //if no valid default selected index found
\r 
 153                         this.expandtab(this.tabs[this.hottabspositions[0]]) //Just select first tab that contains a "rel" attr
\r 
 154                 if (parseInt(this.automodeperiod)>500 && this.hottabspositions.length>1){
\r 
 155                         this.autoruntimer=setInterval(function(){tabinstance.autorun()}, this.automodeperiod)
\r 
 157         } //END int() function
\r 
 159 } //END Prototype assignment