var closeIMG = '/images/closeButton.gif';
var edit = 'Modifier';
var end_edit = 'Fin de modification';

function Box(col) {
	this.id;
	this.idtab;
	this.config = false;
	this.edit = false;
	this.objBox = document.createElement("div");
	this.objBoxHeader = document.createElement("div");
	this.objBoxEdit = document.createElement("a");
	this.objBoxCloseImg = document.createElement("img");
	this.objBoxTitle = document.createElement("div");
	this.objBoxConfig = document.createElement("div");
	this.objBoxContent = document.createElement("div");
	this.objLoadingDiv = document.createElement("div");
	this.objBoxIcon = document.createElement("img");
	

	this.onLoad = function (col, _ID) {
		var self = this;

		this.objBox.className = 'boxItem';
		this.objBoxHeader.onmousedown = selectBoxItem;
		this.objBox.id = 'boxItem' + nbBoxItem;
		this.objBoxHeader.style.cursor = 'move';
		boxItemArray[nbBoxItem] = this.objBox;
		this.idtab = nbBoxItem;
		nbBoxItem++;

		this.objBoxHeader.className = 'boxHeader';
		this.objBoxHeader.onmouseover = function() { if(self.config) self.objBoxEdit.style.display = 'block'; }
		this.objBoxHeader.onmouseout = function() { if(self.config && !self.edit) self.objBoxEdit.style.display = 'none'; }

		this.objBoxIcon.className = 'boxIcon';
		this.objBoxHeader.appendChild(this.objBoxIcon);

		this.objBoxCloseImg.className = 'boxClose';
		this.objBoxCloseImg.src = closeIMG;
		this.objBoxCloseImg.alt = 'Fermer';
		this.objBoxCloseImg.onclick = function() { self.close(); }
		this.objBoxHeader.appendChild(this.objBoxCloseImg);

		this.objBoxEdit.className = 'boxEdit';
		this.objBoxEdit.href = "#";
		this.objBoxEdit.style.display = 'none';
		this.hideConfig();
		this.objBoxHeader.appendChild(this.objBoxEdit);

		this.objBoxTitle.className = 'boxTitle';
		this.objBoxHeader.appendChild(this.objBoxTitle);
	
		this.objBoxConfig.className = 'boxConfig';
		this.objBoxContent.className = 'boxContent';

		this.objBox.appendChild(this.objBoxHeader);
		this.objBox.appendChild(this.objBoxConfig);
		this.objBox.appendChild(this.objBoxContent);

		var col = document.getElementById("column"+col);
		col.insertBefore(this.objBox, col.lastChild);

		this.objLoadingDiv.innerHTML = '<p style="text-align: center;">Chargement en cours...</p>';
		this.objLoadingDiv.className = 'module';

		if(_ID) {
			this.id = _ID;
		}
	
	}

	this.setTitle = function (titre) {
		this.objBoxTitle.innerHTML = titre;
	}

	this.setContent = function (obj) {
		this.objBoxContent.innerHTML = '';
		this.objBoxContent.appendChild(obj);
	}

	this.setConfig = function (obj) {
		this.config = true;
		this.objBoxConfig.innerHTML = '';
		this.objBoxConfig.appendChild(obj);
	}

	this.close = function (ask) {
		if(!ask) {
			if (!confirm('Voulez vous vraiment supprimer cette boite ?')) return;
		}

		boxItemArray[this.idtab] = null;
		boxObjArray[this.idtab] = null;

		this.objBox.parentNode.removeChild(this.objBox);

		var ajaxObj = new Ajax();
		ajaxObj.request("/save.php?action=del&id=" + this.id, {method: 'GET'});
		saveCook();
	}

	this.showConfig = function () {
		var self = this;
		this.edit = true;
		this.objBoxConfig.style.display = 'block';
		this.objBoxEdit.innerHTML = end_edit;
		this.objBoxEdit.onclick = function () { self.hideConfig(); return false; }
		this.objBoxEdit.style.display = 'block';
	}

	this.hideConfig = function () {
		var self = this;
		this.edit = false;
		this.objBoxConfig.style.display = 'none';
		this.objBoxEdit.innerHTML = edit;
		this.objBoxEdit.onclick = function () { self.showConfig(); return false; }
	}

	this.loading = function() {
		this.setContent(this.objLoadingDiv);
	}

	this.setIcon = function(url) {
		if(url) {
			this.objBoxIcon.src = url;
			this.objBoxIcon.style.display = 'block';
		} else {
			this.objBoxIcon.style.display = 'none';
		}
	}

	this.saveParam = function(param) {
		var ajaxObj = new Ajax();
		ajaxObj.URLString = param;
		ajaxObj.request("/save.php?action=param&id=" + this.id, {method: 'POST'});
	}

	this.onLoad(col);
}
