/*
**  jquery.align.js -- jQuery plugin to align items with offset distances. useful for menus/combo boxes for centered layouts.
*   Copyright (c) 2007 Jamez Picard (jamez.ca)
*   Dual licensed under the MIT and GPL licenses:
*   http://www.opensource.org/licenses/mit-license.php
*   http://www.gnu.org/licenses/gpl.html
*   
*   It only reliably works on images as the source element.  links and divs only work consistantly in moz & safari (msie is really a hit-or-miss).
*
*   Revision: $Id$
*/

(function($) {
	// Main Align function.
    $.fn.extend({
		align : function (o,offset) {
		offset = $.extend({ eX:0, eY:0 },offset);
		this.each(function() {
			if ($.alignedIds[o]) return;
			var offsetX = elementX(this)+offset.eX; //alert("x:"+elementX(this)+" y:"+elementY(this));
			var offsetY = elementY(this)+offset.eY;
			//var offsetX = $(this).offset().left;
			//var offsetY = $(this).offset().top;
			//alert("x:"+offsetX+ " y:"+offsetY);
			if (offsetX>0 && offsetY>0) {
				$(o).each(function () {
					if (document.getElementById) {
						this.style.left = offsetX+'px';
						this.style.top = offsetY+'px';
					}
					else if (document.all && !document.getElementById) //ie on mac.
					{	
						eval(this.id+".style.pixelLeft="+brandOffsetX);
						eval(this.id+".style.pixelTop="+brandOffsetY);
						//this.style.pixelLeft=offsetX;
						//this.style.pixelTop=offsetY;
					}
				});
				$.alignedIds[o] = true;
			} // endif
		});
	   }
	});
	
	// resets flag when window is resized.
	$(window).bind('resize', function() {
    $.alignedIds = { };
	});
		
	// internal flag
	$.alignedIds = { };
	// method to check if is already aligned.
	$.isAligned = function(id) { return ($.alignedIds[id]) ? $.alignedIds[id]: false; }
	
	// Helper Align Functions.
	elementX = function (e) {
	  if (e.pageX) return e.pageX;
	  var x = 0;  
	  while (e) {
		if (e.offsetLeft) x += e.offsetLeft;
		e = (e.offsetParent) ? e.offsetParent : null;
	  }
	  return x;
	};
	elementY = function (e) {
	  if (e.pageY) return e.pageY;
	  var y = 0;
	  while (e) {
	  if (e.offsetTop) y += e.offsetTop; 
	  	e = (e.offsetParent) ? e.offsetParent : null;
	  }
	  return y;
	};
	
})(jQuery);
