//
// $Id: visitor.js 3816 2007-09-03 11:19:01Z zeke $
//

live_help_visitor = Object.extend(live_help, {

	// Int: counter used to get operators activity (every 3 requests)
	counter: 0,

	// Flag: indicates first start
	first: true,

	// Flag: indicates that chat can be started
	start_chat: false,

	// Flag: indicates that visitor is invited to chat
	invited: false,

	// Int: indicates current operators status (online/offline, ready/busy)
	operator_status: -1,

	// Int: operator ID
	operator_id: 0,

	//
	// Initialize Live Help
	//
	init: function(params)
	{
		if (params) {
			for (k in params) {
				this[k] = params[k];
			}
		}
		
		this.send_request();
	},

	//
	// Enable/Disable links
	//
	enable_links: function(enable)
	{
		var links = ['lh_start_chat_link', 'lh_status_image_link'];
		var hrefs = ["javascript: live_help.request_chat()", "javascript: live_help.request_chat()"];
		
		for (var n in links) {
			var a = document.getElementById(links[n]);
			if (!a) {
				continue;
			}
			
			if (enable == true) {
				if (links[n] == 'lh_start_chat_link') {
					a.style.display = '';
				}
				var href = hrefs[n];
			} else {
				if (links[n] == 'lh_start_chat_link') {
					a.style.display = 'none';
				}
				var href = 'javascript: void(0);';

			}
			
			if (a.setAttribute) {
				a.setAttribute('href', href);
			} else {
				a.href = href;
			}
		}
	},

	//
	// Send request to server
	//
	send_request: function(action)
	{
		if (typeof(action) == 'undefined') {
			action = '';
		}

		// Indicates, call callback function or not to check the response
		var callback = false;

		if (this.first == true) {
			params = {
				visitor_environment: {
					title: document.title,
					href: location.href,
					browser: browser_info.browser + ", " + browser_info.version,
					os: browser_info.os,
					referer: document.referrer
				},
				actions: {}
			}
			params[this.mode_name] = 'init';
		} else {
			params = {
				actions: {}
			}
			params[this.mode_name] = 'update';
		}

		if (this.counter % 3 == 0 || action == 'stats') {
			params.actions.stats = 'Y';
			callback = true;
		}

		if (this.invited == false) {
			params.actions.check_invitation = 'Y';
			callback = true;
		}

		if (action == 'accept') {
			var today = new Date();
			params.local_time = Math.round(today.getTime() / 1000);
			params.user_name = this.user_name;
			params.operator_id = this.operator_id;
			params.actions.invitation = 'Y';
			callback = true;
			this.invited = false;
		} else if (action == 'decline') {
			params.actions.invitation = 'N';
			this.invited = false;
		}
		
		this.request(params, (this.first == true) ? 'POST' : 'GET', (callback == true) ? 'on_response' : '');

		this.first = (this.first == true) ? false : this.first;

		this.counter++;
		this.start_timer('live_help_visitor.send_request()');
	},

	//
	// Request response event
	//
	on_response: function(js, plain)
	{
		data = this.parse_xml(plain);
		if (!data) {
			/*if (this._timer) {
				clearTimeout(this._timer);
			}

			this.on_error();
			*/
			return false;
		}
		
		var ops = data.getElementsByTagName('operators');
		if (ops.length) {
			// Total operators count recieved
			var op_count = parseInt(this.get_subnode_value(ops[0], 'total'));
			this.update_status('total', op_count);
			if (this.start_chat == true) {
				this.open_chat(op_count > 0);
				this.start_chat = false;
			}

			// Free operators count recieved
			var op_free = parseInt(this.get_subnode_value(ops[0], 'free'));
			this.update_status('free', op_free);
		}

		var op_data = data.getElementsByTagName('operator_data');
		if (op_data.length) {
			this.invited = true;
			this.display_invitation_dialog({operator_id: this.get_subnode_value(op_data[0], 'operator_id'), operator_name: this.get_subnode_value(op_data[0], 'operator_name')});
		}

		return true;
	},

	//
	// Update LiveHelp status image
	//
	update_status: function (param, value)
	{
		if (param == 'total') {
			var total_count_element = document.getElementById('lh_total_count');
			if (total_count_element) {
				total_count_element.innerHTML = value;
			}
			
			var status_image_src = 'live_help_offline.gif';
			var status_image_hint = lh_lang.image_offline_hint;
			var new_status = false;
			if (value > 0) {
				status_image_src = 'live_help_online.gif';
				status_image_hint = lh_lang.image_online_hint;
				new_status = true;
			}
			var status_image = document.getElementById('lh_status_image');
			if (status_image && new_status != this.operator_status) {
				if (status_image.setAttribute) {
					status_image.setAttribute('src', this.status_image_path + status_image_src);
					status_image.setAttribute('title', status_image_hint);
				} else {
					status_image.src = this.status_image_path + status_image_src;
					status_image.title = status_image_hint;
				}
				status_image.style.display = 'inline';
				this.operator_status = new_status;
			}

			this.enable_links(new_status);

		} else if (param == 'free') {
			var free_count_element = document.getElementById('lh_free_count');
			if (free_count_element)	{
				free_count_element.innerHTML = value;
			}
		}
	},

	//
	// Request error handler
	//
	on_error: function()
	{
		this.update_status('free', 0);
		var total_count_element = document.getElementById('lh_total_count');
		if (total_count_element) {
			total_count_element.innerHTML = document.getElementById('lh_free_count').innerHTML = lh_lang.not_available;
		}
		this.enable_links(false);
	},

	//
	// Send chat request to operator
	//
	request_chat: function()
	{
		this.start_chat = true;
		this.send_request('stats');
	},

	//
	// Open chat window
	//
	open_chat: function(allow, force_enter)
	{
		if (allow == undefined) {
			allow = false;
		}
		if (force_enter == undefined) {
			force_enter = false;
		}
			
		if (!allow)	{
			return this.leave_message();
		} else {
			var get_params = this.mode_name + '=open_chat';
			if (force_enter) {
				get_params += '&'+this.action_name+'=force';
			}
		
			this.chatbox = window.open(this.request_url + '&' + get_params, 1000, this.get_popup_params());
		}

		if (!this.chatbox) {
			alert(lh_lang.failed_popup);
		}
	},

	//
	// Open leave message window
	//
	leave_message: function ()
	{
		var params = {
			scrollbars: 'no',
			height: 370
		}
		this.message_box = window.open(this.request_url + '&' + this.mode_name + '=leave_message', 1001, this.get_popup_params(params));
		if (!this.message_box) {
			alert(lh_lang.failed_popup);
		}
	},
	
	//
	// Align invitation dialog
	//
	display_invitation_dialog: function (data) 
	{
		if (!data) {
			return false;
		}

		var dlg = document.getElementById('invitation_dialog_bg');
		if (!dlg) {
			return false;
		}

		this.operator_id = data.operator_id;

		fn_align_element('invitation_dialog_bg');
		var op_id = document.getElementById('op_id');
		if (!op_id)	{
			return false;
		}
		op_id.value = data.operator_id;

		var intro = document.getElementById('invitation_intro');
		if (!intro)	{
			return false;
		}
		intro.innerHTML = lh_lang.invitation_intro.str_replace('[operator]', data.operator_name);
	},

	//
	// Accept invitation to chat
	// 
	accept_invitation: function() 
	{
		var invitation_name_input = document.getElementById('invitation_name');
		this.user_name = invitation_name_input.value;
		if (!this.user_name) {
			alert(lh_lang.please_input_name);
			invitation_name_input.focus();
			return false;
		}
		
		this.send_request('accept');
		fn_show_tag('invitation_dialog_bg', true);
		this.open_chat(true, true);
		
		return true;
	},

	//
	// Decline invitation to chat
	//
	decline_invitation: function() 
	{
		this.send_request('decline');
		fn_show_tag('invitation_dialog_bg', true);
		
		return true;
	}
});
