// ==UserScript==
// @name          Titan TV Ajax Details
// @namespace     http://arantius.com/misc/greasemonkey/
// @description	  Show the descriptions for shows on Titan TV's grid without clicking.
// @include       http://*.titantv.com/*
// ==/UserScript==

function $x(p, context) {
	if (!context) context=document;
	var arr=[];
	var xpr=document.evaluate(
		p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null
	);
	for(i=0;item=xpr.snapshotItem(i);i++){ arr.push(item); }
	return arr;
}

const HOVER_TIME=1000;
var timer=null, detailEl=null, showEl;

function createDetailEl(event) {
	detailEl=document.createElement('div');
	detailEl.setAttribute('class', 'greased_tooltip');
	document.body.appendChild(detailEl);
	return detailEl;
}

function isin(el1, el2) {
	while (el1) {
		if (el1==el2) return true;
		el1=el1.parentNode;
	}
	return false;
}

function mousemove(event) {
	if (event.ctrlKey) return;
	if (!detailEl) return;

	removeDetail(event);

	var x=event.clientX+10;
	if (x+410>window.innerWidth) x=x-420;
	
	var y=event.clientY+10;
	if (y+310>window.innerHeight) y=y-320;
	if (y<10) y=10;

	detailEl.style.left=(x+window.scrollX)+'px';
	detailEl.style.top=Math.max(y+window.scrollY, 10)+'px';

	clearTimeout(timer);
	timer=setTimeout(showDetail, HOVER_TIME, event);
}

function removeDetail(event) {
	var el=event.target;
	while (el && el!=showEl) {
		el=el.parentNode;
		if (el==detailEl) return;
	}
	
	if (!el) {
		detailEl.style.display='none';
		showEl=null;
	}
}

function showDetail(event) {
	var el=event.target;

	while (el) {
		try {
			if ('TD'==el.tagName &&
				el.getAttribute('oldclick') &&
				el.getAttribute('oldclick').match('return Dtl')
			) {
				//console.log('picked as show cell', el);
				break;
			}
		} catch (e) { console.error(e) }
		el=el.parentNode;
	}
	if (!el) return;

	if (showEl==el) return;

	showEl=el;
	detailEl.innerHTML='';
	detailEl.style.display='block';

	var info=el.getAttribute('oldclick').toString().split(',');
	// remove the (useless) first and last piece
	info.pop();
	info.shift();
	// trim spaces off all that are left
	for (i in info) {
		info[i]=info[i].replace(/^\s*/, '').replace(/\s*$/, '');
	}

	var sd=unsafeWindow.sdata['R'+info[0]];
	if (!sd) {
		console.error("Couldn't find station data.");
		return;
	}
	sd=sd.replace(/~~+/, '~').split('~');

	var url="http://ww1.titantv.com/details/index.aspx"
		+"?pvrWatch="+(info[3].replace(/"(.*)"/, '$1'))
		+"&viewerMode=false"
		+"&callsign="+sd[0]
		+"&stationId="+sd[3]
		+"&psipId="+sd[4]
		+"&scheduleId="+info[1]
		+"&channel="+sd[1]
		+"&contentType="+sd[2];

	//console.info('start titan tv ajax to', url);
	GM_xmlhttpRequest({
		method:'GET',
		url:url,
		onload:function(xhr) {
			var html=xhr.responseText;
			html=html.substring(html.indexOf('contentchromebox')-12);
			html=html.substring(0, html.indexOf('</table></div>')+14);
			detailEl.innerHTML=html;
			//console.info('titan tv ajax ...', html);
		}
	});
}

// add styles to the page
GM_addStyle(
	// from /themes/_default/styles/details.css
	'.contentchromebox { position:relative; left:0px; top:0px; width:377px; height:285px; background-color:#F1E8D1; }'+
	'.contentchrometxt { position:relative; width:370px; height:275px; overflow:auto; }'+
	'.chrometxttl { width:5px; height:5px; }'+
	'.chrometxtt  { height:5px; }'+
	'.chrometxttr { width:5px; height:5px; }'+
	'.chrometxtl  { width:5px; }'+
	'.chrometxtc  { width:330px; height:275px; }'+
	'.chrometxtr  { width:5px; }'+
	'.chrometxtbl { width:5px; height:5px; }'+
	'.chrometxtb  { height:5px; }'+
	'.chrometxtbr { width:5px; height:5px; }'+
	'.detailsSection { border-top:solid 1px black; padding-top:4px; padding-bottom:2px; }'+
	'.detailsTitle { font-size:14px; font-weight:bold;}'+
	'.detailsSmallTitle { font-size:12px;}'+
	'.detailsSectionTitle { font-size:12px; font-style:italic; }'+
	// my stuff
	'.greased_tooltip { position: absolute; display: none; overflow: auto; width: 380px; height: 288px; border: 2px inset black; background-color: #F1E8D1; font-size: 10pt; }'+
	// hide annoying moving things
	'td.gridBannerRowHeader, td.gridBannerRowCell { display: none !important; }'
);

// create the tooltip element to hold the detail display
detailEl=createDetailEl();

// register listener
window.addEventListener('mousemove', mousemove, false);

// remove "click for details" tooltips
var td=null, tds=$x('//td[@class="gC" and @onclick]');
for (var i=0; td=tds[i]; i++) {
	td.removeAttribute('title');
	td.setAttribute('oldclick', td.getAttribute('onclick'));
	td.removeAttribute('onclick');
	td.style.cursor='default';
}
