var current = null;

var Containers = {
	
	container_array: new Array(),
	zindex: 10,
	veil: null,
	
	add: function(id, draggable, veiled) {
		this.id = id;
		this.container_array.push(new container(id, veiled));
		this.makeDraggable();	
		this.makeOnTop(id);
		if(veiled == true) {
			this.veilShow(id);
		}
	},
	
	get: function(id) {
		for(i=0; i<this.container_array.length; i++) {
			if(this.container_array[i].id == id) {
				return this.container_array[i];
			}
		}
	},
	
	exists: function(id) {
		for(i=0; i<this.container_array.length; i++) {
			if(this.container_array[i].id == id) {
				return true;
			}
		}
		return false;
	},
	
	veilShow: function(id) {
		$j("#pageContent").append('<div id="veil" class="veil"></div>');
		$("veil").style.opacity = 0.1;
		$j("#veil").fadeTo('slow', 0.8);
		$('veil').style.zIndex = this.zindex++;
		$('container_' + id).style.zIndex = this.zindex++;
    },
	
    veilHide: function(id) {
		$j("#veil").fadeOut("slow");
		$j("#veil").remove();
	},
	
	remove: function(id) {
		if(this.container_array.length > 2) {
			if(this.container_array[(this.container_array.length-1)].id == id) {
				current = this.container_array[(this.container_array.length-2)].id;
			} else {
				current = this.container_array[(this.container_array.length-1)].id;
			}
		}
		for(i=0; i<this.container_array.length; i++) {
			if(this.container_array[i].id == id) {
				Element.remove(id);
				Element.remove('container_' + id);
				this.container_array.splice(i, 1);
			}
		}
	},
		
	makeDraggable: function() {
		$j('.container').draggable({ handle: 'h2', opacity: 0.8, stack: {group: 'container', min:50 } });
	},
	
	makeOnTop: function(id) {
		for(i=0; i<this.container_array.length; i++) {
			if((this.container_array[i].id == id) && (this.container_array[i].draggable != false)) {
				if(($('container_' + id).style.zIndex != this.zindex)) {
					$('container_' + id).style.zIndex = this.zindex++;
				}
			}
		}
		current = id;
	},
	
	close: function(id) {
		this.remove(id);
		this.veilHide(id);
	},
	
	refresh: function(module, id, vars, div) {
		xajax.$(div).innerHTML = '<div style="height:100px; text-align:center;"><div style="height:60px; background-color:#E2E4E7; margin:20px; border:1px solid #2B3159;"><img src="/images/ajax-loader.gif" style="margin-top:15px;"></div></div>';
		xajax.call('createContainer', {callback:renderLoading, parameters:[module, id, vars]});
		xajax.call('removeElementChildren', {parameters:[div]});
	},
	
	collapse: function(id, module, reload_vars) {
		//if(in_array(id, this.container_array)) {
			xajax.call('taskbarCommand', {mode:'synchronous', parameters:['addElement', 'global', id, module, reload_vars]});
			xajax.call('taskbarCommand', {parameters:['display']});
			this.close(id);
		//}
	}
}

container = function(id, veiled) {
	this.id = id;
	this.veiled = veiled;
	return this;
}
