(function($){ 

	//Finding min and max values in array from http://snippets.dzone.com/posts/show/769
	Array.prototype.min = function(){ return Math.min.apply({},this) };
	Array.prototype.max = function(){ return Math.max.apply({},this) };
	
	

	$.fn.masonry = function() {
	    /*
	    var _log = $('#oooo').length ? $('#oooo') : $('<div id="oooo">').appendTo('body').css({
    	    background:'#000',
    	    color: '#fff',
    	    padding: '10px',
    	    width: '400px',
    	    height: '300px',
    	    position: 'fixed',
    	    top: 0,
    	    left: 0,
    	    overflow:'auto',
    	    opacity: .5
    	});
    	
    	var _ = function(cnt) {
    	    console.log(cnt)
    	    _log.html(_log.html() + '<br>---------<br>' + cnt);
    	}
    	*/
    	
		return this.each(function() {
			var wall = $(this);
		
			if ( wall.children().length > 0 ) { // check if the element has anything in it
				
				if( wall.children('.masonryWrap').length == 0 ) {      // checks if the masonryWrap div is already there
					wall.wrapInner('<div class=\"masonryWrap\"></div>');
				}
				var mWrap = wall.children('.masonryWrap');
	            
				var brick = mWrap.children();
				
				if($.browser.safari) {
				    brick.css('display','inline-block')
				}
				
				var brickW = brick.outerWidth(true);
				
				var colCount = Math.floor( mWrap.width() / brickW ) ;
				
				var colH=new Array();
				for ( i=0; i < colCount; i++) {
					colH[ i ] =  0 ;
				}		
				
				mWrap.css({ position: 'relative' });
				
				brick.css({
						'float':'none',
						position: 'absolute',
						display: 'block'
					})
					.each(function(){
						for ( i=colCount-1; i > -1; i-- ) {
							if ( colH[ i ] == colH.min() ) {
								var thisCol = i;
							}
						}
						
						$(this).css({
							top: colH[ thisCol ],
							left: brickW * thisCol
						}).addClass('column-'+thisCol)
						colH[ thisCol ] += $(this).outerHeight(true);
					});
				
				mWrap.height( colH.max() );
			}

			return this; 
		});
	};
})(jQuery);

