jQuery.fn.extend({
  drag: function(params){
    var jQ = jQuery;
    return this.each(function(){
			var clicked = false;
			var start_x;
			var start_y;	
			jQuery(this).find('a').attr('unselectable','on');
			var drag_div = this;
			var left_min = -502;
			var top_min = -1003;
			bgimg = jQuery('#'+this.id + ' img');
			imgx = bgimg.width();
			imgy = bgimg.height();
			jQuery(this).css({cursor: 'move',backgroundImage: "url('"+bgimg.attr('src')+"')",backgroundRepeat:'no-repeat',backgroundPosition: '-256px -502px'});
			bgimg.css('display', 'none');
			jQuery(this).mousedown(function(e){
				clicked = true;
				start_x = Math.round(e.pageX - jQuery(this).eq(0).offset().left);
				start_y = Math.round(e.pageY - jQuery(this).eq(0).offset().top);
			});
			jQuery(this).mouseup(function(e){
				clicked = false;
			});
			jQuery(this).mousemove(function(e){
				if(clicked){ //as we only want this to work while they have clicked	
					bg = jQuery(this).css('backgroundPosition');					
					bg = bg.replace(/px/g, "").split(" ");
					leftpos = parseInt(bg[0],10);
					toppos = parseInt(bg[1],10);	
					var mouse_x = Math.round(e.pageX - jQuery(this).eq(0).offset().left) - start_x;
					var mouse_y = Math.round(e.pageY - jQuery(this).eq(0).offset().top) - start_y;					
					var x = leftpos + (mouse_x);
					var y = toppos + (mouse_y);	
					if (x < left_min) x = left_min;
					if (x > 0) x = 0;
					if (y < top_min) y = top_min;
					if (y > 0) y = 0;					
					start_x = Math.round(e.pageX - jQuery(this).eq(0).offset().left);
					start_y = Math.round(e.pageY - jQuery(this).eq(0).offset().top);	
					jQuery(drag_div).css('backgroundPosition', x+ "px " + y + "px");	
					jQuery('#cursors').css({top:y+'px',left:x+'px'});		
				}
				jQuery(this).mouseout(function(e){
					clicked = false;
				});
			});			
    });
  }
});