var websitealive_chat_enabled = false //this boolean is grabbed from ../../chat-status.txt
	|| location.href.indexOf('chat=true') != -1; //allow chat to be forced to be enabled via URL


(function(){
if(!websitealive_chat_enabled)
	return;

var onlineImage = "http://www.turtlefiji.com/assets/images/spacer.gif"; //1x1
var offlineImage = "http://www.turtlefiji.com/assets/images/spacer-2px.gif"; //2x2

//Note: It would be great if vButton_v3.asp implemented JSONP web service so that
//      the online state could be queried without the following hack, for example:
//      http://.../vButton_v3.asp?callback=myonlinestatus
//      Where myonlinestatus(onlineStatus) is a callback defined in this JS file
//      and which is passed a boolean argument indicating the online status.
// WHAT FOLLOWS IS A HACK!
var img = new Image();
img.onload = img.onerror = function(){
	//Error happened or Spacer image returned so don't initialize chat button (src redirected to offlineImage)
	if((!img.width || !img.height) || (img.height == 1 && img.width == 1)){
		websitealive_chat_enabled = false;
		return;
	}
	
	//Otherwise, image.width and image.height > 1 and we'll consider this to mean chat is enabled
	// Request redirected to onlineImage
	websitealive_chat_enabled = true;
	
	//Now add another top-button for chat
	var topButtonsContainer = document.getElementById('search-box');
	if(!topButtonsContainer)
		return;
	var chatButton = document.createElement('a');
	chatButton.href = 'http://a2.websitealive.com/981/rRouter.asp?groupid=981&websiteid=0&departmentid=1097&dl='+escape(document.location.href),'','width=400,height=400';
	chatButton.className = "chat-with-a-consultant-now";
	chatButton.appendChild(document.createTextNode("Chat with a Consultant now"));
	chatButton.target = "websitealivechat";
	chatButton.onclick = function(e){
		return !window.open(this.href, this.target, 'width=400,height=400');
	};
	topButtonsContainer.appendChild(chatButton);
	
	//Modify the other buttons to have the right dimensions and background positions
	var buttons = topButtonsContainer.getElementsByTagName('a');
	for(var i = 0, len = buttons.length; i < len; i++){
		buttons[i].style.backgroundImage = "url(/assets/images/top-buttons-with-chat.png)";
	}
	buttons[0].style.width = '127px';
	buttons[0].style.backgroundPosition = '0 top';
	buttons[1].style.width = '156px';
	buttons[1].style.backgroundPosition = '-127px top';
	buttons[2].style.width = '195px';
	buttons[2].style.backgroundPosition = 'right top';
};

img.src = "http://a2.websitealive.com/981/Visitor/vButton_v3.asp?groupid=981&departmentid=1097&w=400&h=400"
	+ "&icon_online=" + encodeURIComponent(onlineImage)
	+ "&icon_offline=" + encodeURIComponent(offlineImage);



//AliveChat Live Site Monitor Code
function wsa_include_js(){
	var wsa_host = (("https:" == document.location.protocol) ? "https://" : "http://");
	var js = document.createElement('script');
	js.setAttribute('language', 'javascript');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src',wsa_host + 'a2.websitealive.com/981/Visitor/vTracker_v2.asp?websiteid=0&groupid=981');
	document.getElementsByTagName('head').item(0).appendChild(js);
}
if(window.addEventListener)
	window.addEventListener('load', wsa_include_js, false);
else if(window.attachEvent)
	window.attachEvent('onload', wsa_include_js);

})();

