/*!
 * jquery.qtip.border. The jQuery tooltip plugin - Border radius component
 *
 * DEPRECATED!!!
 * AS OF THE 1.0 RELEASE THIS SCRIPT IS NOW DEPRECATED.
 * PLEASE USE THE NEW qCorner PLUGIN INSTEAD AVAILABLE HERE:
 *    http://craigsworks.com/projects/qcorner
 *
 * 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/
 */

/*jslint onevar: true, browser: true, forin: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, maxerr: 100 */
/*global window: false, jQuery: false */
(function($)
{
	$.fn.qtip.border = {};

	$.fn.qtip.border.create = function()
	{
		var self = this, i, width, radius, color, coordinates, containers, size, betweenWidth, betweenCorners, borderTop, borderBottom, borderCoord, sideWidth, vertWidth;

		// Destroy previous border elements, if present
		self.elements.wrapper.find('.ui-tooltip-border').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 = $.fn.qtip.border.calculate(radius);

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

			// 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; 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="ui-tooltip-border-between" style="height:'+radius+'px; width:'+betweenWidth+'px; background-color:'+color+';"></div>';

		// Create top border container
		borderTop = '<div class=" ui-tooltip-border ui-tooltip-border-top" dir="ltr" style="height:'+radius+'px; ' +
			'margin-left:'+radius+'px;">' + containers.topleft + containers.topright + betweenCorners + '</div>';
		self.elements.wrapper.prepend(borderTop);

		// Create bottom border container
		borderBottom = '<div class="ui-tooltip-border ui-tooltip-border-bottom" dir="ltr" style="height:'+radius+'px; ' +
			'margin-left:'+radius+'px;">' + containers.bottomleft + containers.bottomright + betweenCorners + '</div>';
		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') ];
				$.fn.qtip.border.draw.call(self, $(this), borderCoord);
			});
		}

		// Create a phantom VML element (IE won't show the last created VML element otherwise)
		else if($.browser.msie) self.elements.tooltip.append('<v:image style="behavior:url(#default#VML);"></v:image>');

		// Setup contentWrapper border
		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
	$.fn.qtip.border.draw = function(canvas, coordinates)
	{
		var self = this;

		// Create corner
		var context = canvas.get(0).getContext('2d');
		context.fillStyle = self.options.style.border.color;
		context.clearRect(0,0,300,300);
		context.beginPath();
		context.arc(coordinates[0], coordinates[1], self.options.style.border.radius, 0, Math.PI * 2, false);
		context.fill();
	};

	$.fn.qtip.border.update = function()
	{
		var self = this;

		// Detect new border colour
		color = self.elements.contentWrapper.css({borderColor: '', borderLeftColor: '' }).css('border-left-color');
		if(color === 'transparent'){ return; }
		else{ self.options.style.border.color = color; }

		// Update fillers between borders
		self.elements.tooltip.find('.ui-tooltip-border-between').css({ backgroundColor: self.options.style.border.color });

		if($('<canvas/>').get(0).getContext)
		{
			borders = $.fn.qtip.border.calculate(self.options.style.border.radius);
			self.elements.tooltip.find('.ui-tooltip-wrapper canvas').each(function()
			{
				// Draw new border canvas
				corner = $(this).parent('div[rel]:first').attr('rel');
				$.fn.qtip.border.draw.call(self, $(this), borders[corner]);
			});
		}

		else if($.browser.msie)
		{
			// Set new fillcolor attribute on each border corner
			self.elements.tooltip.find('.ui-tooltip-wrapper [nodeName="arc"]').each(function()
			{
				$(this).attr('fillcolor', self.options.style.border.color);
			});
		}
	}

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