/* Tabs 2 by Radek N. */

addLoadEvent(initTabs);

function initTabs()
{
    
    // creating Tab objects for each ul.tabs
    var uls = document.getElementsByTagName('ul');
    for (var i = 0; i < uls.length; i++) {
        if (uls[i].className.indexOf('tabs') != -1) {            
            new Tab(uls[i]);
        }
    }
}

/* class Tab */
function Tab(ul) 
{
    // private members
    var list = ul; 
    var id = null; // component identyficator 
    var anchors = Array(); // tabs 
    var boxes = Array(); // contents
    var current; // id of current box / rel of current tab
    var that = this; 
    
    initialize();
    
    // private function initialize
    function initialize() 
    {
        anchors = list.getElementsByTagName('a');
        id = anchors[0].getAttribute('rel');
        
        var divs = document.getElementsByTagName('div');
        for (var i = 0; i < divs.length; i++) {
            if ((divs[i].className.indexOf('tabs') != -1) && (divs[i].className.indexOf(id) != -1)) {
                boxes.push(divs[i]);
            }
        }
        
        if (window.console && console.firebug) console.info("component Id = ", id); 
        
        for (var i = 0; i < anchors.length; i++) {
            anchors[i].onclick = function() {
                return changeCurrent(this);
            } 
            anchors[i].onfocus = function() {
                this.blur();
            }
        }
        
        // which tab should be opened at the beginning
        var startRel = null;
        var relFromCookie = readCookie(id);
        var relFromLocation = getRelFromLocation();
        var relFromErrors = getRelFromErrors();        
        startRel = getRel(anchors[0].href);
        if (relFromLocation) startRel = relFromLocation;
        if (relFromCookie) startRel = relFromCookie;     
        if (relFromErrors) startRel = relFromErrors;
        if (window.console && console.firebug) console.debug("startRel = ", startRel); 
        
        for (var i = 0; i < anchors.length; i++) {
            if (getRel(anchors[i].href) == startRel) {
                return changeCurrent(anchors[i]);      
            }
        }       
    }

    // private function getRel
    function getRel(url) 
    { 
        if (url.indexOf('#') != -1) {
            var match = url.split('#');
            return match[match.length-1];
        } else {
            return false;
        }
    }
    
    // private function getRelFromErrors
    function getRelFromErrors()
    {
        var ps;
        for (var i = 0; i < boxes.length; i++) {
            ps = boxes[i].getElementsByTagName('p');
            for (var j = 0; j < ps.length; j++) {
                if (ps[j].className.indexOf('error-on') != -1) {
                    return boxes[i].getAttribute('id');
                }
            }
        }
        return false;
    }
    
    // private function getRelFromLocation
    function getRelFromLocation()
    {
        var relLocation = getRel(window.location.href);    
        for (var i = 0; i < anchors.length; i++) {
            if (getRel(anchors[i].href) == relLocation) {
                return relLocation;    
            }
        }
        return false;
    }
    
    // private function changeTab
    function changeCurrent(anchor)
    {
        // clear all
        for (var i = 0; i < anchors.length; i++) {
            remClass(anchors[i], 'current'); 
        }
        for (var i = 0; i < boxes.length; i++) {
            remClass(boxes[i], 'current'); 
        }
        // set current
        current = getRel(anchor.href);
        createCookie(id, current, 1);
        if (window.console && console.firebug) console.debug("current = ", current);

        addClass(anchor, 'current');
        addClass(document.getElementById(current), 'current');
        return false;
    }
}



