 // $DRUGGER 1.3 #####################################################################################
// jQuery required
// new drag({obj:$('.zoomimage')}); 

function drag(par){ //Обьект поддержки переноса объектов par.obj=$()
	
	if (!par) return false;
	this.callback=par.callback; //Функция отправки смещения
	this.$objs=par.obj; //Объект перетаскивания
	this.bwidth=0; //Длинна бегунка
	
	this.$objs.each(function(){
		$(this).parent().css({'width':$(this).width(),'height':$(this).height(),'left':$(this).position().left,'top':$(this).position().top});
		$(this).css({'cursor':'crosshair','position':'absolute','left':$(this).position().left,'top':$(this).position().top});
		$(this).attr('onMousedown',"return false");
		$(this).attr('onMouseup',"return false");
		$(this).attr('onClick',"return false");
		$(this).attr('onSelectstart',"return false");
		$(this).unbind();
	});
	
	this.dragging=false;
	this.xlimit=(par.xlimit)?{from:par.xlimit.from,to:par.xlimit.to}:{from:-1000000,to:1000000}; //Смещения в результате движения
	this.xlimit_def=(par.xlimit)?{from:par.xlimit.from,to:par.xlimit.to}:{from:-1000000,to:1000000}; //Смещения в результате движения

	this.ylimit=(par.ylimit)?{from:par.ylimit.from,to:par.ylimit.to}:{from:-1000000,to:1000000}; //Смещения в результате движения
	this.ylimit_def=(par.ylimit)?{from:par.ylimit.from,to:par.ylimit.to}:{from:-1000000,to:1000000}; //Смещения в результате движения

	this.xyOffset={x:0,y:0}; //Смещения в результате движения
	this.xyStart={x:-1,y:-1};//Начальные значения
	
	$(document).bind('mousedown',this,function(e){e.data.mousedown(e);});
	$(document).bind('mousemove',this,function(e){e.data.mousemove(e);});
	$(document).bind('mouseup',this,function(e){e.data.mouseup(e);});
		
	this.resize=function(r){ //(r - окно/канвас*100) Изменяет размер бегунка в зависимости от отношения
		if (r>0 && this.xlimit){ //Если размер бегунка изменяется в зависимотси от содержимого контейнера
			r=r>100?100:r;
			var bw=this.xlimit_def.to-this.xlimit_def.from;	
			this.bwidth=bw*r/100;
			$(this.$objs).animate({width:this.bwidth},200);
			this.xlimit.to=this.xlimit_def.to-this.bwidth;
		}
	};
	
	this.mousedown=function(e){
		var $f=false;
		this.$objs.each(function(){
			var $t=$(this);
			var $e=$(e.target);
			 if (this==$(e.target).parent().get(0) || this==$(e.target).get(0) || this==$(e.target).parent().parent().get(0)){ 
				$f=$t;
			 }
		 });

		if ($f){
			this.dragging=$f;
			var l=this.dragging.position().left;
			var t=this.dragging.position().top;
			this.X = e.pageX-l;
			this.Y = e.pageY-t;
			if (this.xyStart.x==-1) this.xyStart.x=l;
			if (this.xyStart.y==-1) this.xyStart.y=t;	
			return false;
		}
	};
	
	this.mousemove=function(e,o){
		if (this.dragging){
			var mCoord={x:e.pageX,y:e.pageY};
			this.xyOffset={x:mCoord.x-this.X-this.xyStart.x,y:mCoord.y-this.Y-this.xyStart.y};
			var xMove=mCoord.x-this.X;
			var yMove=mCoord.y-this.Y;
			
			if (this.xyOffset.x<this.xlimit.from) xMove=this.xlimit.from+this.xyStart.x;
			if (this.xyOffset.x>this.xlimit.to) xMove=this.xlimit.to+this.xyStart.x;
				
			if (this.xyOffset.y<this.ylimit.from) yMove=this.ylimit.from+this.xyStart.y;
			if (this.xyOffset.y>this.ylimit.to) yMove=this.ylimit.to+this.xyStart.y;
				
			this.dragging.css({left:xMove,top:yMove});
			 
			if (this.callback){
				//this.callback({x:(xMove-this.xyStart.x),y:yMove-this.xyStart.y});
				this.callback({
					x:this.xlimit.to?((xMove-this.xyStart.x)*this.xlimit_def.to/this.xlimit.to):0,
					y:this.ylimit.to?((xMove-this.xyStart.y)*this.ylimit_def.to/this.ylimit.to):0
				});
			}
			return false;
		}	
	};
	
	this.mouseup=function(e){
		this.dragging=false;	 
	}

}




