function image_size_containment(maxheight, maxwidth)
{
	this.height = maxheight;
	this.width = maxwidth;
	this.loops = new Array();

	this.create_loop = function(img)
	{
		id = this.loops.length - 1;
		this.loops[id] = img;

		setTimeout("ISC.trigger_loop(" + id + ");", 20);
	}

	this.trigger_loop = function(id)
	{
		this.check(this.loops[id]);
	}

	this.get_dimensions = function(img)
	{
		return {'height' : img.offsetHeight || img.height, 'width' : img.offsetWidth || img.width};
	}

	this.check = function(img)
	{
		dimensions = this.get_dimensions(img);

		if (dimensions['height'] > this.height || dimensions['width'] > this.width)
		{
			this.reduce(img, dimensions);
		}
		else if (dimensions['height'] == 0 && dimensions['width'] == 0)
		{
			this.create_loop(img);
		}
	}

	this.percent = function(percent, value)
	{
		return Math.floor((value / 100) * (100 - percent));
	}

	this.reduce = function(img, dimensions)
	{
		reduce = 0;
		newheight = dimensions['height'];
		newwidth = dimensions['width'];

		while (newheight > this.height || newwidth > this.width)
		{
			newheight = this.percent(reduce++, dimensions['height']);
			newwidth = this.percent(reduce, dimensions['width']);
		}

		img.style.height = newheight + 'px';
		img.style.width = newwidth + 'px';

		img.style.cursor = 'pointer';
		img.onclick = function(){ ISC.openwin(img.src, dimensions['height'] + 50, dimensions['width'] + 50); };
	}

	this.openwin = function(src, height, width)
	{
		window.open(src, 'ffimage', 'scrollbars=no,resizable=no,location=no,toolbar=no,status=no,menubar=no,copyhistory=no,height=' + height + ',width=' + width);
	}
}


ISC = new image_size_containment(image_max_dimension, image_max_dimension);


_vBulletin_Clock = function()
{
this.format = '';
this.offset = 0;
this.xml_sender = null;
this.one_off = 0;
this.zoneam = false;

this.init_counter = function()
{
this.fetch_times = setInterval("vBulletin_Clock.count()", 10000);
}

this.get_hour = function(istf)
{
hours = new Date().getUTCHours() + this.offset + this.one_off;

if (hours < 0)
{
hours += (typeof istf != 'undefined' && istf === true) ? 12 : 24;
}

if (hours > 24)
{
hours -= 24;
}

if (typeof istf != 'undefined' && istf == 'zone')
{
return Math.floor(hours);
}

if (Math.floor(hours) == 24)
{
hours = 0;
}

return Math.floor(hours);
}

this.get_12hour = function()
{
hour = this.get_hour(true);

if (hour > 12)
{
hour -= 12;
}

if (hour == 0)
{
hour = 12;
this.zoneam = true;
}

return hour;
}

this.get_minutes = function()
{
mins = new Date().getUTCMinutes() + (60 * (this.offset - Math.floor(this.offset)));

while (mins > 60)
{
mins -= 60;
this.one_off += 1;
}

if (mins < 10)
{
mins = '0' + mins;
}

return mins;
}

this.get_zone = function(hour, twlvehr)
{
if (this.zoneam)
{
return 'am';
}

return hour > 11 ? 'pm' : 'am';
}

this.count = function()
{
this.one_off = 0;
this.parse = {
'i': this.get_minutes(),
'G': this.get_hour(),
'g': this.get_12hour(),
'a': this.get_zone(this.get_hour('zone')),
'A': this.get_zone(this.get_hour('zone'))
};

timestring = this.format;

for (str in this.parse)
{
timestring = timestring.replace(str, this.parse[str]);
}

vBulletin_Clock.update_times(timestring);
}

this.update_times = function(newtime)
{return false;
span = document.getElementsByTagName('span');
updated = false;

for (s = 0; s < span.length; s++)
{
if (span[s].id == 'ff_nowtime_clock')
{
updated = true;

try
{
span[s].removeChild(span[s].childNodes[0]);
}
catch(e)
{
span[s].innerHTML = '';
}

span[s].appendChild(document.createTextNode(newtime));
}
}

if (!updated)
{
clearInterval(this.fetch_times);
}
}
}

_Market_Session_Tool = function()
{
this.xml_sender = null;
this.market_status_log = {};
this.updates = 0;
this.refresh_rate = 180;

this.init_update = function(real)
{
if (typeof real == 'undefined')
{
this.refresh_rate = (this.refresh_rate * 1000);
setTimeout("Market_Session_Tool.init_update('real')", this.refresh_rate);
return false;
}

if (fetch_object('session_market_tool'))
{
Market_Session_Tool.update();
grabsession = setInterval("Market_Session_Tool.update()", this.refresh_rate);
}
}

this.update = function()
{
if (this.updates >= 30)
{
clearInterval(grabsession);

fetch_object('session_market_tool').getElementsByTagName('td')[1].style.height = '100px';
fetch_object('session_market_tool').getElementsByTagName('td')[1].align = 'left';
fetch_object('session_market_tool').getElementsByTagName('td')[1].innerHTML = '<span class="smallfont" style="padding-left: 16px;">Connection Expired<br /><br /></span><span class="smallfont" style="padding-left: 48px;"><a href="#" onclick="window.location.reload();">Refresh</a></span>';
return false;
}

this.updates++;
this.xml_sender = new vB_AJAX_Handler(true);
this.xml_sender.onreadystatechange(Market_Session_Tool.onreadystatechange);
this.xml_sender.send('marketsession.php', '');
}

this.onreadystatechange = function()
{
if (Market_Session_Tool.xml_sender.handler.readyState == 4 && Market_Session_Tool.xml_sender.handler.status == 200)
{
if (Market_Session_Tool.xml_sender.handler.responseText)
{

if (Market_Session_Tool.xml_sender.handler.responseText == 'offline')
{
Market_Session_Tool.updates = 30;
Market_Session_Tool.update();
return false;
}

Market_Session_Tool.setup_image_loader(Market_Session_Tool.xml_sender.handler.responseText);

fetch_object('session_market_tool').innerHTML = Market_Session_Tool.xml_sender.handler.responseText;
Market_Session_Tool.update_status();
vBulletin_Clock.count();
}
}
}

this.setup_image_loader = function(response)
{
tmpobj = document.createElement('div');
tmpobj.id = 'mimgtmp';
tmpobj.style.display = 'none';
tmpobj.innerHTML = response;

document.body.appendChild(tmpobj);

tmpobj = fetch_object('mimgtmp');

this.preload = new Array();
this.preloaded = 0;
this.toload = tmpobj.getElementsByTagName('img').length;
this.toload++;

for (i = 0; i < tmpobj.getElementsByTagName('img').length; i++)
{
this.preload[i] = new Image();
this.preload[i].onload = Market_Session_Tool.image_loaded;
this.preload[i].src = tmpobj.getElementsByTagName('img')[i].src;
}

this.preload[i] = new Image();
this.preload[i].onload = Market_Session_Tool.image_loaded;
this.preload[i].src = 'images/gradients/gradient_tcat.gif';

tmpobj.parentNode.removeChild(tmpobj);
}

this.image_loaded = function()
{
Market_Session_Tool.preloaded++;

if (Market_Session_Tool.preloaded >= Market_Session_Tool.toload)
{
fetch_object('session_market_tool').innerHTML = Market_Session_Tool.xml_sender.handler.responseText;
Market_Session_Tool.update_status();
}
}

this.update_status = function()
{
for (id in this.market_status_log)
{
if (this.market_status_log[id])
{
fetch_object('market_' + id).style.display = '';
}
}
}

this.market_status = function(mid, value)
{
this.market_status_log[mid] = value;

if (value)
{
this.xml_sender = new vB_AJAX_Handler(true);
this.xml_sender.working = true;
this.xml_sender.mid = mid;
this.xml_sender.onreadystatechange(Market_Session_Tool.got_details);
this.xml_sender.send('marketsession.php', 'id=' + mid + ((typeof _ms_vars != 'undefined') ? ('&' + _ms_vars) : ''));
}
}

this.got_details = function()
{
if (Market_Session_Tool.xml_sender.handler.readyState == 4 && Market_Session_Tool.xml_sender.handler.status == 200)
{
if (Market_Session_Tool.xml_sender.handler.responseText)
{
if (Market_Session_Tool.xml_sender.handler.responseText == 'offline')
{
this.toggle_market(Market_Session_Tool.xml_sender.mid);
return false;
}

fetch_object('market_' + Market_Session_Tool.xml_sender.mid + '_details').innerHTML = Market_Session_Tool.xml_sender.handler.responseText;

Market_Session_Tool.xml_sender.working = false;
}
}
}

this.toggle_market = function(mid)
{
if (fetch_object('marketsession_' + mid).style.display == '')
{
fetch_object('marketsession_' + mid).style.display = 'none';
this.market_status(mid, false);
}
else
{
if (this.xml_sender && this.xml_sender.working)
{
setTimeout("Market_Session_Tool.toggle_market('"+mid+"')", 200);
return false
}
fetch_object('marketsession_' + mid).style.display = '';
this.market_status(mid, true);
}
}
}

_Calendar_History = function(id, type, time, uid)
{
	this.id = id;
	this.limit = 3;
	this.type = type;
	this.loading = false;
	this.time = time;
	this.uid = uid;

	var me = this;

	this.fetch_more = function()
	{
		if (this.loading)
		{
			return false;
		}

		this.loading = true;
		this.limit += 3;
		this.alttitle = this.type.charAt(0).toUpperCase() + this.type.substring(1, this.type.length);

		this.set_loader('', 'Loading...');

		this.xml_sender = new vB_AJAX_Handler(true);
		this.xml_sender.onreadystatechange(this.fetch_more_done);
		this.xml_sender.send('ebase.php?do=ecobasehistory&id=' + this.id + '&limit=' + this.limit + '&type=' + this.type + '&time=' + time);
	}

	this.set_loader = function(title, html)
	{
		fetch_object('calendar_history_loader_' + me.type + '_' + me.id + '_' + me.uid).innerHTML = html;
		fetch_object('calendar_history_loader_' + me.type + '_' + me.id + '_' + me.uid).title = title;
	}

	this.fetch_more_done = function()
	{
		if (me.xml_sender.handler.readyState == 4 && me.xml_sender.handler.status == 200)
		{
			data = me.xml_sender.handler.responseText;
			fetch_object('calendar_history_' + me.type + '_' + me.id + '_' + me.uid).innerHTML = data;
			me.loading = false;
			me.set_loader('Show More ' + me.alttitle, '<img src="' + IMGDIR_MISC + '/morehistory.png" alt="Show More ' + me.alttitle + '" title="Show More ' + me.alttitle + '" border="0" style="vertical-align: top;" /> <span style="text-decoration: underline;">More</span>');

			rows = fetch_object('calendar_history_' + me.type + '_' + me.id + '_' + me.uid).getElementsByTagName('table')[0].rows.length - 1;

			if (rows < me.limit)
			{
				me.set_loader('', '');
			}
		}
	}
}

var cal_historys = new Array();

function do_cal_history(id, type, time, uid)
{
	if (typeof time == 'undefined')
	{
		time = 0;
	}

	if (typeof uid == 'undefined')
	{
		uid = 0;
	}

	if (!cal_historys[id + type + time + uid])
	{
		cal_historys[id + type + time + uid] = new _Calendar_History(id, type, time, uid);
	}

	cal_historys[id + type + time + uid].fetch_more();

	return false;
}