// Rewritten again by Adam Taylor 12/05/09 for Refresh needs

addEvent(window, 'load', init_smartButton);

/*-------------------------------*/

function init_smartButton()
{
	if(document.getElementById('webchat'))
		{
		var preload_img = document.createElement('img')
		preload_img.src = 'http://admin.instantservice.com/resources/smartbutton/6066/' + document.static_invite_dept_id + '/available.gif?' + Math.floor(Math.random()*10001)
		preload_img.id = 'webchat_preloader'

		document.body.appendChild(preload_img)

		if (document.getElementById('webchat_preloader'))
			{addEvent(document.getElementById('webchat_preloader'), 'load', agents_available)}
		}
}

function agents_available()
{
	var webch = document.getElementById('webchat')

	if (webch.className.match('inactive'))
		{
		removeClass(webch, 'inactive')

		var kids = webch.childNodes
		var z = kids.length-1

		for (var i=z; i>=0; i--)
			{
			if (kids[i].nodeName == 'A')
				{
				addEvent(kids[i], 'click', open_chat)
				break;
				}
			}
		}

	return true;
}

function open_chat()
{
	window.open('/bupastatic/webchat/welcomeWebChatStatic.shtml','custclient','width=530,height=680,scrollbars=1')
}


