(function($){

	$.fn.clikThumbnails = function(options){
		
		options = $.extend({
						width:null,
						height:null,
						wrapClass:'thumbWrap',
						square:true,
						externalCss:true,
						valign:'center',
						halign:'center'
						},
						options || {})
		
		var images = $('img',this);
		
		if(!options.width && !options.height){
			
			options.width = $(images[0]).outerWidth(true);
			options.height = $(images[0]).outerHeight(true);
			
			images.each(function(){
				
				width = $(this).outerWidth(true);
				height = $(this).outerHeight(true);
				
				if(width < options.width){
					options.width = width;
				}
				if(height < options.height){
					options.height = height;
				}
			});
		}
		else if(!options.width){
			options.width=options.height;
		}
		else if(!options.height){
			options.height=options.width;
		}
		
		/*if(options.square){
			options.height>options.width?options.height=options.width:options.width=options.height;
		}*/
		
		return images.each(function(){
			imgCss(this);
		});
		
		function imgCss(img){
			var $img = $(img);
			h = (options.height - $img.outerHeight(true))/2;
			w = (options.width - $img.outerWidth(true))/2;
			var vmargin = h;
			var hmargin = w;
			
			switch (options.valign) {
				case "top":
				vmargin = 0;
				break;
				case "bottom":
				vmargin = h * 2;
				break;
			}
			
			switch (options.halign) {
				case "left":
				hmargin = 0;
				break;
				case "right":
				hmargin = w * 2;
				break;
			}
			
			if (h <= 0 && w <= 0) {
				$img.css({
					"margin-top":h,
					"margin-left":w
				});
			}
			else if (h >= 0 && w <= 0) {
				$img.css({"margin-left":w});
				$img.parents('.'+options.wrapClass).height($img.outerHeight(true)).css("margin-top",vmargin);
			}
			else if (h <= 0 && w >= 0) {
				$img.css({"margin-top":h});
				$img.parents('.'+options.wrapClass).width($img.outerWidth(true));//.css("margin-left",hmargin);
				//Adjust caption width if necessary.
				adjustCaption($img);
			}
			else if(h >= 0 && w >= 0){
				$img.parents('.'+options.wrapClass).height($img.outerHeight(true)).css("margin-top",vmargin);
				$img.parents('.'+options.wrapClass).width($img.outerWidth(true));//.css("margin-left",hmargin);
				//Adjust caption width if necessary.
				adjustCaption($img);
			}
		}
		
		function adjustCaption($img){
			var $h3 = $img.siblings('h3').size() == 1 ? $img.siblings('h3') : $img.parent('.'+options.wrapClass).siblings('h3');
			var padding = parseInt($h3.css('padding-left')) + parseInt($h3.css('padding-right'));
			var border = parseInt($h3.css('border-left-width')) + parseInt($h3.css('border-right-width'));
			if($h3.hasClass('bottom') || $h3.hasClass('top')){
				$h3.width($img.outerWidth(true) - padding - border);
			}
			else{
				var wrapBorder = parseInt($img.parents('.'+options.wrapClass).css('border-left-width')) + parseInt($img.parents('.'+options.wrapClass).css('border-right-width'));
				$h3.width($img.outerWidth(true) - padding - border + wrapBorder);
			}
		}
	}
			
})(jQuery)