var adBox = function(){
	var D = document, docHead = D.getElementsByTagName('head')[0], adCSSPath = '', adStyleSheet = null, adWindow 

= null, adTitleBar = null, adTitle = null, adCloseBar = 

null, adContent = null, sTitle = '阿宝工作室 - 专注网页设计与制作', isDisplay = false, isIE = function(){
		return window.ActiveXObject;
	}(), isOpera = function(){
		var ua = navigator.userAgent, A = ua.match(/Opera[\s\/]([^\s]*)/);
		if (A && A[1]) {
			return parseFloat(A[1]);
		}
		else {
			return false;
		}
	}(), isNumber = function(num){
			return typeof num === 'number' && isFinite(num);
	}, position = 'RIGHT';
	
	return {
		init: function(config){
			if (config.cssPath) {
				adCSSPath = config.cssPath;
			}
			sTitle = config.title;
			sUrl = config.url;
			sPath = config.path;
			sContent = config.content;
			adBox.initComponent();
			adBox.initEvents();
			if (config.position) {
				position = config.position;
			}
			adBox.fixPosition();
			if (isNumber(config.showTime) && config.showTime > 0) {
				setTimeout(adBox.close, (config.showTime * 1000));
			}
		},
		initComponent: function(){
			if (!isDisplay) {
				if (adCSSPath) {
					adStyleSheet = D.createElement('link');
					adStyleSheet.id = 'adStyleSheet';
					adStyleSheet.href = adCSSPath;
					adStyleSheet.rel = 'stylesheet';
					adStyleSheet.type = 'text/css';
					adStyleSheet.media = 'screen';
					D.body.appendChild(adStyleSheet);
				}
				adWindow = D.createElement('div');
				adWindow.id = 'y-adwindow';
				adTitleBar = D.createElement('div');
				adTitleBar.id = 'y-adtitlebar';
				adTitle = D.createElement('h2');
				adTitle.innerHTML = sTitle;
				adCloseBar = D.createElement('a');
				adCloseBar.id = 'y-adclosebar';
				adCloseBar.href = "#closeAd";
				adCloseBar.tilte = "关闭窗口";
				adCloseBar.innerHTML = "<img src='/xyj/images/x1.gif'>";
				adContent = D.createElement('div');
				adContent.id='y-adcontent';
				adContent.innerHTML = '<a href="' + sUrl + '" target="_blank">'+ sContent +'</a>';
				adTitleBar.appendChild(adTitle);
				adTitleBar.appendChild(adCloseBar);
				adWindow.appendChild(adTitleBar);
				adWindow.appendChild(adContent);
				D.body.appendChild(adWindow);
				isDisplay = true;
				adBox.fixPosition();
			}
		},
		stopEvent: function(evt){
			adBox.stopPropagation(evt);
			adBox.preventDefault(evt);
		},
		stopPropagation: function(evt){
			if (evt.stopPropagation) {
				evt.stopPropagation();
			}
			else {
				evt.cancelBubble = true;
			}
		},
		preventDefault: function(evt){
			if (evt.preventDefault) {
				evt.preventDefault();
			}
			else {
				evt.returnValue = false;
			}
		},
		initEvents: function(){
			if (isDisplay) {
				adCloseBar.onclick = function(event){
					var evt = event || window.event;
					adBox.close();
					adBox.stopEvent(evt);
				};
				window.onresize = adBox.fixPosition;
				window.onscroll = adBox.fixPosition;
			}
		},
		getDocumentWidth: function(){
			var k = adBox.getScrollWidth();
			var j = Math.max(k, adBox.getViewportWidth());
			return j;
		},
		getDocumentHeight: function(){
			var k = adBox.getScrollHeight();
			var j = Math.max(k, adBox.getViewportHeight());
			return j;
		},
		getScrollWidth: function(){
			var j = (D.compatMode == "CSS1Compat") ? D.body.scrollWidth : D.Element.scrollWidth;
			return j;
		},
		getScrollHeight: function(){
			var j = (D.compatMode == "CSS1Compat") ? D.body.scrollHeight : 

D.documentElement.scrollHeight;
			return j;
		},
		getXScroll: function(){
			var j = self.pageXOffset || D.documentElement.scrollLeft || D.body.scrollLeft;
			return j;
		},
		getYScroll: function(){
			var j = self.pageYOffset || D.documentElement.scrollTop || D.body.scrollTop;
			return j;
		},
		getViewportWidth: function(){
			var j = self.innerWidth;
			var k = D.compatMode;
			if (k || isIE) {
				j = (k == "CSS1Compat") ? D.documentElement.clientWidth : D.body.clientWidth;
			}
			return j;
		},
		getViewportHeight: function(){
			var j = self.innerHeight;
			var k = D.compatMode;
			if ((k || isIE) && !isOpera) {
				j = (k == "CSS1Compat") ? D.documentElement.clientHeight : D.body.clientHeight;
			}
			return j;
		},
		fixPosition: function(){
			if (isDisplay) {
				var adWindowWidth = 261, adWindowHeight = 184, documentWidth = 

adBox.getDocumentWidth(), documentHeight = adBox.getDocumentHeight(), 

viewportWidth = adBox.getViewportWidth(), viewportHeight = adBox.getViewportHeight(), xScroll = adBox.getXScroll(), 

yScroll = adBox.getYScroll(), left = 0, top = 0;

				if (position.toUpperCase() === 'RIGHT') {
					left = xScroll + (viewportWidth - adWindowWidth);
					top = yScroll + (viewportHeight - adWindowHeight);
					if (adWindowWidth > viewportWidth) {
						left = 0;
					}
					if (adWindowHeight > viewportHeight) {
						top = 0;
					}
				}
				else {
					if (position.toUpperCase() === 'CENTER') {
						left = xScroll + ((viewportWidth - adWindowWidth) / 2);
						top = yScroll + ((viewportHeight - adWindowHeight) / 2);
						if (adWindowWidth > viewportWidth) {
							if ((adWindowWidth < documentWidth) && ((adWindowWidth + 

xScroll) < documentWidth)) {
								left = xScroll;
							}
							else {
								left = documentWidth - adWindowWidth;
							}
						}
						else {
							if (adWindowWidth > documentWidth) {
								left = 0;
							}
						}
						
						if (adWindowHeight > viewportHeight) {
							if ((adWindowHeight < documentHeight) && ((adWindowHeight + 

yScroll) <= documentHeight)) {
								top = yScroll;
							}
							else {
								top = documentHeight - adWindowHeight;
							}
						}
						else {
							if (adWindowHeight > documentHeight) {
								top = 0;
							}
						}
					}
				}
				
				adWindow.style.margin = "0";
				adWindow.style.left = left + 'px';
				adWindow.style.top = top + 'px'
			}
		},
		purge: function(d){
			var a = d.attributes, i, l, n;
			if (a) {
				l = a.length;
				for (i = 0; i < l; ++i) {
					n = a[i].name;
					if (typeof d[n] === 'function') {
						d[n] = null;
					}
				}
			}
			a = d.childNodes;
			if (a) {
				l = a.length;
				for (i = 0; i < l; i += 1) {
					adBox.purge(d.childNodes[i]);
				}
			}
		},
		close: function(){
			if (isDisplay) {
				adBox.purge(adWindow);
				adBox.fixPosition = null;
				D.body.removeChild(adWindow);
				if (adStyleSheet) {
					D.body.removeChild(adStyleSheet);
				}
				isDisplay = false;
			}
		}
	}
}();

(function(){
	/*
	 * title - 广告标题文字
	 * url - 广告新闻链接
	 * path - 广告图片路径
	 * content - 广告内容
	 * showTime - 广告显示时间 (可选：1 = 1秒钟，0、负数、字符串、不写均表示一直显示浮动广告)
	 * position - 弹出广告位置(可选：CENTER - 窗口正中间 或 RIGHT - 窗口右下角)
	 */
	adConfig = {
		// 需要调整成外网的实际路径
		cssPath: '/xyj/css/adBox.css',
		title: '尊敬的客户您好：',
		url: '/xyj/slnew/slnew_baoxian/index.html',
		path: '/xyj/images/ad.jpg',
		content: '为答谢广大客户长期以来的支持，我网站将不定期举行“网上预订机票酒店送保险”活动，此次活动从2010年7月14日开始，结束日期见网站公告，望广大客户把握机会，并相互转告，谢谢。',
		showTime: 15,
		position:'right'
	};
	setTimeout(function(){
		adBox.init(adConfig);
	}, 2000);
})();

