(function($){
	$(function(){
	
	/*----- grid image -----*/
	if ($("#grid-wrapper").length) {
		$("#grid-wrapper .grid-image img").each(function(){
			var thumb_size = 175;
			var w = $(this).width();
			var h = $(this).height();
			
			if (w < h) {
				var mtop = Math.round(( h - thumb_size ) / 2);
				var mleft = 0;
			}
			else {
				var mtop = 0;
				var mleft = Math.round(( w - thumb_size ) / 2);
			}
			
			$(this).css('marginTop', '-' + mtop + 'px');
			$(this).css('marginLeft', '-' + mleft + 'px');
		});
	}
	
	/*----- single page -----*/
	if ($("body.single-post").length) {
		var target_div = "#single-wrapper div.category-works div.post-body";
		var target_img = target_div + " p>img";
		var thumb_size = 50;
		
		var arr_img = [];
		$(target_img).each(function(i,elm){
			arr_img.push(elm);
		});
		if (arr_img.length) {
			$(target_div).empty()
				.append('<p id="single-top-image"></p>')
				.append('<p id="single-thumbnail"></p>')
				.hide();
			
			for (var i=0; i<arr_img.length; i++) {
				$("#single-thumbnail").append(arr_img[i]);
			}
			$("#single-thumbnail img:first").clone().appendTo("#single-top-image");
			
			if (arr_img.length == 1) {
				$("#single-thumbnail").hide();
			}
			else {
				$("#single-thumbnail img").wrap('<div class="cropper"></div>');
				$("#single-thumbnail img").each(function(){
					var w = $(this).width();
					var h = $(this).height();
					
					if (w < h) {
						var nw = thumb_size;
						var nh = Math.round(h * (thumb_size / w));
						var mtop = Math.round(( nh - thumb_size ) / 2);
						var mleft = 0;
					}
					else {
						var nw = Math.round(w * (thumb_size / h));
						var nh = thumb_size;
						var mtop = 0;
						var mleft = Math.round(( nw - thumb_size ) / 2);
					}
					
					$(this).data('orgWidth', w);
					$(this).data('orgHeight', h);
					$(this).width(nw);
					$(this).height(nh);
					$(this).css('marginTop', '-' + mtop + 'px');
					$(this).css('marginLeft', '-' + mleft + 'px');
					$(this).parent().width(thumb_size).height(thumb_size); //set .cropper's width/height
				});
				$("#single-thumbnail img").click(function(){
					var self = this;
					var obj = $(self).clone().width( $(self).data('orgWidth') ).height( $(self).data('orgHeight') );
					$("#single-top-image").empty().hide().append(obj).show();
				});
			}
			
			$(target_div).show();
		}
	}
	
	}); // end of document ready
})(jQuery); // end of jQuery name space 

