(function($){ 

	Array.prototype.min = function(){ return Math.min.apply({},this) };
	Array.prototype.max = function(){ return Math.max.apply({},this) };
	$.fn.masonry = function() {
		this.each(function() {
			var wall = $(this);
			if ( wall.children().length > 0 ) {
				if( wall.children('.masonryWrap').length == 0 ) {
					wall.wrapInner('<div class=\"masonryWrap\"></div>');
				}
				var mWrap = wall.children('.masonryWrap');
	
				var brick = mWrap.children();
				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
						});
						colH[ thisCol ] += $(this).outerHeight(true);
					});
				
				mWrap.height( colH.max() );
			}

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

$(function(){
	
	function menuM(){
		$('body:not(.edit_view) .widget_menu01.add_design7 .items ul').masonry();
	}
	$(window).on('resize', menuM);
	menuM();
	$(window).load(function(){
		menuM();
	});
	
});

$(window).load(function(){

	$('.block_menu_1.add_design2 .inner_item:nth-of-type(2n)').each(function(){
		var r = 0;
		var $img = $(this).find('.category_img img');
		var ratio = $img .height() / $img .width();
		var $img2 = $(this).prev('.inner_item').find('.category_img img');
		var ratio2 = $img2 .height() / $img2 .width();
		if(ratio < ratio2){
			r = ratio;
		}else{
			r = ratio2;
		}
		function ResizeS1(){
			$img.css({height: $img.width()*r});
			$img2.css({height: $img2.width()*r});
		}
		ResizeS1();
		$(window).on('resize', ResizeS1);
	});
	
});