jQuery.fn.ThinkImgCrop = function(options) {
	var ImgCrop;
	ImgCrop = new ThinkImgCrop(this, options);
	return ImgCrop;
}

/* ThinkImgCrop Class */
var ThinkImgCrop = function(box, options) {
	var _this = this;
	this.options = jQuery.extend({}, this.defaults, options || {}); // 设置配置项
	this.box = box; // 裁剪容器
	this.box.css({
		'position' : 'relative',
		'left' : 0,
		'top' : 0,
		'z-index' : this.options.zIndex--
	});

	/* 创建图片加载效果框 */
	this.box.append('<div class="Think_img_loading"></div>');
	this.loading = this.box.find('.Think_img_loading').css('z-index', this.options.zIndex + 10);

	/* 初始化图片剪切控件 */
	if (this.options.url)
		this.load_img(this.options.url);
};

/* 给ThinkImgCrop 添加属性和方法 */
ThinkImgCrop.prototype = {
	/* 默认设置 */
	defaults : {
		url : '', // 要裁剪的图片地址
		width : 100, // 裁剪区域宽度
		height : 100, // 裁剪区域高度
		center : true, // 图片默认是否居中
		keys : false, // 是否开启键盘支持
		minusBtn : '#minus', // 缩小按钮选择器
		plusBtn : '#plus', // 放大按钮选择器
		slideBox : '#slideBox',//滑动容器选择器
		slideBtn: '#slideBtn',//滑动块选择器
		step : 1, // 图片每次缩放百分比
		zIndex : 800
	// 遮罩层Z轴高度
	},

	/* 剪切数据 */
	crop : {},

	/* 是否已经载入 */
	isInit : false,

	/* 遮罩层 */
	mask : '<div class="Think_mask_center"></div><div class="Think_mask_top"></div><div class="Think_mask_right"></div><div class="Think_mask_bottom"></div><div class="Think_mask_left"></div>',

	/* 拖动数据 */
	draging : null,

	/* 当前图片缩放比例 */
	scale : 100,

	/* 图片最小缩放比例 */
	scaleMin : 1,

	/* 图片最大缩放比例 */
	scaleMax : 200,
	
	/* 滑动块 */
	slideBtn: null,
	
	/* 滑动数据 */
	slideing: null,

	/* 创建遮罩 */
	create_mask : function() {
		this.boxHeight = this.box.height();// 获取容器高度
		this.boxWidth = this.box.width(); // 获取容器宽度
		this.maskTop = (this.boxHeight - this.options.height) / 2;
		this.maskLeft = (this.boxWidth - this.options.width) / 2;

		this.box.append(this.mask);
		this.box.find('div').css({
			'z-index' : this.options.zIndex
		});
		this.box.find('.Think_mask_top').css({
			'width' : this.boxWidth,
			'height' : this.maskTop
		});
		this.box.find('.Think_mask_bottom').css({
			'width' : this.boxWidth,
			'height' : this.boxHeight - this.maskTop - this.options.height
		});
		this.box.find('.Think_mask_left').css({
			'width' : this.maskLeft,
			'height' : this.options.height,
			'top' : this.maskTop
		});
		this.box.find('.Think_mask_right').css({
			'width' : this.maskLeft,
			'height' : this.options.height,
			'top' : this.maskTop
		});

		/* 设置裁剪框 */
		this.box.find('.Think_mask_center').css({
			'width' : this.options.width,
			'height' : this.options.height,
			'top' : this.maskTop,
			'left' : this.maskLeft
		});

	},

	/* 初始化剪切控件 */
	init : function(img) {
		this.box.find('img').remove();
		this.box.prepend(jQuery(img));
		this.img = this.box.find('img').css({
			'position' : 'relative',
			'z-index' : 1
		}); // 被裁剪图片

		/* 获取当前图片的宽高和偏移量 */
		this.imageWidth = this.imgWidth = this.img.width();
		this.imageHeight = this.imgHeight = this.img.height();
		this.imgLeft = parseInt(this.img.css('left')) || 0;
		this.imgTop = parseInt(this.img.css('top')) || 0;

		/* 计算图片的最小缩放比例 */
		if (this.imageHeight / this.imageWidth > this.options.height
				/ this.options.width) {
			this.scaleMin = 100 * this.options.width / this.imageWidth;
		} else {
			this.scaleMin = 100 * this.options.height / this.imageHeight;
		}
		
		/* 绑定事件 */
		if (!this.isInit) {
			/* 创建遮罩 */
			this.create_mask();

			/* 绑定拖动事件 */
			this.drag();

			/* 绑定放大缩小按钮事件 */
			var _this = this;
			jQuery(this.options.minusBtn).click(function() {
				_this.img_scaling(-1);
			});
			jQuery(this.options.plusBtn).click(function() {
				_this.img_scaling(1);
			});

			/* 开启键盘支持 */
			if (this.options.keys)
				this.keydown();
				
			/* 绑定滑动调整放大比例控件 */
			this.slideBtn = jQuery(this.options.slideBtn);
			this.slideBoxWidth = jQuery(this.options.slideBox).width();
			this.slideBtnWidth = this.slideBtn.width();
			this.slide();
			
			this.isInit = true;
		}

		/* 居中图片 */
		if (this.options.center)
			this.set_img_position((this.boxWidth - this.imageWidth) / 2, (this.boxHeight - this.imageHeight) / 2);

		/* 设置初始裁剪数据 */
		this.set_crop(this.imgLeft, this.imgTop, this.options.width, this.options.height);
		
		/* 根据缩放比例设置滑动条的初始位置 */
		this.set_slide_scale(100); 
	},

	/* 加载图像 */
	load_img : function(url) {
		this.loading.show();
		var img = new Image(); // 创建一个Image对象，实现图片的预下载
		img.src = url;

		var _this = this;
		jQuery(img).load(function() {
			_this.loading.hide();
			_this.scale = 100; // 调整当前缩放比例
			_this.init(img);
		});
	},

	/* 卸载剪切插件 */
	remove : function(width, height) {
		this.box.find("div").remove();
		this.options = this.defaults;
		return null;
	},

	/* 拖动图片 */
	drag : function() {
		var _this = this;
		this.box.mousedown(function(evt) {
			this.onselectstart = function() {
				return false
			}; // 禁止选中文字
			this.unselectable = 'on'; // 禁止获取焦点
			this.style.MozUserSelect = 'none'; // 禁止火狐选中文字
		}).mousemove(
				function(evt) {
					var d = _this.draging;
					if (d !== null) {
						var top = d[3] + evt.pageY - d[1], left = d[2]
								+ evt.pageX - d[0];
						_this.set_img_position(left, top);
					}
				});
		this.box.find('.Think_mask_center').mousedown(
				function(evt) {
					_this.draging = [evt.pageX, evt.pageY, _this.imgLeft,
							_this.imgTop];
				});
		jQuery(document).mouseup(function() {
			_this.draging = null
		});
	},
	
	/* 滑动调整缩放比例 */
	slide: function(){
		var _this = this;
		this.slideBtn.mousedown(function(evt){
			this.onselectstart = function() {return false}; // 禁止选中文字
			this.unselectable = 'on'; // 禁止获取焦点
			this.style.MozUserSelect = 'none'; // 禁止火狐选中文字
			_this.slideing = [evt.pageX, _this.slideLeft];
		});
		jQuery(document)
			.mousemove(function(evt){
				var d = _this.slideing;
				if (d !== null) {
					var left = d[1] + evt.pageX - d[0];
					_this.set_slide(left);
				}
			})
			.mouseup(function() {
				_this.slideing = null
			});
	},
	
	/* 设置滑动块的位置 */
	set_slide: function(left, change){
		if (!this.slideBtn)
			return;
		if (change) {
			left += this.slideLeft;
		}
		left = Math.max(left, 0);
		left = Math.min(left, this.slideBoxWidth);

		this.slideLeft = left;
		this.slideBtn.css('left', left - this.slideBtnWidth / 2);

		this.set_scale_slide(left);
		/* 设置剪切坐标数据 */
		//this.set_crop(left, top, false, false);
	},
	
	/* 根据缩放比例设置滑动块的位置 */
	set_slide_scale: function(scale){
		var left = Math.round(this.slideBoxWidth * (scale - this.scaleMin) / (this.scaleMax - this.scaleMin));
		this.set_slide(left);
	},
	
	/* 根据滑动块的位置计算缩放比例 */
	set_scale_slide: function(left){
		var scale = left * (this.scaleMax - this.scaleMin) / this.slideBoxWidth + this.scaleMin;
		this.set_scaling_img(scale);
	},

	/* 键盘支持 */
	keydown : function() {
		var _this = this;
		jQuery(window).keydown(function(e) {
			var left = 0, top = 0;
			switch (e.keyCode) {
				case 37:
					left -= e.shiftKey ? 1 : 5;
					break;
				case 38:
					top -= e.shiftKey ? 1 : 5;
					break;
				case 39:
					left += e.shiftKey ? 1 : 5;
					break;
				case 40:
					top += e.shiftKey ? 1 : 5;
					break;
			}
			if (left != 0 || top != 0) {
				_this.set_img_position(left, top, true);
				return false; // 阻止window的keydown事件
			}
		});
	},

	/* 设置图片位置 */
	set_img_position : function(left, top, change) {
		if (!this.img)
			return;
		if (change) {
			left += this.imgLeft;
			top += this.imgTop;
		}
		left = Math.min(left, this.maskLeft);
		left = Math.max(left, this.options.width + this.maskLeft- this.imgWidth);
		top = Math.min(top, this.maskTop);
		top = Math.max(top, this.options.height + this.maskTop - this.imgHeight);
		this.imgLeft = left;
		this.imgTop = top;
		this.img.css({
			'left' : left,
			'top' : top
		});
		/* 设置剪切坐标数据 */
		this.set_crop(left, top, false, false);
	},

	/* 设置图片的尺寸 */
	set_img_size : function(width, height) {
		if (!this.img)
			return;
		this.imgWidth = width;
		this.imgHeight = height;
		this.img.css({
			'width' : width,
			'height' : height
		});
		/* 设置剪切尺寸数据 */
		this.set_crop(false, false, this.options.width, this.options.height);
	},

	/* 设置剪切数据 */
	set_crop : function(left, top, width, height) {
		if (left !== false)
			this.crop.x = Math.round(100 * (this.maskLeft - left) / this.scale);
		if (top !== false)
			this.crop.y = Math.round(100 * (this.maskTop - top) / this.scale);
		if (width !== false)
			this.crop.width = Math.round(100 * width / this.scale);
		if (height !== false)
			this.crop.height = Math.round(100 * height / this.scale);
	},

	/* 获取剪切后的数据 */
	get_crop : function() {
		if (!this.img)
			return false;
		else
			return this.crop;
	},

	/* 缩放图片 */
	img_scaling : function(type) {
		var width = (1 + type * this.options.step / 100) * this.imgWidth; // 缩放后的宽度
		var height = (1 + type * this.options.step / 100) * this.imgHeight; // 缩放后的高度
		var scale = 100 * (width / this.imageWidth + height / this.imageHeight) / 2; // 计算当前缩放比例
		if (!(scale < this.scaleMin || scale > this.scaleMax)) {
			this.set_scaling_img(scale, true);
		}
	},
	
	/* 根据缩放比例设置图片的大小及位置 */
	set_scaling_img: function(scale, slide){ 
		var width  = this.imageWidth * scale / 100;
		var height = this.imageHeight * scale / 100;
		var left   = -1 * (this.boxWidth / 2 - this.imgLeft) * (scale / this.scale - 1);
		var top    = -1 * (this.boxHeight / 2 - this.imgTop) * (scale / this.scale - 1);
		this.scale = scale;
		if(slide) this.set_slide_scale(scale);
		this.set_img_size(width, height);
		this.set_img_position(left, top, true);
	}
};
