/*------------------------------------------------------------------------------
Function:       TabInterface()
Author:         Aaron Gustafson (aaron at easy-designs dot net)
Creation Date:  7 December 2006
Version:        1.0
Homepage:       http://github.com/easy-designs/TabInterface.js
License:        MIT License (see MIT-LICENSE)
------------------------------------------------------------------------------*/
function TabInterface(c,d){this.Version="1.0";var j=false,i=document.createElement("ul"),g={div:document.createElement("div"),li:document.createElement("li")};function e(){var x,r,s,p=["h1","h2","h3","h4","h5","h6"],t,v,q,w,u,n,o,y;x=c.getAttribute("id")||"folder-"+d;if(!c.getAttribute("id")){c.setAttribute("id",x)}c.setAttribute("role","application");i.setAttribute("role","tablist");g.div.setAttribute("role","tabpanel");g.div.setAttribute("aria-hidden","true");g.div.setAttribute("tabindex","-1");g.li.setAttribute("role","tab");g.li.setAttribute("aria-selected","false");g.li.setAttribute("tabindex","-1");r=c.firstChild;while(r){s=r.nextSibling;if(r.nodeType==3&&!(/\S/).test(r.nodeValue)){c.removeChild(r)}r=s}for(t=0,v=p.length;t<v;t++){if(c.firstChild.nodeName.toLowerCase()==p[t]){q=p[t];break}}h(c,"tabbed-on");f(c,"tabbed");w=new RegExp("<("+q+")","ig");u=c.innerHTML.replace(w,"||||<$1").split("||||");u.shift();c.innerHTML="";h(i,"index");c.appendChild(i);for(t=0,v=u.length;t<v;t++){n=g.div.cloneNode(true);h(n,"folder");n.setAttribute("id",x+"-"+t);n.setAttribute("aria-labelledby",x+"-"+t+"-tab");n.innerHTML=u[t];c.appendChild(n);o=g.li.cloneNode(true);o.setAttribute("id",x+"-"+t+"-tab");o.setAttribute("aria-describedby",x+"-"+t);o.onclick=b;o.onkeydown=m;o.onfocus=b;y=n.getElementsByTagName(q)[0];if(y.getAttribute("title")){o.innerHTML=y.getAttribute("title")}else{o.innerHTML=y.innerHTML;h(y,"hidden")}i.appendChild(o);if(t===0){h(n,"visible");n.setAttribute("aria-hidden","false");n.setAttribute("tabindex","0");o.setAttribute("aria-selected","true");o.setAttribute("tabindex","0");j=n.getAttribute("id");c.setAttribute("aria-activedescendant",j);h(o,"active")}}}function b(q){q=(q)?q:event;var p=q.target||q.srcElement,o=document.getElementById(j),n=document.getElementById(j+"-tab"),r;p=k(p);r=document.getElementById(p.getAttribute("id").replace("-tab",""));f(n,"active");n.setAttribute("aria-selected","false");n.setAttribute("tabindex","-1");f(o,"visible");o.setAttribute("aria-hidden","true");o.setAttribute("tabindex","-1");h(p,"active");p.setAttribute("aria-selected","true");p.setAttribute("tabindex","0");h(r,"visible");r.setAttribute("aria-hidden","false");r.setAttribute("tabindex","0");j=r.getAttribute("id");c.setAttribute("aria-activedescendant",j)}function h(o,p){var n=(!o.className)?[]:o.className.split(" ");n.push(p);o.className=n.join(" ")}function f(p,q){var o=p.className.split(" ");for(var n=o.length-1;n>=0;n--){if(o[n]==q){o.splice(n,1)}}p.className=o.join(" ")}function k(n){while(n.nodeName.toLowerCase()!="li"){n=n.parentNode}return n}function m(q){q=(q)?q:event;var o=q.target||q.srcElement,n=q.keyCode||q.charCode,p=true;o=k(o);switch(n){case 37:case 38:a(o,"previous",false);p=false;break;case 39:case 40:a(o,"next",false);p=false;break;case 36:a(o,"previous",true);p=false;break;case 35:a(o,"next",true);p=false;break;case 27:o.blur();p=false;break}if(!p){return l(q)}}function a(o,q,n){if(n){if(q=="previous"){o.parentNode.childNodes[0].focus()}else{o.parentNode.childNodes[o.parentNode.childNodes.length-1].focus()}}else{var p=q=="previous"?o.previousSibling:o.nextSibling;if(p){p.focus()}}}function l(n){if(typeof n.stopPropagation=="function"){n.stopPropagation()}else{if(typeof n.cancelBubble!="undefined"){n.cancelBubble=true}}if(n.preventDefault){n.preventDefault()}return false}e()};