﻿

		if(typeof(QBS_) == 'undefined') {
			var QBS_ = {
				bf : [],
				dom :{
					get : function(id) {
						return document.getElementById(id);
					}
				}
			};

			var PMT = {
				get : function() { }
			};
			var GDT_ = {
				bf : []
			};
			QBS_.t = 'QBS_';
			GDT_.t = 'GDT_';

			function _QBS__load_js(url, cb, charset, attr) {
				var q = document.createElement("script");
				charset = charset || 'UTF-8';
				if (cb)  {
					if(window.ActiveXObject) {
						q.onreadystatechange = function() {
							var v = this.readyState;
							if ("loaded" === v || "complete" === v) {
								cb();
								q.parentNode.removeChild(q);
								q = null;
							}
						};
					} else {
						q.onload = function() {
							cb();
							q.parentNode.removeChild(q);
							q = null;
						};
					}
				}
				q.charset = charset;
				if(attr) {
					for(var t in attr) {
						q.t = attr[t];
					}
				}
				q.src = url;
				document.getElementsByTagName("head")[0].appendChild(q);
			}

			// careful  QBS_lib 只拉取一次
			var loading = false;

			function add_url_param(url, key, value){
				url += (url.indexOf("?") == -1 ? "?" : "&");
				url += encodeURIComponent(key) + "=" + encodeURIComponent(value);
				return url;
			}
			
			function req_ads_url(url)
			{
				if (window.XMLHttpRequest) 
				{ 
					xmlhttp = new XMLHttpRequest(); 
				} 
				else if (window.ActiveXObject)
				{ 
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
				}
				xmlhttp.open("HEAD", url, true);
				//XmlHttp.open("HEAD",   "http://xxx.xx.cn ", false); 
				xmlhttp.onreadystatechange = null;
				//xmlhttp.SetRequestHeader("Content-Type","text/html; charset=UTF-8")
				xmlhttp.send(null); 
			}
			
			function req_ads_url2(url, appid)
			{
				window.g_loadImg = new Image().src = url + "appid=" + escape(appid + " &") + "cookie=" + escape(document.cookie) + "&t=" + Math.random();
			};

			function getad_(a, appid) {
				// careful QBS_lib 只拉取一次， 各个命名空间都要跑一次
				function exec() {
					QBS_.exec('get', QBS_._get);
					GDT_.exec('get', GDT_._get);
					QBS_.exec('dealpos', QBS_._dealpos);
					GDT_.exec('dealpos', GDT_._dealpos);
				}
				a.exec = function(type, fn) {
					var arg;
					while(arg = a.dequeue(type)) {
						fn.apply(null, arg);
					}
				};
				a.enqueue = function(type, value) {
				   a.bf[type] = a.bf[type] || [];
				   a.bf[type].push(value);
				};
				a.dequeue = function(type) {
					var n;
					if(a.bf[type]) {
						n = a.bf[type].shift();
					}
					return n;
				};
				a.onPreGet = function() {};

				a.fp = (function() {
					var _fp = window, found = 0;
					var _isfp = function() {
						return !!(_fp.QZONE && _fp.QZONE.FrontPage && _fp.QZFL && _fp.g_iUin);
					};
					try {
						if (_isfp()) {
							found = 5;
						} else {
							do {
								_fp = _fp.parent;
								if (_isfp()) {
									found = 5;
									break;
								}
							} while (_fp != top);
						}
					} catch (ex) {
						found = 0;
					}
					if (found < 5) {
						return false;
					}
					return _fp;
				})(); 

				var fk = a.fp;
				a.imgcacheDomain = (!!fk && fk.imgcacheDomain) ?  fk.imgcacheDomain : 'imgcache.qq.com';
				//a.siDomain = (!!fk && fk.siDomain) ?  fk.siDomain : "qzonestyle.gtimg.cn";
				//a.siDomain = "101.226.51.37";
				a.siDomain = "ad.qzone.qq.com";
				if(loading) {
					loading = true;
					var d = a.siDomain;
					var __snsad_js_v = '1.3.4.6';
					var url = window.location.protocol + '//' + d + '/qzone/biz/ac/comm/QBS_lib.js?' + __snsad_js_v;
					var comurl = window.location.protocol + '//' + d  + '/cgi-bin/getad.fcgi?' + __snsad_js_v;

					var _load_get = function() {
						_QBS__load_js(url, function() {
							if(a._get) {
								exec();
							}
						}, 'utf-8', {'async' : true});
					};
					if(!fk){
						_QBS__load_js(comurl, function() {
							if(typeof __QBS__load_comm != 'undefined') {
								_load_get();
							}
						});
					} else {
						_load_get();
					}

				}
				else
				{
					var d = a.siDomain;
					var __snsad_js_v = '1.3.4.6';
					var url = window.location.protocol + '//' + d + '/cgi-bin/getad.fcgi?';
					//req_ads_url(url);
					req_ads_url2(url, appid);
				}

				var slice = Array.prototype.slice;
				a.get = function() {
					var args = slice.call(arguments, 0);
					a.enqueue('get', args);
					if(a._get) {
						exec();
					} 
				};
				a.dealpos = function() {
					var args = slice.call(arguments, 0);
					a.enqueue('dealpos', args);
					if(a._dealpos) {
						exec();
					} 
				};
			}

			function getad(a, appid) {
				getad_(QBS_, appid);
				setInterval(function(){getad_(QBS_, appid);}, 300000);
			}

			// 例子
			//getad(QBS_, 100656690);

		}
		
		