//**************************************************************
// jQZoom allows you to realize a small magnifier window,close
// to the image or images on your web page easily.
//
// jqZoom version 2.2
// Author Doc. Ing. Renzi Marco(www.mind-projects.it)
// First Release on Dec 05 2007
// i'm looking for a job,pick me up!!!
// mail: renzi.mrc@gmail.com
//**************************************************************

(function($) {

	$.fn.jqueryzoom = function(options) {
		
		var settings = {
			xzoom: 200,
			//zoomed width default width
			yzoom: 200,
			//zoomed div default width
			offset: 10,
			//zoomed div default offset
			position: "right",
			//zoomed div default position,offset position is to the right of the image
			lens: 1,
			//zooming lens over the image,by default is 1;
			preload: 0,
			// preload images
			clickdrag: 1,
			// move only when dragging mouse over area
			autoload: 1,
			// automatically load zoomed image
			resize: 1
			// resize image if browser window resizes
		};
		
		if (options) { 
			$.extend(settings, options);
		}
		
		var noalt = '';
		
		var xpos = 0;
		var ypos = 0;
		
		var scrollx = 0;
		var scrolly = 0;
		
		var mouse = {};
		
		var imageLeft = $(this).offset().left;
		var imageTop = $(this).offset().top;
		
		var imageWidth = $(this).children('img.threeSixty').get(0).offsetWidth;
		var imageHeight = $(this).children('img.threeSixty').get(0).offsetHeight;
		
		var leftpos;
		
		var scalex;
    var scaley;

		

		var zoomMover = function(e) { 
			
			
			var xOffset = 0;
			var yOffset = 0;
			
			
			
			mouse = new MouseEvent(e);
			
			var bigwidth = $(".bigimg").get(0).offsetWidth;
			var bigheight = $(".bigimg").get(0).offsetHeight;

			scalex = (bigwidth / imageWidth);
			scaley = (bigheight / imageHeight);
			
			var jqZoomPup = {
				width: Math.round ( (settings.xzoom) / scalex ),
				height: Math.round ( (settings.yzoom) / scaley )
			}
			
			$("div.jqZoomPup").width( jqZoomPup.width );
			$("div.jqZoomPup").height( jqZoomPup.height );
			
			
			
			// console.log ( "xOffset: %d, yOffset: %d", xOffset, yOffset );
			
			if (settings.lens) {
				$("div.jqZoomPup").css('visibility', 'visible');
			}
			

			
			xpos = mouse.x - $("div.jqZoomPup").width() / 2 - imageLeft;
			ypos = mouse.y - $("div.jqZoomPup").height() / 2 - imageTop;
			
			
			if ( !e.originalEvent ) {
				// before user click...
				// xpos = 
				
				xpos = xOffset - ( mouse.x - $("div.jqZoomPup").width() / 2 - imageLeft );
				ypos = yOffset - ( mouse.y - $("div.jqZoomPup").height() / 2 - imageTop );
				
				// console.log ( "first position" );
				
				var xOuterCenter = $( "img.threeSixty" ).width () * 0.5;
				var yOuterCenter = $( "img.threeSixty" ).height () * 0.5;
				var xInnerCenter = jqZoomPup.width * 0.5;
				var yInnerCenter = jqZoomPup.height * 0.5;
				xOffset = xOuterCenter - xInnerCenter;
				yOffset = yOuterCenter - yInnerCenter;
				
			}
			
			
			if (settings.lens) {
				
				//xpos = (mouse.x - $("div.jqZoomPup").width() / 2 < imageLeft) ? xOffset: (mouse.x + $("div.jqZoomPup").width() / 2 > imageWidth + imageLeft) ? (imageWidth - $("div.jqZoomPup").width() - 2) : xpos;
				//ypos = (mouse.y - $("div.jqZoomPup").height() / 2 < imageTop) ? yOffset: (mouse.y + $("div.jqZoomPup").height() / 2 > imageHeight + imageTop) ? (imageHeight - $("div.jqZoomPup").height() - 2) : ypos;
				
				// console.log ( "mouse.x: %d, jqZoomPup.width: %d, imageLeft: %d", mouse.x, jqZoomPup.width, imageLeft );
				
				
				if (mouse.x - $("div.jqZoomPup").width() / 2 < imageLeft) {
					xpos = xOffset;
				} else if (mouse.x + $("div.jqZoomPup").width() / 2 > imageWidth + imageLeft) {
					xpos = imageWidth - $("div.jqZoomPup").width() - 2;
				}
				
				if (mouse.y - $("div.jqZoomPup").height() / 2 < imageTop) {
					ypos = yOffset;
				} else if (mouse.y + $("div.jqZoomPup").height() / 2 > imageHeight + imageTop) {
					ypos = imageHeight - $("div.jqZoomPup").height() - 2;
				}				
				
				$("div.jqZoomPup").css({
					top: ypos,
					left: xpos
				});
				
			}
			
			
			
			scrolly = ypos;
			$("div.zoomdiv").get(0).scrollTop = scrolly * scaley;
			scrollx = xpos;
			$("div.zoomdiv").get(0).scrollLeft = (scrollx) * scalex;
			
		}
		
		var startZoom = function(eve) {
			
			eve.preventDefault();
			
			noalt = $(this).children("img.threeSixty").attr("alt");
			$(this).children("img.threeSixty").attr("alt", '');
			
			if (settings.position == "right") {
				
				if (imageLeft + imageWidth + settings.offset + settings.xzoom > screen.width) {
			 		leftpos = imageLeft - settings.offset - settings.xzoom;
				} else {
					leftpos = imageLeft + imageWidth + settings.offset;
				}
			} else {
				leftpos = imageLeft - settings.xzoom - settings.offset;
				if (leftpos < 0) {
					leftpos = imageLeft + imageWidth + settings.offset;
				}
			}
			
			if (!settings.lens) { 
				$(this).css('cursor', 'crosshair');
			} 
			
			
			$(document.body).mousemove(zoomMover); //mousemove
			
		}
		var stopZoom = function() {
			
			$(this).children("img.threeSixty").attr("alt", noalt);
			$(document.body).unbind("mousemove");
			if (settings.lens) {
			 	// $("div.jqZoomPup").remove();
			}
			// $("div.zoomdiv").remove();
			
		}
		
		if (settings.clickdrag) {
			$(this).mousedown(startZoom);
			$(this).mouseup(stopZoom);
			// when mouse leaves stop
			//$(this).mouseout(stopZoom);
			$(this).bind("mouseleave", stopZoom);
		}
		else {
			$(this).hover(startZoom,stopZoom);
		}
		
		var count = 0;
		
		if (settings.preload) {
			
			$('body').append("<div style='display:none;' class='jqPreload" + count + "'>&nbsp;</div>");
			
			$(this).each(function() {
			
				var imagetopreload = $(this).children("img.threeSixty").attr("jqimg");
				var content = jQuery('div.jqPreload' + count + '').html();
				jQuery('div.jqPreload' + count + '').html(content + '<img src=\"' + imagetopreload + '\">');
				
			});
			
		}
		
		$.fn.unloadBackground = function() {
			if ($("div.zoomdiv").get().length != 0) {
				$("div.zoomdiv").fadeOut('slow',function() {
					$(this).remove();
					$('.jqZoomPup, #full-screener').remove();
				});
			}
		}
		
		$.fn.unload = function() {
			$('.jqzoom').removeClass('jqzoom')
			$(this).unbind('mousedown');
			$(this).unbind('mouseup');
			$(this).unbind('mouseleave');
			$.fn.unloadBackground();
			$(document.body).unbind("mousemove");
			$(window).unbind('resize');
			return this;
		};
		
		$.fn.loadBackground = function() {
			var bigimage = $('div.jqzoom').children("img.threeSixty").attr("rel"); // <-----
			
			if ($("div.zoomdiv").get().length == 0) {
				
				$('div.wrapper').after("<div class='zoomdiv'><img src='"+__SERVER_ROOT+"skin/frontend/default/default/images/opc-ajax-loader.gif' style='margin-top:400px' /></div>");
				$("div.zoomdiv").width(settings.xzoom);
				$("div.zoomdiv").height(settings.yzoom);
				
				var bigImg = new Image();
				bigImg.src = bigimage;
				$(bigImg).css({opacity: 0});
				
				$(bigImg).load(function() {
					
					

					$(this).addClass('bigimg');
					
					$('div.zoomdiv').hide();
					//$('div.zoomdiv').fadeTo(0);
					$('div.zoomdiv').html(bigImg);
					
					
						$('div.zoomdiv').fadeIn('fast', function () {			
							zoomMover({pageX: 0, pageY: 0});
							
							setTimeout(function() {
								$(bigImg).animate({opacity: 1});
							}, 100);
									
							//console.log ( "$('div.zoomdiv').get(0).scrollLeft: ", $("div.zoomdiv").get(0).scrollLeft );
							$('#full-details-button').click(gotoDetailsState);
						});

					//zoomMover({pageX: 0, pageY: 0});
					
					$(window).resize(function() {
						
						settings.xzoom = $( window ).width ();
						settings.yzoom = $( window ).height () - 75;
						$("div.zoomdiv").width( settings.xzoom );
						$("div.zoomdiv").height( settings.yzoom );
						
						zoomMover({pageX: 0, pageY: 0});
						
					});
				});
				
				$('div.jqzoom').append("<div class='jqZoomPup'>&nbsp;</div>");
				
				$("div.zoomdiv").show();
				
			}
			
		}
		
		if(settings.autoload) {
			$.fn.loadBackground();
		}
			
		// if(settings.resize) {
		// 	$(window).resize(loadBackground);
		// }
		
	}

})(jQuery);

function MouseEvent(e) {
	this.x = e.pageX;
	this.y = e.pageY;

}


