var $$ = function(param) {
	var node = $(param)[0];
	var id = $.data(node);
	$.cache[id] = $.cache[id] || {};
	$.cache[id].node = node;
	return $.cache[id];
};

(function($){

	$.fn.center = function (absolute) {
		return this.each(function () {
			var t = $(this);

			t.css({
				position:	absolute ? 'absolute' : 'fixed', 
				left:		'50%', 
				top:		'50%', 
				zIndex:		'99'
			}).css({
				marginLeft:	'-' + (t.outerWidth() / 2) + 'px', 
				marginTop:	'-' + (t.outerHeight() / 2) + 'px'
			});

			if (absolute) {
				t.css({
					marginTop:	parseInt(t.css('marginTop'), 10) + $(window).scrollTop(), 
					marginLeft:	parseInt(t.css('marginLeft'), 10) + $(window).scrollLeft()
				});
			}
		});
	};

	$.fn.maxSize = function(opts) {
    opts = $.extend({
			vOffset:              0,
			hOffset:              0,
			leftSpace:            0,
			topSpace:             0,
			rightSpace:           0,
			bottomSpace:          0,
			zIndex:               1000,
			verticalAlign:        'top',
			horizontalAlign:      'left',
			maxAtOrigImageSize:   false,
			rhowImmediately:      true,
      container:            window,
			click:                null
    }, opts);

		function find_width_and_height(originalWidth,originalHeight,ratio) {
			var pageWidth  = $( opts.container ).width()  - opts.hOffset,
					pageHeight = $( opts.container ).height() - opts.vOffset;

			width  = pageWidth;
			height = width / ratio;
			
			if ( height < pageHeight ){
				height = pageHeight - (opts.topSpace + opts.bottomSpace);
				width  = height*ratio;
			}

			// If maxAtRatio == true and your new width is larger than originalWidth, size to originalWidth
      arrayImageSize = opts.maxAtOrigImageSize && width > originalWidth ?  
        [originalWidth,originalHeight] : 
        [width,height];
			return arrayImageSize;
		}

		function setup(elem){
			elem.hide();
			positionImage(elem);
			$(window).load(function(){
				getOriginalDimensions(elem);
				resizeImage(elem);
				if (opts.showImmediately) {
					elem.show();
				}
				
				$(window).resize(function(){
					resizeImage(elem);
				});
			});
		}

		function positionImage(elem){
			elem.css({
				'overflow':   'hidden',
				'position':   'absolute',
				'z-index':    opts.zIndex,
				'left':       opts.leftSpace,
				'top':        opts.topSpace
			});
			
			$('html').css({'overflow':'hidden'});
			
			if (opts.verticalAlign == 'bottom'){
				elem.css({'bottom':opts.bottomSpace});
			} else if (opts.horizontalAlign == 'right'){
				elem.css({'right':opts.rightSpace});
			}
		}

		function resizeImage(elem){
			var oW               = parseInt(elem.attr('origWidth'), 10),
					oH               = parseInt(elem.attr('origHeight'),10),
					ratio            = elem.attr('ratio'),
					width_and_height = find_width_and_height(oW,oH,ratio);
			elem
				.width(  width_and_height[0] )
				.height( width_and_height[1] );
		}

    function max_height(pageHeight,ratio){
      height = pageHeight - (opts.topSpace + opts.bottomSpace);  
      width  = height*ratio;
    }
    
    function max_width(pageWidth,ratio){
      width  = pageWidth - (opts.leftSpace + opts.rightSpace); 
      height = width/ratio;
    }
		
		function getOriginalDimensions(elem){
			var w     = elem.width(),
					h     = elem.height(),
				  ratio = (parseInt(w,10) / parseInt(h,10)).toFixed(2);
			elem
				.attr('origWidth',  w)
				.attr('origHeight', h)
				.attr('ratio',  ratio);
		}
		
		function find_ratio(w,h) {
			return 
		}

    return this.each(function() {
			var $this = $(this);
			setup($this);
			if (opts.click) {
				$this.click(function(){
					opts.click.call($this);
				});
			}
		});
	};
	
})(jQuery);
