(function($){
	$.fn.tawFisheye = function(options) {
		
		first_img = $('img:first', this);
		var sfwidth = first_img.width();
		var sfheight = first_img.height();
		
		if(sfwidth == null || sfheight == null) return false;
		
		img = new Image();
		img.src = first_img.attr("src");
		var sowidth = img.width;
		var soheight = img.height;
		
		
		var defaults = {
			duration: "slow",
			startWidth: sfwidth + "px",
			startHeight: sfheight + "px",
			endWidth: sowidth + "px",
			endHeight: soheight + "px"
		};
		
		var options = $.extend(defaults, options);	
		
		curr_top_pos = $(first_img).css("top");
				
		if(curr_top_pos == "auto") curr_top_pos = "0px";
		
		
		var indexOfPxStartHeight = options.startHeight.indexOf("px");
		var indexOfPxEndHeight = options.endHeight.indexOf("px");
		var indexOfPcStartHeight = options.startHeight.indexOf("%");
		var indexOfPcEndHeight = options.startHeight.indexOf("%");
		var indexOfEmStartHeight = options.startHeight.indexOf("em");
		var indexOfEmEndHeight = options.startHeight.indexOf("em");
		
		if(indexOfPxStartHeight != -1) {
			var sh = options.startHeight.substr(0, indexOfPxStartHeight);
			var eh = options.endHeight.substr(0, indexOfPxEndHeight);
		} else if(indexOfPcStartHeight != -1) {
			var sh = options.startHeight.substr(0, indexOfPcStartHeight);
			var eh = options.endHeight.substr(0, indexOfPcEndHeight);
		} else if(indexOfEmStartHeight != -1) {
			var sh = options.startHeight.substr(0, indexOfEmStartHeight);
			var eh = options.endHeight.substr(0, indexOfEmEndHeight);
		} else {
			zoom_top_pos = "0px";
		}
		
		zoom_top_pos = ((sh - eh) / 2) + "px";		
		
		if(navigator.appName == "Opera") {
			var padding_error = $(first_img).css("margin-top");
			padding_error = padding_error.substr(0, padding_error.indexOf("px"));
			curr_top_pos = curr_top_pos.substr(0, curr_top_pos.indexOf("px"));
			curr_top_pos = (curr_top_pos - padding_error) + "px";
		}
		
		
					
		
		return this.each(function() {
								 
			var obj = $(this);
			var $imgs = $('img', obj);
			if($imgs.length === 0){
				return false;
			
			} else {
				var loadedImages = [];
				$imgs.each(function(i, val){
					$(this).bind('load', function(){
					
					}).each(function(){
						
						owidth = options.startWidth;
						oheight = options.startHeight;
						fwidth = options.endWidth
						fheight = options.endHeight;
						
						$(this).mouseover(function(){
							$(this).stop();
							$(this).animate({
								width: fwidth,
								height: fheight,
								top: zoom_top_pos
							}, options.duration, "", function(){
								$(this).css({ "top": zoom_top_pos });
							});
						}).mouseout(function(){
							$(this).stop();
							$(this).animate({
								width: owidth,
								height: oheight,
								top: curr_top_pos
							}, options.duration, "", function(){
								$(this).css({ "top": curr_top_pos });
							});
						});
					});
				});
			}
		
		});
	};
})(jQuery);
