jQuery.fn.extend({
  drag: function(params){
    var jQ = jQuery;
    return this.each(function(){
			var clicked = false;
			var start_x;
			var start_y;	
			var drag_div = this;
			divY = $(this).height();
			divX = $(this).width();
			//hide the image & add as bg image
			bgimg = $('#'+this.id + ' img');
			imgx = bgimg.width();
			imgy = bgimg.height();
			fixX = divX - 1840;
			fixY = divY - 710;
			$(this).css('background', "url('"+bgimg.attr('src')+"') no-repeat left bottom");
			bgimg.css('display', 'none');
			bg = $(this).css('background-position');
			//bgimg.css('margin-top', '300px');
			//mouse down
			$(this).mousedown(function(e){
				$(this).css('cursor', 'move');
				clicked = true;
				start_x = Math.round(e.pageX - $(this).eq(0).offset().left);
				start_y = Math.round(e.pageY - $(this).eq(0).offset().top);
			});
			//mouse up
			$(this).mouseup(function(e){
				clicked = false;
			});
			//mouse move
			$(this).mousemove(function(e){
				if(clicked){ //as we only want this to work while they have clicked	
					bg = $(this).backgroundPosition();		
					if(bg.indexOf('%')>=1 || bg == 'left bottom'){
						leftpos = 0;
						toppos = -460;
					} else {
						bg = bg.replace("px", "").replace("px", "").split(" ");
						leftpos = parseInt(bg[0]);
						toppos = parseInt(bg[1]);	
					}
					var mouse_x = Math.round(e.pageX - $(this).eq(0).offset().left) - start_x;
					var mouse_y = Math.round(e.pageY - $(this).eq(0).offset().top) - start_y;					
					var x = leftpos + (mouse_x);
					var y = toppos + (mouse_y);
					if(x>0)x=0;
					if(y>0)y=0;
					if(x < fixX)x = fixX;
					if(y < fixY)y = fixY;
					start_x = Math.round(e.pageX - $(this).eq(0).offset().left);
					start_y = Math.round(e.pageY - $(this).eq(0).offset().top);					
					$(drag_div).css('background-position', x + "px " + y + "px");					
				}
			});	
			$(window).resize(function(){
//				divY = $(this).height();
				divX = $(this).width();
				fixX = divX - 1840;
//				fixY = divY - 710;
				$(drag_div).css('background-position', fixX + "px " + fixY + "px");
			});	
    });
  }
});

jQuery.fn.backgroundPosition = function() {
    var p = $(this).css('background-position');
    if(typeof(p) === 'undefined') return $(this).css('background-position-x') + ' ' + $(this).css('background-position-y');
    else return p;
  };
