/* $Id: jquery.jqzoom.CMRG.js,v 1.5 2009/03/26 09:01:55 eherrera Exp $ */
(function($){

		$.fn.jqueryzoom = function(options){

		var active = true;
		var ready = true;
		
		this.deactivate = function(){
			active = false;
			hide();
		};
		
		this.activate = function(){
			active = true;		
		};
		
		this.zoomReady = function(sts){
			var tReady = sts ? true: false;
			if (tReady) {
				$("div.zoomdiv", "#"+settings.IdContainer).show();
			}
			ready = tReady;
			
		};
		
		var settings = {
				xzoom: 200,		//zoomed width default width
				yzoom: 200,		//zoomed div default width
				xoffset: false,		//zoomed div default left offset
				yoffset: false,		//zoomed div default top offset
				lens:1, //zooming lens over the image,by default is 1;
				preload: 1,
				IdContainer: false, //-REQUIRED- ID for the container div defining zoom position(must have position:relative)
				initXPos: false,
				initYPos: false

			};

		if(options) {
			$.extend(settings, options);
		}

	    var noalt='';
		
		var show = function(){
        
	        if (!active) return;   
		    var imageLeft = $(this).offset().left;                
		    var imageTop = $(this).offset().top;
			
	           
		    var imageWidth = $(this).children('img').get(0).offsetWidth;
		    var imageHeight = $(this).children('img').get(0).offsetHeight;
	
	
	        noalt= $(this).children("img").attr("alt");
	
			var bigimage = $(".jqimage", this).attr("src");
	
	        $(this).children("img").attr("alt",'');
	
		    if(settings.IdContainer && $("div.zoomdiv", "#"+settings.IdContainer).length == 0){
			$("#"+settings.IdContainer).prepend("<div class='zoomdiv'><img class='bigimg' src='"+bigimage+"'/></div>");
		    }
		    else if($("div.zoomdiv", "#"+settings.IdContainer).length == 1){
		    	$("img", $("div.zoomdiv", "#"+settings.IdContainer)).attr('src', bigimage);
		    }
			if ($('.jqZoomPup', this).length == 0) $(this).append("<div class='jqZoomPup'>&nbsp;</div>");
	
	
		    if (settings.xoffset) $("div.zoomdiv", "#"+settings.IdContainer).css({ left: settings.xoffset});
		    if (settings.yoffset) $("div.zoomdiv", "#"+settings.IdContainer).css({ top: settings.yoffset});
	
		    $("div.zoomdiv", "#"+settings.IdContainer).width(settings.xzoom);
	
		    $("div.zoomdiv", "#"+settings.IdContainer).height(settings.yzoom);
	
	        if (ready) $("div.zoomdiv", "#"+settings.IdContainer).show();
	
	        if(!settings.lens){
	          $(this).css('cursor','crosshair');
			}
	
		 	$(document.body).mousemove(function(e){
				if (!ready) return;
				
      			mouse = new MouseEvent(e);

			    var bigwidth = $(".bigimg", "#"+settings.IdContainer).get(0).offsetWidth;

			    var bigheight = $(".bigimg", "#"+settings.IdContainer).get(0).offsetHeight;

			    var scaley ='x';

			    var scalex= 'y';


			    if(isNaN(scalex)|isNaN(scaley)){
	
				    var scalex = (bigwidth/imageWidth);
	
				    var scaley = (bigheight/imageHeight);
	
	
	
				    $("div.jqZoomPup").width((settings.xzoom)/scalex );
	
		    		$("div.jqZoomPup").height((settings.yzoom)/scaley);
	
	                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(settings.lens){
	
	                xpos = (mouse.x - $("div.jqZoomPup").width()/2 < imageLeft ) ? 0 : (mouse.x + $("div.jqZoomPup").width()/2 > imageWidth + imageLeft ) ?  (imageWidth -$("div.jqZoomPup").width() -2)  : xpos;
	
					ypos = (mouse.y - $("div.jqZoomPup").height()/2 < imageTop ) ? 0 : (mouse.y + $("div.jqZoomPup").height()/2  > imageHeight + imageTop ) ?  (imageHeight - $("div.jqZoomPup").height() -2 ) : ypos;
	
                }
	
	
                if(settings.lens){
	
	                $("div.jqZoomPup").css({ top: ypos,left: xpos });
	
	            }
	
	
	
				scrolly = ypos;
				scrollx = xpos;
				
				var tY = scrolly * scaley;
				var tX = (scrollx) * scalex;
				
				$("div.zoomdiv", "#"+settings.IdContainer).get(0).scrollTop = tY;

				$("div.zoomdiv", "#"+settings.IdContainer).get(0).scrollLeft = tX;

			    });
	    };
		
		var hide = function(){
			if(settings.lens){
		       $("div.jqZoomPup").remove();
		       }
			$("div.zoomdiv", "#"+settings.IdContainer).fadeOut();
		};
		
	    $(this).hover(
	    	show,
	    	function(){
           		$(this).children("img").attr("alt",noalt);
	       		$(document.body).unbind("mousemove");
	       		$("div.zoomdiv", "#"+settings.IdContainer).fadeOut();
				//hide();
	    	}
	    ); 

        count = new Date().valueOf();

		if(settings.preload){

		$('body').append("<div style='display:none;' class='jqPreload"+count+"'>sdsdssdsd</div>");

		$(this).each(function(){

        var imagetopreload= $(".jqimage", this).attr("src");

        var content = jQuery('div.jqPreload'+count+'').html();

        jQuery('div.jqPreload'+count+'').html(content+'<img src=\"'+imagetopreload+'\">');

		});

		}
		return this;
		}

})(jQuery);

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


}


