/**
 * Forex Factory
 * Syndication Delivery Model
 * (C) Copyright Forex Factory, All Rights Reserved
 * Code may not be copied in part or whole
 * * * * * * * * * * * * 
**/

var IMGDIR_MISC = 'http://www.forexfactory.com/images/misc/';
ok = 0;

set_tz_cookies = function()
{
	time_zone = fetch_object('sel_timezoneoffset');
	time_zone = time_zone.options[time_zone.options.selectedIndex].value;

	dst_setting = fetch_object('sel_dst');
	dst_setting = dst_setting.options[dst_setting.options.selectedIndex].value;

	tf_setting = fetch_object('sel_timeformat');
	tf_setting = tf_setting.options[tf_setting.options.selectedIndex].value;

	ffSyndication_Calendar.update_cookies(dst_setting, time_zone, tf_setting);

	return false;
}

show_timeopts = function()
{
	if (fetch_object('timeoptions').style.display == 'none')
	{
		fetch_object('timeoptions').style.display = '';
		fetch_object('ff_wscal_events').style.display = 'none';
		fetch_object('timeimg').src = fetch_object('timeimg').src.replace('clock_small', 'excal_tclose');
	}
	else
	{
		fetch_object('timeoptions').style.display = 'none';
		fetch_object('ff_wscal_events').style.display = '';
		fetch_object('timeimg').src = fetch_object('timeimg').src.replace('excal_tclose', 'clock_small');
	}

	return false;
}

load_rate_info = function(eventid, ebaseid, actual, forecast, dateline)
{
	return ffSyndication_Rate.load_rate_info(eventid, ebaseid, actual, forecast, dateline);
}

_s_MAIN = function()
{

this.display_synd = function()
{
	if (fetch_object(this.params.split('|')[2]))
	{
		try
		{
			fetch_object(this.params.split('|')[2]).innerHTML = _sds_html;
		}
		catch(e)
		{
			fetch_object(this.params.split('|')[2]).innerHTML = '';

			sInsert = document.createElement('div');
			sInsert.innerHTML = _sds_html;

			fetch_object(this.params.split('|')[2]).appendChild(sInsert);
		}
		if (this.params.split('|')[0] == 'ws_cal.php')
		{
			vBulletin.add_event("vBmenuShow")
			vBulletin.add_event("vBmenuHide");

			vbmenu_register("callegend", false, true);
			vbmenu_register("calfilters", false, true);

			if (typeof mass_filter_check != 'undefined')
			{
				mass_filter_check('currency');
				mass_filter_check('impact');
			}
		}

		var spans = fetch_tags(fetch_object(this.params.split('|')[2]), 'span');
		var trs = fetch_tags(fetch_object(this.params.split('|')[2]), 'tr');

		for (var t = 0; t < trs.length; t++)
		{
			if (trs[t].id.match(/^detail_row_(\d+)$/) && !is_ie)
			{
				trs[t].style.display = 'none';
			}
		}

		for (var s = 0; s < spans.length; s++)
		{
			if (spans[s].id.match(/^detail_load_(\d+)$/))
			{
				parts = spans[s].innerHTML.split('|');

				spans[s].innerHTML = '';
				spans[s].style.display = '';

				load_detail_button(spans[s].id, parts[0], parts[1], parts[2], parts[3]);

				if (fetch_tags(spans[s], 'a')[0] && fetch_tags(spans[s], 'a')[0].id && fetch_tags(spans[s], 'a')[0].id.match(/^detail_/))
				{
					fetch_tags(spans[s], 'a')[0].onclick = this.fetch_details;
					fetch_tags(spans[s], 'img')[0].setAttribute('detailstar', parts[3]);

					if (this.darkicons)
					{
						fetch_tags(spans[s], 'img')[0].src = fetch_tags(spans[s], 'img')[0].src.replace('.gif', '_dark.gif');
					}
				}
			}
		}
	}
}

	this.url = '';
	this.options = '';
	this.statechange = function(){ return false; };
	this.jsfiles = {
		0: 'http://yui.yahooapis.com/2.7.0/build/event/event-min.js',
		1: 'http://www.forexfactory.com/clientscript/yui/yahoo-dom-event/yahoo-dom-event.js',
		2: 'http://www.forexfactory.com/clientscript/vbulletin_global.js?v=599',
		3: 'http://www.forexfactory.com/clientscript/vbulletin_alt_global.js',
		4: 'http://www.forexfactory.com/clientscript/vbulletin_menu.js',
		5: 'http://www.forexfactory.com/clientscript/vbulletin_calendar_filters.js',
		6: 'http://www.forexfactory.com/clientscript/vbulletin_ajax_refreshdata.js'
	};
	this.extfiles = 8; // total files loaded on init, use for loader
	this.extloaded = 0;
	this.loaded = 0;
	this.loadedall = false;
	this.getdetails = false;
	this.detailfor = -1;
	this.detailimg = false;
	this.opendetails = {};
	this.loadedfooter = false;
	this.darkicons = false;
	this.synd = 'ws_cal.php';
	this.params = 'ws_cal.php|wscal_parent|_s_ff_syn|_ff_wscal_loadwrap|__ff_synd_footer__|Calendar';
	this.loaderid = '';
	this.loadtext = '';

	this.init = function(url, options, dataParams)
	{
		this.url = url;
		this.options = options;

		if (typeof dataParams != 'undefined')
		{
			this.params = dataParams;
			this.synd = dataParams.split('|')[0];
		}

		if (options.match(/dark=1/))
		{
			this.darkicons = true;
		}

		if (options.match(/&colors\[8\]=(.*[^&])&/))
		{
			tcolor = unescape(RegExp.$1.split('&')[0]);
		}
		else
		{
			tcolor = '#000000';
		}

		if (options.match(/&colors\[7\]=(.*[^&])&/))
		{
			bcolor = unescape(RegExp.$1.split('&')[0]);
		}
		else
		{
			bcolor = 'transparent';
		}

		switch (this.params.split('|')[0])
		{
			case 'ws_cal.php':
			{
				this.loadtext = 'Loading Forex Factory Calendar';
			}
			break;

			case 'ws_market.php':
			{
				this.loadtext = 'Loading Forex Factory Sessions';

				if (options.match(/&colors\[9\]=(.*[^&])&/))
				{
					tcolor = unescape(RegExp.$1.split('&')[0]);
				}
			}
			break;

			case 'ws_rates.php':
			{
				this.loadtext = 'Loading Forex Factory Rates';

				if (options.match(/&colors\[9\]=(.*[^&])&/))
				{
					tcolor = unescape(RegExp.$1.split('&')[0]);
				}
			}
			break;

			default:
			{
				this.loadtext = 'Loading ' + this.params.split('|')[0];
			}
			break;
		}

		this.loaderid = this.params.split('|')[1] + '_' + this.params.split('|')[3] + '_' + (Math.random() * 999);

		this.create_footericon();
		this.create_loader(tcolor, bcolor);
		this.load_core();

		return this;
	}

	this.create_footericon = function()
	{
		if (!document.getElementById(this.params.split('|')[4]) || this.loadedfooter)
		{
			return false;
		}

		this.loadedfooter = true;

		span = document.createElement('span');

		span.style.cssFloat = 'right';
		span.style.styleFloat = 'right';

		icon = document.createElement('img');
		icon.src = IMGDIR_MISC + 'wscal_credit.gif';
		icon.border = 0;
		icon.style.verticalAlign = 'top';
		icon.style.paddingTop = '1px';

		ilink = document.createElement('a');
		ilink.href = 'http://www.forexfactory.com/';
		ilink.target = '_blank';

		span.appendChild(ilink)
		ilink.appendChild(icon);

		document.getElementById(this.params.split('|')[4]).innerHTML = '<div style="vertical-align: middle;">' + document.getElementById(this.params.split('|')[4]).innerHTML + '</div>';

		document.getElementById(this.params.split('|')[4]).insertBefore(span, document.getElementById(this.params.split('|')[4]).getElementsByTagName('*')[0]);
		document.getElementById(this.params.split('|')[4]).style.padding = '0px';
		document.getElementById(this.params.split('|')[4]).style.paddingLeft = '15px';
		document.getElementById(this.params.split('|')[4]).style.height = '16px';
	}

	this.create_loader = function(color, bcolor)
	{
		if (!document.getElementById(this.params.split('|')[3]))
		{
			return false;
		}

		if (document.getElementById(this.params.split('|')[2]))
		{
			document.getElementById(this.params.split('|')[2]).style.background = bcolor;
		}

		var loadbar = '<div align="center" style="margin: 0; padding: 0;"><table cellpadding="0" cellspacing="0" border="0" width="154" align="center" style="margin: 0; padding: 0;">';
		loadbar += '<tr>';
		loadbar += '<td style="padding: 0; margin: 0;" align="center">';
		loadbar += '<div style="text-align: left; border: 1px solid ' + color + '; background: ' + bcolor + '; margin: 0; padding: 0;">';
		loadbar += '<div style="height: 9px; width: 1%; background: #339900; margin: 0; padding: 0;" id="' + this.loaderid + '"><!-- --></div>';
		loadbar += '</div>';
		loadbar += '</td>';
		loadbar += '</tr>';
		loadbar += '<tr>';
		loadbar += '<td style="padding: 0; margin: 0; white-space: nowrap;" align="center">';
		loadbar += '<div style="color: ' + color + '; margin: 0; padding: 0; padding-top: 8px; font-size: 11px;">' + this.loadtext + '</div>';
		loadbar += '</td>';
		loadbar += '</tr>';
		loadbar += '</table></div>';

		document.getElementById(this.params.split('|')[3]).innerHTML = loadbar;
	}

	this.load_rate_info = function(eventid, ebaseid, actual, forecast, dateline)
	{
		this.container = fetch_object('rate_info_' + ebaseid);

		this.container.style.display = this.container.style.display == 'none' ? '' : 'none';

		if (this.container.style.display == 'none')
		{
			return false;
		}

		this.onreadystatechange(this.load_rate_done);
		this.send('rateinfo.php?external=1&eventid=' + eventid + '&ebaseid=' + ebaseid + '&actual=' + actual + '&forecast=' + forecast + '&dateline=' + dateline);

		return false;
	}

	this.load_rate_done = function()
	{
		this.container.innerHTML = _sds_html;
	}

	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);
		}
	}

	this.market_status = function(mid, value)
	{
		if (!this.market_status_log)
		{
			this.market_status_log = new Array();
		}

		this.market_status_log[mid] = value;

		if (value)
		{
			YAHOO.util.Get.script('http://www.forexfactory.com/marketsession.php?id=' + mid + '&external=1&' + this.options,
			{
				scope: this,
				onSuccess: this.market_display
			});
		}
	}

	this.market_display = function()
	{
		fetch_object('market_' + _msUpdate + '_details').innerHTML = _ff_syn_ms_market;
	}

	this.fetch_details = function(e)
	{
		if (ffSyndication_Calendar.getdetails)
		{
			return false;
		}

		imgobj = ffSyndication_Calendar.detailimg = fetch_tags(fetch_object(this.id), 'img')[0];

		eventid = this.id.split('_');
		eventid = eventid[eventid.length - 1];

		if (ffSyndication_Calendar.opendetails[eventid])
		{
			// Close the details...
			imgobj.src = IMGDIR_MISC + 'calendar_detail' + (imgobj.getAttribute('detailstar') == '1' ? '_star' : imgobj.getAttribute('detailstar') == '2' ? '_alert' : '') + '' + ((ffSyndication_Calendar.darkicons) ? '_dark' : '') + '.gif';
			imgobj.title = imgobj.getAttribute('oldtitle');

			imgobj.width = 17;
			imgobj.height = 18;
			imgobj.style.padding = '0 0 0 0';

			ffSyndication_Calendar.opendetails[eventid] = false;
			fetch_object('detail_row_' + eventid).style.display = 'none';

			return false;
		}

		ffSyndication_Calendar.getdetails = true;
		ffSyndication_Calendar.detailfor = eventid;

		imgobj.src = IMGDIR_MISC + 'progress' + ((ffSyndication_Calendar.darkicons) ? '_dark' : '') + '.gif';
		imgobj.height = '16';
		imgobj.width = '16';
		imgobj.style.padding = '0 0 2px 0';

		var _wscal_detail_view_ = '';
		ffSyndication_Calendar.onreadystatechange(ffSyndication_Calendar.fetched_details);
		ffSyndication_Calendar.send('calendar.php?do=getdetail&eventid=' + eventid + '&external=1');

		return false;
	}

	this.fetched_details = function()
	{
		this.detailimg.src = IMGDIR_MISC + 'calendar_detail_down' + ((this.darkicons) ? '_dark' : '') + '.gif';
		this.detailimg.setAttribute('oldtitle', this.detailimg.title);
		this.detailimg.title = 'Close Detail';

		this.detailimg.height = '16';
		this.detailimg.width = '16';

		fetch_object('detail_row_' + this.detailfor).style.display = '';
		fetch_tags(fetch_object('detail_row_' + this.detailfor), 'div')[0].innerHTML = _wscal_detail_view_;

		// This fixes instances where websites have no document type declarations
		fetch_tags(fetch_object('detail_row_' + this.detailfor), 'div')[0].style.padding = '0px';
		fetch_tags(fetch_object('detail_row_' + this.detailfor), 'td')[0].style.padding = '10px';

		this.getdetails = false;
		this.opendetails[this.detailfor] = true;
	}

	this.update_cookies = function(dst, tz, tf)
	{
		setter = '&act=updateoptions&sel_dst=' + dst + '&time_zone=' + tz + '&sel_timeformat=' + tf;
		this.options += setter;

		this.onreadystatechange(this.last_start);
		this.send(this.synd + '?do=fetch');

		this.options = this.options.replace(setter, '') + '&settime=' + (Math.random() * 1234);
	}

	this.goto = function(what)
	{
		this.options = this.options.replace(/\&days=(today|tomorrow|yesterday)/g, '') + '&days=' + what;

		this.last_start();

		return false;
	}

	this.calendar_filters = function(form, remfilter)
	{
		urlbits = new Array('updatefilters=1');

		inputs = fetch_tags(form, 'input');

		for (var i = 0; i < inputs.length; i++)
		{
			if (inputs[i].type != 'submit' && inputs[i].checked)
			{
				urlbits.push(inputs[i].name + '=' + inputs[i].value);
			}
		}

		if (remfilter)
		{
			urlbits.push('remove=1');
		}

		temp = '&' + urlbits.join('&');

		this.options += temp;
		this.last_start();

		this.options = this.options.replace(temp, '');

		return false;
	}

	this.load_core = function()
	{
		if (!this.jsfiles[this.loaded])
		{
			vBulletin_init();
			this.loadedall = true;
			this.start();

			return false;
		}

		for (js in this.jsfiles)
		{
			if (js == this.loaded)
			{
				YAHOO.util.Get.script(this.jsfiles[js],   
				{
					scope: this,
					onSuccess: this.load_core_done
				});
			}
		}
	}

	this.load_core_done = function()
	{
		this.loaded++;

		if (this.jsfiles[this.loaded])
		{
			this.push_loader();
		}

		this.load_core();
	}

	this.push_loader = function()
	{
		if (!document.getElementById(this.loaderid))
		{
			return false;
		}

		this.extloaded++;

		done = Math.ceil(this.extloaded / (this.extfiles / 100));

		if (done > 100)
		{
			done = 100;
		}

		document.getElementById(this.loaderid).style.width = done + '%';
	}

	this.start = function()
	{
		this.create_footericon();

		if (this.options.match(/dark=1/))
		{
			this.darkicons = true;
		}

		// Fetch our CSS
		this.onreadystatechange(this.start_nd);
		this.sendcss(this.synd + '?do=fetchcss');
	}

	this.start_nd = function()
	{
		this.push_loader();
		this.onreadystatechange(this.last_start);
		this.sendcss('clientscript/vbulletin_css/header.css?');
	}

	this.last_start = function()
	{
		this.push_loader();

		// Fetch our Calendar
		var _sds_html = '';
		this.onreadystatechange(this.display_synd);
		this.send(this.synd + '?do=fetch');
	}

	this.onreadystatechange = function(obj)
	{
		this.statechange = obj;
	}

	this.send = function(script)
	{
		YAHOO.util.Get.script(this.url + script + this.options + '&nocache=' + (Math.random() * 9999),   
		{
			scope: this,
			onSuccess: this.statechange
		});
	}

	this.sendcss = function(script)
	{
		YAHOO.util.Get.css(this.url + script + this.options + '&nocache=' + (Math.random() * 9999),
		{
			scope: this,
			onSuccess: this.statechange
		});
	}
}