/* <![CDATA[ */
(function($) {
	window.TDPGIC = {
		_DEBUG: false,
		_defaults: {
			channel: null,
			container: 'ICContainer',
			fields: { Name: 'Name', Comment1: 'Comment1', Comment2: 'Comment2', Comment3: 'Comment3', Comment4: 'Comment4', Comment5: 'Comment5' },
			css: 'https://secure.logmeinrescue.com/InstantChat/Standard/InstantChat.css',
			required: null,
			_static: false,
			status: null,
			onCheckComplete: function(e) { if (TDPGIC.status) { TDPGIC.initChat(); } }
		},
		_parentId: '',
		channels: { Sales: '7733555' },
		messages: { Sales: 'There are no sales representatives available at this time. Please try again later' },
		callback: function(status) {
			TDPGIC.status = TDPGIC._DEBUG ? true : status;
			$('body').trigger('load.TDPGIC');
		},
		check: function() {
			if (TDPGIC._DEBUG) { TDPGIC.callback(true); return false; }
			if (!TDPGIC.validForm(TDPGIC._parentId)) { return false; }
			$.ajax({
				contentType: 'application/json; charset=utf-8',
				dataType: 'jsonp',
				url: 'http://widgets.findaproperty.com/helpdeskavailability.svc/IsHelpDeskAvailable?method=TDPGIC.callback&chatChannel=' + TDPGIC.channel
			});
		},
		hideRequired: function(id) { $('#' + id + 'Span').addClass('display-none').removeClass('validate'); },
		showRequired: function(id) { $('#' + id + 'Span').addClass('validate').removeClass('display-none'); },
		initChat: function() {
			if (!TDPGIC.status && !TDPGIC._DEBUG) { return false; }
			var ICLoader = new RescueInstantChatLoader();
			ICLoader.ICContainer = TDPGIC.container;
			ICLoader.HostedCSS = TDPGIC.css;
			ICLoader.EntryID = TDPGIC.channel;
			$.each(TDPGIC.fields, function(i, v) {
				var f = $('#' + TDPGIC._parentId + v);
				if (f.length) {
					ICLoader[i] = f.val();
					if (TDPGIC._DEBUG) { $('#DEBUG').append('<br /><strong>' + i + ':<' + '/strong> ' + ICLoader[i]); }
				}
			});
			ICLoader.Start();
			var offset = $('div.entry', '#Chat' + TDPGIC._parentId).offset();
			$('div.entry', '#Chat' + TDPGIC._parentId).hide();
			$('#' + TDPGIC.container).css({ left: (offset.left - 30) + 'px', top: (offset.top - 5) + 'px' }).show();
		},
		setup: function(options) {
			if (typeof options !== 'object') { $.extend(TDPGIC, TDPGIC._defaults); return; }
			if ((typeof options.channel === 'string') && (options.channel.length)) { TDPGIC.channel = options.channel; TDPGIC._static = true; }
			if ((typeof options.container === 'string') && (options.container.length)) { TDPGIC.container = options.container; }
			if ((typeof options.css === 'string') && (options.css.length)) { TDPGIC.css = options.css; }
			if (typeof options.debug === 'boolean') { TDPGIC._DEBUG = options.debug; }
			if (typeof options.fields === 'object') {
				$.each(options.fields, function(i, v) { if (TDPGIC.fields[i]) { TDPGIC.fields[i] = v; } });
			}
			if (typeof options.messages === 'object') {
				$.each(options.messages, function(i, v) { if (TDPGIC.channels[i] && v.length) { TDPGIC.messages[i] = v; } });
			}
			if (options.required && options.required instanceof Array) { TDPGIC.required = options.required; }
			if (typeof options.onCheckComplete === 'function') { $('body').unbind('load.TDPGIC'); $('body').bind('load.TDPGIC', options.onCheckComplete); }
		},
		validForm: function() {
			var r = true;
			if (TDPGIC.required instanceof Array && !TDPGIC._DEBUG) {
				$.each(TDPGIC.required, function(i, v) {
					var el = $('#' + v, '#Chat' + TDPGIC._parentId);
					if (el.length && !$('#' + v).val().length) { r = false; TDPGIC.showRequired(v); }
				});
			}
			return r;
		}
	};
	TDPGIC.setup();

	$(function() {
		if (TDPGIC._DEBUG) { $('body').append('<div class="error" id="DEBUG" style="position:absolute;top:1em;left:1em;padding:1em 2em;background-color:#eee;border:2px solid #c00;">DEBUG MODE<' + '/div>'); }
		$('#' + TDPGIC.container).hide();
		$('input:button', 'div.post').bind('click', function(e) {
			TDPGIC._parentId = $(this).parents('div[id^=Chat]')[0].id.replace(/^Chat/, '');
			if ($(this).data('channel')) { TDPGIC.channel = $(this).data('channel'); }
			else {
				if ((TDPGIC.channel !== null) && !TDPGIC._static) { TDPGIC.channel = null; }
			}
			if ((TDPGIC.channel === null) && TDPGIC.channels[TDPGIC._parentId]) { TDPGIC.channel = TDPGIC.channels[TDPGIC._parentId]; }
			if (TDPGIC.channel === null) {
				throw new Error('System.ArgumentException: No channel defined.'); return false;
			}
			if (!arguments.callee.init) {
				if (TDPGIC._DEBUG) { $('#DEBUG').append('<br /><strong>Channel:<' + '/strong> ' + TDPGIC.channel); }
				$(this).data('channel', TDPGIC.channel);
				if (TDPGIC.required instanceof Array) {
					$.each(TDPGIC.required, function(i, v) {
						$('#' + v).bind('focus', function(e) { TDPGIC.hideRequired(this.id); });
					});
				}
				var c = $('#' + TDPGIC.container);
				c.css({ height: c.height() + 'px', width: c.width() + 'px' });
				$.getScript('https://secure.logmeinrescue.com/InstantChat/InstantChat.aspx', TDPGIC.check);
				arguments.callee.init = true;
			}
			else { TDPGIC.check(); }
		});
	});
})(jQuery);
/* ]]> */
