/*
* @version : 2.2 (Patch)
* @update : 2009-08-21
*/
/*!
 * jquery.qtip. The jQuery tooltip plugin
 *
 * Copyright (c) 2009 Craig Thompson
 * http://craigsworks.com
 *
 * Licensed under MIT
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Launch  : February 2009
 * Version : 1.0.0-rc1
 * Released: Friday 24th April, 2009 - 19:45
 */
(function($)
{
	// Implementation
	$.fn.qtip = function(options, blanket)
	{
		var i, id, interfaces, opts, obj, command, config;

		if(!blanket) blanket = false;

		// Return API if requested
		if(typeof options == 'string')
		{
			// Make sure API data exists if requested
			if(options == 'api' && typeof $(this).eq(0).data('qtip') == 'object')
				return $(this).eq(0).data('qtip');

			// No API data exists, inform via log
			else if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.NO_TOOLTIP_PRESENT, 'qtip');
		}

		// Validate provided options
		else
		{
			// Set null options object if no options are provided
			if(!options) options = {};

			// Sanitize option data
			if(typeof options.content !== 'object') options.content = { text: options.content };
			if(typeof options.content.title !== 'object') options.content.title = { text: options.content.title };
			if(typeof options.position !== 'object') options.position = { corner: options.position };
			if(typeof options.position.corner !== 'object') options.position.corner = { target: options.position.corner, tooltip: options.position.corner };
			if(typeof options.show !== 'object') options.show = { when: options.show };
			if(typeof options.show.when !== 'object') options.show.when = { event: options.show.when };
			if(typeof options.show.effect !== 'object') options.show.effect = { type: options.show.effect };
			if(typeof options.hide !== 'object') options.hide = { when: options.hide };
			if(typeof options.hide.when !== 'object') options.hide.when = { event: options.hide.when };
			if(typeof options.hide.effect !== 'object') options.hide.effect = { type: options.hide.effect };
			if(typeof options.style !== 'object') options.style = { name: options.style };
			options.style = sanitizeStyle(options.style);

			// Build main options object
			opts = $.extend(true, {}, $.fn.qtip.defaults, options);

			// Inherit all style properties into one syle object and include original options
			opts.style = buildStyle.call({ options: opts }, opts.style);
			opts.user = $.extend(true, {}, options);
		};

		// Iterate each matched element
		return $(this).each(function() // Return original elements as per jQuery guidelines
		{
			// Check for API commands
			if(typeof options == 'string')
			{
				command = options.toLowerCase();
				interfaces = $(this).data('interfaces');

				// Make sure API data exists
				if(typeof interfaces == 'object')
				{
					// Check if API call is a BLANKET DESTROY command
					if(blanket && command == 'destroy')
						while(interfaces.length > 0) interfaces[interfaces.length-1].destroy();

					// API call is not a BLANKET DESTROY command
					else
					{
						// Check if it's a blanket command (Effects all qTips)
						if(blanket === false) interfaces = [ interfaces[interfaces.length - 1] ];

						// Execute command on chosen qTips
						for(i = 0; i < interfaces.length; i++)
						{
							if(command == 'destroy') interfaces[i].destroy();

							// Only call API if tooltip is rendered and it wasn't a destroy call
							else if(interfaces[i].status.rendered === true)
							{
								if(command == 'show') interfaces[i].show();
								else if(command == 'hide') interfaces[i].hide();
								else if(command == 'focus') interfaces[i].focus();
								else if(command == 'disable') interfaces[i].disable(true);
								else if(command == 'enable') interfaces[i].disable(false);
							};
						};
					};
				};
			}

			// No API commands, continue with qTip creation
			else
			{
				// Create unique configuration object
				config = $.extend(true, {}, opts);
				config.hide.effect.length = opts.hide.effect.length;
				config.show.effect.length = opts.show.effect.length;

				// Sanitize target options
				if(config.position.container === false) config.position.container = $(document.body);
				if(config.position.target === false) config.position.target = $(this);
				if(config.show.when.target === false) config.show.when.target = $(this);
				if(config.hide.when.target === false) config.hide.when.target = $(this);

				// Determine tooltip ID (Reuse array slots if possible)
				id = $.fn.qtip.interfaces.length
				for(i = 0; i < id; i++)
				{
					if(typeof $.fn.qtip.interfaces[i] == 'undefined'){ id = i; break; };
				};

				// Instantiate the tooltip
				obj = new qTip($(this), config, id);

				// Add API data and append to interfaces object
				$.fn.qtip.interfaces[id] = obj;
				$(this).data('qtip', obj);
				if($(this).data('interfaces')) $(this).data('interfaces').push(obj);
				else $(this).data('interfaces', [obj]);
			};
		});
	};

	// Instantiator
	function qTip(target, options, id)
	{
		// Declare this reference
		var self = this;

		// Setup class attributes
		self.id = id;
		self.options = options;
		self.status = {
			rendered: false,
			disabled: false,
			focused: false
		};
		self.elements = {
			target: target.addClass(self.options.style.classes.target),
			tooltip: null,
			wrapper: null,
			content: null,
			contentWrapper: null,
			title: null,
			tip: null,
			bgiframe: null
		};
		self.cache = {
			mouse: {},
			position: {}
		};
		self.timers = {};

		// Define exposed API methods
		$.extend(self, self.options.api,
		{
			show: function(event)
			{
				var returned, solo;

				// Make sure tooltip is rendered and if not, return
				if(!self.status.rendered)
				{
					if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'show');
					return self;
				};

				// Only continue if element is visible
				if(self.elements.tooltip.css('display') !== 'none') return self;

				// Stop show timer and animation queue
				self.elements.tooltip.stop(true, true);

				// Call API method and if return value is false, halt
				returned = self.beforeShow.call(self, event);
				if(returned === false) return self;

				// Define afterShow callback method
				function afterShow()
				{
					self.onShow.call(self, event);

					// Prevent antialias from disappearing in IE7 by removing filter attribute
					if($.browser.msie) self.elements.tooltip.get(0).style.removeAttribute('filter');
				};

				// Maintain toggle functionality if enabled
				if(typeof self.options.show.when.target.data('qtip-toggle') == 'number')
					self.options.show.when.target.data('qtip-toggle', 1);

				// Update tooltip position
				self.updatePosition(event, (self.options.show.effect.length > 0));

				// Hide other tooltips if tooltip is solo
				if(typeof self.options.show.solo == 'object') solo = $(self.options.show.solo);
				else if(self.options.show.solo === true) solo = $('div.qtip').not(self.elements.tooltip);
				if(solo) solo.each(function(){ if($(this).qtip('api').status.rendered === true) $(this).qtip('api').hide(); });

				// Show tooltip
				if(typeof self.options.show.effect.type == 'function')
				{
					self.options.show.effect.type.call(self.elements.tooltip, self.options.show.effect.length);
					self.elements.tooltip.queue(function(){ afterShow(); $(this).dequeue(); });
				}
				else
				{
					switch(self.options.show.effect.type.toLowerCase())
					{
						case 'fade':
							self.elements.tooltip.fadeIn(self.options.show.effect.length, afterShow);
							break;
						case 'slide':
							self.elements.tooltip.slideDown(self.options.show.effect.length, function()
							{
								afterShow();
								self.updatePosition(event, true);
							});
							break;
						case 'grow':
							self.elements.tooltip.show(self.options.show.effect.length, afterShow);
							break;
						default:
							self.elements.tooltip.show(null, afterShow);
						break;
					};

					// Add active class to tooltip
					self.elements.tooltip.addClass(self.options.style.classes.active);
				};

				// Focus tooltip if absolutely positioned
				if(self.options.position.type.search(/(fixed|absolute)/) !== -1) self.focus();

				// Call API method and log event
				self.onShow.call(self, event);
				if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_SHOWN, 'show');

				return self;
			},

			hide: function(event)
			{
				var returned;

				// Make sure tooltip is rendered and if not, return
				if(!self.status.rendered)
				{
					if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'hide');
					return self;
				};

				// Only continue if element is visible
				if(self.elements.tooltip.css('display') === 'none') return self;

				// Stop show timer and animation queue
				clearTimeout(self.timers.show);
				self.elements.tooltip.stop(true, true);

				// Call API method and if return value is false, halt
				returned = self.beforeHide.call(self, event);
				if(returned === false) return self;

				// Define afterHide callback method
				function afterHide(){ self.onHide.call(self, event); };

				// Maintain toggle functionality if enabled
				if(typeof self.options.show.when.target.data('qtip-toggle') == 'number')
					self.options.show.when.target.data('qtip-toggle', 0);

				// Hide tooltip
				if(typeof self.options.hide.effect.type == 'function')
				{
					self.options.hide.effect.type.call(self.elements.tooltip, self.options.hide.effect.length);
					self.elements.tooltip.queue(function(){ afterHide(); $(this).dequeue(); });
				}
				else
				{
					switch(self.options.hide.effect.type.toLowerCase())
					{
						case 'fade':
							self.elements.tooltip.fadeOut(self.options.hide.effect.length, afterHide);
							break;
						case 'slide':
							self.elements.tooltip.slideUp(self.options.hide.effect.length, afterHide);
							break;
						case 'grow':
							self.elements.tooltip.hide(self.options.hide.effect.length, afterHide);
							break;
						default:
							self.elements.tooltip.hide(null, afterHide);
							break;
					};

					// Remove active class to tooltip
					self.elements.tooltip.removeClass(self.options.style.classes.active);
				};

				// Call API method and log event
				self.onHide.call(self, event);
				if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_HIDDEN, 'hide');

				return self;
			},

			updatePosition: function(event, animate)
			{
				var i, target, tooltip, coords, mapName, imagePos, newPosition, ieAdjust, ie6Adjust, borderAdjust, mouseAdjust, offset, curPosition, returned

				// Make sure tooltip is rendered and if not, return
				if(!self.status.rendered)
				{
					if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'updatePosition');
					return self;
				};

				// Define property objects
				target = {
					position: { left: 0, top: 0 },
					dimensions: { height: 0, width: 0 },
					corner: self.options.position.corner.target
				};
				tooltip = {
					position: self.getPosition(),
					dimensions: self.getDimensions(),
					corner: self.options.position.corner.tooltip
				};

				// Target is an HTML element
				if(self.options.position.target !== 'mouse')
				{
					// If the HTML element is AREA, calculate position manually
					if(self.options.position.target.get(0).nodeName == 'AREA')
					{
						// Retrieve coordinates from coords attribute and parse into integers
						coords = self.options.position.target.attr('coords').split(',');
						for(i = 0; i < coords.length; i++) coords[i] = parseInt(coords[i]);

						// Setup target position object
						mapName = self.options.position.target.parent('map').attr('name');
						imagePos = $('img[usemap="#'+mapName+'"]:first').offset();
						target.position = {
							left: Math.floor(imagePos.left + coords[0]),
							top: Math.floor(imagePos.top + coords[1])
						};

						// Determine width and height of the area
						switch(self.options.position.target.attr('shape').toLowerCase())
						{
							case 'rect':
								target.dimensions = {
									width: Math.floor(Math.abs(coords[2] - coords[0])),
									height: Math.floor(Math.abs(coords[3] - coords[1]))
								};
								break;

							case 'circle':
								target.dimensions = {
									width: coords[2],
									height: coords[2]
								};

								target.position.left += coords[2]; target.position.top += coords[2];
								break;

							case 'poly':
								target.dimensions = {
									width: coords[0],
									height: coords[1]
								};

								for(i = 0; i < coords.length; i++)
								{
									if(i % 2 == 0)
									{
										if(coords[i] > target.dimensions.width)
											target.dimensions.width = coords[i];
										if(coords[i] < coords[0])
											target.position.left = Math.floor(imagePos.left + coords[i]);
									}
									else
									{
										if(coords[i] > target.dimensions.height)
											target.dimensions.height = coords[i];
										if(coords[i] < coords[1])
											target.position.top = Math.floor(imagePos.top + coords[i]);
									};
								};

								target.dimensions.width = target.dimensions.width - (target.position.left - imagePos.left);
								target.dimensions.height = target.dimensions.height - (target.position.top - imagePos.top);
								break;

							default:
								if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 4, $.fn.qtip.constants.INVALID_AREA_SHAPE, 'updatePosition');
								return self;
								break;
							};

						// Adjust position by 2 pixels (Positioning bug?)
						target.dimensions.width -= 2; target.dimensions.height -= 2;
					}

					// Target is a regular HTML element, find position normally
					else if(self.options.position.target.add(document.body).length !== 1)
					{
						// Setup target position and dimensions objects
						target.position = self.options.position.target.offset();
						target.dimensions = {
							height: self.options.position.target.outerHeight(),
							width: self.options.position.target.outerWidth()
						};
					}

				// Target is the document
				else
				{
					target.position = { left: $(document).scrollLeft(), top: $(document).scrollTop() };
					target.dimensions = { height: $(window).height(), width: $(window).width() };
				};

				// Calculate correct target corner position
				newPosition = $.extend({}, target.position);
				if(target.corner.search(/right/i) !== -1)
					newPosition.left += target.dimensions.width;

				if(target.corner.search(/bottom/i) !== -1)
					newPosition.top += target.dimensions.height;

				if(target.corner.search(/((top|bottom)Middle)|center/) !== -1)
					newPosition.left += (target.dimensions.width / 2);

				if(target.corner.search(/((left|right)Middle)|center/) !== -1)
					newPosition.top += (target.dimensions.height / 2);
			}

			// Mouse is the target, set position to current mouse coordinates
			else
			{
				// Setup target position and dimensions objects
				target.position = newPosition = { left: self.cache.mouse.x, top: self.cache.mouse.y };
				target.dimensions = { height: 1, width: 1 };
			};

			// Calculate correct target corner position
			if(tooltip.corner.search(/right/i) !== -1)
				newPosition.left -= tooltip.dimensions.width;

			if(tooltip.corner.search(/bottom/i) !== -1)
				newPosition.top -= tooltip.dimensions.height;

			if(tooltip.corner.search(/((top|bottom)Middle)|center/) !== -1)
				newPosition.left -= (tooltip.dimensions.width / 2);

			if(tooltip.corner.search(/((left|right)Middle)|center/) !== -1)
				newPosition.top -= (tooltip.dimensions.height / 2);

			// Setup IE adjustment variables (Pixel gap bugs)
			ieAdjust = ($.browser.msie) ? 1 : 0; // And this is why I hate IE...
			ie6Adjust = ($.browser.msie && parseInt($.browser.version.charAt(0)) === 6) ? 1 : 0; // ...and even more so IE6!

			// Adjust for border radius if Middle positioning is present and tips enabled
			if(self.options.style.tip.corner !== false)
			{
				if(tooltip.corner.search(/((top|bottom)Middle)|center/) !== -1)
					newPosition.left -= 5 - ieAdjust

				if(tooltip.corner.search(/((left|right)Middle)|center/) !== -1)
					newPosition.top -= 5 + ieAdjust - ie6Adjust;
			};

			// Adjust for border
			if(self.options.style.border.radius > 0)
			{
				borderAdjust = self.options.style.border.radius;

				if(tooltip.corner.search(/top/) !== -1)
					newPosition.top -= borderAdjust - 3 + ieAdjust;
				else if(tooltip.corner.search(/bottom/) !== -1)
					newPosition.top += borderAdjust - 2;
				else if(tooltip.corner.search(/left/) !== -1)
					newPosition.left -= borderAdjust - 3 + ieAdjust;
				else if(tooltip.corner.search(/right/) !== -1)
					newPosition.left += borderAdjust - 3 + ieAdjust;

				if(tooltip.corner.search(/Top/) !== -1)
					newPosition.top -= (borderAdjust * 2) - 3 - (ie6Adjust * 3);
				else if(tooltip.corner.search(/Bottom/) !== -1)
					newPosition.top += (borderAdjust * 2) - 2 - ieAdjust;
				else if(tooltip.corner.search(/Left/) !== -1)
					newPosition.left -= (borderAdjust * 2) - 3;
				else if(tooltip.corner.search(/Right/) !== -1)
					newPosition.left += (borderAdjust * 2) - 3;

				// IE only adjustments (Pixel perfect!)
				if(ieAdjust)
				{
					if(tooltip.corner.search(/topMiddle/) !== -1)
						newPosition.top += 1
					else if(tooltip.corner.search(/bottomMiddle/) !== -1)
						newPosition.top -= 1
					else if(tooltip.corner.search(/leftMiddle/) !== -1)
						newPosition.left += 1
					else if(tooltip.corner.search(/rightMiddle/) !== -1)
						newPosition.left -= 1
				};
			};

			// Add user option adjustments
			if(self.options.position.adjust.screen === true)
				newPosition = screenAdjust.call(self, newPosition, target, tooltip);
			else
			{
				newPosition.left += self.options.position.adjust.x;
				newPosition.top += self.options.position.adjust.y;
			};

			// If mouse is the target, prevent tooltip appearing directly under the mouse
			if(self.options.position.adjust.mouse === true)
			{
				if(self.options.position.adjust.screen === true)
					mouseAdjust = self.elements.tip.attr('rel');
				else
					mouseAdjust = self.options.position.corner.tooltip;

				newPosition.left += (mouseAdjust.search(/right/i) !== -1) ? -6 : 6;
				newPosition.top += (mouseAdjust.search(/bottom/i) !== -1) ? -6 : 6;
			};

			// Initiate bgiframe plugin in IE6 if tooltip overlaps a select box or object element
			if(!self.elements.bgiframe && $.browser.msie && parseInt($.browser.version.charAt(0)) == 6)
			{
				$('select, object').each(function()
				{
					offset = $(this).offset();
					offset.right = offset.top + $(this).height();
					offset.bottom = offset.bottom + $(this).width();

					if(newPosition.top >=  offset.bottom && newPosition.top <= offset.top
					&& newPosition.left <= offset.right && newPosition.left >= offset.left)
						bgiframe.call(self);
				});
			};

			// Set new tooltip position if its moved, animate if enabled
			curPosition = self.getPosition();
			if(newPosition.left != curPosition.left || newPosition.top != curPosition.top)
			{
				// Call API method and if return value is false, halt
				returned = self.beforePositionUpdate.call(self, event);
				if(returned === false) return self;

				if(animate === true)
					self.elements.tooltip.animate(newPosition, 200, 'swing');
				else
					self.elements.tooltip.css(newPosition);

				// Update bgiframe position if enabled
				if(self.cache.bgiframe === true)
					self.elements.bgiframe.css(newPosition);

				// Call API method and log event
				self.onPositionUpdate.call(self, event);
				if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_POSITION_UPDATED, 'updatePosition');
			};

			return self;
			},

			updateWidth: function(newWidth)
			{
			// Make sure tooltip is rendered and if not, return
			if(!self.status.rendered)
			{
				if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'updateWidth');
				return self;
			};

			// Make sure supplied width is a number and if not, return
			if(newWidth && typeof newWidth !== 'number')
			{
				if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, 'newWidth must be of type number', 'updateWidth');
				return self;
			};

			// Calculate the new width if one is not supplied
			if(!newWidth)
			{
				// Explicit width is set
				if(typeof self.options.style.width.value == 'number')
					newWidth = self.options.style.width.value;

				// No width is set, proceed with auto detection
				else
				{
					// Set width to auto initally to determine new width and hide other elements
					self.elements.tooltip.css({ width: 'auto' });
					self.elements.contentWrapper.siblings().add(self.elements.tip).hide();

					// Set position and zoom to defaults to prevent IE hasLayout bug
					if($.browser.msie)
						self.elements.wrapper.css({ position: 'static', zoom: 'normal' });

					// Set the new width
					newWidth = self.getDimensions().width + 1;

					// Make sure its within the maximum and minimum width boundries
					if(!self.options.style.width.value)
					{
						if(newWidth > self.options.style.width.max) newWidth = self.options.style.width.max
						if(newWidth < self.options.style.width.min) newWidth = self.options.style.width.min
					};
				};
			};

			// Adjust newWidth by 1px if width is odd (IE6 rounding bug fix)
			if(newWidth % 2 !== 0) newWidth -= 1;

			// Set the new calculated width and unhide other elements
			self.elements.tooltip.width(newWidth);
			self.elements.contentWrapper.siblings().add(self.elements.tip).show();

			// Set the border width, if enabled
			if(self.options.style.border.radius)
			{
				self.elements.tooltip.find('.qtip-betweenCorners').each(function(i)
				{
					$(this).width(newWidth - (self.options.style.border.radius * 2));
				})
			};

			// IE only adjustments
			if($.browser.msie)
			{
				// Reset position and zoom to give the wrapper layout (IE hasLayout bug)
				self.elements.wrapper.css({ position: 'relative', zoom: '1' }).width(newWidth);

				// Adjust BGIframe height and width if enabled
				if(self.cache.bgiframe === true) self.elements.bgiframe.width(newWidth).height(dimensions.height);
			}

			// Log event and return
			if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_WIDTH_UPDATED, 'updateWidth');
			return self;
			},

			updateStyle: function(name)
			{
			var tip, borders, context, corner, coordinates;

			// Make sure tooltip is rendered and if not, return
			if(!self.status.rendered)
			{
				if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'updateStyle');
				return self;
			};

			// Return if style is not defined or name is not a string
			if(typeof name !== 'string' || !$.fn.qtip.styles[name])
			{
				if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.STYLE_NOT_DEFINED, 'updateStyle');
				return self;
			};

			// Set the new style object
			self.options.style = buildStyle.call(self, $.fn.qtip.styles[name], self.options.user.style);

			// Update initial styles of content and title elements
			self.elements.content.css( jQueryStyle(self.options.style) );
			if(self.options.content.title.text !== false)
				self.elements.title.css( jQueryStyle(self.options.style.title, true) );

			// Update CSS border colour
			self.elements.contentWrapper.css({ borderColor: self.options.style.border.color })

			// Update tip color if enabled
			if(self.options.style.tip.corner !== false)
			{
				if($('<canvas>').get(0).getContext)
				{
					// Retrieve canvas context and clear
					tip = self.elements.tooltip.find('.qtip-tip canvas:first');
					context = tip.get(0).getContext('2d');
					context.clearRect(0,0,300,300);

					// Draw new tip
					corner = tip.parent('div[rel]:first').attr('rel');
					coordinates = calculateTip(corner, self.options.style.tip.size.width, self.options.style.tip.size.height);
					drawTip.call(self, tip, coordinates, self.options.style.tip.color || self.options.style.border.color);
				}
				else if($.browser.msie)
				{
					// Set new fillcolor attribute
					tip = self.elements.tooltip.find('.qtip-tip [nodeName="shape"]');
					tip.attr('fillcolor', self.options.style.tip.color || self.options.style.border.color)
				};
			};

			// Update border colors if enabled
			if(self.options.style.border.radius > 0)
			{
				self.elements.tooltip.find('.qtip-betweenCorners').css({ backgroundColor: self.options.style.border.color })

				if($('<canvas>').get(0).getContext)
				{
					borders = calculateBorders(self.options.style.border.radius)
					self.elements.tooltip.find('.qtip-wrapper canvas').each(function()
					{
						// Retrieve canvas context and clear
						context = $(this).get(0).getContext('2d');
						context.clearRect(0,0,300,300);

						// Draw new border
						corner = $(this).parent('div[rel]:first').attr('rel')
						drawBorder.call(self, $(this), borders[corner],
						self.options.style.border.radius, self.options.style.border.color);
					});
				}
				else if($.browser.msie)
				{
					// Set new fillcolor attribute on each border corner
					self.elements.tooltip.find('.qtip-wrapper [nodeName="arc"]').each(function()
					{
						$(this).attr('fillcolor', self.options.style.border.color)
					});
				};
			};

			// Log event and return
			if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_STYLE_UPDATED, 'updateStyle');
			return self;
			},

			updateContent: function(content, reposition)
			{
			var parsedContent, images, loadedImages;

			// Make sure tooltip is rendered and if not, return
			if(!self.status.rendered)
			{
				if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'updateContent');
				return self;
			}

			// Make sure content is defined before update
			else if(!content)
			{
				if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, 'You must specify some content with which to update', 'updateContent');
				return false;
			}

			// Call API method and set new content if a string is returned
			parsedContent = self.beforeContentUpdate.call(self, content);
			if(typeof parsedContent == 'string') content = parsedContent;
			else if(parsedContent === false) return;

			// Append new content if its a DOM array and show it if hidden
			if(content.jquery && content.length > 0)
				self.elements.content.append(content.clone(true).show());

			// Content is a regular string, insert the new content
			else self.elements.content.html(content);

			// Check if images need to be loaded before position is updated to prevent mis-positioning
			images = self.elements.content.find('img');
			if(images.length > 0)
			{
				loadedImages = 0;
				images.each(function(i)
				{
					$('<img src="'+ $(this).attr('src') +'" />')
						.load(function(){ if(++loadedImages == images.length) afterLoad(); });
				});
			}
			else afterLoad();

			function afterLoad()
			{
				// Update the tooltip width
				self.updateWidth();

				// If repositioning is enabled, update positions
				if(reposition !== false)
				{
					self.updatePosition(self.elements.tooltip.is(':visible'), true);
					if(self.options.style.tip.corner !== false) positionTip.call(self);
				};
			};

			// Call API method and log event
			self.onContentUpdate.call(self);
			if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_CONTENT_UPDATED, 'loadContent');

			return self;
			},

			loadContent: function(url, data, method)
			{
				var returned;

				// Make sure tooltip is rendered and if not, return
				if(!self.status.rendered)
				{
					if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'loadContent');
					return self;
				};

				// Call API method and if return value is false, halt
				returned = self.beforeContentLoad.call(self);
				if(returned === false) return self;

				// Load content using specified request type
				if(method == 'post')
					$.post(url, data, setupContent);
				else
					$.get(url, data, setupContent);

				function setupContent(content)
				{
					// Update the content
					self.updateContent(content);

					// Call API method and log event
					self.onContentLoad.call(self);
					if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_CONTENT_LOADED, 'loadContent');
				};

				return self;
			},

			focus: function(event)
			{
				var curIndex, newIndex, returned;

				// Make sure tooltip is rendered and if not, return
				if(!self.status.rendered)
				{
					if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'focus');
					return self;
				};

				// Set z-index variables
				curIndex = parseInt( self.elements.tooltip.css('z-index') );
				newIndex = 100 + $('.qtip').length - 1;

				// Only update the z-index if it has changed and tooltip is not already focused
				if(!self.status.focussed && curIndex !== newIndex)
				{
					// Call API method and if return value is false, halt
					returned = self.beforeFocus.call(self, event);
					if(returned === false) return self;

					// Reduce all other tooltip z-index by 1
					$('.qtip').not(self.elements.tooltip).each(function()
					{
						$(this).css({ zIndex: parseInt( $(this).css('z-index') ) - 1 });

						if($(this).qtip('api').status.rendered === true)
							$(this).qtip('api').status.focused = false;
					})

					// Set the new z-index and set focus status to true
					self.elements.tooltip.css({ zIndex: newIndex });
					self.status.focused = true;

					// Call API method and log event
					self.onFocus.call(self, event);
					if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_FOCUSED, 'focus');
				};

				return self;
			},

			disable: function(state)
			{
				// Make sure tooltip is rendered and if not, return
				if(!self.status.rendered)
				{
					if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'disable');
					return self;
				};

				if(state)
				{
					// Tooltip is not already disabled, proceed
					if(!self.status.disabled)
					{
						// Set the disabled flag and log event
						self.status.disabled = true;
						if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_DISABLED, 'disable');
					}

					// Tooltip is already disabled, inform user via log
					else if($.fn.qtip.debug)  $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.TOOLTIP_ALREADY_DISABLED, 'disable');
				}
				else
				{
					// Tooltip is not already enabled, proceed
					if(self.status.disabled)
					{
						// Reassign events, set disable status and log
						self.status.disabled = false;
						if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_ENABLED, 'disable');
					}

					// Tooltip is already enabled, inform the user via log
					else if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.TOOLTIP_ALREADY_ENABLED, 'disable');
				};

				return self;
			},

			destroy: function()
			{
				var i, returned, interfaces;

				// Call API method and if return value is false, halt
				returned = self.beforeDestroy.call(self);
				if(returned === false) return self;

				// Make sure tooltip is rendered and if not, return
				if(self.status.rendered)
				{
					// Remove event handlers and remove element
					self.options.show.when.target.unbind('mousemove.qtip', self.updatePosition);
					self.options.show.when.target.unbind('mouseout.qtip', self.hide);
					self.options.show.when.target.unbind(self.options.show.when.event + '.qtip');
					self.options.show.when.target.removeData("qtip-toggle");
					self.options.hide.when.target.unbind(self.options.hide.when.event + '.qtip');
					self.elements.tooltip.unbind(self.options.hide.when.event + '.qtip');
					self.elements.tooltip.unbind('mouseover.qtip', self.focus);
					self.elements.tooltip.remove();
				};

				// Remove API references and restore last qTip id if present
				interfaces = self.elements.target.data('interfaces');
				if(typeof interfaces == 'object' && interfaces.length > 0)
				{
					// Remove API from interfaces array
					for(i = 0; i < interfaces.length - 1; i++)
						if(interfaces[i].id == self.id) interfaces.splice(i, 1)
				}
				delete $.fn.qtip.interfaces[self.id];

				// Set qTip data to previous tooltips API if available
				if(typeof interfaces == 'object' && interfaces.length > 0)
					self.elements.target.data('qtip', interfaces[interfaces.length -1]);
				else
					self.elements.target.removeData('qtip');

				// Call API method and log destroy
				self.onDestroy.call(self);
				if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_DESTROYED, 'destroy');

				return self.elements.target;
			},

			getPosition: function()
			{
				var show, offset;

				// Make sure tooltip is rendered and if not, return
				if(!self.status.rendered)
				{
					if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'getPosition');
					return self;
				};

				show = (self.elements.tooltip.css('display') !== 'none') ? false : true;

				// Show and hide tooltip to make sure coordinates are returned
				if(show) self.elements.tooltip.css({ visiblity: 'hidden' }).show();
				offset = self.elements.tooltip.offset();
				if(show) self.elements.tooltip.css({ visiblity: 'visible' }).hide();

				return offset;
			},

			getDimensions: function()
			{
				var show, dimensions;

				// Make sure tooltip is rendered and if not, return
				if(!self.status.rendered)
				{
					if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'getDimensions');
					return self;
				};

				show = (!self.elements.tooltip.is(':visible')) ? true : false;

				// Show and hide tooltip to make sure dimensions are returned
				if(show) self.elements.tooltip.css({ visiblity: 'hidden' }).show();
				dimensions = {
					height: self.elements.tooltip.outerHeight(),
					width: self.elements.tooltip.outerWidth()
				};
				if(show) self.elements.tooltip.css({ visiblity: 'visible' }).hide();

				return dimensions;
			}
		});

		// Create the tooltip
		construct.call(self);
	};

	// Define priamry construct function
	function construct()
	{
		var self, showTarget, showEvent;
		self = this;

		// If prerendering is disabled, create tooltip on showEvent
		if(self.options.content.prerender === false
		&& self.options.show.when.event !== false
		&& self.options.show.ready !== true)
		{
			showTarget = self.options.show.when.target;
			showEvent = self.options.show.when.event;

			showTarget.bind(showEvent+'.qtip-create', function(event)
			{
				// Unbind show event and render the tooltip
				showTarget.unbind(showEvent+'.qtip-create');
				render();

				// Start event sequence
				self.cache.mouse = { x: event.pageX, y: event.pageY };
				showTarget.trigger(showEvent);
			});
		}

		// Prerendering is enabled, create tooltip straight away
		else render();

		function render()
		{
			var adjust, content, url, data, method, tempLength;

			// Set rendered status to true
			self.status.rendered = true;

			// Call API method
			self.beforeRender.call(self);

			// Create initial tooltip elements
			/*self.elements.tooltip =  '<div qtip="'+self.id+'" ' +
			'class="qtip '+(self.options.style.classes.tooltip || self.options.style)+'"' +
			'style="display:none; -moz-border-radius:0; -webkit-border-radius:0; border-radius:0;' +
			'position:'+self.options.position.type+'; z-index:'+(6000 + $('.qtip').length)+';">' +
			'  <div class="qtip-wrapper" style="position:relative; overflow:hidden; text-align:left;">' +
			'    <div class="qtip-contentWrapper" style="overflow:hidden;">' +
			'       <div class="qtip-content '+self.options.style.classes.content+'"></div>' +
			'</div></div></div>';*/
			self.elements.tooltip =  '<div qtip="'+self.id+'" ' +
			'class="qtip '+(self.options.style.classes.tooltip || self.options.style)+'"' +
			'style="display:none;' +
			'position:'+self.options.position.type+'; z-index:'+(6000 + $('.qtip').length)+';">' +
			'  <div class="qtip-wrapper" style="position:relative; text-align:left;">' +
			'    <div class="qtip-contentWrapper" style="">' +
			'       <div class="qtip-content '+self.options.style.classes.content+'"></div>' +
			'</div></div></div>';

			// Set API data and append to container element
			self.elements.tooltip = $(self.elements.tooltip);
			self.elements.tooltip.appendTo(self.options.position.container).data('qtip', self);

			// Setup element references
			self.elements.wrapper = self.elements.tooltip.children('div:first');
			self.elements.contentWrapper = self.elements.wrapper.children('div:first');
			self.elements.content = self.elements.contentWrapper.children('div:first').css( jQueryStyle(self.options.style) );

			// Fix IE specific bugs
			if($.browser.msie)
			{
				// hasLayout bug - http://www.satzansatz.de/cssd/onhavinglayout.html
				$(self.elements.wrapper).css({ zoom: '1' });

				// Internet Explorer 6 ONLY
				if(parseInt($.browser.version.charAt(0)) == 6)
				{
					// Move content up 2 pixels to prevent gap bug when using border radius
					adjust = (self.options.style.border.radius > 4) ? 1 : 0;
					adjust += (self.options.style.border.width > 4) ? 1 : 0;
					self.elements.content.css({ marginTop: -adjust });
				};
			};

			// If an explicit width is set, updateWidth prior to setting content to prevent dirty rendering
			if(typeof self.options.style.width.value == 'number') self.updateWidth();

			// Create borders and tips if supported by the browser
			if($('<canvas>').get(0).getContext || $.browser.msie)
			{
				createBorder.call(self);
				createTip.call(self);
			}

			// Neither canvas or VML is supported, tips and borders cannot be drawn!
			else
			{
				// Set defined border width
				//self.elements.contentWrapper.css({ border: self.options.style.border.width+'px solid '+self.options.style.border.color  });

				// Reset border radius and tip
				self.options.style.border.radius = 0;
				self.options.style.tip.corner = false;

				// Inform via log
				if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.CANVAS_VML_NOT_SUPPORTED, 'render');
			};

			// Use the provided content string
			if(typeof self.options.content.text == 'string')
				content = self.options.content.text;

			// Use the DOM array provided for the content
			else if(self.options.content.jquery && self.options.content.length > 0)
				content = self.options.content;

			// Use title attribute of target element for content
			else if(self.options.content.text === false)
			{
				content = self.elements.target.attr('title').replace("\\n", '<br />');
				self.elements.target.attr('title', ''); // Remove title attribute to prevent default tooltip showing
			}

			// No valid content was provided, inform via log
			else
			{
				content = '&nbsp;';
				if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.NO_VALID_CONTENT, 'render');
			};

			// Set the tooltips content and create title if enabled
			self.updateContent(content);
			if(self.options.content.title.text !== false) createTitle.call(self);

			// Retrieve ajax content if provided
			if(self.options.content.url !== false)
			{
				url = self.options.content.url;
				data = self.options.content.data;
				method = self.options.content.method || 'get';
				self.loadContent(url, data, method);
			};

			// Assign events and toggle tooltip
			assignEvents.call(self);
			if(self.options.show.ready === true) self.show();

			// Call API method and log event
			self.onRender.call(self);
			if($.fn.qtip.debug) $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_RENDERED, 'render');
		};
	};

	// Create borders using canvas and VML
	function createBorder()
	{
		var self, i, width, radius, color, coordinates, containers, size, betweenWidth, betweenCorners, borderTop, borderBottom, borderCoord, sideWidth, vertWidth;
		self = this;

		// Destroy previous border elements, if present
		self.elements.wrapper.find('.qtip-borderBottom, .qtip-borderTop').remove();

		// Setup local variables
		width = self.options.style.border.width;
		radius = self.options.style.border.radius;
		color = self.options.style.border.color || self.options.style.tip.color;

		// Calculate border coordinates
		coordinates = calculateBorders(radius);

		// Create containers for the border shapes
		containers = {};
		for(i in coordinates)
		{
			// Create shape container
			containers[i] = '<div rel="'+i+'" style="'+((i.search(/Left/) !== -1) ? 'left' : 'right') + ':0; ' +
			'position:absolute; height:'+radius+'px; width:'+radius+'px; overflow:hidden; line-height:0.1px; font-size:1px">';

			// Canvas is supported
			if($('<canvas>').get(0).getContext)
			containers[i] += '<canvas height="'+radius+'" width="'+radius+'" style="vertical-align: top"></canvas>';

			// No canvas, but if it's IE use VML
			else if($.browser.msie)
			{
			size = radius * 2 + 3;
			containers[i] += '<v:arc stroked="false" fillcolor="'+color+'" startangle="'+coordinates[i][0]+'" endangle="'+coordinates[i][1]+'" ' +
				'style="width:'+size+'px; height:'+size+'px; margin-top:'+((i.search(/bottom/) !== -1) ? -2 : -1)+'px; ' +
				'margin-left:'+((i.search(/Right/) !== -1) ? coordinates[i][2] - 3.5 : -1)+'px; ' +
				'vertical-align:top; display:inline-block; behavior:url(#default#VML)"></v:arc>';

			};

			containers[i] += '</div>';
		};

		// Create between corners elements
		betweenWidth = self.getDimensions().width - (Math.max(width, radius) * 2);
		betweenCorners = '<div class="qtip-betweenCorners" style="height:'+radius+'px; width:'+betweenWidth+'px; ' +
			'overflow:hidden; background-color:'+color+'; line-height:0.1px; font-size:1px;">';

		// Create top border container
		/*borderTop = '<div class="qtip-borderTop" dir="ltr" style="height:'+radius+'px; ' +
			'margin-left:'+radius+'px; line-height:0.1px; font-size:1px; padding:0;">' +
			containers['topLeft'] + containers['topRight'] + betweenCorners;*/

		borderTop = '<div class="qtip-borderTop" dir="ltr" style="line-height:0.1px; font-size:1px; padding:0;">';
		self.elements.wrapper.prepend(borderTop);

		// Create bottom border container
		/*borderBottom = '<div class="qtip-borderBottom" dir="ltr" style="height:'+radius+'px; ' +
			'margin-left:'+radius+'px; line-height:0.1px; font-size:1px; padding:0;">' +
			containers['bottomLeft'] + containers['bottomRight'] + betweenCorners;*/
		borderBottom = '<div class="qtip-borderBottom" dir="ltr" style="line-height:0.1px; font-size:1px; padding:0;">';
		self.elements.wrapper.append(borderBottom);

		// Draw the borders if canvas were used (Delayed til after DOM creation)
		if($('<canvas>').get(0).getContext)
		{
			self.elements.wrapper.find('canvas').each(function()
			{
			borderCoord = coordinates[ $(this).parent('[rel]:first').attr('rel') ];
			drawBorder.call(self, $(this), borderCoord, radius, color);
			})
		};

		// Setup container
		sideWidth = Math.max(radius, (radius + (width - radius)) )
		vertWidth = Math.max(width - radius, 0);
		self.elements.contentWrapper.css({
			border: '0px solid ' + color,
			borderWidth: vertWidth + 'px ' + sideWidth + 'px'
		})
	};

	// Border canvas draw method
	function drawBorder(canvas, coordinates, radius, color)
	{
		// Create corner
		var context = canvas.get(0).getContext('2d');
		context.fillStyle = color;
		context.beginPath();
		context.arc(coordinates[0], coordinates[1], radius, 0, Math.PI * 2, false);
		context.fill();
	};

	// Create tip using canvas and VML
	function createTip(corner)
	{
		var self, color, coordinates, coordsize, path;
		self = this;

		// Destroy previous tip, if there is one
		if(self.elements.tip !== null) self.elements.tip.remove();

		// Setup color and corner values
		color = self.options.style.tip.color || self.options.style.border.color;
		if(self.options.style.tip.corner === false) return;
		else if(!corner) corner = self.options.style.tip.corner;

		// Calculate tip coordinates
		coordinates = calculateTip(corner, self.options.style.tip.size.width, self.options.style.tip.size.height);

		// Create tip element
		/*self.elements.tip =  '<div class="'+self.options.style.classes.tip+'" dir="ltr" rel="'+corner+'" style="position:absolute; ' +
			'height:'+self.options.style.tip.size.height+'px; width:'+self.options.style.tip.size.width+'px; ' +
			'margin:0 auto; line-height:0.1px; font-size:1px;">';*/

		// Use canvas element if supported
		if($('<canvas>').get(0).getContext) {
			//self.elements.tip += '<canvas height="'+self.options.style.tip.size.height+'" width="'+self.options.style.tip.size.width+'"></canvas>';

		// Canvas not supported - Use VML (IE)
		} else if($.browser.msie)
		{
			// Create coordize and tip path using tip coordinates
			coordsize = self.options.style.tip.size.width + ',' + self.options.style.tip.size.height;
			path = 'm' + coordinates[0][0] + ',' + coordinates[0][1];
			path += ' l' + coordinates[1][0] + ',' + coordinates[1][1];
			path += ' ' + coordinates[2][0] + ',' + coordinates[2][1];
			path += ' xe';

			// Create VML element
			self.elements.tip += '<v:shape fillcolor="'+color+'" stroked="false" filled="true" path="'+path+'" coordsize="'+coordsize+'" ' +
			'style="width:'+self.options.style.tip.size.width+'px; height:'+self.options.style.tip.size.height+'px; ' +
			'line-height:0.1px; display:inline-block; behavior:url(#default#VML); ' +
			'vertical-align:'+((corner.search(/top/) !== -1) ? 'bottom' : 'top')+'"></v:shape>';

			// Create a phantom VML element (IE won't show the last created VML element otherwise)
			self.elements.tip += '<v:image style="behavior:url(#default#VML);"></v:image>';

			// Prevent tooltip appearing above the content (IE z-index bug)
			self.elements.content.css('position', 'relative');
		};

		// Attach new tip to tooltip element
		self.elements.tooltip.prepend(self.elements.tip + '</div>');

		// Create element reference and draw the canvas tip (Delayed til after DOM creation)
		self.elements.tip = self.elements.tooltip.find('.'+self.options.style.classes.tip).eq(0);
		if($('<canvas>').get(0).getContext)
			drawTip.call(self, self.elements.tip.find('canvas:first'), coordinates, color);

		// Set the tip position
		positionTip.call(self, corner);
	};

	// Canvas tip drawing method
	function drawTip(canvas, coordinates, color)
	{
		// Setup properties
		var context = canvas.get(0).getContext('2d');
		context.fillStyle = color;

		// Create tip
		context.beginPath();
		context.moveTo(coordinates[0][0], coordinates[0][1]);
		context.lineTo(coordinates[1][0], coordinates[1][1]);
		context.lineTo(coordinates[2][0], coordinates[2][1]);
		context.fill();
	};

	function positionTip(corner)
	{
		var self, ieAdjust, paddingCorner, paddingSize, currentMargin, newMargin;
		self = this;

		// Return if tips are disabled or tip is not yet rendered
		if(self.options.style.tip.corner === false || !self.elements.tip) return;
		if(!corner) corner = self.elements.tip.attr('rel');

		// Setup adjustment variables
		ieAdjust = positionAdjust = ($.browser.msie) ? 1 : 0;

		// Set initial position
		self.elements.tip.css(corner.match(/left|right|top|bottom/)[0], 0);

		// Set position of tip to correct side
		if(corner.search(/top|bottom/) !== -1)
		{
			// Adjustments for IE6 - 0.5px border gap bug
			if($.browser.msie)
			{
			if(parseInt($.browser.version.charAt(0)) === 6)
				positionAdjust = (corner.search(/top/) !== -1) ? -3 : 1;
			else
				positionAdjust = (corner.search(/top/) !== -1) ? 1 : 2;
			};

			if(corner.search(/Middle/) !== -1)
			self.elements.tip.css({ left: '50%', marginLeft: -(self.options.style.tip.size.width / 2) });

			else if(corner.search(/Left/) !== -1)
			self.elements.tip.css({ left: self.options.style.border.radius - ieAdjust });

			else if(corner.search(/Right/) !== -1)
			self.elements.tip.css({ right: self.options.style.border.radius + ieAdjust });

			if(corner.search(/top/) !== -1)
			self.elements.tip.css({ top: -positionAdjust });
			else
			self.elements.tip.css({ bottom: positionAdjust });

		}
		else if(corner.search(/left|right/) !== -1)
		{
			// Adjustments for IE6 - 0.5px border gap bug
			if($.browser.msie)
			positionAdjust = (parseInt($.browser.version.charAt(0)) === 6) ? 1 : ((corner.search(/left/) !== -1) ? 1 : 2);

			if(corner.search(/Middle/) !== -1)
			self.elements.tip.css({ top: '50%', marginTop: -(self.options.style.tip.size.height / 2) });

			else if(corner.search(/Top/) !== -1)
			self.elements.tip.css({ top: self.options.style.border.radius - ieAdjust });

			else if(corner.search(/Bottom/) !== -1)
			self.elements.tip.css({ bottom: self.options.style.border.radius + ieAdjust });

			if(corner.search(/left/) !== -1)
			self.elements.tip.css({ left: -positionAdjust });
			else
			self.elements.tip.css({ right: positionAdjust });
		};

		// Adjust tooltip padding to compensate for tip
		//paddingCorner = 'padding-' + corner.match(/left|right|top|bottom/)[0];
		// paddingSize = self.options.style.tip.size[ (paddingCorner.search(/left|right/) !== -1) ? 'width' : 'height' ];
		//self.elements.tooltip.css(paddingCorner, paddingSize);

		// Match content margin to prevent gap bug in IE6 ONLY
		if($.browser.msie && parseInt($.browser.version.charAt(0)) == 6)
		{
			currentMargin = parseInt(self.elements.tip.css('margin-top'));
			newMargin = currentMargin + parseInt(self.elements.content.css('margin-top'))
			self.elements.tip.css({ marginTop: newMargin });
		};
	};

	// Create title bar for content
	function createTitle()
	{
		var self = this;

		// Destroy previous title element, if present
		if(self.elements.title !== null) self.elements.title.remove();

		// Create title element
		self.elements.title = $('<div>')
			.addClass(self.options.style.classes.title)
			.css( jQueryStyle(self.options.style.title, true) )
			.html(self.options.content.title.text)
			.prependTo(self.elements.contentWrapper);

		// Create title close buttons if enabled
		if(self.options.content.title.button !== false
		&& typeof self.options.content.title.button == 'string')
		{
			$('<a href="#" style="float:right; position:relative;">').attr('href', '#')
			.addClass(self.options.style.classes.button)
			.html(self.options.content.title.button)
			.prependTo(self.elements.title)
			.click(function(){ if(!self.status.disabled) self.hide() });
		};
	};

	// Assign hide and show events
	function assignEvents()
	{
		var self, showTarget, hideTarget, inactiveEvents;
		self = this;

		// Setup event target variables
		showTarget = self.options.show.when.target;
		hideTarget = self.options.hide.when.target;

		// Add tooltip as a hideTarget is its fixed
		if(self.options.hide.fixed) hideTarget = hideTarget.add(self.elements.tooltip);

		// Check if the hide event is special 'inactive' type
		if(self.options.hide.when.event == 'inactive')
		{
			// Define events which reset the 'inactive' event handler
			inactiveEvents = ['click', 'dblclick', 'mousedown', 'mouseup', 'mousemove',
			'mouseout', 'mouseenter', 'mouseleave', 'mouseover' ];

			// Define 'inactive' event timer method
			function inactiveMethod()
			{
			if(self.status.disabled === true) return;

			//Clear and reset the timer
			clearTimeout(self.timers.inactive);
			self.timers.inactive = setTimeout(function()
			{
				// Unassign 'inactive' events
				$(inactiveEvents).each(function()
				{
					hideTarget.unbind(this+'.qtip-inactive');
					self.elements.content.unbind(this+'.qtip-inactive');
				});

				// Hide the tooltip
				self.hide();
			}
			, self.options.hide.delay);
			};
		}

		// Check if the tooltip is 'fixed'
		else if(self.options.hide.fixed === true)
		{
			self.elements.tooltip.bind('mouseover.qtip', function()
			{
			if(self.status.disabled === true) return;

			// Reset the hide timer
			clearTimeout(self.timers.hide);
			});
		};

		// Define show event method
		function showMethod(event)
		{
			if(self.status.disabled === true) return;

			// If set, hide tooltip when inactive for delay period
			if(self.options.hide.when.event == 'inactive')
			{
			// Assign each reset event
			$(inactiveEvents).each(function()
			{
				hideTarget.bind(this+'.qtip-inactive', inactiveMethod);
				self.elements.content.bind(this+'.qtip-inactive', inactiveMethod);
			});

			// Start the inactive timer
			inactiveMethod();
			};

			// Clear hide timers
			clearTimeout(self.timers.show);
			clearTimeout(self.timers.hide);

			// Start show timer
			self.timers.show = setTimeout(function(){ self.show(event); }, self.options.show.delay);
		};

		// Define hide event method
		function hideMethod(event)
		{
			if(self.status.disabled === true) return;

			// Prevent hiding if tooltip is fixed and event target is the tooltip
			if(self.options.hide.fixed === true
			&& self.options.hide.when.event.search(/mouse(out|leave)/i) !== -1
			&& $(event.relatedTarget).parents('.qtip').length > 0)
			{
			// Prevent default and popagation
			event.stopPropagation();
			event.preventDefault();

			// Reset the hide timer
			clearTimeout(self.timers.hide);
			return false;
			};

			// Clear timers
			clearTimeout(self.timers.show);
			clearTimeout(self.timers.hide);

			// Start hide timer
			self.timers.hide = setTimeout(function(){ self.hide(event); }, self.options.hide.delay);
		};

		// Both events and targets are identical, apply events using a toggle
		if((self.options.show.when.target.add(self.options.hide.when.target).length === 1
		&& self.options.show.when.event == self.options.hide.when.event
		&& self.options.hide.when.event !== 'inactive')
		|| self.options.hide.when.event == 'unfocus')
		{
			showTarget.data('qtip-toggle', 0);

			// Bind 'unfocus' method if enabled
			if(self.options.hide.when.event == 'unfocus')
			self.elements.tooltip.attr('unfocus', true);

			// Use a toggle to prevent hide/show conflicts
			showTarget.bind(self.options.show.when.event + '.qtip', function(event)
			{
			if(parseInt($(this).data('qtip-toggle')) === 0)
				showMethod(event);
			else
				hideMethod(event);
			});
		}

		// Events are not identical, bind normally
		else
		{
			showTarget.bind(self.options.show.when.event + '.qtip', showMethod);

			// If the hide event is not 'inactive', bind the hide method
			if(self.options.hide.when.event !== 'inactive')
			hideTarget.bind(self.options.hide.when.event + '.qtip', hideMethod);
		};

		// Focus the tooltip on mouseover
		if(self.options.position.type.search(/(fixed|absolute)/) !== -1)
			self.elements.tooltip.bind('mouseover.qtip', self.focus);

		// If mouse is the target, update tooltip position on mousemove
		if(self.options.position.target === 'mouse')
		{
			showTarget.bind('mousemove.qtip', function(event)
			{
			// Set the new mouse positions if adjustment is enabled
			self.cache.mouse = { x: event.pageX, y: event.pageY };

			// Update the tooltip position only if the tooltip is visible and adjustment is enabled
			if(self.status.disabled === false
			&& self.options.position.adjust.mouse === true
			&& self.elements.tip.css('display') !== 'none')
				self.updatePosition(event);
			});
		};
	};

	// Screen position adjustment
	function screenAdjust(position, target, tooltip)
	{
		var self, adjustedPosition, newCorner, overflow, corner;
		self = this;

		// Setup corner and adjustment variable
		if(tooltip.corner == 'center') return target.position // TODO: 'center' corner adjustment
		adjustedPosition = $.extend({}, position);
		newCorner = { x: false, y: false };

		// Define overflow properties
		overflow = {
			left: (adjustedPosition.left < $.fn.qtip.cache.screen.scroll.left),
			right: (adjustedPosition.left + tooltip.dimensions.width + 2 >= $.fn.qtip.cache.screen.width + $.fn.qtip.cache.screen.scroll.left),
			top: (adjustedPosition.top < $.fn.qtip.cache.screen.scroll.top),
			bottom: (adjustedPosition.top + tooltip.dimensions.height + 2 >= $.fn.qtip.cache.screen.height + $.fn.qtip.cache.screen.scroll.top)
		};

		// Determine new positioning properties
		self.cache.position.adjust = {
			left: (overflow.left && (tooltip.corner.search(/right/i) != -1 || (tooltip.corner.search(/right/i) == -1 && !overflow.right))),
			right: (overflow.right && (tooltip.corner.search(/left/i) != -1 || (tooltip.corner.search(/left/i) == -1 && !overflow.left))),
			top: (overflow.top && tooltip.corner.search(/top/i) == -1),
			bottom: (overflow.bottom && tooltip.corner.search(/bottom/i) == -1)
		};

		// Tooltip overflows off the left side of the screen
		if(self.cache.position.adjust.left)
		{
			if(self.options.position.target !== 'mouse')
			adjustedPosition.left = target.position.left + target.dimensions.width;
			else
			adjustedPosition.left = self.cache.mouse.x

			newCorner.x = 'Left';
		}

		// Tooltip overflows off the right side of the screen
		else if(self.cache.position.adjust.right)
		{
			if(self.options.position.target !== 'mouse')
			adjustedPosition.left = target.position.left - tooltip.dimensions.width;
			else
			adjustedPosition.left = self.cache.mouse.x - tooltip.dimensions.width;

			newCorner.x = 'Right';
		};

		// Tooltip overflows off the top of the screen
		if(self.cache.position.adjust.top)
		{
			if(self.options.position.target !== 'mouse')
			adjustedPosition.top = target.position.top + target.dimensions.height;
			else
			adjustedPosition.top = self.cache.mouse.y

			newCorner.y = 'top';
		}

		// Tooltip overflows off the bottom of the screen
		else if(self.cache.position.adjust.bottom)
		{
			if(self.options.position.target !== 'mouse')
			adjustedPosition.top = target.position.top - tooltip.dimensions.height;
			else
			adjustedPosition.top = self.cache.mouse.y - tooltip.dimensions.height;

			newCorner.y = 'bottom';
		};

		// Don't adjust if resulting position is negative
		if(adjustedPosition.left < 0)
		{
			adjustedPosition.left = position.left;
			newCorner.x = false;
		};
		if(adjustedPosition.top < 0)
		{
			adjustedPosition.top = position.top;
			newCorner.y = false;
		};

		// Change tip corner if positioning has changed and tips are enabled
		if(self.options.style.tip.corner !== false)
		{
			// Determine new corner properties
			corner = new String(tooltip.corner);
			if(newCorner.x !== false) corner = corner.replace(/Left|Right|Middle/, newCorner.x);
			if(newCorner.y !== false) corner = corner.replace(/top|bottom/, newCorner.y);

			// Adjust tip if position has changed and tips are enabled
			if(corner !== self.elements.tip.attr('rel')) createTip.call(self, corner);
		};

		return adjustedPosition;
	};

	// Build a jQuery style object from supplied style object
	function jQueryStyle(style, title)
	{
		var styleObj, i;

		styleObj = $.extend(true, {}, style);
		for(i in styleObj)
		{
			if(title === true && i.search(/(tip|classes)/i) !== -1)
			delete styleObj[i];
			else if(i.search(/(width|border|tip|title|classes|user)/i) !== -1)
			delete styleObj[i];
		};

		return styleObj;
	};

	// Sanitize styles
	function sanitizeStyle(style)
	{
		if(typeof style.tip !== 'object') style.tip = { corner: style.tip };
		if(typeof style.tip.size !== 'object') style.tip.size = { width: style.tip.size, height: style.tip.size };
		if(typeof style.border !== 'object') style.border = { width: style.border };
		if(typeof style.width !== 'object') style.width = { value: style.width };
		if(typeof style.width.max == 'string') style.width.max = parseInt(style.width.max.replace(/([0-9]+)/i, "$1"));
		if(typeof style.width.min == 'string') style.width.min = parseInt(style.width.min.replace(/([0-9]+)/i, "$1"));

		// Convert deprecated x and y tip values to width/height
		if(typeof style.tip.size.x == 'number')
		{
			style.tip.size.width = style.tip.size.x;
			delete style.tip.size.x;
		};
		if(typeof style.tip.size.y == 'number')
		{
			style.tip.size.height = style.tip.size.y;
			delete style.tip.size.y;
		};

		return style;
	};

	// Build styles recursively with inheritance
	function buildStyle()
	{
		var self, i, styleArray, styleExtend, finalStyle, ieAdjust;
		self = this;

		// Build style options from supplied arguments
		styleArray = [true, {}];
		for(i = 0; i < arguments.length; i++)
			styleArray.push(arguments[i]);
		styleExtend = [ $.extend.apply($, styleArray) ];

		// Loop through each named style inheritance
		while(typeof styleExtend[0].name == 'string')
		{
			// Sanitize style data and append to extend array
			styleExtend.unshift( sanitizeStyle($.fn.qtip.styles[ styleExtend[0].name ]) );
		};

		// Make sure resulting tooltip className represents final style
		styleExtend.unshift(true, {classes:{ tooltip: 'qtip-' + (arguments[0].name || 'defaults') }}, $.fn.qtip.styles.defaults);

		// Extend into a single style object
		finalStyle = $.extend.apply($, styleExtend);

		// Adjust tip size if needed (IE 1px adjustment bug fix)
		ieAdjust = ($.browser.msie) ? 1 : 0;
		finalStyle.tip.size.width += ieAdjust;
		finalStyle.tip.size.height += ieAdjust;

		// Force even numbers for pixel precision
		if(finalStyle.tip.size.width % 2 > 0) finalStyle.tip.size.width += 1;
		if(finalStyle.tip.size.height % 2 > 0) finalStyle.tip.size.height += 1;

		// Sanitize final styles tip corner value
		if(finalStyle.tip.corner === true)
			finalStyle.tip.corner = (self.options.position.corner.tooltip === 'center') ? false : self.options.position.corner.tooltip;

		return finalStyle;
	};

	// Tip coordinates calculator
	function calculateTip(corner, width, height)
	{
		// Define tip coordinates in terms of height and width values
		var tips = {
			bottomRight:   [[0,0],              [width,height],      [width,0]],
			bottomLeft:    [[0,0],              [width,0],           [0,height]],
			topRight:      [[0,height],         [width,0],           [width,height]],
			topLeft:       [[0,0],              [0,height],          [width,height]],
			topMiddle:     [[0,height],         [width / 2,0],       [width,height]],
			bottomMiddle:  [[0,0],              [width,0],           [width / 2,height]],
			rightMiddle:   [[0,0],              [width,height / 2],  [0,height]],
			leftMiddle:    [[width,0],          [width,height],      [0,height / 2]]
		};
		tips.leftTop = tips.bottomRight;
		tips.rightTop = tips.bottomLeft;
		tips.leftBottom = tips.topRight;
		tips.rightBottom = tips.topLeft;

		return tips[corner];
	};

	// Border coordinates calculator
	function calculateBorders(radius)
	{
		var borders;

		// Use canvas element if supported
		if($('<canvas>').get(0).getContext)
		{
			borders = {
			topLeft: [radius,radius], topRight: [0,radius],
			bottomLeft: [radius,0], bottomRight: [0,0]
			};
		}

		// Canvas not supported - Use VML (IE)
		else if($.browser.msie)
		{
			borders = {
			topLeft: [-90,90,0], topRight: [-90,90,-radius],
			bottomLeft: [90,270,0], bottomRight: [90, 270,-radius]
			};
		};

		return borders;
	};

	// BGIFRAME JQUERY PLUGIN ADAPTION
	//   Special thanks to Brandon Aaron for this plugin
	//   http://plugins.jquery.com/project/bgiframe
	function bgiframe()
	{
		var self, html;
		self = this;

		// Setup iframe HTML string
		html = '<iframe class="qtip-bgiframe" frameborder="0" tabindex="-1" src="javascript:false" '+
			'style="display:block; position:absolute; z-index:-1; filter:alpha(opacity=\'0\'); />';

		// Append the new HTML and setup element reference
		self.elements.tooltip.prepend(html);
		self.elements.bgiframe = self.elements.tooltip.children('.qtip-bgiframe:first');
	};

	// Adjust positions of the tooltips on window resize or scroll if enabled
	var adjustTimer;
	$(window).bind('resize scroll', function(event)
	{
		clearTimeout(adjustTimer);
		adjustTimer = setTimeout(function()
		{
			// Readjust cached screen values
			if(event.type === 'scroll')
			$.fn.qtip.cache.screen.scroll = { left: $(window).scrollLeft(), top: $(window).scrollTop() };
			else
			{
			$.fn.qtip.cache.screen.width = $(window).width();
			$.fn.qtip.cache.screen.height = $(window).height();
			};

			$('.qtip').each(function()
			{
			// Access current elements API
			var api = $(this).qtip('api');

			// Update position if resize or scroll adjustments are enabled
			if(api.options.position.adjust.scroll && event.type === 'scroll'
			|| api.options.position.adjust.resize && event.type === 'resize')
				api.updatePosition(event, true);
			})
		}
		, 100);
	})

	// Hide unfocus toolipts on document mousedown
	$(document).bind('mousedown.qtip', function(event)
	{
		if($(event.target).parents('div.qtip').length === 0)
		{
			$('.qtip[unfocus]').each(function()
			{
			var api = $(this).qtip("api");

			// Only hide if its visible and not the tooltips target
			if($(this).is(':visible') && !api.status.disabled
			&& $(event.target).add(api.elements.target).length > 1)
				api.hide();
			})
		};
	});

	// Define qTip API interfaces array
	$.fn.qtip.interfaces = []

	// Setup library cache with window scroll and dimensions
	$.fn.qtip.cache = {
		screen: {
			scroll: { left: $(window).scrollLeft(), top: $(window).scrollTop() },
			width: $(window).width(),
			height: $(window).height()
		}
	};

	// Define configuration defaults
	$.fn.qtip.defaults = {
		// Content
		content: {
			prerender: false,
			text: false,
			url: false,
			data: null,
			title: {
				text: false,
				button: false
			}
		},
		// Position
		position: {
			target: false,
			corner: {
				target: 'bottomRight',
				tooltip: 'topLeft'
			},
			adjust: {
				x: 0, y: 0,
				mouse: true,
				screen: false,
				scroll: true,
				resize: true
			},
			type: 'absolute',
			container: false
		},
		// Effects
		show: {
			when: {
				target: false,
				event: 'mouseover'
			},
			effect: {
				type: 'fade',
				length: 100
			},
			delay: 140,
			solo: true,
			ready: false
		},
		hide: {
			when: {
				target: false,
				event: 'mouseout'
			},
			effect: {
				type: 'fade',
				length: 100
			},
			delay: 0,
			fixed: false
		},
		// Callbacks
		api: {
			beforeRender: function(){},
			onRender: function(){},
			beforePositionUpdate: function(){},
			onPositionUpdate: function(){},
			beforeShow: function(){},
			onShow: function(){},
			beforeHide: function(){},
			onHide: function(){},
			beforeContentUpdate: function(){},
			onContentUpdate: function(){},
			beforeContentLoad: function(){},
			onContentLoad: function(){},
			beforeDestroy: function(){},
			onDestroy: function(){},
			beforeFocus: function(){},
			onFocus: function(){}
		}
	};

	$.fn.qtip.styles = {
		defaults: {
			background: 'white',
			color: '#111',
			overflow: '',
			textAlign: 'left',
			width: {
				min: 0,
				max: 250
			},
			padding: '5px 9px',
			border: {
				width: 1,
				radius: 0,
				color: '#d3d3d3'
			},
			tip: {
				corner: false,
				color: false,
				size: { width: 13, height: 13 },
				opacity: 1
			},
			title: {
				background: '#e1e1e1',
				fontWeight: 'bold',
				padding: '7px 12px'
			},
			classes: {
				target: '',
				tip: 'qtip-tip',
				title: 'qtip-title',
				content: 'qtip-content',
				active: 'qtip-active'
			}
		},
		cream: {
			border: {
				width: 3,
				radius: 0,
				color: '#F9E98E'
			},
			title: {
				background: '#F0DE7D',
				color: '#A27D35'
			},
			background: '#FBF7AA',
			color: '#A27D35',

			classes: { tooltip: 'qtip-cream' }
		},
		light: {
			border: {
				width: 3,
				radius: 0,
				color: '#E2E2E2'
			},
			title: {
				background: '#f1f1f1',
				color: '#454545'
			},
			background: 'white',
			color: '#454545',

			classes: { tooltip: 'qtip-light' }
		},
		dark: {
			border: {
				width: 3,
				radius: 0,
				color: '#303030'
			},
			title: {
				background: '#404040',
				color: '#f3f3f3'
			},
			background: '#505050',
			color: '#f3f3f3',

			classes: { tooltip: 'qtip-dark' }
		},
		red: {
			border: {
			width: 3,
			radius: 0,
			color: '#CE6F6F'
			},
			title: {
			background: '#f28279',
			color: '#9C2F2F'
			},
			background: '#F79992',
			color: '#9C2F2F',

			classes: { tooltip: 'qtip-red' }
		},
		green: {
			border: {
			width: 3,
			radius: 0,
			color: '#A9DB66'
			},
			title: {
			background: '#b9db8c',
			color: '#58792E'
			},
			background: '#CDE6AC',
			color: '#58792E',

			classes: { tooltip: 'qtip-green' }
		},
		blue: {
			border: {
			width: 3,
			radius: 0,
			color: '#ADD9ED'
			},
			title: {
			background: '#D0E9F5',
			color: '#5E99BD'
			},
			background: '#E5F6FE',
			color: '#4D9FBF',

			classes: { tooltip: 'qtip-blue' }
		}
	};
})(jQuery);



$.fn.qtip.styles.promotion = { // Last part is the name of the style
	width: 680,
	padding: '0px 30px',
	background: 'transparent',
	color: 'black',
	textAlign: 'left',
	border: {
		width: 0,
		radius: 0,
		color: '#FFFFFF'
	},
	classes: { tooltip: 'qtip-promotion' },
	name: 'green' // Inherit the rest of the attributes from the preset dark style
}

$.fn.qtip.styles.menu = { // Last part is the name of the style
	width: 369,
	padding: '0px 17px',
	background: 'transparent',
	color: 'black',
	textAlign: 'left',
	border: {
		width: 0,
		radius: 0,
		color: '#FFFFFF'
	},
	classes: { tooltip: 'qtip-menu' },
	name: 'green' // Inherit the rest of the attributes from the preset dark style
}

$.fn.qtip.styles.menu_right = { // Last part is the name of the style
	width: 369,
	padding: '0px 17px',
	background: 'transparent',
	color: 'black',
	textAlign: 'left',
	border: {
		width: 0,
		radius: 0,
		color: '#FFFFFF'
	},
	classes: { tooltip: 'qtip-menu_right' },
	name: 'green' // Inherit the rest of the attributes from the preset dark style
}

$.fn.qtip.styles.tooltip1 = { // Last part is the name of the style
	width: 515,
	padding: '0px 25px',
	background: 'transparent',
	color: '#4E2D33',
	textAlign: 'left',
	border: {
		width: 0,
		radius: 0,
		color: '#FFFFFF'
	},
	classes: { tooltip: 'qtip-tooltip1' },
	name: 'green' // Inherit the rest of the attributes from the preset dark style
}

$.fn.qtip.styles.tooltip2 = { // Last part is the name of the style
	width: 355,
	padding: '0px 25px',
	background: 'transparent',
	color: '#4E2D33',
	textAlign: 'left',
	border: {
		width: 0,
		radius: 0,
		color: '#FFFFFF'
	},
	classes: { tooltip: 'qtip-tooltip2' },
	name: 'green' // Inherit the rest of the attributes from the preset dark style
}
