Subtly = function() { this.init(); };

(function() {
Subtly.prototype = {
	init: function() {
	
	},
	options: {
		bubble_time: {
			value: 2000,
			label: 'Bubble Time',
			description: 'How long the subtly bubble will remain visible.',
			type: 'int'
		},
		bubble_opacity: {
			value: 0.8,
			label: 'Bubble Opacity',
			description: 'The opactiy of the bubble.',
			type: 'int'
		},
		use_spacer: {
			value: false,
			label: 'Use Spacer',
			description: 'Use Spacer',
			type: 'bool'
		},
		mode: {
			value: 1,
			label: 'Mode',
			description: 'Determines the type of messages that should show in the preview: 0 for all, 1 system messages only, 2 admin messages only.',
			type: 'enum',
			options: '0,1,2'
		},
		style: {
			value: 'lumi-light',
			label: 'Style',
			description: 'Style to use for the notification window.',
			type: 'str'
		},
		flash_color: {
			value: '#d6e3fa', //B8D2FF -blue FCF300 -yellow
			label: 'Flash Color',
			description: 'When the preview bar is already shown, the background color will flash to this.',
			type: 'str'
		},
		display_time: {
			value: 0,
			label: 'Display Time',
			description: 'Determines, in milliseconds, how long the preview should show for.',
			type: 'int'
		},
		display_at: {
			value: 'bottom',
			label: 'Display At',
			description: 'Determines where the preview should show on the page.',
			type: 'enum',
			options: 'top,bottom'
		},
		show_avatar: {
			value: false
		},
		show_nickname: {
			value: true
		},
		filler_text: {
			value: 'Type your message here, then hit enter/return to send it.'
		},
		show_self: {
			value: true
		},
		show_input: {
			value: true
		},
		show_last: {
			value: 5
		},
		show_opener: {
			value: 'Live Chat'
		}
	},
	
	parseLinks: function(text)
	{
		return text.replace(/(http|https|ftp|ed2k|itunes|aim|yahoo):\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\-\.]*(\?\S+)?)?)?/ig, function(match, protocol, hostname, port, path, extra) {
			if(typeof protocol == 'undefined') protocol = '';
			if(typeof hostname == 'undefined') hostname = '';
			if(typeof port == 'undefined') port = '';
			if(typeof path == 'undefined') path = '';

			target = (hostname == window.location.hostname) ? '_self' : '_blank';

			return '<a href="' + protocol + '://' + hostname + port + path + '" target="' + target + '">' + hostname + port + path + '</a>';
		});
	},

	hidePreviewBar: function(callback) {
		var jSubtly = $('#lumi-subtly-lumi');
			
		if($('#lumi-subtly-reply-message').hasClass('lumi-focused')) return false;
		
		if(subtly.options.show_opener.value && !$('#lumi-subtly-opener:visible').length)
			$('#lumi-subtly-opener').slideToggle(400);
	
		$('#lumi-subtly-spacer').remove();
	
		jSubtly.removeClass('lumi-showing').stop(true, true);
	
		var animation = (subtly.options.display_at.value == 'top') ? { top: -jSubtly.height() + 'px' } : { bottom: -jSubtly.height() + 'px' };
	
		if(jSubtly.length) {
			jSubtly.addClass('lumi-hiding').animate(animation, 500, function() {
				$(this).removeClass('lumi-hiding').css('display', 'none');
			
				if($.isFunction(callback))
					callback.apply(true);
			});
		}
	},
	showPreviewBar: function(callback) {
		var jSubtly = $('#lumi-subtly-lumi');
			
		if(!jSubtly.length) return false;
	
		jSubtly.css('display', 'block');
	
		if(this.options.use_spacer.value)
			setTimeout(function() {
				$('#lumi-subtly-spacer').height(jSubtly.height());
			}, 700);
		
		if(jSubtly.css(this.options.display_at.value) == '0px' && $.isFunction(callback)) return callback.apply(false);
		
		if(this.options.show_opener.value && $('#lumi-subtly-opener:visible').length)
			$('#lumi-subtly-opener').slideToggle(400);
	
		var animation = (this.options.display_at.value == 'top') ? { top: 0 } : { bottom: 0 };
		
		if(!$('#lumi-subtly-spacer').length && this.options.use_spacer.value) {
			var spacer_data = '<div id="lumi-subtly-spacer" style="height:' + jSubtly.height() + 'px;"></div>';
			
			if(this.options.display_at.value == 'top')
				$(spacer_data).prependTo(document.body);
			else
				$(spacer_data).appendTo(document.body);
		}
			
		jSubtly.addClass('lumi-showing').animate(animation, 500, function() {
			this.className = this.className.replace('lumi-showing', '');
				
			if($.isFunction(callback))
				callback.apply(true);
		});
	},
	showItem: function(message, author, avatar, id) {	
		if(!id)
			var id = +new Date;
			
		var message = this.parseLinks(message);
		var self = this;
	
		var main_template = $('<div id="lumi-subtly-lumi" class="' + this.options.style.value + '"><div class="lumi-subtly-message-wrapper lumi-last lumi-' + id + '"></div></div>');
	
		if(this.options.show_nickname.value && author)
			$('<span class="lumi-subtly-nickname">' + author + '</span><span class="lumi-subtly-divider">: </span>').appendTo($('.lumi-subtly-message-wrapper', main_template));
		
		$('<span class="lumi-subtly-message">' + message + '</span>').appendTo($('.lumi-subtly-message-wrapper', main_template));
	
		var bubble_template = $('<div id="lumi-subtly-bubble" class="' + this.options.style.value + '"><div class="lumi-subtly-bubble-message">' + ((this.options.show_nickname.value) ? author + ': ' + message : message) + '</div></div>');
	
		if(this.subtly_preview)
			clearTimeout(this.subtly_preview);
	
		var jSubtly = $('#lumi-subtly-lumi');
	
		if(!this.options.show_avatar.value)
			jSubtly.addClass('lumi-no-avatar');
		if(!this.options.show_input.value)
			jSubtly.addClass('lumi-no-input');
					
		if(!jSubtly.length) {
			var first = true;
			
			if(this.options.show_opener.value) {
				$('#habla_window_div').hide();
				$('<div id="lumi-subtly-opener" style="display:none;" class="lumi-subtly-' + this.options.display_at.value + '">' + this.options.show_opener.value + '</div>').appendTo(document.body);
				$('#lumi-subtly-opener').click(function() {
					self.showPreviewBar();
				});
			}
						
			if(this.options.show_input.value) {
				$('<div id="lumi-subtly-reply"><input type="text" id="lumi-subtly-reply-message" /></div>').appendTo(main_template);
				$('#lumi-subtly-reply-message', main_template).keypress(function(event) {
					if(self.input_timeout) clearTimeout(self.input_timeout);
					
					var key = (window.event) ? event.keyCode : event.which;
					
					var _this = this;
					var e = e;
					
					self.input_timeout = setTimeout(function() {						
						if(key == 13 || key == 3) {
							$('#habla_wcsend_input').val(_this.value);
							$('#habla_chatform_form')[0].onsubmit();
						
							self.showItem(_this.value, 'me');
							
							_this.value = '';
							
							setTimeout(function() {
								_this.focus();
							} , 10);
						}
					}, 10);
				})
			}
			
			$('<div id="lumi-subtly-close"></div>').appendTo(main_template);
			$('<div id="lumi-powered">Powered by <a href="http://www.olark.com/">Olark</a></div>').appendTo(main_template);
		
			var jSubtly = $(main_template).appendTo(document.body);
		
			if(!this.options.show_avatar.value)
				jSubtly.addClass('lumi-no-avatar');
			if(!this.options.show_input.value)
				jSubtly.addClass('lumi-no-input');
		
			jSubtly.addClass('lumi-subtly-' + this.options.display_at.value);
		
			if(this.options.display_at.value == 'top')
				jSubtly.css('top', -jSubtly.height() + 'px');
			else
				jSubtly.css('bottom', -jSubtly.height() + 'px');
		
			$('input', jSubtly).focus(function() {
				if(self.options.display_time.value)
					self.subtly_preview = setTimeout(self.hidePreviewBar, self.options.display_time.value);
			}).blur(function() {
				if(self.options.display_time.value)
					self.subtly_preview = setTimeout(self.hidePreviewBar, self.options.display_time.value);
			});
		
			if(this.options.filler_text.value.length) {
				$('input', jSubtly).val(this.options.filler_text.value).addClass('lumi-filled').focus(function() {
					if($(this).hasClass('lumi-filled')) {
						this.value = '';
						this.className = 'lumi-focused';
					}
				}).blur(function() {
					if(!this.value.length) {
						this.className = 'lumi-filled';
						this.value = self.options.filler_text.value;
					} else
						this.className = '';
				});
			}
		
			$('#lumi-subtly-close', jSubtly).click(this.hidePreviewBar);
		
			this.showPreviewBar();
		} else {
			var first = (jSubtly.css(this.options.display_at.value) != '0px');
				
			this.showPreviewBar(function() {
				jSubtly.removeClass('lumi-showing').removeClass('lumi-hiding');
						
				var current = $('.lumi-subtly-message-wrapper', jSubtly);
				current.removeClass('lumi-last');
				
				if(current.length == self.options.show_last.value)
					current.eq(0).remove();
				
				$('.lumi-subtly-message-wrapper', main_template).appendTo(jSubtly).addClass('lumi-last');
			});
		}
			
		if(this.options.bubble_time.value > 0 && first) {
			$('#lumi-subtly-bubble').remove();
		
			$(bubble_template).appendTo(document.body);
	
			var bubble = $('#lumi-subtly-bubble');
	
			var bubble_css = {
				marginLeft: -(bubble.outerWidth() / 2),
				opacity: 0,
				left: bubble.offset().left
			};
	
			if(this.options.display_at.value == 'top')
				bubble_css.top = (window.innerHeight / 2) - (bubble.outerHeight() / 2);
			else
				bubble_css.bottom = (window.innerHeight / 2) - (bubble.outerHeight() / 2);
		
			var bubble_animation = {
				opacity: 0.2,
				width: jSubtly.outerWidth() - 26,
				height: jSubtly.outerHeight() - 26,
				marginLeft: 0,
				left: jSubtly.offset().left
			};
	
			if(this.options.display_at.value == 'top')
				bubble_animation.top = 0;
			else
				bubble_animation.bottom = 0;
	
			bubble.css(bubble_css).stop(true, true).animate({
				opacity: this.options.bubble_opacity.value
			}, 500)
				.animate({ opacity: this.options.bubble_opacity.value }, this.options.bubble_time.value)
				.animate(bubble_animation, 1000)
				.animate({
					opacity: 0,
				}, 200, function() {
					$(this).remove();
				});
		}
			
		setTimeout(function() {
			if(!(orig_bgcolor = jSubtly.data('bgcolor'))) {
				orig_bgcolor = jSubtly.css('backgroundColor')
				jSubtly.data('bgcolor', orig_bgcolor);
			}

			jSubtly.animate({
				backgroundColor: self.options.flash_color.value
			}, 300).animate({
				backgroundColor: orig_bgcolor
			}, 1000);
		}, (first) ? 500 : 100);
	
		if(this.options.display_time.value)
			this.subtly_preview = setTimeout(this.hidePreviewBar, this.options.display_time.value);
	}
};
})(jQuery);

var subtly = new Subtly();

subtly.showItem('May I help you find something', 'Help');

hbl.eventmgr.register('receive_message', function(obj) {
	var query;
	
	if(obj.type == 'start') return false;
		
	var last_message = obj.msg[obj.msg.length - 1];
	
	subtly.showItem(last_message[1], last_message[0]);
});