/*!
* jquery.qtip.tips. The jQuery tooltip plugin - Speech bubble tips component
*
* Copyright (c) 2009 Craig Thompson
* http://craigsworks.com
*
* Licensed under MIT
* http://www.opensource.org/licenses/mit-license.php
*
* Launch	: August 2009
* Version  : TRUNK - NOT FOR USE IN PRODUCTION ENVIRONMENTS!!!!
*
* FOR STABLE VERSIONS VISIT: http://craigsworks.com/projects/qtip/download/
*/
(function($)
{
	$.fn.qtip.tips = {};

	$.fn.qtip.tips.create = function(corner)
	{
		var self = this, color, coordinates, coordsize, path;

		// Setup color, type and corner values
		if(self.options.style.tip.corner === false) return;
		corner = corner || self.options.style.tip.corner;
		type = self.options.style.tip.type || corner;
		size = self.options.style.tip.size;

		// Calculate tip coordinates
		coordinates = $.fn.qtip.tips.calculate(type.toString(), size.width, size.height);

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

		// Create tip element
		self.elements.tip = $('<div class="ui-tooltip-tip ui-widget-content" dir="ltr" rel="'+corner.x+corner.y+'" style="' +
			'height:'+size.height+'px; width:'+size.width+'px;"></div>').prependTo(self.elements.tooltip);

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

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

			// Create VML element and a phantom image (IE won't show the last created VML element otherwise)
			self.elements.tip.append('<v:shape path="'+path+'" coordsize="'+coordsize+'"' +
				' style="width:'+size.width+'px; height:'+size.height+'px; behavior:url(#default#VML); vertical-align:'+corner.y+'">' +
				'		<v:stroke joinstyle="miter" miterlimit="1000" style="behavior:url(#default#VML);" weight="0" on="false"></v:stroke> ' +
				' </v:shape>' +
				'<v:image style="behavior:url(#default#VML);"></v:image>');

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

		// Fix IE small tip bug
		if(corner.y == 'top' && corner.precedance == 'y' && $.browser.msie){
			self.elements.tip.css({ marginTop: 1 });
		}

		// Cache and set the tip position
		self.cache.tip = corner;
		$.fn.qtip.tips.update.call(self);
		$.fn.qtip.tips.position.call(self, corner);
	};

	$.fn.qtip.tips.position = function(corner)
	{
		var self = this, ieAdjust, paddingCorner, paddingSize, newMargin;

		// Return if tips are disabled or tip is not yet rendered
		if(self.options.style.tip.corner === false || !self.elements.tip){ return false; }
		else if(!corner){ corner = self.cache.tip; }

		// Set initial position and  adjustment variable
		self.elements.tip.css(corner.x, 0).css(corner.y, 0);
		ieAdjust = Number($.fn.qtip.cache.ie6);

		// Set position of tip to correct side
		if(corner.precedance == 'y')
		{
			// Adjustments for IE6 - 0.5px border gap bug
			if($.browser.msie)
			{
				if($.fn.qtip.cache.ie6) ieAdjust = (corner.x == 'top') ? -3 : 1;
				else ieAdjust = (corner.x == 'top') ? 1 : 2;
			}

			switch(corner.x)
			{
				case 'middle': self.elements.tip.css({ left: '50%', marginLeft: -(self.options.style.tip.size.width / 2) });
					break;
				case 'left': self.elements.tip.css({ left: -ieAdjust });
					break;
				case 'right': self.elements.tip.css({ right: ieAdjust });
					break;
			}

			if(corner.y == 'top')
				self.elements.tip.css({ top: -ieAdjust });
			else
				self.elements.tip.css({ bottom: ieAdjust });
		}
		else
		{
			// Adjustments for IE6 - 0.5px border gap bug
			if($.browser.msie) ieAdjust = ($.fn.qtip.cache.ie6) ? 1 : (corner.x == 'left' ? 1 : 2);

			switch(corner.y)
			{
				case 'middle': self.elements.tip.css({ top: '50%', marginTop: -(self.options.style.tip.size.height / 2) });
					break;
				case 'top': self.elements.tip.css({ top: -ieAdjust });
					break;
				case 'bottom': self.elements.tip.css({ bottom: ieAdjust });
					break;
			}

			if(corner.x == 'left')
				self.elements.tip.css({ left: -ieAdjust });
			else
				self.elements.tip.css({ right: ieAdjust });
		}

		// Adjust tooltip padding to compensate for tip
		paddingCorner = 'padding-' + corner[corner.precedance];
		paddingSize = self.options.style.tip.size[ corner.precedance === 'x' ? 'width' : 'height' ];
		self.elements.tooltip.css('padding', 0).css(paddingCorner, paddingSize);

		// Match content margin to prevent gap bug in IE6 ONLY
		if($.fn.qtip.cache.ie6)
		{
			newMargin = parseInt(self.elements.tip.css('margin-top')) || 0;
			newMargin += parseInt(self.elements.content.css('margin-top')) || 0;
			self.elements.tip.css({ marginTop: newMargin });
		}
	};

	$.fn.qtip.tips.update = function()
	{
		var self = this, color, context, coordinates;

		// Detect new tip colour and reset background to transparent
		color = self.elements.tip.css('background-color');
		self.options.style.tip.color = (color === 'transparent') ? self.elements.contentWrapper.css('border-top-color') : color;
		self.elements.tip.css('background-color', 'transparent');

		if($('<canvas/>').get(0).getContext)
		{
			// Grab tip coordinates
			coordinates = $.fn.qtip.tips.calculate.call(self, self.cache.tip, self.options.style.tip.size.width, self.options.style.tip.size.height);

			// Setup canvas properties
			context = self.elements.tip.find('canvas').get(0).getContext('2d');
			context.fillStyle = self.options.style.tip.color;
			context.miterLimit = 0;

			// Draw the canvas tip (Delayed til after DOM creation)
			context.clearRect(0,0,3000,3000);
			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.closePath();
			context.fill();
		}

		else if($.browser.msie)
		{
			// Set new fillcolor attribute
			tip = self.elements.tooltip.find('.ui-tooltip-tip [nodeName="shape"]');
			tip.attr('fillcolor', self.options.style.tip.color || self.options.style.border.color);
		}
	}

	// Tip coordinates calculator
	$.fn.qtip.tips.calculate = function(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];
	};
})(jQuery);