var tip = {
	
	tipobj:null,
	tipiframe:null,
	
	properties:{		
		width:'400px',
		height:null,
		backgroundColor:'#ffffff',
		padding:'10px',
		minWidth:null,
		border:'1px solid #113d66'
	},
	
	
	selectIsNear:null,
	sth:null,
	sthX:null,
	sthY:null,
	showCloseBt:true,
	
	enabletip1:false,
	enabletipstat:false,
	position:'event',
	offsetxpoint:-60, //Customize x offset of tooltip;
	offsetypoint:20, //Customize y offset of tooltip;
	
	
	init:function(){
		(document.getElementById && !document.all) ? ns6 = true : ie = true;
		document.body.onmousemove=tip.postip;
	},
	
	avoid:function(selectElem){		
		var t=tip;		
		t.sth = $(selectElem);
		if (t.sth) {
			var arr = t.findPos(t.sth);
			t.sthX = arr[0];
			t.sthY = arr[1];
		}
	},
	
	
	
	postip:function(e, center){	
		if (tip.enabletip1){
			var t=tip, pos = t.getPos(e, center);
			t.tipobj.style.left = pos.x + "px";
			t.tipobj.style.top = pos.y + "px";	
			if (ie && t.selectIsNear && t.sth && pos.x < t.sthX && pos.x + obj.offsetWidth > t.sthX && pos.y < t.sthY && pos.y + obj.offsetHeight > t.sthY ) t.sth.style.visibility="hidden";	
		}
	},
	
	
	getPos:function(e, center, pos_obj){
		
		var t=tip, tb=(document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body, obj=pos_obj ? pos_obj : t.tipobj, x, y;
			
			
			if (!center) {
				if (t.position === 'center') {
					var w = window.innerWidth ? window.innerWidth : tb.clientWidth;
					x = (ie ? tb.scrollLeft : window.pageXOffset) + (w - obj.offsetWidth)/2;
				}
				
				
				else if (t.position === 'event') {
					
					var curX = (ns6) ? e.pageX : event.clientX + tb.scrollLeft;
					var rightedge = ie && !window.opera ? tb.clientWidth-event.clientX-t.offsetxpoint : window.innerWidth-e.clientX-t.offsetxpoint-20;
					var leftedge = (t.offsetxpoint<0) ? t.offsetxpoint*(-1) : -1000;
					
					x = (rightedge < obj.offsetWidth) ? 
					(ie ? tb.scrollLeft + event.clientX - obj.offsetWidth : window.pageXOffset + e.clientX-obj.offsetWidth) : 
					((curX<leftedge) ? "5" : curX + t.offsetxpoint);
				}
				
				var curY = (ns6) ? e.pageY : event.clientY + tb.scrollTop;
				var bottomedge = ie && !window.opera ? tb.clientHeight-event.clientY-t.offsetypoint : window.innerHeight-e.clientY-t.offsetypoint-20;
				var topedge = ie && !window.opera ? event.clientY-t.offsetypoint : e.clientY-t.offsetypoint-20;
					
				y = (bottomedge < obj.offsetHeight && topedge > obj.offsetHeight) ? 
				(ie ? tb.scrollTop + event.clientY - obj.offsetHeight - t.offsetypoint : window.pageYOffset + e.clientY - obj.offsetHeight - t.offsetypoint) : 
				curY + t.offsetypoint;
			}
			
			
			else {
				var w = window.innerWidth ? window.innerWidth : tb.clientWidth;
				var h = window.innerHeight ? window.innerHeight : tb.clientHeight;
				x = (ie ? tb.scrollLeft : window.pageXOffset) + (w - obj.offsetWidth)/2;
				y = (ie ? tb.scrollTop : window.pageYOffset) + (h - obj.offsetHeight)/2;
				y = y < 0 ? 0 : y;
			}
			
		
			return {x:Math.round(x), y:Math.round(y)};
	},
	
	ietruebody:function(){
		return (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body;
	},
	
	startTip:function(txt) {		
		var t=this;		
		if (t.enabletipstat) return;
		if (!t.tipobj) t.makeTipObj();
		t.enabletip1 = true;
		t.tipobj.style.visibility="visible";
		t.tipcontent.innerHTML = txt;
	},
	
	stopTip:function() {
		var t=this;
		if (t.enabletipstat) return;
		t.enabletip1 = false;
		t.tipcontent.innerHTML = ' ';
		t.tipobj.style.visibility="hidden";
		if (t.tipiframe) document.body.removeChild(t.tipiframe);
		if (t.sth) t.sth.style.visibility="visible";	
	},
	
	startStatTip:function(txt, e){
		var t=this, center = e && e != 'center' ? false : true;
		if (txt) {
			t.startTip (txt);
			t.postip(e, center);
		}
		t.enabletip1 = false;
		t.enabletipstat = true;
	},
	stopStatTip:function(e){
		var t=this;
		if (!t.enabletipstat) return;
		if ((ie && event.button != 1) || (ns6 && e.button != 0)) return;
		t.enabletip1 = false;
		t.enabletipstat = false;
		if (t.onStopStatTip) t.onStopStatTip();
		setTimeout('tip.stopTip()', 150);
	},
	
	
	findPos:function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} 
			while (obj = obj.offsetParent);
		}
		return [curleft,curtop];
	},
	
	makeTipObj:function() {
		var t=this;
		if (!t.tipobj) {			
			var tip = $('tip');
			if (tip) {
				t.tipobj = tip;
			}
			else {				
				t.tipobj = document.createElement('div');
				t.tipobj.setAttribute("id", "tip");
				t.tipobj.style.visibility = 'hidden';
				t.tipobj.style.position = 'absolute';
				t.tipobj.style.zIndex = 100000;	
				
				for (var p in t.properties){
					if (t.properties[p] !== null) t.tipobj.style[p] = t.properties[p];
				}
				
				t.tipobj.innerHTML=t.showCloseBt ? 
					'<div style="text-align:right;">' + 
					'<img src="/content/images/close.png" style="cursor:pointer;" onmousedown="stopStatTip(event)"></div><div id="tipcontent" style="height:100%;"></div>' : 
					'<div id="tipcontent" style="height:100%;"></div>';				
				document.body.appendChild(t.tipobj);
				t.tipcontent = $('tipcontent');
			}
		}
	},
	
	
	//......popup
	
	popup_parent:null,

	showPopUp:function(id){
		var obj = $(id), p=$('popup'), b, cont;
		if (!p)  tip.createPopUp();
		
		p=$('popup');
		b=$('popup_b');
		cont = $('popupcontent');
		
		tip.popup_parent = obj.parentNode;
		cont.appendChild(tip.popup_parent.removeChild(obj));
		
		tip.centerPopUp();
		
		b.style.visibility="visible";
		p.style.visibility="visible";
	},
	
	centerPopUp:function(){
		var p=$('popup'), pos = tip.getPos(false, 'center', p);
		pos = tip.getPos(false, 'center', p);
		p.style.left = pos.x + "px";
		p.style.top = pos.y + "px";	
	},
	
	hidePopUp:function(){
		$('popup').style.visibility="hidden";
		$('popup_b').style.visibility="hidden";
		tip.popup_parent.appendChild($('popupcontent').childNodes[0]);
	},

	createPopUp:function(){
		
		var t=tip, p, b, pr;
		var properties_back = {visibility:'hidden', position:'absolute', top:0, left:0,
						background:'#000', width:'100%', height:'100%', 
						filter:'alpha(opacity=60)', 
						'-moz-opacity': '0.6', 
						'-khtml-opacity': '0.6', 
						opacity: '0.6'   
						};
		var properties = {visibility:'hidden', position:'absolute', top:0, left:0, 
							background:t.properties.backgroundColor, 
							border:t.properties.border, 
							padding:t.properties.padding};
		
			b = document.createElement('div');
			b.setAttribute("id", "popup_b");
					
			for (pr in properties_back){
				if (properties_back[pr] !== null) b.style[pr] = properties_back[pr];
			}	
				
			document.body.appendChild(b);
			
			p = document.createElement('div');
			
			p.setAttribute("id", "popup");
			for (pr in properties){
				if (properties[pr] !== null) p.style[pr] = properties[pr];
			}
					
			p.innerHTML = '<div style="text-align:right;">' +
						'<img src="/content/images/close.png" style="cursor:pointer;" onmousedown="tip.hidePopUp()"></div>' +
						'<div id="popupcontent" style="height:100%; padding:10px;"></div>';				
			document.body.appendChild(p);
			
	}
	
}



function startTip(txt) {
	tip.startTip(txt);
}
function stopTip() {
	tip.stopTip();
}
function startStatTip(){
	tip.startStatTip();
}
function stopStatTip(e){
	tip.stopStatTip(e);
}

addLoadEvent(tip.init);
