/**
 * SetaContainer
 * -------------------------
 * Copyright 2005 - Setasign - Jan Slabon
 * http://www.setasign.de
 */
function SetaVisibleSwitcher(e) {
	this.e = e;
	this.e.SetaVisibleSwitcher = this;
	this.classAtt = document.all && !window.opera ? 'className' : 'class';
	this.opened = this.e.getAttribute('status') == 'opened';
	this.refIDs = this.e.getAttribute('toswitch') ? this.e.getAttribute('toswitch').split('|') : [];
	this.chainIDs = this.e.getAttribute('chain') ? this.e.getAttribute('chain').split('|') : [];
	
	
	var cN = this.e.getAttribute(this.classAtt).split(' ');
	var _ = [];
	for (var i = 0; i < cN.length; i++) {
	    _ = cN[i].split(':');
        if (_.length > 1) {
            switch(_[0]) {
                case 'toswitch':
                    this.refIDs = _[1].split('|');
                break;
                case 'chain':
                    this.chainIDs = _[1].split('|');
                break;
                case 'status':
                    this.opened = _[1] == 'opened';
                break;
	       }
	    }
	}
	
	this.openedElement = null;
    this.closedElement = null;	
    if (typeof this.e.onclick == 'function')
        this.e._onclick = this.e.onclick;
        
	var elements, l, a, i, status, cN;
	
	var tagNames = ['A','SPAN','DIV','P'];
	for (a = 0; a < tagNames.length; a++) {
	    elements = this.e.getElementsByTagName(tagNames[a]);
	    l = elements.length;
	    for (i = 0; i < l; i++) {
	        if (elements[i].nodeType == 1 && elements[i].getAttribute(this.classAtt) 
    		    && elements[i].getAttribute(this.classAtt).indexOf('status-') == 0) {
                cN = elements[i].getAttribute(this.classAtt);
    			status = cN.substr(7);
    			if (status == 'opened')
    				this.openedElement = elements[i];
    			else 
    				this.closedElement = elements[i];
    		}
    	}
	}
	
	
	
	this.e.onclick = function(e) {
	    var e = e || event;
        e.cancelBubble = this.getAttribute("cancelBubble") == "true";
	    var l = this.SetaVisibleSwitcher.refIDs.length, visible;
		
		for (var i = 0; i < l; i++) {
			var e = document.getElementById(this.SetaVisibleSwitcher.refIDs[i]);
			opened = e.style.display == 'block';
			e.style.display = opened ? 'none' : 'block';
		}
		
		for (i = 0; i < this.SetaVisibleSwitcher.chainIDs.length; i++) {
		    var _e = document.getElementById(this.SetaVisibleSwitcher.chainIDs[i]);
		    if (_e.SetaVisibleSwitcher.opened) {
		        _e.onclick(_e);
		    }
		}
		
		this.SetaVisibleSwitcher.opened =! this.SetaVisibleSwitcher.opened;
		this.SetaVisibleSwitcher.updateStatus();
		if (typeof this._onclick == 'function')
			this._onclick();
		
	}
	
	this.updateStatus = function() {
        if (this.openedElement != null) {
    		this.openedElement.style.display = this.opened  ? 'inline' : 'none';	
        }
        
	    if (this.closedElement != null) {
        	this.closedElement.style.display = !this.opened ? 'inline' : 'none';	
	    }
	}
	
	this.updateStatus();
}
