// Cross-browser addEventListener()/attachEvent() replacement.

function addEvent(elt, name, handler, atEnd) {
  name = name.replace(/^(on)?/, 'on'); 
  var prev = elt[name];
  var tmp = '__tmp';
  elt[name] = function(e) {
    if (!e) e = window.event;
    var result;
    if (!atEnd) {
      elt[tmp] = handler; result = elt[tmp](e); elt[tmp] = null; // delete() does not work in IE 5.0 (???!!!)
      if (result === false) return result;
    }
    if (prev) {
      elt[tmp] = prev; result = elt[tmp](e); elt[tmp] = null;
    }
    if (atEnd && result !== false) {
      elt[tmp] = handler; result = elt[tmp](e); elt[tmp] = null;
    }
    return result;
  }
  return handler;
}



function GeneralPreview() { this.construct() }
GeneralPreview.prototype = {
	TIP_TIME:          500,
	OUT_TIME:          100,
	divTip:            null,

	baseUrl:           
		'',
	html:             
		'<div style="position:absolute; background:#E6F2FB; border:1px solid #000000; width:350px; font-weight:normal; overflow:hidden">'+
		'<div style="background:black; color: white;"><div style="border-bottom:1px solid silver"><b>Please wait...</b></div></div>' +
		'<div style="padding-left:10px; padding-right:10px; color: black; background: #E6F2FB;"><b>Loading data.</b></div>' +
		'</div>',
	form: 
		'<input type="text" style="margin:0px; padding:0px 40px 0px 4px; width:100%; border-width:0px; border-bottom: 1px solid silver" />',
	navigation:
		'',

	construct: function() {
		this.nocache = {};
	},
	
	scheduleShowTip: function(elt, event) {
		var th = this;
		if (th.timeoutTip) th.timeoutTip=clearTimeout(th.timeoutTip);
		if (th.timeoutMouseOut) th.timeoutMouseOut=clearTimeout(th.timeoutMouseOut);
		if (th.divTip && th.divTip.elt == elt) return; // already shown
		th.timeoutTip = setTimeout(function() { th.timeoutTip=null; th.showTip(elt, event) }, th.TIP_TIME);
	},

	scheduleHideTip: function() {
		var th = this;
		if (th.timeoutTip) th.timeoutTip=clearTimeout(th.timeoutTip);
		if (th.timeoutMouseOut) th.timeoutMouseOut=clearTimeout(th.timeoutMouseOut);
		th.timeoutMouseOut = setTimeout(function() { th.timeoutMouseOut=null; th.hideTip() }, th.OUT_TIME);
	},

	showTip: function(elt, event) {
		var th = this;
		th.locked = false;
		th.hideTip();
		// Create tip.
		var span = document.createElement("div"); span.innerHTML = th.html;
		th.divTip = span.childNodes[0];
		th.divTip.elt = elt;
		// Position tip.
		document.body.appendChild(th.divTip);
		var coord = th.getAbsPos(elt);
		var width = th.divTip.offsetWidth;
		var docWidth = document.body.scrollWidth;
		if (coord.x + width > docWidth) coord.x = docWidth - width - 10;
		if (coord.x < 0) coord.x = 0;
		//th.divTip.style.left = coord.x + "px";
		th.divTip.style.left = (coord.x + 10) + "px";
		th.divTip.style.top = (coord.y + elt.offsetHeight + 1) + "px";
		// Enents for tip.
		addEvent(th.divTip, 'onmouseover', function(e) {
			th.scheduleShowTip(elt);
			return true;
		})
		addEvent(th.divTip, 'onmouseout', function() {
			if (th.inConfirm) return;
			th.scheduleHideTip();
			return true;
		})
		
		// Load topic data.
		th.loadPost(elt.postInfo);
	},
	
	hideTip: function() {
		var th = this;
		if (th.locked) return;
		if (!th.divTip) return;
		if (th.divTip.inp) th.divTip.elt.editValue = th.divTip.inp.value;
		th.divTip.parentNode.removeChild(th.divTip); 
		th.divTip = null;
	},

	loadPost: function(info) {
		var th = this;
		var req = new Subsys_JsHttpRequest_Js();
		req.onreadystatechange = function() {
			//window.console && window.console.out(req.responseText, '', 'Shell');
			if (req.readyState != 4) return;
			if (!th.divTip) return;
			th.fillTip(req.responseJS);
		}
		req.caching = true;
		req.SID = window.SID;
		req.open('GET', this.baseUrl+'/ajax.general.php', true);
		req.send({ 'url': info });
	},

	// Called when data is ready to fill the tip.
	fillTip: function(responseJS) {
		
		var th = this;
		var elt = th.divTip.elt;

		var talentID = responseJS.talentID;
		var title = responseJS.title;
		var data    = responseJS.text;
		
		// Dequote title.
//		var tmp = document.createElement("span");
//		tmp.innerHTML = data;
//		data = tmp.innerText;

		// Fill the tip.
		var divSubj = th.divTip.childNodes[0];
		var divText = th.divTip.childNodes[1];

		divSubj.childNodes[0].innerHTML = "<span align=center><b>"+ title + "</b></span>"
		divText.innerHTML = 
			'' + data;
	},

	getAbsPos: function(p) {
		var s = { x:0, y:0 };
		while (p.offsetParent) {
			s.x += p.offsetLeft;
			s.y += p.offsetTop;
			p = p.offsetParent;
		}
		return s;
	}
}