/*
 * SergiusD CropImage 1.0
 *
 * Copyright (c) 2008 SergiusD (http://sergiusd.ru)
 * Date: 2008-08-08
 */

$.fn.CropImageCreate = function(src, bw, bh, w, h, s, opt) {
	var id = $(this).get(0).id;
	if (!id) {alert('Container must have ID'); return;}
	document.getElementById(id).crop_image = new SergiusdCropImage();
	document.getElementById(id).crop_image.set(id, src, bw, bh, w, h, s, opt);
}

$.fn.CropImageGetData = function() {
	var id = $(this).get(0).id;
	if (!id || document.getElementById(id).crop_image==undefined) {alert('Unknown object'); return;}		
	return document.getElementById(id).crop_image.getCoords();
}
$.fn.SetCropImageData = function(cx, cy, cw, ch) {	var id = $(this).get(0).id;	if (!id || document.getElementById(id).crop_image==undefined) {alert('Unknown object'); return;}			document.getElementById(id).crop_image.setCoords(cx, cy, cw, ch);
}

function SergiusdCropImage() {

		this.defaults = {
			background: '#fffcc4',
			opacity: 70,
			img_lt: '/data/cropimage_lt.gif',
			img_rt: '/data/cropimage_rt.gif',
			img_lb: '/data/cropimage_rt.gif',
			img_rb: '/data/cropimage_1t.gif',
			img_w: 10,
			img_h: 10
		};
	
		this.id = '';
		this.aw = 0; // all
		this.ah =  0;
		this.mw = 0; // min
		this.mh = 0;
		this.l = 0; // box
		this.r = 0;
		this.w = 0;
		this.h = 0;
		this.x = 0; // mouse pos
		this.y = 0;
		this.s = 0; // scale = w/h
		this.src = '';
		this.press = 0;
		this.btn = '';
		this.box = 0;
		this.opts = {};
	
		this.build = function() {
			$('#'+this.id)
				.css('position', 'relative')
				.html('');
			$('<div></div>')
				.attr('id', this.id+'_bg')
				.css({
					background: this.opts.background,
					opacity: this.opts.opacity/100
				})
				.appendTo('#'+this.id);
			$('<div></div>')
				.attr('id', this.id+'_box')
				.css({
					cursor: 'move',
					position: 'relative'
				})
				.append(
					'<img id="'+this.id+'_lt" src="'+this.opts.img_lt+'" style="position: absolute; left: 0; top: 0; cursor: nw-resize;" width="'+this.opts.img_w+'" height="'+this.opts.img_h+'" alt="" />' +
					'<img id="'+this.id+'_rt" src="'+this.opts.img_rt+'" style="position: absolute; right: 0; top: 0; cursor: ne-resize;" width="'+this.opts.img_w+'" height="'+this.opts.img_h+'" alt="" />' +
					'<img id="'+this.id+'_lb" src="'+this.opts.img_rt+'" style="position: absolute; left: 0; bottom: 0; cursor: sw-resize;" width="'+this.opts.img_w+'" height="'+this.opts.img_h+'" alt="" />' +
					'<img id="'+this.id+'_rb" src="'+this.opts.img_lt+'" style="position: absolute; right: 0; bottom: 0; cursor: se-resize;" width="'+this.opts.img_w+'" height="'+this.opts.img_h+'" alt="" />'
				)
				.appendTo('#'+this.id+'_bg');
		};
	
		this.set = function(id, src, aw, ah, mw, mh, s, options) {
			this.opts = $.extend({}, this.defaults, options);
			this.id = id;
			if (!$('#'+this.id)[0]) {alert('cropimage.js: Dont extst id="'+this.id+'"'); return false;}
			this.build();
			this.src = src;
			this.box = $('#'+this.id+'_box');
			this.aw = aw;
			this.ah = ah;
			this.mw = mw;
			this.mh = mh;
			this.s = s?(mw/mh):0;
			if (this.s) {
				if (aw/this.s <= ah) {
					this.w = aw;
					this.h = aw/this.s;
				} else {
					this.w = ah*this.s;
					this.h = ah;
				}
			} else {
				this.w = aw;
				this.h = ah;
			}
			this.l = Math.round((this.aw-this.w)/2);
			this.t = Math.round((this.ah-this.h)/2);
			
			$('#'+this.id)
				.css({width: this.aw+'px', height: this.ah+'px', background: 'url('+this.src+') center center no-repeat'});
			$('#'+this.id+'_bg').css({textAlign: 'left', height: this.ah+'px'});
			this.box.css('background-image', 'url('+this.src+')');
			this.setBox();
			if (!$.browser.msie)
	    	document.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.MOUSEMOVE);
			//if ($.browser.msie) document.ondragstart = function() {return false;};
	
			var this_obj = this;
			this.box.select(function() {return false;});
			$('#'+this.id+'_lt').mousedown(function(e) {this_obj.mouseDown('lt', e); return false;});
			$('#'+this.id+'_rt').mousedown(function(e) {this_obj.mouseDown('rt', e); return false;});
			$('#'+this.id+'_lb').mousedown(function(e) {this_obj.mouseDown('lb', e); return false;});
			$('#'+this.id+'_rb').mousedown(function(e) {this_obj.mouseDown('rb', e); return false;});
			$('#'+this.id+'_box').mousedown(function(e) {this_obj.mouseDown('c', e); return false;});
			$(document).mouseup(function() {this_obj.mouseUp();});
			$('#'+this.id+'_bg').mousemove(function(e) {this_obj.mouseMove(e); return false;});
		};
		
		this.mouseDown = function(pos, event) {
			if (this.press) return;
			this.mouseXY(event);
			this.press = 1;
			this.btn = pos;
		};
		
		this.mouseUp = function() {
			this.press = 0;
		};
		
		this.mouseMove = function(event) {
			if (this.press==0) return;
			var old_x = this.x;
			var old_y = this.y;
			var old_x2 = this.w+this.l;
			var old_y2 = this.h+this.t;
			this.mouseXY(event);
			var dx = this.x - old_x;
			var dy = this.y - old_y;
			switch (this.btn) {
				case 'c':
					this.l += dx;
					this.t += dy;
					if (this.w < this.mw) {this.l-=this.mw-this.w; this.x+=this.mw-this.w; this.w=this.mw;}
					if (this.h < this.mh) {this.t-=this.mh-this.h; this.y+=this.mh-this.h; this.h=this.mh;}
					break;
				case 'lt':
					this.w -= dx;
					this.h -= dy;
					this.l = old_x2-this.w;
					this.t = old_y2-this.h;
					if (this.w < this.mw) {this.l-=this.mw-this.w; this.x-=this.mw-this.w; this.w=this.mw;}
					if (this.h < this.mh) {this.t-=this.mh-this.h; this.y-=this.mh-this.h; this.h=this.mh;}
					if (this.s) {
						if (Math.abs(dx)>Math.abs(dy)) {
							var old_h = this.h;
							this.scopeH();
							this.t -= this.h - old_h;
						} else {
							var old_w = this.w;
							this.scopeW();
							this.l -= this.w - old_w;
						}
					}
					break;
				case 'rt':
					this.w += dx;
					this.h -= dy;
					this.t = old_y2-this.h;
					if (this.w < this.mw) {this.x+=this.mw-this.w; this.w=this.mw;}
					if (this.h < this.mh) {this.t-=this.mh-this.h; this.y-=this.mh-this.h; this.h=this.mh;}
					if (this.s) {
						if (Math.abs(dx)>Math.abs(dy)) {
							var old_h = this.h;
							this.scopeH();
							this.t -= this.h - old_h;
						} else {
							this.scopeW();
						}
					}
					break;
				case 'lb':
					this.w -= dx;
					this.h += dy;
					this.l = old_x2-this.w;
					if (this.w < this.mw) {this.l-=this.mw-this.w; this.x-=this.mw-this.w; this.w=this.mw;}
					if (this.h < this.mh) {this.y+=this.mh-this.h; this.h=this.mh;}
					if (this.s) {
						if (Math.abs(dx)>Math.abs(dy)) {
							this.scopeH();
						} else {
							var old_w = this.w;
							this.scopeW();
							this.l -= this.w - old_w;
						}
					}
					break;
				case 'rb':
					this.w += dx;
					this.h += dy;
					if (this.w < this.mw) {this.x+=this.mw-this.w; this.w=this.mw;}
					if (this.h < this.mh) {this.y+=this.mh-this.h; this.h=this.mh;}
					if (this.s) {
						if (Math.abs(dx)>Math.abs(dy)) {
							this.scopeH();
						} else {
							this.scopeW();
						}
					}
					break;
			}
			this.setBox();
		};
		
		this.scopeH = function() {
			if (this.w > this.aw) this.w = this.aw;
			this.h = this.w/this.s;
			if (this.h > this.ah) {
				this.h = this.ah;
				this.w = this.h*this.s;
			}
		};
		
		this.scopeW = function() {
			if (this.h > this.ah) this.h = this.ah;
			this.w = this.h*this.s;
			if (this.w > this.aw) {
				this.w = this.aw;
				this.h = this.w/this.s;
			}
		};
		
		this.setBox = function() {
			// min & max
			if (this.w<this.mw) {
				this.w = this.mw;
			} else if (this.w>this.aw) {
				this.w = this.aw;
			}
			if (this.h<this.mh) {
				this.h = this.mh;
			} else if (this.h>this.ah) {
				this.h = this.ah;
			}
			// edge
			if (this.l<0) this.l = 0;
			if (this.t<0) this.t = 0;
			if (this.l+this.w > this.aw) this.l = this.aw-this.w;
			if (this.t+this.h > this.ah) this.t = this.ah-this.h;
			// apply
			this.box.css({left: this.l+'px', top: this.t+'px', width: this.w+'px', height: this.h+'px',
				backgroundPosition: '-'+this.l+'px -'+this.t+'px'});
		};
		
		this.mouseXY = function(event) {
			var e = event || window.event;
			this.x = e.pageX;
			this.y = e.pageY;
		};
		
		this.getCoords = function() {			return {				src: this.src,				left: Math.floor(this.l),				top: Math.floor(this.t),				width: Math.floor(this.w),				height: Math.floor(this.h)			};		};		this.setCoords = function(cx, cy, cw, ch) { 			this.l = cx; 			this.t = cy; 			this.w = cw; 			this.h = ch;			this.setBox();			return 1;			};		
}
