 
/*
    Animates a tabbed pane created as follows:


<logic:notEmpty name="tpl_detail">
                    <ul id="detail_templatetabs" class="shadetabs">
                        <logic:iterate id="detail" name="tpl_detail">
                            <li><a href="..<bean:write name="detail" property="path"/>" rel="detail_template_contentarea">
                                    <logic:present name="detail" property="attribute(titleKey)">
                                        <bean:message name="detail" property="attribute(titleKey)"/>
                                    </logic:present>
                                    <logic:notPresent name="detail" property="attribute(titleKey)">
                                        -tab-
                                    </logic:notPresent>
                            </a></li>
                        </logic:iterate>
                        
                    </ul>
                    
                    <div id="detail_template_contentarea" class="contentstyle">
                    </div>


    tabselementId: the id of teh ul element containing tabs
    containerelementId: the html id of the element that will contain the tabbed pane
   
  
*/
function PE_TabbedPane(tabselementId,containerElementId,selectedid) {
   
    this.container=$(containerElementId);
    this.tablist=$(tabselementId);
    
    this.loadingTab=false;
    
    if (!this.container)
        alert("TabbedPane:Container element  not present");
    if (!this.tablist)
        alert("TabbedPane:Tablist element not present");
    
    this.currentPanelUrl='';

    var thisobj=this;

    //reads all tabs in the panel
    
    var linklist=this.tablist.getElementsBySelector("li a");
    for (var i=0;i<linklist.length;i++) {
        
        
        linklist[i].tabpanel=this;
        linklist[i].onclick=function () {
                                this.tabpanel.switchPanel(this);
                                return false;
                            }
    }
        
        
    if (linklist.length==0)
        return;
    
    

this.switchPanel= function(aelement) {
    if (this.loadingTab)
        return;
    
    this.loadingTab=true;
    
    var href=aelement.getAttribute("href");
    var sourceAttr=aelement.getAttribute("source");
    var thisobj=this;
    
    
    this.tablist.getElementsBySelector("li").each(
        function(el) {
            el.removeClassName("selected");
            
        }
    )

    var anc=$(aelement).ancestors();

    
    for (var i=0;i<anc.length;i++)
        if (anc[i].tagName.toLowerCase()=="li") {
            anc[i].addClassName("selected");
            break;
        }

    var childs=this.container.childElements();
    for (i=0;i<childs.length;i++)
        childs[i].style.visibility="hidden";
    
    if (sourceAttr && sourceAttr!="") {
        //static content
        var sourcediv=aelement.getAttribute("source");
        
        thisobj.container.update($(sourcediv).innerHTML);
        thisobj.loadingTab=false;
    }
    else {
        document.body.style.cursor = 'wait';
        PEAjaxUtils.scheduleSessionRefresh();
        new Ajax.Request(href, {
           method: 'get',
             parameters: {
                    tmt: (new Date()).getTime()
                },
                  onSuccess: function(transport) {
                    if (thisobj.currentPanelUrl!='')
                        PEEventManager.removeObservers(thisobj.currentPanelUrl);
                    thisobj.currentPanelUrl=href;

                    PEEventManager.setCurrentPortlet(href);
                    thisobj.container.update(transport.responseText);
                    thisobj.loadingTab=false;

                    //alert("finish callback");
                  },
                  onComplete: function() {
                      document.body.style.cursor = 'default';

                  }
                });
     
    }

    
}


//loads first tab
if (selectedid)
    this.switchPanel($(selectedid));
else
    this.switchPanel(linklist[0]);


                                                     
}