/* (C) 2009 KuBu Stream. www.kubu.ro. Ver. 1.1 */
(function($){
	$.fn.kbZoom = function(options) {
		var opts = $.extend({},$.fn.kbZoom.defaults,options);
		return this.each(function() {
			$this = $(this);
			
			$this.o = opts;
			$this.data("opt",$this.o);			
			$this.step  = 20;
			$this.crtX  = 0;
			$this.crtY  = 0;
			$this.crtFX  = 0;
			$this.crtFY  = 0;	
			
			$this.mout = 0;
			$this.zmin = 0;
			
			$this			
			.mouseover(function(e){
				if ($this.mout == 1) {
					return;
				}
				if ($this.zmin == 1) {
					return;
				}
				$this.crtX  = 0;
				$this.crtY  = 0;
				$this.crtFX  = 0;
				$this.crtFY  = 0;	
				$(this).find("img").css("left",0).css("top",0);
				$this.step  = 20;				
			})
			.mousemove(function(e){
				if ($this.mout == 1) {
					return;
				}
				if ($this.zmin == 1) {
					return;
				}
				$this.mout = 0;
				
				var xp0 = $(this).position().left;
				var yp0 = $(this).position().top;
				var x = e.pageX - xp0;
				var y = e.pageY - yp0;				
				
				if ($this.step == 1) { //aici muta poza mare
					var tt = $(this).data("opt");
					if (x < tt.ignoreX)
						x = tt.ignoreX;
					if (y < tt.ignoreY)
						y = tt.ignoreY;
					if (x > tt.width - tt.ignoreX)
						x = tt.width-tt.ignoreX;
					if (y > tt.height - tt.ignoreY)
						y = tt.height-tt.ignoreY;
					
					var xi = -(x-tt.ignoreX)*(tt.widthLarge-tt.width)/(tt.width-tt.ignoreX-tt.ignoreX);
					var yi = -(y-tt.ignoreY)*(tt.heightLarge-tt.height)/(tt.height-tt.ignoreY-tt.ignoreY);
					
					$(this).find("img").css("left",xi).css("top",yi);
				} else
				if ($this.step == 0) {					//aici nu face nimic
					
					
					$this.crtX = x;
					$this.crtY = y;
				} else
				if ($this.step == 20) {			//prima data intra aici.
					$this.css({
						display:	'block',
						position:	'relative',
						overflow: 	'hidden',
						left: 		0,
						top:		0,
						width: 		$this.o.width,
						height: 	$this.o.height
					});
					
					$this.find("img").css({
						position: 'absolute',
						left:	0,
						top:	0,
						width:	$this.o.width,
						height:	$this.o.height
					});
					
					$this.step = 2;
				} 
				if ($this.step == 2) {				
					var oo = $(this);
					var tt = $(this).data("opt");					
					$(this).find("img").css({
						left:	0,
						top:	0,
						width:	tt.width,
						height:	tt.height
					});									
					if ($.fn.kbZoom.tmObj > 0) {
						clearTimeout($.fn.kbZoom.tmObj);
						$.fn.kbZoom.tmObj = 0;
					}
					$(this).find("img").attr('src',$(this).find("img").attr("longdesc"));
					$.fn.kbZoom.tmObj = setTimeout(function() {
						var x = $this.crtX;
						var y = $this.crtY;
						var obj = oo;
						var obj2 = tt;
						
						$this.crtFX = -x*(obj2.widthLarge-obj2.width)/obj2.width;
						$this.crtFY = -y*(obj2.heightLarge-obj2.height)/obj2.height;
						
						$this.zmin = 1;
						obj.find("img").animate({
							width: obj2.widthLarge,
							height: obj2.heightLarge,
							left: $this.crtFX,
							top: $this.crtFY
						},obj2.timeZoomIn,function() {
							$this.zmin = 0;
							if ($this.mout == 1) {
								$this.step = 2;
								obj.find('img').animate({
									width:tt.width,
									height:tt.height,
									left:0,
									top:0
								},tt.timeZoomOut,"linear",function() {
									$this.mout = 0;
								});
							} else
								$this.step = 1;
						});
					}	,	tt.timeToWait);
					$this.step = 0;
				}
				
				
			})
			.mouseout(function(e){
				if ($this.mout == 1) {
					return;
				}
				$this.mout = 1;
				if ($this.zmin == 1) {
					return;
				}
				
				var tt = $(this).data("opt");
				$(this).find('img').animate({
					width:tt.width,
					height:tt.height,
					left:0,
					top:0
				},tt.timeZoomOut,"linear",function() {
					$this.mout = 0;
				});
				$this.step=2;
				if ($.fn.kbZoom.tmObj >0) {
					clearTimeout($.fn.kbZoom.tmObj);
					$.fn.kbZoom.tmObj = 0;
				}
			});
		});
	};
	
	$.fn.kbZoom.tmObj = 0;	
	
	$.fn.kbZoom.defaults = {
		width 		: 	150,
		height		: 	150,
		widthLarge	:	250,
		heightLarge	:	250,
		ignoreX		:	25,
		ignoreY		:	25,
		timeZoomIn	:	500,
		timeZoomOut	:	300,
		timeToWait	:	300
	};
	
})(jQuery);
