var stockinfo={
	loadingText:'Please wait, loading...',
	get:function stockinfo__get(pid) {
		var box=stockinfo.getbox(pid);
		if (!box) return true;
		stockinfo.show(box);
		if (!box.hasLoadedStockInfo) {
			xajax_stockinfo(pid);
		}
		return false;
	},
	hide:function stockinfo__hide(box) {
		box.style.display='none';
	},
	show:function stockinfo__show(box) {
		box.style.display='';
	},
	getbox:function stockinfo__getbox(pid) {
		var box=document.getElementById('stock-info-'+pid);
		if (!box) {
			var btn=document.getElementById('stock-info-button-'+pid);
			if (!btn) {
				return false;
			}
			box=stockinfo.createbox(pid);
			document.body.appendChild(box);
			box.button=btn;
		}
		this.placeBox(box);
		return box;
	},
	createbox:function stockinfo__createbox(pid) {
		var box=document.createElement('div');
		box.id='stock-info-'+pid;
		box.className='stock-info-container not-loaded';
		stockinfo.hide(box);
		box.hasLoadedStockInfo=false;
		box.appendChild(document.createTextNode(stockinfo.loadingText));
		box.onclick=function() { stockinfo.hide(box); };
		return box;
	},
	loaded:function stockinfo__loaded(pid) {
		var box=stockinfo.getbox(pid);
		if (!box) return;
		box.hasLoadedStockInfo=true;
		box.className='stock-info-container';
		this.placeBox(box);
		if (box.button && box.button.blur) {
			box.button.blur();
		}
	},
	placeBox:function stockinfo__placeBox(box) {
		if (!box.wantedPos) {
			box.wantedPos=this.findPos(box.button);
		}
		var pos=box.wantedPos;
		if (!pos.x || !pos.y) {
			return;
		}
		if (!box.pageType) {
			box.pageType=this.findPageType(box.button);
		}
		var left=pos.x-box.offsetWidth;
		var top=pos.y;
		if (box.hasLoadedStockInfo) {
			left+=box.button.offsetWidth+1;
			if (box.pageType=='full') {
				top+=box.button.offsetHeight+1-box.offsetHeight;
			} else {
				top--;
			}
		}
		box.style.position='absolute';
		box.style.left=left+'px';
		box.style.top=top+'px';
		if (!box.offsetParent || box.offsetWidth==0) {
			setTimeout(function() {
				if (box.offsetParent && box.offsetWidth!=0) {
					stockinfo.placeBox(box);
				}
			},0);
		}
	},
	findPos:function stockinfo__findPos(obj) {
		var curLeft=0;
		var curTop=0;
		if (obj.offsetParent) {
			while(1) {
				curLeft+=obj.offsetLeft;
				curTop+=obj.offsetTop;
				if (!obj.offsetParent)
					break;
				obj=obj.offsetParent;
			}
		} else if(obj.x && obj.y) {
			curLeft+=obj.x;
			curTop+=obj.y;
		}
		return {x:curLeft,y:curTop};
	},
	findPageType:function stockinfo__findPageType(btn) {
		var re=/(^|\s)content-view-(line|full)(\s|$)/;
		while(btn) {
			if (btn.className) {
				var res=re.exec(btn.className);
				if (res!=null) {
					return res[2];
				}
			}
			btn=btn.parentNode;
		}
		return 'line';
	}
};

