/**
* EasyDrag 1.4 - Drag & Drop jQuery Plug-in
*
* Thanks for the community that is helping the improvement
* of this little piece of code.
*
* For usage instructions please visit http://fromvega.com
*/

(function($){

	// to track if the mouse button is pressed
	var isMouseDown    = false;

	// to track the current element being dragged
	var currentElement = null;

	// callback holders
	var dropCallbacks = {};
	var dragCallbacks = {};

	// global position records
	var lastMouseX;
	var lastMouseY;
	var lastElemTop;
	var lastElemLeft;
	
	// track element dragStatus
	var dragStatus = {};	
	
	//right product zoom image
	var imageZoom = null;

	//zoomBox dimensions
	var zoomBoxWidth = 118;
	var zoomBoxHeight = 150;

	//product image dimensions
	var productImageWidth = 400;
	var productImageHeight = 400;

	//scale of zoom image compared to grey zoom box
	var imageZoomScale = 3;

	// returns the mouse (cursor) current position
	jQuery.getMousePosition = function(e){
		var posx = 0;
		var posy = 0;

		if (!e) var e = window.event;

		if (e.pageX || e.pageY) {
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) {
			posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
		}
		
		return { 'x': posx, 'y': posy };
	};

	// updates the position of the current element being dragged
	jQuery.updatePosition = function(e)
	{
		var pos = jQuery.getMousePosition(e);
		
		var setX = 0;
		var setY = 0;

		imageOffset = jQuery("#productImage").offset();

		var relPosX = pos.x - imageOffset.left;
		var relPosY = pos.y - imageOffset.top;

		//alert(relPosX + "|" + relPosY);

		if(relPosX < zoomBoxWidth/2)
		{
			setX = 0;
		}

		else if(relPosX > (productImageWidth - zoomBoxWidth/2))
		{
			setX = productImageWidth - zoomBoxWidth;
		}

		else
		{
			setX = relPosX - zoomBoxWidth/2;
		}

		if(relPosY < zoomBoxHeight/2)
		{
			setY = 0;
		}

		else if(relPosY > (productImageHeight - zoomBoxHeight/2))
		{
			setY = productImageHeight - zoomBoxHeight;
		}

		else
		{
			setY = relPosY - zoomBoxHeight/2;
		}

		jQuery("#zoomBox").css("top", setY);
		jQuery("#zoomBox").css("left", setX);
		imageZoom.css("top",  setY*(-1 * imageZoomScale));
		imageZoom.css("left", setX*(-1 * imageZoomScale));
	};
	
	
	// On ne l'utilise pas mais on ne sais jamais.
	jQuery.updatePositionWhenZoom = function(e) {
		
		currentElement = e;
		
		if (parseInt(jQuery(currentElement).css("left")) < limitBorderx) {
			jQuery(currentElement).css("left", limitBorderx);
		}
		if (parseInt(jQuery(currentElement).css("left")) > 0) {
			jQuery(currentElement).css("left", 0);
		} 
		
		if (parseInt(jQuery(currentElement).css("top")) < limitBordery) {
			jQuery(currentElement).css("top", limitBordery);
		}
		if (parseInt(jQuery(currentElement).css("top")) > 0) {
			jQuery(currentElement).css("top", 0);
		} 
		
	};
	
	jQuery.updatePositionWhenZoom_hide = function(e) {
		
		currentElement = e;
		
		if (parseInt(centerLeft) < limitBorderx) {
			centerLeft = limitBorderx;
		}
		if (parseInt(centerLeft) > 0) {
			centerLeft = 0;
		} 
		if (parseInt(centerTop) < limitBordery) {
			centerTop = limitBordery;
		}
		if (parseInt(centerTop) > 0) {
			centerTop = 0;
		} 
		
	};


	// when the mouse is moved while the mouse button is pressed
	jQuery(document).mousemove(function(e){
	var pos = jQuery.getMousePosition(e);
	
			if(isMouseDown && dragStatus[currentElement.id] == 'on'){
				// update the position and call the registered function
				jQuery.updatePosition(e);
				
				if(dragCallbacks[currentElement.id] != undefined){
					dragCallbacks[currentElement.id](e, currentElement);
				}

				return false;
			}

	});

	// when the mouse button is released
	jQuery(document).mouseup(function(e){
		if(isMouseDown && dragStatus[currentElement.id] == 'on'){
			isMouseDown = false;
			if(dropCallbacks[currentElement.id] != undefined){
				dropCallbacks[currentElement.id](e, currentElement);
			}
			return false;
		}
	});

	// register the function to be called while an element is being dragged
	jQuery.fn.ondrag = function(callback){
		return this.each(function(){
			dragCallbacks[this.id] = callback;
		});
	};

	// register the function to be called when an element is dropped
	jQuery.fn.ondrop = function(callback){
		return this.each(function(){
			dropCallbacks[this.id] = callback;
		});
	};
	
	// stop the element dragging feature
	jQuery.fn.dragOff = function(){
		return this.each(function(){
			dragStatus[this.id] = 'off';
		});
	};
	
	
	jQuery.fn.dragOn = function(){
		return this.each(function(){
			dragStatus[this.id] = 'on';
		});
	};

	// set an element as draggable - allowBubbling enables/disables event bubbling
	jQuery.fn.easydrag = function(allowBubbling)
	{

		return this.each(function()
		{
			imageZoom = jQuery("#imageZoom");

			// if no id is defined assign a unique one
			if(undefined == this.id || !this.id.length) this.id = "easydrag"+(new Date().getTime());

			// set dragStatus 
			dragStatus[this.id] = "on";
			
			// change the mouse pointer
			jQuery(this).css("cursor", "none");

			// set it as absolute positioned
			jQuery(this).css("position", "absolute");		
			
			//jQuery(this).css("top", 100);
			//jQuery(this).css("left", 0);

			imageZoom.css("top", 100*imageZoomScale*-1);
			imageZoom.css("left", 0);

			jQuery(this).attr("width", zoomBoxWidth);

			// when an element receives a mouse press
			jQuery(this).mouseover(function(e)
			{

				// set z-index
				jQuery(this).css("z-index", "10000");

				// update track variables
				isMouseDown    = true;
				currentElement = this;

				// retrieve positioning properties
				jQuery.updatePosition(e);

				return allowBubbling ? true : false;	
			});
			
			jQuery(this).mouseout(function(e)
			{
				// set z-index
				jQuery("#zoomBox").css("display","none");
				jQuery("#productZoom").css("display","none");
				jQuery("#productInfoBox").css("display","block");

				return false;
			});

			jQuery("#productImage").click(function(e)
			{
				var zoomBox = jQuery("#zoomBox");
				
				jQuery("#productInfoBox").css("display","none");
				zoomBox.css("display","block");
				jQuery("#productZoom").css("display","block");

				jQuery.updatePosition(e);

				//alert(setX + "|" + setY);
				return true;
			});
		});
	};


})(jQuery);
