AE.namespace('AE.widget.scrollShowDiv')
AE.widget.scrollShowDiv = function(){
	this._oTarget;
	this._oConfig;
	this._showed = false;
	this._request;
	this._onShowEvent;
	this._anim;
	this._y = 0;
	this._Y = 0;
	this._YY = 0;
	this._defConfig = {
		delayTime : 500,
		overflowHeight : 18,
		alwaysRefresh : false,
		showLoading : true,
		loadingImage : globalImgServer+'/images/eng/style/icon/loading.gif',
		usingCache : true,
		dataType : 'html',
		showAnim : false,
		showFrame : false,
		search_delete : true,
		iframe_delete : true
	}
}
AE.widget.scrollShowDiv.prototype = {
	_getFrameSuccess : function(o){
		var _self = this;
		if (this._oConfig.showAnim && this._oConfig.animConf){//show animation
			this._anim = new YAHOO.util.Anim(this._oTarget, this._oConfig.animConf);
			this._anim.onTween.subscribe(function(){
				if (_self._oConfig.stopCondition(_self._oTarget)) _self._anim.stop();
			});
			this._anim.onComplete.subscribe(function(){
				_self.onShowEvent.fire();
			});
			this._anim.animate();
		}
		else{
			setTimeout(function(){
				_self.onShowEvent.fire();
			}, 200);
		}
		if (!this._oConfig.showFrame){//do not show frame
			//if ((this._oConfig.dataType == 'imag' || this._oConfig.dataType == 'flas') && this._oConfig.showLoading) this._oTarget.removeChild(this._oTarget.firstChild);
			if (this._oConfig.dataType == 'body') this._oTarget.innerHTML = o.contentWindow.document.body.innerHTML;
			else if (this._oConfig.dataType == 'html') this._oTarget.innerHTML = o.contentWindow.document.documentElement.innerHTML;
			else this._oTarget.innerHTML = o.contentWindow.document.getElementById(this._oConfig.dataType).innerHTML;
		}
		else{
			if (this._oConfig.showLoading) this._oTarget.removeChild(this._oTarget.firstChild);
			o.style.display = 'block';
			if (this._oConfig.defWidth) o.style.width = (this._oConfig.defWidth + 2) + 'px';
			else o.style.width = '100%';
			o.style.height = o.contentWindow.document.body.scrollHeight + 'px';
		}
	},
	
	_getJsonSuccess : function(o){
		if (this._oConfig.jsonType == 'image'){
			if (this._oConfig.showLoading) this._oTarget.removeChild(this._oTarget.firstChild);
			for(var i in scrollShowDiv_json){
				var img = document.createElement('img');
				img.src = scrollShowDiv_json[i];
				img.alt = '';
				this._oTarget.appendChild(img);
			}
		}
		if (this._oConfig.jsonType == 'flash'){
			if (this._oConfig.showLoading) this._oTarget.removeChild(this._oTarget.firstChild);
			for(var i in scrollShowDiv_json){
				var div = document.createElement('div');
				div.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
							+ ' width="'+scrollShowDiv_json[i]['width']+'" height="'+scrollShowDiv_json[i]['height']+'">'
							+ '<param name="movie" value="'+scrollShowDiv_json[i]['src']+'" /><param name="wmode" value="opaque" /><param name="loop" value="true" /><param name="quality" value="high" /><param name="allowScriptAccess" value="always" />'
							+ '<embed src="'+scrollShowDiv_json[i]['src']+'" loop="true" wmode="opaque" quality="high"'
							+ ' width="'+scrollShowDiv_json[i]['width']+'" height="'+scrollShowDiv_json[i]['width']+'"'
							+ ' type="application/x-shockwave-flash" allowScriptAccess="always"></embed></object>';
				this._oTarget.appendChild(div);
			}
		}
		this.onShowEvent.fire();
	},
	
	_showContent : function(){
		if (this._YY == this._Y){
			this._showed = true;
			if (!this._oConfig.usingCache) this._oConfig.url += '?' + Date.parse(new Date());
			var _self = this;
			//this.request = YAHOO.util.Connect.asyncRequest('GET', this.oConfig.url, {success:function(o){_self.getHotSuccess(o);},failure:function(){_self.getHotFailure();},argument:[]});
			//this.request = YAHOO.util.Get.script(this.oConfig.url,{scope:this,onSuccess:function(o){this.getHotSuccess(o);},onFailure:function(o){this.getHotFailure(o);}});
			//var proxy = $('proxy').contentWindow;
			//this.request = proxy.YAHOO.util.Connect.asyncRequest('GET',this.oConfig.url,{success:function(o){_self.getHotSuccess(o);},failure:function(){_self.getHotFailure();},argument:[]});
			if (this._oConfig.dataType == 'json'){
				YAHOO.util.Get.script(this._oConfig.url,{scope:this,onSuccess:function(o){_self._getJsonSuccess(o);},onFailure:function(o){}});
			}
			/*
			else if (this._oConfig.dataType == 'imag'){
				var img = document.createElement('img');
				YUE.on(img, 'load',  function(){
					_self._getSuccess(img);
				});
				img.src = this._oConfig.url;
				img.alt = '';
				this._oTarget.appendChild(img);
			}
			else if (this._oConfig.dataType == 'flas'){
				var div = document.createElement('div');
				div.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
							+ ' width="'+this._oConfig.defWidth+'" height="'+this._oConfig.defHeight+'">'
							+ '<param name="movie" value="'+this._oConfig.url+'" /><param name="wmode" value="opaque" /><param name="loop" value="true" /><param name="quality" value="high" /><param name="allowScriptAccess" value="always" />'
							+ '<embed src="'+this._oConfig.url+'" loop="true" wmode="opaque" quality="high"'
							+ ' width="'+this._oConfig.defWidth+'" height="'+this._oConfig.defHeight+'"'
							+ ' type="application/x-shockwave-flash" allowScriptAccess="always"></embed></object>';
				this._oTarget.appendChild(div);
				_self._getSuccess(div);
			}
			*/
			else{
				var ifm = document.createElement('iframe');
				YUE.on(ifm, 'load',  function(){
					_self._getFrameSuccess(ifm);
				});
				if(this._oConfig.iframe_delete) this._oConfig.url += '?iframe_delete=true';
				ifm.src = this._oConfig.url;
				ifm.scrolling = 'no';
				ifm.frameBorder = 0;
				if(this._oConfig.search_delete) ifm.rel = 'nofollow';
				ifm.style.display = 'none';
				this._oTarget.appendChild(ifm);
			}
		}
	},
	
	_showDelay : function(){
		this._Y = YUD.getDocumentScrollTop() + YUD.getViewportHeight();
		if (this._Y >= this._y && (!this._showed || this._oConfig.alwaysRefresh)){
			if (this._oConfig.showLoading) this._oTarget.innerHTML = '<img src="'+this._oConfig.loadingImage+'" alt="loading..." />';
			if (!this._oConfig.alwaysRefresh) YUE.removeListener(window, "scroll", this._showDelay);
			this._YY = YUD.getDocumentScrollTop() + YUD.getViewportHeight();
			var _self = this;
			setTimeout(function(){_self._showContent();}, this._oConfig.delayTime);
		}
	},
	
	/**
	target explanation :
		this can be an object or an object id.
	config explanation :
		**url : 		the url of target page where contain the context we need.
		*defHeight : 	the container's height, it will be better to be defined.
		*defWidth : 	it will be better to be defined when showFrame is true, and the default value is 100%.
		*dataType :		this value can be 'body', 'html', 'json' or html element's id.
		jsonType :		when dataType is json, this variable can be 'image' or 'flash' or any user defined value.
		delayTime : 		the time delayed to show
		overflowHeight : 	the height overflow container that touch off show event ( it can be a negative number, and when it is 0 the overflowHeight is the container height.)
		alwaysRefresh : 	whether always refresh the div everytime window scroll to the container's position, the default value is false.
		showLoading : 	whether show loading pic, the default value is true.
		loadingImage : 	this variable supports self-defined image for loading.
		usingCache : 	whether use context cache at this event, the default value is true. 
		showAnim : 		whether show animation when event is touched off, the default value is false. when dataType == 'json' this config will be unuseable.
		*showFrame : 	whether show iframe object directly, the default value is false.
		*animConf : 	if showAnim is true, this property must be defined, e.x : {height : {to : 280}}
		*stopCondition : 	if showAnim is true, this property must be defined, e.x : function(o){return o.offsetHeight >= o.lastChild.offsetHeight;}
		*onShow : 		the trigger that is touched off at div showed. when dataType == 'json' this function must be defined.
	*/
	init : function(target, config){
		if (config.url){
			this._oTarget = get(target);
			this._oConfig = YL.merge(this._defConfig, config || {});
			
			if (!this._oConfig.showAnim && this._oConfig.defHeight){
				this._oTarget.style.height = this._oConfig.defHeight + 'px';
			}
			if (this._oConfig.defWidth){
				this._oTarget.style.width = this._oConfig.defWidth + 'px';
			}
			if (this._oConfig.showAnim){
				this._oTarget.style.overflow = 'hidden';
				this._oTarget.style.height = '0px';
			}
				
			this.onShowEvent = new YAHOO.util.CustomEvent('onShow', null, false, YAHOO.util.CustomEvent.FLAT);
			if (this._oConfig.onShow) {
				this.onShowEvent.subscribe(this._oConfig.onShow, this, true);
			}
			
			if (YUD.getY(this._oTarget) < YUD.getViewportHeight()){
				this._showDelay();
			}
			else{
				if (this._oConfig.overflowHeight == 0)
					this._y = YUD.getY(this._oTarget) + this._oTarget.offsetHeight;
				else
					this._y = YUD.getY(this._oTarget) + this._oConfig.overflowHeight;
					
				YUE.on(window, 'scroll', this._showDelay, this, true);
			}
		}
	},
	
	getTarget : function(){
		return this._oTarget;
	}
}