// classe Box

Box.prototype = {

    setStructure: function() {
	var content = this.header.innerHTML;
        this.header.innerHTML = "";
        this.header.appendChild(this.icon);
        this.icon.appendChild(this.openImg);
        this.openImg.src = this.openImgUrl;
        this.icon.appendChild(this.closedImg);
        this.closedImg.src = this.closedImgUrl;
        this.header.appendChild(this.titre);
        this.header.lastChild.innerHTML = content;
    },

    setStyle: function() {
        if(this.box.className == "") this.box.className = "item";
        if(this.header.className == "") this.header.className = "entete";
	if(this.titre.className == "") this.titre.className = "titre";
	if(this.util && this.util.className == "") this.util.className = "clearFlow";
        if(this.body.className == "") this.body.className = "corps";
        this.icon.style.cursor = "pointer";
    },

    setOnClick: function() {
	var current = this;
	this.icon.onclick = function() {
	    if(current.openImg.style.display == "none") {
		current.expand();
	    } else {
		current.collapse();
	    };
	};
    },
    
    expand: function(){
	this.registerState(false);
        this.openImg.style.display = "inline";
        this.closedImg.style.display = "none";
        this.body.style.display = "block";
    },

    collapse: function() {
	this.registerState(true);
	this.openImg.style.display = "none";
        this.closedImg.style.display = "inline";
        this.body.style.display = "none";
    },

    registerState: function(etat){
	document.cookie = this.idBox+'='+etat;
    },

    deregisterState: function(etat){
    }
}

// Constructeur de boites

function Box(idBox, collapsed) {
    this.idBox = idBox;
    this.box = document.getElementById(idBox);
    this.header = Element.getChildElements(this.box,"div")[0];
    this.body = Element.getChildElements(this.box,"div")[1];
    this.icon = document.createElement("span");
    this.titre = document.createElement("span");
    this.openImg = document.createElement("img");
    this.closedImg = document.createElement("img");
    this.openImgUrl = "medias/standard/images/b_plus2_on.png";
    this.closedImgUrl = "medias/standard/images/b_plus2_off.png";
    
    this.setStructure();
    this.setStyle();
    this.setOnClick();
    this.registerState(collapsed);
    if(collapsed) {
	this.collapse();
    } else {
	this.expand();
    };
}

// attribution du constructeur a la classe
Box.prototype.constructor = Box;

// classe d'un volet escamotable

function BoxEscamotable(idBox, collapsed){
    this.idBox = idBox;
    this.box = document.getElementById(idBox);
    this.header = Element.getChildElements(this.box,"div")[0];
    this.body = Element.getChildElements(this.box,"div")[1];
    this.icon = document.createElement("span");
    this.titre = document.createElement("span");
    this.openImg = document.createElement("img");
    this.closedImg = document.createElement("img");
    this.openImgUrl = "medias/standard/images/b_plus_on.png";
    this.closedImgUrl = "medias/standard/images/b_plus_off.png";

    this.setOnClick = function() {		// surcharge du comportement setOnClick de la boite simple
	var current = this;
	this.icon.onclick = function() {
	    if(current.openImg.style.display == "none") {
		current.expand();
		current.box.className="on";
	    } else {
		current.collapse();
		current.box.className="off";
	    };
	};
    };
    
    this.setStructure();
    this.setStyle();
    this.setOnClick();
    if(collapsed) {
	this.collapse();
	this.box.className="off";
    } else {
	this.expand();
	this.box.className="on";
    };
}

BoxEscamotable.prototype = Box.prototype;
BoxEscamotable.prototype.constructor = BoxEscamotable;

function BoxComplete(idBox){

    this.box = document.getElementById(idBox);
    this.idBox = idBox;
    this.header = Element.getChildElements(this.box,"div")[0];
    this.body = Element.getChildElements(this.box,"div")[1];
    this.icon = document.createElement("span");
    this.titre = document.createElement("span");
    this.util = document.createElement("div");
    this.closedImg = document.createElement("img");
    this.closedImg.className = 'ie';
    this.closedImgUrl = "medias/standard/images/b_fermerWidget.png";

    this.setStructure = function() {		// surcharge du comportement setStructure de la boite simple
	var content = this.header.innerHTML;
        this.header.innerHTML = "";
        this.header.appendChild(this.icon);
        this.icon.appendChild(this.closedImg);
        this.closedImg.src = this.closedImgUrl;
        this.header.appendChild(this.titre);
        this.header.lastChild.innerHTML = content;
        this.header.appendChild(this.util);
    };

    this.setOnClick = function() {		// surcharge du comportement setOnClick de la boite simple
	var current = this;
	this.icon.onclick = function() {
	    current.close();
	};
    };
    
    this.close = function(){
	document.getElementById('page').removeChild(document.getElementById(this.idBox));
    };
    
    this.setStructure();
    this.setStyle();
    this.setOnClick();
}

BoxComplete.prototype = Box.prototype;

