/*!
 * 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;

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

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

      // Calculate tip coordinates
      coordinates = $.fn.qtip.tips.calculate(type.toString(), 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.x+corner.y+'" 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.y+'"></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.contentWrapper.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)
         $.fn.qtip.tips.draw.call(self, self.elements.tip.find('canvas:first'), coordinates, color);

      // 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.position.call(self, corner);
   };

   // Canvas tip drawing method
   $.fn.qtip.tips.draw = function(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();
   };

   $.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;
      if(!corner) corner = self.cache.tip;

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

      // Set initial position
      self.elements.tip.css(corner.x, 0).css(corner.y, 0);

      // 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)
               positionAdjust = (corner.x == 'top') ? -3 : 1;
            else
               positionAdjust = (corner.x == 'top') ? 1 : 2;
         };

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

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

         else if(corner.x == 'right')
            self.elements.tip.css({ right: self.options.style.border.radius + ieAdjust });

         if(corner.y == 'top')
            self.elements.tip.css({ top: -positionAdjust });
         else
            self.elements.tip.css({ bottom: positionAdjust });

      }
      else
      {
         // Adjustments for IE6 - 0.5px border gap bug
         if($.browser.msie)
            positionAdjust = ($.fn.qtip.cache.ie6) ? 1 : (corner.x == 'left' ? 1 : 2);

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

         else if(corner.y == 'top')
            self.elements.tip.css({ top: self.options.style.border.radius - ieAdjust });

         else if(corner.y == 'bottom')
            self.elements.tip.css({ bottom: self.options.style.border.radius + ieAdjust });

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

      // Adjust tooltip padding to compensate for tip
      paddingCorner = 'padding-' + corner[corner.precedance];
      paddingSize = self.options.style.tip.size[ (paddingCorner.search(/left|right/) !== -1) ? 'width' : 'height' ];
      self.elements.tooltip.css('padding', 0);
      self.elements.tooltip.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 });
      };
   };

   // 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);