/*
 * Copyright (c) 2011 HitHot.cc
 */
(function($) {

$.fn.thumbs = function(options) {
	var $thumbs = this;
	
	if (options == 'destroy') {
		return Thumbs.destroy($thumbs);
	}
	
	if( $thumbs.data('thumbs') ) {
		return $thumbs;
	}
	
	var center = {},
	defaults = {
		center: true,
		allowZoomIn: true,
		opacity: 1,
		fixWidth: 170,
		fixHeight: 170
	};
	
	var finalOptions = {};
	$.extend(true, finalOptions, defaults, options);
	
	return $thumbs.each(function(){
		var $thumb = $(this),
		clone = $thumb.clone(true),
		centered = 'centered' in $thumb.data() && $thumb.data('centered');
		
		if (!centered) {
			$thumb.options = finalOptions;
			$thumb.data('centered', true);
			
			if (finalOptions.center) {
				Thumbs.centerImg( $thumb );
			}
			
			var data = {
				'raw': clone
			};
			
			$thumb.data('thumbs', data);
		}
	});
}


var Thumbs = {
		
	centerImg: function($thumb) {
		var $img = $thumb.is('img') ? $thumb : $thumb.find('img');
		
		if ($img.complete || $img.readyState === 4) {
		    // give up
		} else {
			$img.bind('load', function() {
				
				var css;
				var thisW = $thumb.css('width').replace(/px$/g, "");
				var thisH = $thumb.css('height').replace(/px$/g, "");
				var imgW = $img.css('width').replace(/px$/g, "");
				var imgH = $img.css('height').replace(/px$/g, "");
				//console.log($img.attr('src') + '/ thisW:' + thisW + '/ thisH:' + thisH + '/ imgW' + imgW + '/ imgH' + imgH + '/ ' + $thumb.options.allowZoomIn);
				
				thisW = thisW == 0 ? $thumb.options.fixWidth : thisW;
				thisW = $thumb.options.fixWidth = Math.min($thumb.options.fixWidth, thisW);
				thisH = thisH == 0 ? $thumb.options.fixHeight : thisH;
				thisH = $thumb.options.fixHeight = Math.min($thumb.options.fixHeight, thisH);
				imgW = imgW == 0 ? $thumb.options.fixWidth : imgW;
				imgH = imgH == 0 ? $thumb.options.fixHeight : imgH;
				
				if ($thumb.options.allowZoomIn && (imgW < thisW || imgH < thisH)) {
					
					//console.log('thisW:' + thisW + '/ thisH:' + thisH + '/ imgW' + imgW + '/ imgH' + imgH);
					if (imgW < thisW) {
						var scale = thisW / imgW;
						var mt = Math.min(0, Math.round((thisH - (scale * imgH)) / 4)); 
						
						css = {
							'width': thisW + 'px',
							'margin-top': mt + 'px',
							'z-index': 0
							};
					}
					else {
						var scale = thisH / imgH;
						var ml = Math.min(0, Math.round((thisW - (scale * imgW)) / 2)); 
						
						css = {
							'height': thisH + 'px',
							'margin-left': ml + 'px',
							'z-index': 0
							};
					}
					
				}
				/*else if (true) {
					css = {
						'width': thisW + 'px'
					}
					
					$thumb.css({'background': 'url(' + $img.attr('src') + ')', 'opacity': 0.3});
				}*/
				else {
					var setW = true;
					
					if (imgW > $thumb.options.fixWidth && imgH * ($thumb.options.fixWidth / imgW) >= $thumb.options.fixHeight) {
						imgH = Math.round(imgH * ($thumb.options.fixWidth / imgW));
						imgW = $thumb.options.fixWidth;
						//alert("a:" + imgW + "," + imgH + "/" + thisW + "," + thisH);
					}
					else if (imgH > $thumb.options.fixHeight && imgW * ($thumb.options.fixHeight / imgH) >= $thumb.options.fixWidth) {
						imgW = Math.round(imgW * ($thumb.options.fixHeight / imgH));
						imgH = $thumb.options.fixHeight;
						setW = false;
						//alert("b:" + imgW + "," + imgH + "/" + thisW + "," + thisH);
					}
					else {
						//alert("c:" + imgW + "," + imgH + "/" + thisW + "," + thisH);
					}
					
					var ml = Math.min(0, ((thisW-imgW) / 2));
					var mt = Math.min(0, ((thisH-imgH) / 4));
					//console.log('ml:' + ml + "/ mt:" + mt);
					
					if (setW) {
						css = {
							'width': imgW + 'px',
							'margin-left': ml + 'px',
							'margin-top': mt + 'px',
							'opacity': $thumb.options.opacity,
							'z-index': 0
							};
					}
					else {
						css = {
							'height': imgH + 'px',
							'margin-left': ml + 'px',
							'margin-top': mt + 'px',
							'opacity': $thumb.options.opacity,
							'z-index': 0
							};
					}
				}
				
				$img.css( css );
				$img.fadeIn( 200 );
		    });
		}
		
		return $thumb;
	},

	/*
	 * Private: Removes all the added thumbnail html
	 *
	 * @name     thumbs.destroy
	 * @author   Joan Piedra (http://joanpiedra.com)
	 * @example  Thumbs.destroy($thumbs);
	 *
	 */
	destroy: function($thumbs) {
		$thumbs.each(function(index) {
			var $thumb = $(this),
			data = $thumb.data('thumbs');
			
			if (!data) {
				return;
			}
		});
	}

}

})(jQuery);
