/* -----------------------------------------------------------------------

	Kevin Eye
	eye@buffalo.edu
	
	PositionFix.js
	
	Fix bottom: _px and right: _px when position: absolute in IE. Just
	include; it will detect IE, traverse the DOM, and apply the patches
	automatically.

----------------------------------------------------------------------- */

var PositionFix = {
	'idCounter' : 0,
	'makeId' : function() {
		return '_fixpos_'+(++PositionFix.idCounter);
		},
	
	'fixRightVarWidth' : function(el) {
		var offset = el.currentStyle['right'];
		if(offset.substr(offset.length-2) != 'px') return;
		var offset = parseInt(offset);
		var id = el.id;
		if(!id) id = el.id = PositionFix.makeId();
		el.style.setExpression('width', id+'.offsetParent.offsetWidth - (parseInt('+id+'.currentStyle["borderLeftWidth"])||0) - (parseInt('+id+'.currentStyle["borderRightWidth"])||0) - (parseInt('+id+'.currentStyle["paddingLeft"])||0) - (parseInt('+id+'.currentStyle["paddingRight"])||0) - (parseInt('+id+'.currentStyle["marginLeft"])||0) - (parseInt('+id+'.currentStyle["marginRight"])||0) - (parseInt('+id+'.offsetParent.currentStyle["borderLeftWidth"])||0) - (parseInt('+id+'.offsetParent.currentStyle["borderRightWidth"])||0) - (parseInt('+id+'.currentStyle["left"])||0) - '+offset);
		},
	
	'fixRightFixedWidth' : function(el) {
		var offset = el.currentStyle['right'];
		if(offset.substr(offset.length-2) != 'px') return;
		var offset = parseInt(offset);
		var id = el.id;
		if(!id) id = el.id = PositionFix.makeId();
		el.style.setExpression('left', id+'.offsetParent.offsetWidth - (parseInt('+id+'.currentStyle["borderLeftWidth"])||0) - (parseInt('+id+'.currentStyle["borderRightWidth"])||0) - (parseInt('+id+'.currentStyle["paddingLeft"])||0) - (parseInt('+id+'.currentStyle["paddingRight"])||0) - (parseInt('+id+'.currentStyle["marginLeft"])||0) - (parseInt('+id+'.currentStyle["marginRight"])||0) - (parseInt('+id+'.offsetParent.currentStyle["borderLeftWidth"])||0) - (parseInt('+id+'.offsetParent.currentStyle["borderRightWidth"])||0) - (parseInt('+id+'.currentStyle["width"])||0) - '+offset);
		},
	
	'fixBottomVarHeight' : function(el) {
		var offset = el.currentStyle['bottom'];
		if(offset.substr(offset.length-2) != 'px') return;
		var offset = parseInt(offset);
		var id = el.id;
		if(!id) id = el.id = PositionFix.makeId();
		el.style.setExpression('height', id+'.offsetParent.offsetHeight - (parseInt('+id+'.currentStyle["paddingTop"])||0) - (parseInt('+id+'.currentStyle["paddingBottom"])||0) - (parseInt('+id+'.currentStyle["borderTopWidth"])||0) - (parseInt('+id+'.currentStyle["borderBottomWidth"])||0) - (parseInt('+id+'.currentStyle["marginTop"])||0) - (parseInt('+id+'.currentStyle["marginBottom"])||0) - (parseInt('+id+'.offsetParent.currentStyle["borderTopWidth"])||0) - (parseInt('+id+'.offsetParent.currentStyle["borderBottomWidth"])||0) - (parseInt('+id+'.currentStyle["top"])||0) - '+offset);
		},
	
	'fixBottomFixedHeight' : function(el) {
		var offset = el.currentStyle['bottom'];
		if(offset.substr(offset.length-2) != 'px') return;
		var offset = parseInt(offset);
		var id = el.id;
		if(!id) id = el.id = PositionFix.makeId();
		el.style.setExpression('top', id+'.offsetParent.offsetHeight -  (parseInt('+id+'.currentStyle["paddingTop"])||0) - (parseInt('+id+'.currentStyle["paddingBottom"])||0) - (parseInt('+id+'.currentStyle["borderTopWidth"])||0) - (parseInt('+id+'.currentStyle["borderBottomWidth"])||0) - (parseInt('+id+'.currentStyle["marginTop"])||0) - (parseInt('+id+'.currentStyle["marginBottom"])||0) - (parseInt('+id+'.offsetParent.currentStyle["borderTopWidth"])||0) - (parseInt('+id+'.offsetParent.currentStyle["borderBottomWidth"])||0) - (parseInt('+id+'.currentStyle["height"])||0) - '+offset);
		},
	
	'fixElement' : function(el) {
		if(el.currentStyle['right'] != 'auto') {
			if(el.currentStyle['width'] == 'auto') {
				PositionFix.fixRightVarWidth(el);
				}
			else {
				PositionFix.fixRightFixedWidth(el);
				}
			}
		if(el.currentStyle['bottom'] != 'auto') {
			if(el.currentStyle['height'] == 'auto') {
				PositionFix.fixBottomVarHeight(el);
				}
			else {
				PositionFix.fixBottomFixedHeight(el);
				}
			}
		},
	
	'fixDom' : function(dom) {
		PositionFix.fixElement(dom);
		for(var i = 0; i < dom.children.length; i++)
			PositionFix.fixDom(dom.children[i]);
		},
	
	'fixAll' : function() {
		PositionFix.fixDom(document.body);
		},
	
	'init' : function() {
		if (window.attachEvent) window.attachEvent('onload', PositionFix.fixAll);
		}
	
	};

PositionFix.init();
