/*--------------------------------------------\\
||  D23-Topic Preview Hover                   ||
||  Written By:  Dean                         ||
||  Website:  http://www.dscripting.com       ||
||  Copyright (c) 2007 - All Rights Reserved  ||
\\--------------------------------------------*/

function d23_tph()
{
	this.offset_x = 10;
	this.offset_y = 20;
	this.cutoff   = 300;
	this.pre_show = false;
	this.show     = false;
	this.timer1   = null;
	this.timer2   = null;
	this.obj      = null;
	this.data     = null;
	this.more     = null;
	this.title    = null;
	this.cur_x    = null;
	this.cur_y    = null;
}

d23_tph.prototype.initiate = function()
{
	d23_tph.obj   = my_getbyid('d23_tph_window');
	d23_tph.data  = my_getbyid('d23_tph_window_data');
	d23_tph.more  = my_getbyid('d23_tph_window_readmore');
	d23_tph.title = my_getbyid('d23_tph_window_title');
}

d23_tph.prototype.set_cutoff = function(l)
{
	this.cutoff = (parseInt(l) > 0) ? parseInt(l) : 300;
}

d23_tph.prototype.display = function(e)
{
	if (!d23_tph.obj || !d23_tph.data)
	{
		return false;
	}

	var id = (this.tid) ? this.tid : this.id.replace(new RegExp("[^\d]", 'ig'), '');
	var d  = my_getbyid('d23_tph_preview_'+id);

	if (!d)
	{
		return false;
	}

	if (d.innerHTML.replace(new RegExp("(\n|\r|&nbsp;|&amp;nbsp;|\s)", 'ig'), '').length <= 0)
	{
		return false;
	}

	d23_tph.show                = true;
	d23_tph.data.innerHTML      = d.innerHTML.replace(new RegExp("(\n|\r)", 'ig'), "<br />");
	d23_tph.more.style.display  = 'none';
	d23_tph.obj.style.display   = 'block';
	d23_tph.data.style.overflow = 'hidden';
	d23_tph.obj.style.width     = 'auto';
	d23_tph.obj.style.height    = 'auto';
	d23_tph.data.style.height   = 'auto';

	var l = d.innerHTML.length;
	if (l > d23_tph.cutoff || d23_tph.obj.offsetWidth > d23_tph.cutoff)
	{
		d23_tph.obj.style.width = '300px';
	}

	if (d23_tph.data.offsetHeight > d23_tph.cutoff)
	{
		d23_tph.more.style.display  = 'block';
		d23_tph.obj.style.height    = '300px';
		d23_tph.data.style.height   = parseInt(d23_tph.obj.style.height)-parseInt(d23_tph.title.offsetHeight)-parseInt(d23_tph.more.offsetHeight)-10+'px';
	}

	d23_tph.event_add(window, 'mousemove', d23_tph.position);
	setTimeout(''+d23_tph.position(e), 10);

	this._title = this.title;
	this.title  = null;
}

d23_tph.prototype.position = function(e)
{
	if (!d23_tph.obj || !d23_tph.data)
	{
		return false;
	}

	if (!d23_tph.show)
	{
		return;
	}

	if (!e)
	{
		e = window.event;
	}

	d23_tph.obj.style.position = 'absolute';
	d23_tph.obj.style.zIndex   = 2000;

	d23_tph.cur_x = e.clientX;
	d23_tph.cur_y = e.clientY;

	var w = self.innerWidth  || (document.documentElement && document.documentElement.clientWidth)  || document.body.clientWidth;
	var h = self.innerHeight || (document.documentElement && document.documentElement.clientHeight) || document.body.clientHeight;
	var x = self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft)   || document.body.scrollLeft;
	var y = self.pageYOffset || (document.documentElement && document.documentElement.scrollTop)    || document.body.scrollTop;
	var r = w-d23_tph.cur_x-d23_tph.offset_x;
	var b = h-d23_tph.cur_y-d23_tph.offset_y;
	var l = (d23_tph.offset_x < 0) ? d23_tph.offset_x*(-1) : -1000;

	if (r < d23_tph.obj.offsetWidth)
	{
		d23_tph.obj.style.left = x+d23_tph.cur_x-d23_tph.obj.offsetWidth+'px';
	}
	else if (d23_tph.cur_x < l)
	{
		d23_tph.obj.style.left = '5px';
	}
	else
	{
		d23_tph.obj.style.left = x+d23_tph.cur_x+d23_tph.offset_x+'px';
	}

	if (b < d23_tph.obj.offsetHeight)
	{
		d23_tph.obj.style.top = y+d23_tph.cur_y-d23_tph.obj.offsetHeight-d23_tph.offset_y+'px';
	}
	else
	{
		d23_tph.obj.style.top = y+d23_tph.cur_y+d23_tph.offset_y+'px';
	}
}

d23_tph.prototype.vanish = function(e)
{
	if (!d23_tph.obj || !d23_tph.data)
	{
		return;
	}

	if (this._title)
	{
		this.title  = this._title;
		this._title = null;
	}

	d23_tph.show = false;
	d23_tph.event_remove(window, 'mousemove', d23_tph.position);
	setTimeout("d23_tph.obj.style.display = 'none'", 10);
}

d23_tph.prototype.event_add = function(o, t, f, c)
{
	if (typeof(c) == 'undefined' || c == null && c !== true)
	{
		c = false;
	}

	if (o.addEventListener)
	{
		o.addEventListener(t, f, c);
		return true;
	}
	else if (o.attachEvent)
	{
		o.attachEvent('on'+t, f);
		return true;
	}

	return false;
}

d23_tph.prototype.event_remove = function(o, t, f, c)
{
	if (typeof(c) == 'undefined' || c == null && c !== true)
	{
		c = false;
	}

	if (o.removeEventListener)
	{
		o.removeEventListener(t, f, c);
		return true;
	}
	else if (o.detachEvent)
	{
		o.detachEvent('on'+t, f);
		return true;
	}

	return false;
}

d23_tph = new d23_tph();
d23_tph.initiate();

//----------------------------------
// Taken from ipb_forum.js
//----------------------------------

forum_init_topic_links = new Function();
forum_init_topic_links = function()
{
	//----------------------------------
	// Get page links...
	// Gah.. another safari bug
	//----------------------------------

	var pagelinks = document.getElementsByTagName('a');

	//----------------------------------
	// Sort through and grab topic links
	//----------------------------------

	for ( var i = 0 ; i <= pagelinks.length ; i++ )
	{
		try
		{
			if ( ! pagelinks[i].id )
			{
				continue;
			}
		}
		catch(e)
		{
			continue;
		}

		var linkid   = pagelinks[i].id;
		var linkname = linkid.replace( /^(.*)-(\d+)$/, "$1" );

		if ( linkname == 'tid-link' )
		{
			pagelinks[i].tid         = _get_tid_from_id(linkid);
			pagelinks[i].onmouseover = d23_tph.display;
			pagelinks[i].onmouseout  = d23_tph.vanish;

			if (perm_can_edit)
			{
				pagelinks[i].onmouseup   = topic_link_event_mouseup;
			}

			if (topic_links_init)
			{
				d23_tph.event_remove(pagelinks[i], 'mousedown', d23_tph.vanish);
				if (perm_can_edit)
				{
					d23_tph.event_remove(pagelinks[i], 'mousedown', topic_link_event_mousedown);
				}
			}

			d23_tph.event_add(pagelinks[i], 'mousedown', d23_tph.vanish);
			if (perm_can_edit)
			{
				d23_tph.event_add(pagelinks[i], 'mousedown', topic_link_event_mousedown);
			}

			if (!topic_links_init)
			{
				pagelinks[i].title = pagelinks[i].title + '. ' + lang_clickhold;
			}
		}
	}
	
	topic_links_init = 1;
}
