﻿﻿jQuery.fn.extend({
	dongFullAd : function(obj){
		if (obj==null) obj = {};
		var width = "100%";
		var height = "100%";
		var showTime = 4000;
		var objThis = this;
		var _frist = true;
		
		if (obj.width!=null){
			width = obj.width;
		}
		if (obj.height!=null){
			height = obj.height;
		}
		if (obj.showTime!=null){
			showTime = obj.showTime;
		}
		if (obj.button_close!=null){
			obj.button_close.click(function(){
				hideAd();
			});
		}
		
		var time = null;

		objThis.ready(function(){
			if (_frist){
				if (objThis.find("img").size()>0){
					loadImages(objThis.find("img"));
				}else{
					showAd();
				}
			}
		});
		
		
		function loadImages(img){
			if (img.size()>0){
				loadNumImage(0 , showAd);
			}
			
			function loadNumImage(n , showAd_){
				if (n>=img.size()){
					showAd_();
				}else{
					var tmpImg = new Image();
					tmpImg.src=img.eq(n).attr("src");
					if (tmpImg.complete){
						loadNumImage(n+1 , showAd_);
					}else{
						tmpImg.onload = function (){
							loadNumImage(n+1 , showAd_);
						}
					}
				}
			}
		}
		
		function showAd(){
			objThis.css({ "width":width , "height":height });
			
			objThis.slideDown("slow" , function(){
				time=setTimeout(hideAd,showTime);
			});
		}

		function hideAd(){
			clearTimeout(time);
			_frist = false;
			
			objThis.slideUp("fast");
		}
		
		
	}
});
