
function clearElement(element) {
	while(element.hasChildNodes()) {
		element.removeChild(element.firstChild);
	}
}

function clearElementById(id) {
	var element = document.getElementById(id);
	clearElement(element);
}

function ShowBlockItem(name, showblock, parent, data, static, url_function, fill_function) {
	this.name = name;
	this.showblock = showblock;

	this.parent = parent;
	this.index = this.parent.index;

	this.data = data;
	this.data.index = this.index;

	this.url_function = url_function;
	this.fill_function = fill_function;

	this.load = false;
	this.static = static;

	this.results = null;
}


ShowBlockItem.prototype.call_url_function = function() {
	if (this.url_function) {
		return this.url_function(this);
	}
}

ShowBlockItem.prototype.call_fill_function = function() {
	if (this.fill_function) {
		return this.fill_function(this,this.data,this.results);
	}
}

ShowBlockItem.prototype.dispOn = function() {
	clearTimeout(this.timer);
	this.data.style.visibility = "visible";
}

ShowBlockItem.prototype.dispOff = function(noshedule) {
	if (noshedule) {
		this.data.style.visibility = 'hidden';
	} else {
		this.timer = setTimeout('showblock.item('+this.index+').dispOff(true)', 2500);
	}
}



ShowBlockItem.prototype.disp = function(results) {

	if (!this.load) {
		clearElement(this.data);
		this.results = results;
		this.load = true;
		this.call_fill_function();
	}

	if (this.results.length > 0) {
		this.data.style.visibility = 'visible';
	} else {
		this.data.style.visibility = 'hidden';
	}
}


function ShowBlock() {
	this.items = [];
};


ShowBlock.prototype.register = function(id_name, data_id_name, static, url_function, fill_function) {
	var o = document.getElementById(id_name);
	var o_data = document.getElementById(data_id_name);
	if (o && o_data) {
		o.index = this.items.length;
		o_data.index = o.index;
		this.items.push(new ShowBlockItem(id_name, this, o, o_data, static, url_function, fill_function));

		o.onmouseover = function() { showblock.go(this.index) }
		// o.onmouseout = function() { showblock.dispOff(this.index,false) }

		o_data.onmouseover = function() { showblock.go(this.index) }
		o_data.onmouseout = function() { showblock.dispOff(this.index,false) }

	} else {
		alert('Can not register '+id_name);
	}
};

ShowBlock.prototype.item = function(index) {
	if (this.items[index]) {
		return this.items[index];
	} else {
		return false;
	}
};


ShowBlock.prototype.go = function(index) {
	var item = this.items[index];
	if (item) {
		if (item.load) {
			item.disp(item.results);
		} else {
			var oScript = document.createElement('script');
			oScript.setAttribute('type','text/javascript');
			var url = item.call_url_function();
			if (url) {
				oScript.setAttribute('src',url);
				item.parent.appendChild(oScript);
			}
		}
	}
}

ShowBlock.prototype.disp = function(index,results) {
	var item = this.item(index);
	if (item) {
		item.disp(results);
		
	}
}

ShowBlock.prototype.dispOn = function(index) {
	var item = this.item(index);
	if (item) {
		item.dispOn();
	}
}

ShowBlock.prototype.dispOff = function(index,noshedule) {
	var item = this.item(index);
	if (item) {
		item.dispOff(noshedule);
	}
}



