
// 广告加载
function AdPopup(conf){
	var config = {
		ctId: 'picG',				
		outerHeight: 300,			
		outerWidth: 500,			
		adHeight: 300,				
		adWidth: 300,				
		adUrl: 'http://pmm.people.com.cn/main/s?user=people|nonechannel|B11_2013&db=people&border=0&local=yes&js=ie&kv=people|finance;third|69230;fourth|;newsid|22891002',//'http://pmm.people.com.cn/main/s?user=people|nonechannel|B11_2013&amp;db=people&amp;border=0&amp;local=yes&amp;js=ie&amp;kv=people|auto;third|69231;fourth|;newsid|22865596',
									
		adType: 'script'			
	};
	this._conf = AdPopup.mergeInto(config, conf);
	this.init();
}
AdPopup.mergeInto = function(objA, objB) {
	if (!objA || !objB) {
		return objA;
	}
	for(var key in objB) {
		if (objB.hasOwnProperty(key)) {
			objA[key] = objB[key];
		}
	}
	return objA;
};

AdPopup.prototype.init = function() {
	this._ctElem = document.getElementById(this._conf.ctId);
	this._adCtElem = this.getWrapElem();
	this._adCtElem.style.display = "none";
	this._ctElem.appendChild(this._adCtElem);
};

AdPopup.prototype.getWrapElem = function() {
	var adWrap = document.createElement('div');
	adWrap.id = this._conf.wrapId;
	var position = this.getPosition();
	adWrap.style.cssText = this.getCssText(position);
	var opacityElem = document.createElement('div');
	var adCtElem = document.createElement('div');
	opacityElem.style.cssText = this.getCssText(null, true);
	adCtElem.style.cssText = this.getCssText(null);
	adCtElem.appendChild(this.getAdElem());
	var nextPage = document.createElement('div');
	nextPage.style.width = this._conf.nextPageWidth + 'px';
	nextPage.style.height = this._conf.nextPageHeight + 'px';
	nextPage.style.position = 'absolute';
	nextPage.style.top = 0;
	nextPage.style.right = 0;
	nextPage.style['marginLeft'] = '10px';
	nextPage.style['marginRight'] = '10px';
	
	nextPage.innerHTML = this._conf.nextPageHtml;
	adCtElem.appendChild(nextPage)
	adWrap.appendChild(opacityElem);
	adWrap.appendChild(adCtElem);
	return adWrap;
};

AdPopup.prototype.getAdElem = function() {
	var adElem = document.createElement('div');
	adElem.id = 'innerAd';
	//adElem.style.float = 'left';
	adElem.style.height = this._conf.adHeight + 'px';
	adElem.style.width = this._conf.adWidth + 'px';
	adElem.style.position = 'absolute';
	adElem.style.top = '0px';
	adElem.style.left = '0px';
	adElem.style.margin = 0;
	adElem.style.padding = 0;
	var innerElem = document.createElement('div');
	innerElem.id='innerElem';
	
	loadScript(innerElem, this._conf.adUrl);
	adElem.appendChild(innerElem);
	// adElem.appendChild(script);
	return adElem;
};
AdPopup.prototype.getNextPicsElem = function() {
	var nextPicsElem = document.createElement('div');

};

AdPopup.prototype.getPosition = function() {
	var imgElem = this._ctElem.getElementsByTagName('img')[0];
	var ctWidth = this._ctElem.offsetWidth;
	var ctHeight = this._ctElem.offsetHeight;
	var left = (ctWidth - this._conf.outerWidth)/2;
	var right = (ctHeight - this._conf.outerHeight)/2;
	right = (right<0) ? 0 : right;
	left = (left<0) ? 0 : left;
	return {
		left: left,
		right: right
	};
};
AdPopup.prototype.getCssText = function(position, bOpacity) {
	var cssText = 'position:absolute;margin:0;padding:0;'
		+ 'width:' + this._conf.outerWidth + 'px'
		+ ';height:' + this._conf.outerHeight + 'px;';
	if(position) {
		cssText +=  ('left:' + position.left + 'px'
		+ ';top:' + position.right + 'px;');
	} else {
		cssText += ('left:0;top:0;');
	}
	if (bOpacity == true) {
		cssText += 'background-color:#000;opacity:0.8;filter:alpha(opacity=80);';
	}
	return cssText;
};

AdPopup.prototype.show = function() {
	//this._ctElem.appendChild(this._adCtElem);
	this._adCtElem.style.display = "block";
};

