var DpProactiveChat = {
	
	/**
	 * Checks for proactive chats are performed with script tags.
	 * This is the last script tag that was added.
	 * @type {HTMLElement}
	 */
	scriptEl: null,
	
	/**
	 * Proactive chat DHTML popup windows are contained in an object
	 * to setup/show/hide and loaded on demand. This is the script tag.
	 * @type {HTMLElement}
	 */
	popScriptEl: null,
	
	
	
	
	
	/**
	 * Initiate proactive chat. This does the work of checking etc.
	 */
	init: function() {
		// Proactive chat relies upon
		// the tracking features
		if (!DpLoc) {
			return;
		}
		
		// Null means the trackid hasn't been fetched yet, try again in a sec
		if (DpLoc.trackId == null) {
			window.setTimeout(function() {
				DpProactiveChat.init();
			}, 1000);
			return;
		}
		
		// If there is a cookie saying to ignore, stop now
		if (DpLoc.getCookie('dpprochat_ignore')) {
			return;
		}
		
		// Somethings wrong, stop
		if (DpLoc.trackId < 1) {
			return;
		}
		
		this.performCheck();
	},
	
	
	
	
	
	/**
	 * Sends the check request by creaitng a new script tag.
	 * 
	 * @see #serverResponse
	 */
	performCheck: function() {
		if (this.scriptEl) {
			document.getElementsByTagName('head')[0].removeChild(this.scriptEl);
			this.scriptEl = null;
		}
		
		var url = DpLoc.getServerUrl('proactive_chat.php', { cmd: 'check', tid: DpLoc.trackId, 'x': (new Date()).getTime() });
		this.scriptEl = DpLoc.createScriptTag(url);
	},
	
	
	
	
	
	/**
	 * When the check is complete, the server calls this method.
	 * 
	 * @see #performCheck
	 * @param {Object} info Information from the server about the chat 
	 */
	serverResponse: function(info) {
		if (info) {
			this.showPop(info);
		} else {
			// Check again in 8 secs
			window.setTimeout(function() {
				DpProactiveChat.performCheck();
			}, 8000);
		}
	},
	
	
	
	
	
	/**
	 * A proactive chat has come in. Need to load the style elements and then show it
	 * to the user.
	 * 
	 * @param {Object} info
	 */
	showPop: function(info) {
		// Load the popup object script
		// Check for undefiend first incase its already included
		if (typeof DpProactiveChatPopCode == 'undefined' && !this.popScriptEl) {
			var url = DpLoc.getServerUrl('proactive_chat.php', { cmd: 'popcode', tid: DpLoc.trackId, x: (new Date()).getDate() });
			this.popScriptEl = DpLoc.createScriptTag(url);
		}
		
		// If the popcode isnt ready yet, try again in a sec
		if (typeof DpProactiveChatPopCode == 'undefined') {
			window.setTimeout(function() {
				DpProactiveChat.showPop(info);
			}, 1000);
			return;
		}
		
		DpProactiveChatPopCode.init(info, this);
		DpProactiveChatPopCode.showPop();
	},
	
	
	
	ignore: function() {
		var url = DpLoc.getServerUrl('proactive_chat.php', { tid: DpLoc.trackId, cmd: 'ignore' });
		DpLoc.sendMiscData(url, true);
	},
	
	disable: function() {
		var url = DpLoc.getServerUrl('proactive_chat.php', { tid: DpLoc.trackId, cmd: 'disable' });
		DpLoc.sendMiscData(url, true);
	},
	
	show: function() {
		window.open(DpLoc.serverUrl + 'chat.php?do=form&proactive=1&tid=' + DpLoc.trackId, 'chatpop', 'toolbar=no,location=no,status=no,menubar=no,width=750,height=520,resizable=yes,scrollbars=yes');	
	}
};