// dynamic iframe start
// 2005.2.16 by	guoo.net
// 2005.7.2	modified by	guoo.net
// 2005.12.1 modified by guoo.net

var DynamicIFrameManager = {
	callerList : [], 
	debug : false, 
	add : function(caller) {
		this.callerList[this.callerList.length] = caller;
	}, 
	find : function(id) {
		for(var	i=0; i<this.callerList.length; i++) {
			if(this.callerList[i].id==id) return this.callerList[i];
		}
		return null;
	}, 
	findByIFrameName : function(name) {
		for(var	i=0; i<this.callerList.length; i++) {
			if(this.callerList[i].name==name) return this.callerList[i];
		}
		return null;
	}
}

var DynamicIFrameData = Class.create();
DynamicIFrameData.prototype = {
	source : '', 
	callback : null, 
	form : null, 
	args : [], 
	initialize : function(source, callback, form, args) {
		var index = source.indexOf('?');
		this.source	= source;
		this.callback =	callback;
		this.form =	form;
		this.args =	args;
	}
}

var DynamicIFrameCaller = Class.create();
DynamicIFrameCaller.prototype = {
	id : '', 
	name : '', 
	active : false, 
	iframe : null, 
	data : null, 
	initialize : function(id, data) {
		DynamicIFrameManager.add(this);
		this.id	= id;
		this.name =	'dynamic_iframe_'+id;
		this.active	= false;
		this.data = data;

		var	html;
		if(!DynamicIFrameManager.debug)
			html = '<iframe	id="'+this.name+'" name="'+this.name+'"	frameborder="0"	style="position:absolute; left:0; top:0; width:0; height:0;"></iframe>';
		else
			html = '<iframe	id="'+this.name+'" name="'+this.name+'"	frameborder="0"	width="100%" height="200"></iframe>';

		document.body.insertAdjacentHTML('afterBegin', html);
	}, 

	call : function() {
		this.active	= true;
		this.iframe	= document.getElementById(this.name);
		if(!this.iframe) {
			alert('iframe creating fail.');
			return;
		}
		if(this.data.form && this.data.form.tagName=='FORM') {
			this.data.form.target = this.name;
			this.data.form.action = this.data.source;
			this.data.form.submit();

		}else {
			var	source = this.data.source;
			if(source.indexOf('?')!=-1)	source += '&dummy='+Math.random();
			else source	+= '?dummy='+Math.random();
			this.iframe.src	= source;
		}
	}
}

function onLoadDynamicIFrame(windowObj,	html) {
	var	caller = DynamicIFrameManager.findByIFrameName(windowObj.name);
	if(!caller) {
		alert('can not find	caller object.');
		return;
	}
	if(html) {
		html = html.replace(/\{di_sl\}/g, "\\");
		html = html.replace(/\{di_qt\}/g, "'");
		html = html.replace(/\{di_nl\}/g, '\n');
	}
	var	args = caller.data.args;
	caller.data.args.push('');
	caller.data.args.unshift();
	caller.data.args[0] = html;
	if(typeof caller.data.callback==='function') {
		caller.data.callback.apply(this, caller.data.args);
	}
	caller.active = false;
}

function dynamicIFrame(id, source, callback, form) {
	var	args = [];
	if(arguments.length > 3)
		args = $A(arguments).slice(3);

	var	data = new DynamicIFrameData(source, callback, form, args);
	var	caller	= DynamicIFrameManager.find(id);
	if(caller) {
		caller.data = data;
	}else {
		caller = new DynamicIFrameCaller(id, data);
	}
	caller.call();
}
// dynamic iframe finish
