var whoisloading = 0;
var cursorOverToolTip = false;
var toolTipTimer;
var ToolTipAjaxManager = $.manageAjax.create('ToolTip', {
	queue : 'clear',
	abortOld : true
});

// helper function, don't call
function positionTooltip($this) {
	var w = $this.innerWidth(), offset = $this.offset();

	var toolTipWidth = $('#ToolTip_DIV').innerWidth();
	var toolTipHeight = $('#ToolTip_DIV').innerHeight();

	var windowHeight = $(window).height();
	var windowWidth = $(window).width();

	var scrollTop = $(window).scrollTop();
	var scrollLeft = $(window).scrollLeft();

	var visibleBorderTop = scrollTop;
	var visibleBorderBottom = scrollTop + windowHeight;

	var visibleBorderLeft = scrollLeft;
	var visibleBorderRight = scrollLeft + windowWidth;

	var offsetTop = (offset.top - toolTipHeight) - 1;
	var offsetLeft = offset.left + w + 1;

	if (offsetTop + toolTipHeight > visibleBorderBottom)
		offsetTop -= (offsetTop + toolTipHeight) - (visibleBorderBottom - 2);
	else if (offsetTop < visibleBorderTop)
		offsetTop += (visibleBorderTop - offsetTop) + 2;

	if (offsetLeft + toolTipWidth > visibleBorderRight)
		offsetLeft -= (offsetLeft + toolTipWidth) - (visibleBorderRight - 4);
	else if (offsetLeft < visibleBorderLeft)
		offsetLeft += (visibleBorderLeft - offsetLeft) + 2;

	$('#ToolTip_DIV').css('top', offsetTop);
	$('#ToolTip_DIV').css('left', offsetLeft);
}

/*
 * shows a ToolTip_DIV on an show or an episode element
 * 
 * $this = jquery object of the html element that the ToolTip_DIV is to go on
 * type = either 'show' or 'episode' id = the id of the show or episode
 * $postionAbove = the jquery object of the html element that the tooltip should
 * be place above and 'point to'
 */
function ShowToolTip($this, type, id, $postionAbove) {
	if ($postionAbove == undefined)
		$postionAbove = $this;

	var url = $('base').attr('href');
	if (url == undefined)
		url = '/';

	if (type.toLowerCase() == 'show')
		url += 'ToolTip/Show/' + id + '/';
	else if (type.toLowerCase() == 'archiveshow')
		url += 'ToolTip/ArchiveShow/' + id + '/';
	else if (type.toLowerCase() == 'archiveepisode')
		url += 'ToolTip/ArchiveEpisode/' + id + '/';
	else if (type.toLowerCase() == 'movie')
		url = '/ToolTip/Movie/' + id + '/';
	else
		url += 'ToolTip/Episode/' + id + '/';

	var this_id = $this.attr('id');

	whoisloading = this_id;
	clearTimeout(toolTipTimer);
	toolTipTimer = setTimeout(function() {
		if (!$this.data('ToolTip_DIV_text'))// &&
											// !$this.data('ToolTip_DIV_loading')){
			{
				$this.data('ToolTip_DIV_loading', 1);
				ToolTipAjaxManager.clear('tooltip', true);
				ToolTipAjaxManager.add('tooltip', {
					url : url,
					success : function(data) {
						// the data could have been fetched with the
						// jquery.xdomainajax plugin
					if (data.responseText !== undefined) {
						data = data.responseText;
					}
					$this.data('ToolTip_DIV_text', data);
					$('#ToolTip_DIV').html(data);
					positionTooltip($postionAbove);
					$('#ToolTip_DIV').fadeIn(400);
				}
				});
			} else {
				$('#ToolTip_DIV').html($this.data('ToolTip_DIV_text'));
				positionTooltip($postionAbove);
				$('#ToolTip_DIV').fadeIn(400);
			}
		}, 500);

	$this.bind('mouseleave', function() {
		clearTimeout(toolTipTimer);
		ToolTipAjaxManager.clear('tooltip', true);
		setTimeout(function() {
			if (!cursorOverToolTip) {
				hideTooltip();
			}
		}, 1);
	});
}

function hideTooltip() {
	$('#ToolTip_DIV').fadeOut(400);
	whoisloading = 0;
	// clearTimeout(toolTipTimer);
}

// Tooltip for the episode download page

function ShowEpisodeDownloadToolTip($this,title,season,number,length,airdate,desc) {
	/*if ($postionAbove == undefined)*/
		$postionAbove = $this;
	var this_id = $this.attr('id');
	if(length == "unknown")
		length="";
	if(airdate == "unknown")
		airdate="";
	if(season == "unknown")
		season="";
	if(number == "unknown")
		number="";
	if(desc == "unknown")
	desc="";
	
	var data = '<div class="info_hover_box"><div class="content"><div>'+title+'</div><div><div><span>'+season+''+number+' '+length+'</span></div><div><span>'+airdate+'</span></div></div><div class="line">&nbsp;</div><div> <span>'+desc+'</span></div></div></div>';
	
	whoisloading = this_id;
	clearTimeout(toolTipTimer);
	toolTipTimer = setTimeout(function() {
		if (!$this.data('ToolTip_DIV_text'))// &&
											// !$this.data('ToolTip_DIV_loading')){
			{
				$this.data('ToolTip_DIV_loading', 1);
				ToolTipAjaxManager.clear('tooltip', true);

				$this.data('ToolTip_DIV_text', data);
				$('#ToolTip_DIV').html(data);
				positionTooltip($postionAbove);
				$('#ToolTip_DIV').fadeIn(400);
			} else {
				$('#ToolTip_DIV').html($this.data('ToolTip_DIV_text'));
				positionTooltip($postionAbove);
				$('#ToolTip_DIV').fadeIn(400);
			}
		}, 500);

	$this.bind('mouseleave', function() {
		clearTimeout(toolTipTimer);
		ToolTipAjaxManager.clear('tooltip', true);
		setTimeout(function() {
			if (!cursorOverToolTip) {
				hideTooltip();
			}
		}, 1);
	});
}

$(function() {
	$('body').prepend('<div id="ToolTip_DIV"></div>');
	$('#ToolTip_DIV').hover(function() {
		cursorOverToolTip = true;
	}, function() {
		cursorOverToolTip = false;
		hideTooltip();
	});
});

