(function($) {
	$.fn.crop = function(o,p,t) {	
		
		o=$.extend({
			self: 			this,
			image:			this.is("img") ? this : this.find('img:first'),
			ratio:			{ x: 70, y: 80 },
			preview:		'.preview'			
		}, o);	
		
		if(!o.image.length) return this;
	
		o._ratio = o.ratio.x/o.ratio.y;
		o.b = 1;
		
		t = this[0];
	
		
		if(!t.crop_image) 
		{			
			t.crop_image = {};
			t.crop_image.o = $("<div style='z-index:11;background:white;position:absolute;left:0;top:0;margin:1px 0px 0px 1px;'>");
			if(!$.browser.msie)t.crop_image.o.css("opacity", 0.7);			
			t.crop_image.c = $("<div style='z-index:12;position:absolute;border:1px dotted black;'>")
				.bind('mousedown', function(e) { $.fn.crop.md(e,o,t.crop_image); return false; });
			t.crop_image.m = $("<div style='cursor:move;position:relative;overflow:hidden;'>");
			t.crop_image.i = $("<img style='position:absolute;' src='"+this.attr("src")+"' />");
			t.crop_image.m.append(t.crop_image.i);
			t.crop_image.r = $("<div style='cursor:nw-resize;position:absolute;z-index:14;bottom:-5px;right:-6px;height:13px;width:13px;background:url(/images/css/base/icon/cropper.png)'>")
				.bind('mousedown', function(e) { $.fn.crop.md(e,o,t.crop_image,true); return false; });
			t.crop_image.c.append(t.crop_image.r).append(t.crop_image.m);
			if($.browser.msie)document.ondragstart=function(e){return false;};			
			this.wrap("<div style='position:relative'>").after(t.crop_image.c).after(t.crop_image.o);
			if(o.preview){
				t.crop_image.preview = $('<img src="'+o.image.attr('src')+'" />').appendTo(o.preview);			 
				t.crop_image.preview.wrap("<div style='overflow:hidden;position:relative;'>");
			}
		}
		
		t.crop_image._w=this.width();t.crop_image._h=this.height();
		if(t.crop_image._w==0 || t.crop_image._h==0) this.bind("load", function() {				
			t.crop_image._w=$(this).width();t.crop_image._h=$(this).height();
			$.fn.crop.load(o,t.crop_image);
		});
		else $.fn.crop.load(o,t.crop_image);
		return this;	
	}
	$.fn.crop.load = function(o,p) {
		p.w=o.w?o.w:o.ratio.x;p.h=o.h?o.h:o.ratio.y;p.x=o.x?o.x:10;p.y=o.y?o.y:10;
		p.o.css({width:p._w,height:p._h});
		if($.browser.msie)p.o.css("background", "url(/images/css/base/white-pixel_70.png)");
		$.fn.crop.move(p.x-o.b,p.y-o.b,0,0,o,p);
		$.fn.crop.resize(p.x+p.w,p.y+p.h,0,0,o,p);
		if(o.preview && p.preview.length ){
			p.pw=o.ratio.x;p.ph=o.ratio.y;
			p.preview.css({position:'absolute'}).parent().width(p.pw).height(p.ph);	
			$.fn.crop.preview(o,p,true);
		}
	};
	$.fn.crop.md = function(e,o,p,r) {			
		p.sx=e.pageX-parseInt(r?p.x+p.w:p.x);p.sy=e.pageY-parseInt(r?p.y+p.h:p.y);		
		$(document).bind('mousemove',function(e){$.fn.crop.mm(e,o,p,r);});
		$(document).bind('mouseup',function(e){$.fn.crop.mu(e,o,p,r);});
	};
	$.fn.crop.mm = function(e,o,p,r) {			
		if(!r) $.fn.crop.move(e.pageX,e.pageY,p.sx,p.sy,o,p);
		else $.fn.crop.resize(e.pageX,e.pageY,p.sx,p.sy,o,p);
		if(p.preview) $.fn.crop.preview(o,p,r);			
	};
	$.fn.crop.move = function(x,y,sx,sy,o,p) {
		p.x=Math.min(p._w-p.w,Math.max(0,x-sx));
		p.y=Math.min(p._h-p.h,Math.max(0,y-sy));
		p.c.css({left:p.x-o.b,top:p.y-o.b});
		p.i.css({left:-p.x,top:-p.y});
	};
	$.fn.crop.resize = function(x,y,sx,sy,o,p) {
		p.w=Math.min(p._w,Math.max(p.x,x-sx))-p.x;
		p.h=(1/o._ratio)*p.w;//Math.min(p._h,Math.max(p.y,y-sy))-p.y;
		if(p.h>p._h-p.y){p.h=p._h-p.y;p.w=(o._ratio)*p.h;}
		p.m.css({width:p.w,height:p.h});
	};
	$.fn.crop.preview = function(o,p,r) {
		p.preview.css({left:-p.x*(p.pw/p.w),top:-p.y*(p.ph/p.h)});
		if(r)p.preview.width(p.pw*(p._w/p.w)).height(p.ph*(p._h/p.h));
	};
	$.fn.crop.mu = function(e,o,p,r) {
		$(document).unbind('mouseup').unbind('mousemove');
		if(o.update){o.width=p.w;o.height=p.h;o.x=p.x;o.y=p.y;o.update(o);}
	};
})(jQuery);
