(function($){

	$.fn.extend({

		//pass the options variable to the function
		cliozoom: function(options) {

			//Set the default values, use comma to separate the settings:
			var defaults = {
				mode: 'ls',
				startOpacity: 1.0,
				scaleFactor: 4,
				startPosX: 20,
				startPosY: 20,
				duration : 500,
				easing: 'linear'
			}

			var options =  $.extend(defaults, options);

			return this.each(function() {
				var o = options;

				//Assign current element to variable, in this case is IMG element
				var obj = $(this);
				if (obj.get(0).nodeName!='IMG') {
					return;
				}
				var trigger = obj.parent();
				if (trigger.get(0).nodeName!='A') {
					trigger = obj;
				}
				else {
					// Make image clickable in IE6/7
					obj.bind('click',function() {
						document.location.href = trigger.get(0).href;
						return false;
					});
					
					trigger.append('<span class="borderoverlay"></span>');
					$('.borderoverlay').css('opacity','0.5');
				}

				var widthStart = obj.width();
				var heightStart = obj.height();

				var widthEnd = widthStart * o.scaleFactor;
				var heightEnd = heightStart * o.scaleFactor;

				var jQueryHeightBugOffset = jQuery.support.cssFloat ? 0 : 1;

				var posXFactor = widthStart/(parseInt(o.startPosX));
				var posYFactor = heightStart/(parseInt(o.startPosY));

				var leftPos = Math.floor((widthStart - widthEnd)/posXFactor);
				var topPos = Math.floor((heightStart - heightEnd)/posYFactor);

				obj.wrap('<span style="margin-bottom:4px;position:relative;display:block;overflow:hidden;width:' + (widthStart-jQueryHeightBugOffset) + 'px;height:' + (heightStart-jQueryHeightBugOffset) + 'px;" />');
				 if (o.mode=='sl') {
					obj.css({
						'opacity'	: o.startOpacity,
						'position'	: 'relative'
					});
					trigger.bind('mouseenter', function() {
						obj.stop().animate({
							'opacity'	: '1.0',
							'height' : heightEnd + 'px',
							'width'  : widthEnd + 'px',
							'top'    : topPos + 'px',
							'left'   : leftPos + 'px'
						},{
							duration: o.duration,
							easing: o.easing
						});
					}).bind('mouseleave', function() {
						obj.stop().animate({
							'opacity'	: o.startOpacity,
							'height' : heightStart + 'px',
							'width'  : widthStart + 'px',
							'top'    : '0px',
							'left'   : '0px'
						},{
							duration: o.duration,
							easing: o.easing
						});
					});
				}
				else {
					obj.css({
						'position'	: 'relative',
						'opacity'	: o.startOpacity,
						'height' : heightEnd + 'px',
						'width'  : widthEnd + 'px',
						'top'    : topPos + 'px',
						'left'   : leftPos + 'px'
					});

					trigger.bind('mouseenter', function() {
						obj.stop().animate({
							'opacity'	: '1.0',
							'height' : heightStart + 'px',
							'width'  : widthStart + 'px',
							'top'    : '0px',
							'left'   : '0px'
						},{
							duration: o.duration,
							easing: o.easing
						});
					}).bind('mouseleave', function() {
						obj.stop().animate({
							'opacity'	: o.startOpacity,
							'height' : heightEnd + 'px',
							'width'  : widthEnd + 'px',
							'top'    : topPos + 'px',
							'left'   : leftPos + 'px'
						},{
							duration: o.duration,
							easing: o.easing
						});
					});
				}
			});
		}
	});

})(jQuery);

