/*
 jQuery Loupe v1.1
 Copyright (C) 2008 Chris Iufer (chris@iufer.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>

*/

jQuery(function(){

	var loupe = {'width' : 200, 'height': 151};
	
	jQuery('<div id="thejLoupe"/>').addClass('thejLoupe').css('position','absolute').css('width',loupe.width+'px').css('height',loupe.height+'px').css('z-index','9999').css('backgroundColor','rgba(0,0,0,0.25)').hide().appendTo('body');
	jQuery('<div id="zoomWrapper" />').css('width',loupe.width-10+'px').css('height',loupe.height-10+'px').css('overflow','hidden').css('marginTop','5px').css('marginLeft','5px').appendTo('#thejLoupe');

	jQuery('.jLoupe').each(function(){
                var s = '';
                if (typeof(loupeFullImage) != undefined)
                    s = loupeFullImage;
                else
                    s = (jQuery(this).attr('longdesc') != undefined) ? jQuery(this).attr('longdesc') :
                        ((jQuery(this).attr('rel') != undefined) ? jQuery(this).attr('rel') : jQuery(this).attr('src'));
                if (typeof(loupeImageWidth) != undefined)
                    this.width = loupeImageWidth;
                else
                    this.width = jQuery(this).attr('width');
                if (typeof(loupeImageHeight) != undefined)
                    this.height = loupeImageHeight;
                else
                    this.height = jQuery(this).attr('height');
                var i = jQuery('<img />').bind('load',function(){
				jQuery(this).data('size',{'width':this.width, 'height':this.height});
			}).attr('src', s).hide().appendTo('#zoomWrapper'); //hide().
		jQuery(this).data('zoom',i);
	})
	.bind('mousemove', function(e){ 
		var o = jQuery(this).offset();
		var i = jQuery(this).data('zoom');
                jQuery('#thejLoupe').css('left',e.pageX+10+'px').css('top',e.pageY+10+'px');
		var zlo = ((e.pageX - o.left) / this.width) * jQuery(i).data('size').width - (loupe.width/2) - 14;
		var zto = ((e.pageY - o.top) / this.height) * jQuery(i).data('size').height - (loupe.height/2) - 14;
		jQuery(i).css('marginLeft', zlo * -1 + 'px').css('marginTop', zto * -1 + 'px').show();
	})
	.bind('mouseout', function(e){
		jQuery(this).data('zoom').hide();
		jQuery('#thejLoupe').hide();
	})
	.bind('mouseover', function(e){
		jQuery(this).data('zoom').show();
		jQuery('#thejLoupe').show();
	});

        
});
