/*!
 * Fix Display Table Cell jQuery Plugin
 * This plugin emulates display: table-cell in IE < 8 
 *
 * Copyright (c) 2011 Dmitriy Kubyshkin (http://kubyshkin.ru)
 * Licensed under the MIT license
 */
(function($) { // Making $ accessible even in noConflict
  $.fn.fixDisplayTableCell = function(options){
    options = jQuery.extend({
      force: false // forces plugin usage in all browsers
    },options);
    
    if(!options.force && !$.browser.mozilla && !(($.browser.msie && $.browser.version < 8)))
    {
      return $(this);
    }
    
    return $(this).each(function(){
      // Getting necessary elements
      var item = $(this);
      var children = item.children();
      var tableWidth = item.width();
      
      // Creating table that will do calculations for us
      var row = $(document.createElement('tr'));
      var table = $(document.createElement('table'))
        .css({
          position: 'absolute',
          left: 0,
          top: -1000,
          width: tableWidth
        })
        .attr({
          border: '0',
          cellpadding: '0',
          cellspacing: '0'
        })
        .append(row).appendTo($('body'));
      
      // Changing styles to calculate correct width and appending children
      // to the table to do browser-based cell calculations
      children.each(function(){
        var child = $(this).css({
          'display': 'inline'
        });
        var grandChild = child.children().css({
          'float': 'left',
          'display': 'inline'
        });
        var cloneSize = {
          width: child.outerWidth(),
          height: '1px'
        }
        var clone = $(document.createElement('div')).css(cloneSize);
        var td = $(document.createElement('td')).append(clone);
        row.append(td);
      });
      
      // Now applying calculated widths back to real elements
      var sum = 0;
      row.children().each(function(index){
        var menuItem = children.eq(index);
        var width = $(this).width();
        sum += width;
        width -= menuItem.outerWidth() - menuItem.innerWidth();
        menuItem.css({
          display: 'block',
          width: width,
          'float': 'left'
        });
        menuItem.children().css({
          display: 'block',
          'float': 'none'
        });
      });
      
      // Cleanup
      table.remove();
    });
  };
})(jQuery);
