//jQuery marque tool v 1.0
//Copyright Ivan Makeev 2009
jQuery.marque = {
	rect: null,
	ecord: [0,0],
	helper: [0,0,0,0,0],
	adj_type: null,
	jtarget: null,

	in_rect: 
		function(e) {
			var rect = jQuery.marque.rect;
			if((e.clientX >= rect.left) && (e.clientX <= rect.right) && (e.clientY >= rect.top) && (e.clientY <= rect.bottom)) {
				return true;
			}
			return false;
		},
	mmove:
		function (e) {
			e.preventDefault();
			if(!jQuery.marque.in_rect(e)) { return;}
			jtarget = jQuery.marque.jtarget;
			rect = jQuery.marque.rect;
			helper = jQuery.marque.helper;
			w = jtarget.width(); h=jtarget.height(); 
			var x1 = ecord[0], y1 = ecord[1], x2 = e.clientX - rect.left, y2 = e.clientY-rect.top;
			if(adj_type=='v') {
				if (x1 > x2) { var tmp = x2; x2 = x1; x1 = tmp; }
				helper[0].css({left: x1, top: y1, width: x2-x1});
			}
			if(adj_type=='h') {
				if (y1 > y2) { var tmp = y2; y2 = y1; y1 = tmp; }
				helper[0].css({left: x1, top: y1, height: y2-y1});
			}
			if(adj_type=='vh') {
				if (x1 > x2) { var tmp = x2; x2 = x1; x1 = tmp; }
				if (y1 > y2) { var tmp = y2; y2 = y1; y1 = tmp; }
				helper[0].css({left: x1, top: y1, width: x2-x1, height: y2-y1});
			}
			hh = helper[0].height(); hw = helper[0].width(); hdom = helper[0].get(0);
			helper[1].css({
				top: 0,
				left: 0,
				width: w,
				height: helper[0].css("top")});
			helper[2].css({
				top: helper[0].css("top"),
				left: hw+hdom.offsetLeft+'px',
				width: w-hw-hdom.offsetLeft+'px',
				height: helper[0].css("height")});
			helper[3].css({
				top: hdom.offsetTop+hh+'px',
				left: 0,
				width: w,
				height: h - hdom.offsetTop - hh + 'px'});
			helper[4].css({
				top: hdom.offsetTop+'px',
				left: 0,
				width: hdom.offsetLeft+'px',
				height: hh+'px'});
			
		},
	
	medge: 
		function (e) {
			e.stopPropagation();
			cur = $(e.target).css("cursor");
			if(cur=='e-resize'||cur=='w-resize') {
				adj_type = 'v';
			}
			else 
				if(cur=='s-resize'||cur=='n-resize') {
					adj_type = 'h';
				}
				else
					adj_type = 'vh';
			pos = helper[0].position();
			if(cur=='sw-resize'||cur=='s-resize'||cur=='se-resize') {
				ecord[1]=pos.top;
			}
			else if (cur=='nw-resize'||cur=='n-resize'||cur=='ne-resize') {
				ecord[1]=pos.top + helper[0].height();
			}
			if(cur=='ne-resize'||cur=='e-resize'||cur=='se-resize') {
				ecord[0]=pos.left;
			}
			else if (cur=='nw-resize'||cur=='w-resize'||cur=='sw-resize') {
				ecord[0]=pos.left + helper[0].width();
			}
				
			jtarget.unbind('mousemove');
			jtarget.mousemove(jQuery.marque.mmove);
		},

	mup:
		function (e) {
			jQuery.marque.jtarget.unbind('mousemove');
			jQuery.marque.jtarget.unbind('mouseup');
			jQuery.marque.jtarget.unbind('click');
		},
	
	init:
		function (e) {
			e.preventDefault();
			jtarget = jQuery.marque.jtarget;
			jtarget.children("div").remove();
			helper = jQuery.marque.helper;
			if(jQuery.support.opacity) { //false in IE
				target = e.currentTarget;
			}
			else {
				target = e.originalEvent.srcElement;
			}
			jQuery.marque.rect = target.getBoundingClientRect();
			for(var i = 0; i< 5; i++) {
				helper[i] = jQuery(document.createElement("div"));
				jtarget.append(helper[i]);
				helper[i].css({
					'top': '0',
					'left': '0',
					'position':'absolute',
					'height':'0','width':'0'});
				helper[i].mouseup(jQuery.marque.mup);
			}
			helper[0].css({'border':'dotted 1px yellow'});
			for(i = 0; i < 8; i++) {
				prom = jQuery(document.createElement("div"));
				helper[0].append(prom);
				prom.mouseup(jQuery.marque.mup);
				prom.css({'width':'5px','height':'5px','background':'black','border':'yellow 1px solid','position':'absolute'});
				switch(i) {
				case 0: prom.css({'top': 	'-3px', 'left' :'-3px','cursor':'nw-resize'});break;
				case 1: prom.css({'top': 	'-3px', 'left' :'-3px','margin-left':'50%' ,'cursor':'n-resize'}) ;break;
				case 2: prom.css({'top': 	'-3px', 'right':'-3px','cursor':'ne-resize'});break;
				case 3: prom.css({'top': 	'50%' , 'right':'-3px','margin-top':'-3px', 'cursor':'e-resize'}) ;break;
				case 4: prom.css({'bottom': '-3px', 'right':'-3px','cursor':'se-resize'});break;
				case 5: prom.css({'bottom': '-3px', 'left' :'-3px','margin-left':'50%' ,'cursor':'s-resize'}) ;break;
				case 6: prom.css({'bottom': '-3px', 'left' :'-3px','cursor':'sw-resize'});break;
				case 7: prom.css({'top': 	'50%' , 'left' :'-3px','margin-top':'-3px', 'cursor':'w-resize'}) ;break;
				}
			}
			var color = jQuery.marque.rnd_color();
			helper[0].children("div").mousedown(jQuery.marque.medge);
			for(i = 1; i< 5; i++) {
				helper[i].css({
				'opacity':'0.5',
				'background':color});
			}
			jtarget.mousemove(jQuery.marque.mmove);
			jtarget.mouseup(jQuery.marque.mup);
			ecord = [e.clientX - jQuery.marque.rect.left, e.clientY - jQuery.marque.rect.top];
			adj_type= 'vh';
		},
	rnd_color:
		function () {
			var colors = ['0,204,237', '253,190,1', '43,7,241', '255,149,1', '241,0,21', '40,239,0', '237,1,137'];
			return 'rgb('+colors[parseInt(Math.random()*6)]+')';
		},
	marque: 
		function(){
			jQuery.marque.helper = [0,0,0,0,0];
			jQuery.marque.jtarget = this;
			this.css("cursor","crosshair");
			this.unbind();
			jQuery.marque.jtarget.unbind();
			this.mousedown(jQuery.marque.init);
			this.mouseup(jQuery.marque.mup);
			this.click(jQuery.marque.mup);
		},
	getRect:
		function(){
			helper = jQuery.marque.helper;
			if( helper[0] === 0 ) return false;
			var pos = helper[0].position();
			return  {top:pos.top,left:pos.left,width:helper[0].width(),height:helper[0].height()};
		},
	destroy: function(){
		this.css("cursor","");
		this.unbind();
		this.children("div").remove();
	}
};

jQuery.fn.extend({
		getRect: jQuery.marque.getRect,
		marqueDestroy : jQuery.marque.destroy,
		marque : jQuery.marque.marque
});

