/*======================================================================*\
|| #################################################################### ||
|| # vBulletin Custom File - Licence Number L4648f56
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2000-2006 Jelsoft Enterprises Ltd. All Rights Reserved. ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/

_vBulletin_Clock = function()
{
	this.format = ''; // format of time (G = 24 hour, i = minute, g = 12 hour, a = am/pm)
	this.offset = 0;  // offset from GMT
	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()
	{
		// 60 updates = 60 minutes = 1 hour
		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.mid = mid;
			this.xml_sender.onreadystatechange(Market_Session_Tool.got_details);
			this.xml_sender.send('marketsession.php', 'id=' + mid);
		}
	}

	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;
			}
		}
	}

	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
		{
			fetch_object('marketsession_' + mid).style.display = '';
			this.market_status(mid, true);
		}
	}
}

/*======================================================================*\
|| ####################################################################
|| # SVN: $RCSfile$ - $Rev$
|| ####################################################################
\*======================================================================*/
