var chat = Wt.chat.PublicChat, chatAvailable = false;

jQuery(function () {
	var cssUrl = 'http' + ( /(scottbrand\.com)|(jivesoftware\.com)/i.test(location.hostname) ? 's' : '' ) + '://' + location.hostname + '/css/chat.css';
	chat.init({
	    msgUnavailable: "Sorry, chat is temporarily unavailable",
	    site: 'www.scottbrand.com', 
	    statusCallback: testStatus,
	    startOptions: {
	        window: {
	            top: 100,
	            left: 100,
	            width: 375,
	            height: 600,
	            cssUrl: cssUrl,
	            headerHtml: '',
	            footerHtml: '<p>Your comments will be treated in confidence.</p>',
	            title: 'Chat with a Live Representative! '
	        },
	        initialText: 'init_text',
	        info: {
	            'subject': 'Scott'
	        }
	    }
	});


    jQuery('a.cmdChat').click(function() {
    	if (chatAvailable) {
			chat.start({
		        info: {
		            'product': 'Scott'
		        }
		    });
		} else {
	        var msgWindow = window.open('', 'chatWindow', 'top=100,left=100,height=375,width=375,menubar=0,resizable=0,location=0,status=0,scrollbars=0');
	        var htmlString = "";
	        htmlString += "<html><head><title>Live chat unavailable</title><link rel='stylesheet' type='text/css' href='" + cssUrl + "' /></head>";

	        var chatMessage = chat.msg;

	        if (chatMessage == null || chatMessage == "") {
	            chatMessage = "Live chat is currently unavailable. Please check back with us later or you can call 1-877-856-7268.";
	        }
	        htmlString += "<body><div id='offTitle'><span><em>Sorry.</em></span></div><div id='offMessage'>" + chatMessage + "</div></body>";
	        htmlString += "</html>";
	        msgWindow.document.write(htmlString);
	    }
	});
    

	function testStatus () {
	    if (chat.status === 'ready') {
	        jQuery('.requires-chat').removeClass('unavailable');
	        chatAvailable = true;
	    }
	}
});
