(function($) {

/**
 * Fix IE6's double-margin problem
 *  This fix doesn't use display:inline hack, so you are free to make boxes as you prefer!
 *
 *  I just know Dean Edwards's script and I love it, but I don't like how they fixed this issue: with display:inline.
 *    In fact they put their solution in "squish file", because "Some of these bug fixes may have adverse effects so they are 
 *      not included in the standard library. Add your own if you want."
 *    So I have developed this script.
 *    Actually this script is fully compatible with Dean's script and both of them can coexist in the same page.
 *
 *  @example $('div').fixBoxModel(); // note: this has to be the FIRST thing (or second after fixBoxModel) you have to do on $('document').ready
 *  @example Note: next example is not correct because it creates problems with images with border...
 *    $('*').fixBoxModel(); // Not correct, do not use it, use div in place of *
 *
 *  @param Map options
 *  @option boolean force Force fix even if IE7 fix js is detected
 *  
 *  @license You can use it free of charge for personal and commercial websites.
 *      You can't sell this code.
 *      You have to leave the @license, the @author's name and website even in minified (or similar) js files
 *      Thanks.
 *  @name fixDoubleMargin
 *  @type jQuery
 *  @cat Plugins/Fixes
 *  @author Alessandro Coscia (php_staff [/\] yahoo [-] it || http://www.programmatorephp.it/jquery)
 */
$.fn.fixDoubleMargin = function(options) {
    settings = jQuery.extend({
      force: true // Force fix even if IE7 fix js is detected
    }, options);
    
    // Double margin
    // check dependencies
    if (typeof $.browser.browser != 'function')
      alert('jQBrowser v0.2+ plugin of jQuery needed:\n Download it from:\n http://davecardwell.co.uk/javascript/jquery/plugins/jquery-browserdetect/');
      
    // forced by setting or IE7 fix js detected?
    if (settings.force || typeof(IE7) != 'object') {
      // fix only for ie < 7
      if (($.browser.browser() == 'Internet Explorer') && ($.browser.version.number() < 7)) {
        // For each div
        return $(this).each(function() {
          // if this is a Float element
          if ($(this).css('float') != 'none') {
            // Get actual margin
            var top = parseInt($(this).css('margin-top'));
            var right = parseInt($(this).css('margin-right'));
            var bottom = parseInt($(this).css('margin-bottom'));
            var left = parseInt($(this).css('margin-left'));
            
            // Set new margin
            top = parseInt(top / 2);
            if (isNaN(top)) top = 0;
            right = parseInt(right / 2);
            if (isNaN(right)) right = 0;
            bottom = parseInt(bottom / 2);
            if (isNaN(bottom)) bottom = 0;
            left = parseInt(left / 2);
            if (isNaN(left)) left = 0;
            // If previous element is not float
            if ($(this).prev().css('float') == 'none') {
              $(this).css({marginTop: top*2});
              $(this).css({marginBottom: bottom*2});
              // $(this).css('border', '5px orange solid'); // test purpose
              if ($(this).css('float') == 'left') {
                $(this).css({marginLeft: left});
                $(this).css({marginRight: right*2});
              }
              else { // floar right
                $(this).css({marginLeft: left*2});
                $(this).css({marginRight: right});
              }
            }
            // If previous element is float
            else {
              var nextFloat = $(this).next().css('float');
              var prevFloat = $(this).prev().css('float');
              var thisFloat = $(this).css('float');
              // $(this).css('border', '5px green solid'); // test purpose
              if (nextFloat == 'none' && prevFloat != thisFloat) {
                if (thisFloat == 'left')
                  $(this).css({marginLeft: left});
                else
                  $(this).css({marginRight: right});
              }
            }
          }
        });
      }
    }
    return this;
  }
})(jQuery);
