/*the following line is used for checking source against javascript lint*/
/*global window document */

if (!window.Msn) { window.Msn = {}; } // Future proof in case the framework is released.

Msn.IeMinWidth = new function() {
  var me = this; // save a self-reference

	// shortcuts
	var w = window;
	var d = document;

	// enforces the minimum width on the element passed or raising the event
	function enforce(el) {
		if (!el) {
			el = w.event.srcElement;
		}

		// if not already set, we set an expando property to persist the original width from css, since 
		// we may set width to min-width and need to reset width back to width. 
		if (!el.getAttribute("w")) {
			el.setAttribute("w", el.currentStyle["width"]);
		}

		// Determine whether to use width or min-width
		// d.body.parentElement.currentStyle.fontSize returns the view/text size menu choice as this:
		// 16pt: Largest, 14pt: Larger; 12pt: Medium; 10pt: Smaller; 9pt - Smallest
		var useMin = (parseInt(d.body.parentElement.currentStyle.fontSize) < 12);

		// when useMin is true, we set width to value from css for min-width, usually absolute units (e.g. px)
		// when useMin is false, we set width to value from css for min-width, usually relative units (e.g. em, ex, %)
		el.style.width = useMin ? el.currentStyle["min-width"] : el.getAttribute("w");
	}
	
	function init() {
		//only runs for IE
		if (!d.all) {
			return;
		}

		// els is an array of id values that are used by GTL that have minwidth associated with 
		// them for high level layouts
		var els = new Array('wrapper', 'nav', 'subhead', 'area1', 'area2', 'area3');

		for (var i=0; i<els.length; i++) {
			var el = d.getElementById(els[i]);
			if (el) {
				enforce(el);
				el.onresize = enforce;
			}
		}
	}

  /////////////////////////////////////////////////////////////////////////////
  
  //
  // script to execute NOW
  //
  
  this.oncreate = function() {
    // safari isn't mozilla based, so we can't use attachEvent
    // to link into the event system. But since we don't care about
    // the load or keydown events for Safari, just don't do anything
    if ( d.attachEvent ) {
      w.attachEvent( "onunload", me.ondestroy );
    }

    // initialize elements that require ie min-width support
    init(); 
  };

  this.ondestroy = function() {
    w.detachEvent( "onunload", me.ondestroy );
  };
  
  // set event handlers
  this.oncreate();

  // create the closure
  return this;
};
