/*
 模拟 title tips
 javascript for Tooltips by Alessandro Fulciniti & 浪子
 design by jeftom
 website: http://www.tesign.cn
 调用：
 <script type="text/javascript" src="/js/tip/tips.js"></script>
 tipstitle="内容"
*/
function $element() {
	var elements = new Array();

	for (var i = 0; i < arguments.length; i++) {
	var element = arguments[i];
	if (typeof element == 'string')
	element = document.getElementById(element);

	if (arguments.length == 1) {
		return element;
	}

	elements.push(element);
	}

	return elements;
}

// 引入css文件
function importCss(){
	var l=CreateEl("link");
	l.setAttribute("type","text/css");
	l.setAttribute("rel","stylesheet");
	l.setAttribute("href","/js/tip/tips.css");
	l.setAttribute("media","screen");
	document.getElementsByTagName("head")[0].appendChild(l);
}

// 创建HTML对象
function CreateEl(t,c){
	var x=document.createElement(t);
	x.className=c;
	//x.style.display="block";
	return(x);
}

// 半透明处理
function setOpacity(el){
	el.style.filter="alpha(opacity:95)";
	el.style.KHTMLOpacity="0.95";
	el.style.MozOpacity="0.95";
	el.style.opacity="0.95";
}

//兼容性
if(!document.attachEvent)//Not IE
{
	document.attachEvent = function(){document.addEventListener(arguments[0].substr(2),arguments[1],arguments[2])}
}

if(!window.attachEvent)//Not IE
{
	window.attachEvent = function(){window.addEventListener(arguments[0].substr(2),arguments[1],arguments[2])}
}

//位置偏移
var qTipX = -10; //This is qTip's X offset//
var qTipY = 10; //This is qTip's Y offset//

tooltip = {
	name : "global_client_tip",
	offsetX : qTipX,
	offsetY : qTipY,
	tip : null
}

tooltip.init = function () {
	try{
	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";
	if(!tipContainerID){ var tipContainerID = "global_client_tip"; }
	var tipContainer = document.getElementById(tipContainerID);

	if(!tipContainer) {
		importCss();
		tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		tipContainer.setAttribute("id", tipContainerID);

		tip_mask = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "iframe") : document.createElement("iframe");
		tip_mask.setAttribute("id", "tip_mask");
		tip_mask.setAttribute("frameborder", "0");
		tip_mask.setAttribute("scrolling", "no");
		tipContainer.appendChild(tip_mask);

		client_tip_up = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		client_tip_up.setAttribute("id", "client_tip_up");
		tipContainer.appendChild(client_tip_up);

		client_tip = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		client_tip.setAttribute("id", "client_tip");
		tipContainer.appendChild(client_tip);
		
		document.getElementsByTagName("body").item(0).appendChild(tipContainer);
	}

	this.tip = $element(this.name);
	if (this.tip)
	{
		document.attachEvent("onmousemove",function (e) {tooltip.move(e)});
	}
	var sTitle = "";
	var copyit_ = "";
	//串连onmouseover事件
	document.attachEvent("onmouseover",function(e)
		{
		try{
		if(typeof(event)=="undefined"){
			copyit_ = e.target.getAttribute("copyit")
		}else {
			e = event;
			copyit_ = e.srcElement.copyit;
		};
		if(e.srcElement.type!="select-one"&&!copyit_){ 
			if(typeof(event)=="undefined"){
				sTitle = e.target.getAttribute("title")
				e.target.removeAttribute("title");
			}else {
				e = event;
				sTitle = e.srcElement.title;
				e.srcElement.title = "";
			};

			if(!sTitle == "")
			{
				tooltip.show(sTitle);
			}
		}
		}catch(e){}
		}
	);

	//串连onmouseout事件
	document.attachEvent('onmouseout',function(e){
	  try{
	  	if(typeof(event)=="undefined"){
			copyit_ = e.target.getAttribute("copyit")
		}else {
			e = event;
			copyit_ = e.srcElement.copyit;
		};
		if(e.srcElement.type!="select-one"&&!copyit_){
			if(typeof(event)=="undefined"){
				e.target.setAttribute("title",sTitle);
			}else {
				e = event;
				e.srcElement.title = sTitle;
			};

			tooltip.hide();
		}
		}catch(e){}
	}
	);
	}catch(e){}
}

//移动事件
tooltip.move = function (evt) {
    try{
	//Get mouse X & Y
	var x=0, y=0;
	if (document.all) {//IE
		x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		x += window.event.clientX;
		y += window.event.clientY;
	} else {//Good Browsers
		x = evt.pageX;
		y = evt.pageY;
	}
	this.tip.style.left = (x + this.offsetX) + "px";
	this.tip.style.top = (y + this.offsetY) + "px";
	// 设置iframe mask的宽度和高度
	if (document.getElementById('tip_mask') && document.getElementById('client_tip'))
	{
		document.getElementById('tip_mask').style.width = document.getElementById('client_tip').offsetWidth + 'px';
		document.getElementById('tip_mask').style.height = document.getElementById('client_tip').offsetHeight + 'px';
	}
	}catch(e){}
}

tooltip.show = function (text) {
	if (!this.tip) return;
	this.tip.lastChild.innerHTML = text;
	this.tip.style.display = "block";
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.lastChild.innerHTML = "";
	this.tip.style.display = "none";
}
function autotipload(){
	if(!document.getElementById('global_client_tip')) {
		window.setTimeout("tooltip.init()",300);
	}
}
//load的时候初始化自定义显示方式
window.attachEvent("onload",autotipload);
//假如页面加载不完
window.setTimeout("autotipload()",6000);


/**********复制功能***********/

//位置偏移
var qTipXCopy = 5; //This is qTip's X offset//
var qTipYCopy = -5; //This is qTip's Y offset//
tooltipCopy = {
	name : "global_client_tip_copyit",
	offsetX : qTipXCopy,
	offsetY : qTipYCopy,
	tip : null,
	timeoutid : null
}
// 引入css文件
function importCssCopy(){
	var l=CreateEl("link");
	l.setAttribute("type","text/css");
	l.setAttribute("rel","stylesheet");
	l.setAttribute("href","/js/tip/tipscopy.css");
	l.setAttribute("media","screen");
	document.getElementsByTagName("head")[0].appendChild(l);
}
tooltipCopy.init = function () {
	try{
	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";
	if(!tipContainerID){ var tipContainerID = "global_client_tip_copyit"; }
	var tipContainer = document.getElementById(tipContainerID);

	if(!tipContainer) {
		importCssCopy();
		tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		tipContainer.setAttribute("id", tipContainerID);
			

		tip_mask = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "iframe") : document.createElement("iframe");
		tip_mask.setAttribute("id", "tip_mask_copyit");
		tip_mask.setAttribute("frameborder", "0");
		tip_mask.setAttribute("scrolling", "no");
		tipContainer.appendChild(tip_mask);

		//client_tip_up = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		//client_tip_up.setAttribute("id", "client_tip_up_copyit");
		//tipContainer.appendChild(client_tip_up);

		client_tip = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		client_tip.setAttribute("id", "client_tip_copyit");
		tipContainer.appendChild(client_tip);

		document.getElementsByTagName("body").item(0).appendChild(tipContainer);
	}

	this.tip = $element(this.name);
	if (this.tip){
		//document.attachEvent("onmousemove",function (e) {tooltipCopy.move(e)});
	}
	var copyit_ = "";

	//串连onmouseover事件
	document.attachEvent("onmouseover",function(e){
		try{
		if(e.srcElement.type!="select-one"){
			if(typeof(event)=="undefined"){
				copyit_ = e.target.getAttribute("copyit")
			}else {
				e = event;
				copyit_ = e.srcElement.copyit;
			};
			if(copyit_&&copyit_!="")
			{   
			   if(this.timeoutid){
			   	window.clearTimeout(this.timeoutid);
			   	tooltipCopy.hide();
			   }
				tooltipCopy.show(copyit_);
				//获得复制的内容更
				this.timeoutid = window.setTimeout("tooltipCopy.hide()",5000);
			    if(getParentElement(e.toElement,"global_client_tip_copyit") == null){
					tooltipCopy.move(e);
			    }
			}
		}
		}catch(e){}
		}
	);

	//串连onmouseout事件
	document.attachEvent('onmouseout',function(e){
	  try{
			if(e.srcElement.type!="select-one"){
			    if(getParentElement(e.toElement,"global_client_tip_copyit") == null && e.fromElement.nodeName!="TD"){
					tooltipCopy.hide();
			    }
			}
		}catch(e){}
	}
	);
	}catch(e){}
}
//移动事件
tooltipCopy.move = function (evt) {
    try{
		//Get mouse X & Y
		var x=0, y=0;
		if (document.all) {//IE
			x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
			y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
			x += window.event.clientX;
			y += window.event.clientY;
		} else {//Good Browsers
			x = evt.pageX;
			y = evt.pageY;
		}
		this.tip.style.left = (x + this.offsetX) + "px";
		this.tip.style.top = (y + this.offsetY) + "px";
		// 设置iframe mask的宽度和高度
		if (document.getElementById('tip_mask_copyit') && document.getElementById('client_tip_copyit')){
			document.getElementById('tip_mask_copyit').style.width = document.getElementById('client_tip_copyit').offsetWidth + 'px';
			document.getElementById('tip_mask_copyit').style.height = document.getElementById('client_tip_copyit').offsetHeight + 'px';
		}
	}catch(e){}
}

tooltipCopy.show = function (text) {
	if (!this.tip||!text) return;
	var shittxt=text;
	if(shittxt.length>20){
		shittxt  = shittxt.substring(0,20)+"...";
	}
	var html = "<table id='tab_tc' border='0' width='150'><tr><td width='34'><input type='button' class='asms_copy' style='width:34px;' onclick=tooltipCopy.copy('"+text+"') /></td>";
  	html += "<td>"+shittxt+"</td></tr></table>";
	this.tip.lastChild.innerHTML = html;
	//this.tip.lastChild.innerHTML = "<input type='button' class='asms_copy' onclick=tooltipCopy.copy('"+text+"')><br>&nbsp;&nbsp;&nbsp;<b>"+shittxt+"</b>";
	this.tip.style.display = "block";
}

tooltipCopy.hide = function () {
	if (!this.tip) return;
	this.tip.lastChild.innerHTML = "";
	this.tip.style.display = "none";
}
tooltipCopy.copy = function(text){
	window.clipboardData.setData("Text",text);
	tooltipCopy.hide();
}
function setTrCopy(tableid,title_){
	tableid=$(tableid);
    if(!tableid||tableid.rows.length < 2){
    	return ;
    }
  	var col=-1;
	for (j=0; j < tableid.rows(0).cells.length; j++) {
		var tabtitle=tableid.rows(0).cells(j).innerText;
		tabtitle==""?"":tabtitle.replace(/[\r\n]/g,'');
		if(tabtitle == title_){
			col=j;
		}
	}
	if(col<0){
		return ;
	}
 	for (i=1; i < tableid.rows.length; i++) {
 		tableid.rows(i).cells(col).copyit=trimText(tableid.rows(i).cells(col));
 	}
}
//跨行复制PNR
function setTrCopy2(tableid,col){
	tableid=$(tableid);
    if(!tableid||tableid.rows.length < 2){
    	return ;
    }
 	for (var i=1; i < tableid.rows.length; i++) {
 		if(tableid.rows(i).cells(col)!=null){
 			tableid.rows(i).cells(col).copyit=trimText(tableid.rows(i).cells(col));
 		}
 	}
}
//得到标签下面纯文本
function trimText(obj){
    while(obj.hasChildNodes()){
        obj = obj.firstChild;
    }
	return obj.nodeValue==null?null:obj.nodeValue.replace(/[\r\n]/g,'');
}
//循环得到指定层级的节点元素
function getParentElement(obj,pid){
    while( obj != null && obj.id != pid){
        obj = obj.parentElement;
    }
    return obj;
}
function autotiploadCopy(){
	if(!document.getElementById('global_client_tip_copyit')) {
		window.setTimeout("tooltipCopy.init()",300);
	}
}
//load的时候初始化自定义显示方式
window.attachEvent("onload",autotiploadCopy);
//假如页面加载不完
window.setTimeout("autotiploadCopy()",6000);
