/*
 * 
 * ADOBE CONFIDENTIAL
 * ___________________
 * 
 * Copyright 2011 Adobe Systems Incorporated
 * All Rights Reserved.
 * 
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */

(function($) {
  $.fn.toBrowserWidth = function(options) {
    return this.each(function() {
      var $element = $(this).css({ marginLeft:0}); // Reset left margin originally set by Muse
      var widthDelta = $element.outerWidth() - $element.width(); // Measure extra width for layout calculation later
      var marginDelta = 0; // parseInt($element.css('marginRight')) + $element.width();
      var savedWidth = 0;
      var delayedFn = null;
      var doLayout = function() {

          if (savedWidth == $(window).width()) return;
          savedWidth = $(window).width();

          $element.css({ left:0, width:0 }); // Reset position and prevent scrollbar flickering (still flickers a bit on IE)
          if (delayedFn === null) {
              delayedFn = function() {
                  var offset = $element.offset(); // Calculate global offset of "relative" position
                  $element.css({ width:$(window).width() - widthDelta, 
                                 left:-offset.left, 
                                 marginRight: -$(window).width() + widthDelta + marginDelta});
                  delayedFn = null;
              };
              if ($.browser.msie && $.browser.version < "9.0") {
                  setTimeout(delayedFn, 1);
              } else {
                  delayedFn();
              }
          }
      };
      $(window).resize(doLayout); // Layout on resize
      $(document).ready(doLayout); // Layout asap
    });
  }
}(jQuery));
