var active_sent_menu = false;
var sent_opened = false;
var sent_loop_process = false;

if (fetch_object('sentiment_chart_area'))
{
	var sent_chart = new SWFObject('includes/sentiment/sentiment.swf?v=1.0.1.6', 'sentiment_chart', '360', '174', '9', '#f5f5ff');
	sent_chart.addParam('menu', 'false');
}

var current_history = 5;
var count = 0;

function get_call_count()
{
	return count++;
}

function remove_log(logid)
{
	if (!confirm('Are you sure you want to delete this entry?'))
	{
		return false;
	}

	rlog = new vB_AJAX_Handler(true);
	rlog.onreadystatechange(removed_log);
	rlog.send('sentiment.php', 'do=removelog&id=' + logid);

	return false;
}

function removed_log()
{
	if (rlog.handler.readyState == 4 && rlog.handler.status == 200 && rlog.handler.responseText == 'ok')
	{
		if (is_ie)
		{
			rlog.handler.abort();
		}

		current_history -= 5;

		sentiment_history();
	}
}

function sentiment_switch(newsent)
{
	try
	{
		fetch_object('chart_sentiment').focus();
	}
	catch(e){}

	for (var s = 0; s < fetch_object('chart_sentiment').options.length; s++)
	{
		if (fetch_object('chart_sentiment').options[s].text == newsent)
		{
			fetch_object('chart_sentiment').options[s].selected = true;
		}
	}

	try
	{
		fetch_object('chart_sentiment').blur();
	}
	catch(e){}

	update_chart();

	window.location.href = window.location.href.split('#')[0] + '#chart';

	return false;
}

function update_chart()
{
	if (!fetch_object('sentiment_chart'))
	{
		sent_chart.write('sentiment_chart_area');
	}

	sentiment = fetch_object('chart_sentiment').options[fetch_object('chart_sentiment').options.selectedIndex].value;
	breakdown = fetch_object('chart_breakdown').options[fetch_object('chart_breakdown').options.selectedIndex].value;

	expires = new Date();
	expires.setTime(expires.getTime()+(1000*86400*365));

	set_cookie('schart_sent', sentiment, expires);
	set_cookie('schart_brkd', breakdown, expires);

	try
	{
		fetch_object('sentiment_chart').refresh_chart(breakdown, sentiment, '');
	}
	catch(e)
	{
		setTimeout("update_chart()", 1000);
	}
}

function sentiment_history()
{
	current_history += 5;

	fetch_object('sentiment_more_loading').style.display = '';
	fetch_object('sentiment_more').style.display = 'none';

	fetch = new vB_AJAX_Handler(true);
	fetch.onreadystatechange(sentiment_history_done);
	fetch.send('sentiment.php', 'do=history&n=' + current_history + '&nocache=' + get_call_count());

	return false;
}

function sentiment_history_done()
{
	if (fetch.handler.readyState == 4 && fetch.handler.status == 200)
	{
		if (fetch.handler.responseXML)
		{
			if (fetch_tags(fetch.handler.responseXML, 'history')[0])
			{
				fetch_object('sentiment_history').innerHTML = fetch.fetch_data(fetch_tags(fetch.handler.responseXML, 'history')[0]).split('__alLsHoWiNg__')[0];
			}

			if (fetch.fetch_data(fetch_tags(fetch.handler.responseXML, 'history')[0]).split('__alLsHoWiNg__')[1] == 1)
			{
				fetch_object('sentiment_more_loading').style.display = 'none';
				fetch_object('sentiment_more').style.display = 'none';
				fetch_object('sentiment_more_row').style.display = 'none';
			}
			else
			{
				fetch_object('sentiment_more_loading').style.display = 'none';
				fetch_object('sentiment_more').style.display = '';
			}

			if (is_ie)
			{
				fetch.handler.abort();
			}
		}
	}
}

function sentiment_info_hide()
{
	try
	{
		if (!sent_opened && fetch_object('sentiment_' + active_sent_menu))
		{
			sentiment_info(active_sent_menu, true);
		}
	}
	catch(e)
	{
	}

	sent_opened = false;

	vbmenu_hide();
}

function sentiment_info(id, hide)
{
	if (hide)
	{
		fetch_object('sentiment_' + id).style.display = 'none';

		return false;
	}

	if (active_sent_menu && active_sent_menu != id)
	{
		sentiment_info_hide();
	}

	if (!sent_loop_process)
	{	// allows any browser to correctly format left-hand points the 2nd time around, like IE8
		sent_loop_process = true;

		sentiment_info(id, false);

		sent_loop_process = false;
	}

	active_sent_menu = id;
	sent_opened = true;

	fetch_object('sentiment_' + id).style.display = '';
	fetch_object('sentiment_' + id).style.position = 'absolute';

	offset = vB_Popup_Menu.prototype.fetch_offset(fetch_object('sentiment_link_' + id));

	offset['left'] += parseInt(fetch_object('sentiment_link_' + id).offsetWidth) + 4;
	offset['top'] -= 16;

	if ((offset['left'] + fetch_object('sentiment_' + id).offsetWidth + 14) >= document.body.clientWidth && (offset['left'] - (fetch_object('sentiment_' + id).offsetWidth + 14)) > 0)
	{
		offset['left'] -= (parseInt(fetch_object('sentiment_' + id).offsetWidth) + parseInt(fetch_object('sentiment_link_' + id).offsetWidth) + 24);

		fetch_object('sentiment_point_' + id).style.display = 'none';
		fetch_object('sentiment_point_alt_' + id).style.display = '';
		fetch_object('sentiment_point_alt_' + id).style.left = (fetch_object('sentiment_' + id).offsetWidth - 10) + 'px';
	}
	else
	{
		fetch_object('sentiment_point_' + id).style.display = '';
		fetch_object('sentiment_point_alt_' + id).style.display = 'none';
	}

	fetch_object('sentiment_' + id).style.top	= offset['top']		+ 'px';
	fetch_object('sentiment_' + id).style.left	= offset['left']	+ 'px';
}

if (fetch_object('sentiment_chart_area'))
{
	YAHOO.util.Event.onDOMReady(function () 
	{
		update_chart();
	});
}

if (window.attachEvent && !is_saf)
{
	document.attachEvent('onclick', sentiment_info_hide);
	window.attachEvent('onresize', sentiment_info_hide);
}
else if (document.addEventListener && !is_saf)
{
	document.addEventListener('click', sentiment_info_hide, false);
	window.addEventListener('resize', sentiment_info_hide, false);
}
else
{
	window.onclick = sentiment_info_hide;
	window.onresize = sentiment_info_hide;
}
