// dynamic iframe start
// 2005.2.16 by	guoo.net

//emul ie
if (typeof HTMLElement!='undefined') {
	if (HTMLElement.prototype) {
		HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode) {
			switch(where) {
			case 'beforeBegin':
				this.parentNode.insertBefore(parsedNode,this)
				break;
			case 'afterBegin':
				this.insertBefore(parsedNode,this.firstChild);
				break;
			case 'beforeEnd':
				this.appendChild(parsedNode);
				break;
			case 'afterEnd':
				if (this.nextSibling) this.parentNode.insertBefore(parsedNode,this.nextSibling);
				else this.parentNode.appendChild(parsedNode);
				break;
			}
		};

		HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr) {
			var r = this.ownerDocument.createRange();
			r.setStartBefore(this);
			var parsedHTML = r.createContextualFragment(htmlStr);
			this.insertAdjacentElement(where,parsedHTML)
		};


		HTMLElement.prototype.insertAdjacentText = function(where,txtStr) {
			var parsedText = document.createTextNode(txtStr)
			this.insertAdjacentElement(where,parsedText)
		};


		HTMLElement.prototype.__defineSetter__("innerHTML", function (str) {
			var r = this.ownerDocument.createRange();
			r.selectNodeContents(this);
			r.deleteContents();
			var df = r.createContextualFragment(str);
			this.appendChild(df);
			return str;
		});

		HTMLElement.prototype.__defineSetter__("outerHTML", function (str) {
			var r = this.ownerDocument.createRange();
			r.setStartBefore(this);
			var df = r.createContextualFragment(str);
			this.parentNode.replaceChild(df, this);
			return str;
		});


		HTMLElement.prototype.__defineGetter__("innerHTML", function () {
			return getInnerHTML(this);
		});

		HTMLElement.prototype.__defineGetter__("outerHTML", function () {
			return getOuterHTML(this)
		});
	}
}


if(typeof HTMLBodyElement!='undefined') {
	if (Event.prototype) {
		Event.prototype.__defineGetter__("clientWidth", function() {
			return window.innerWidth;
		});

		Event.prototype.__defineGetter__("clientHeight", function() {
			return window.innerHeight;
		});
	}
}

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+'" src="about:blank;" name="'+this.name+'" frameborder="0" style="position:absolute; left:0; top:0; width:0; height:0;"></iframe>';
		else
			html = '<iframe	id="'+this.name+'" src="about:blank;" 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 dynamicIFrameDebug(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);
	}
	DynamicIFrameManager.debug = true;
	caller.call();
	DynamicIFrameManager.debug = 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

