/*
 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(document).ready(function(){
	
	var loupe = {'width' : 417, 'height': 381};
	
	var image = jQuery('#message').position();
	
	var iZoom;
	var iSize = {'width': 0, 'height': 0};
	
	jQuery('<div id="thejLoupe"/>').addClass('thejLoupe').css('position','absolute').css('width',loupe.width+'px').css('height',loupe.height+'px').prependTo('#conteneur_img_zoom');
	//jQuery('<div id="Loader"/>').appendTo('#thejLoupe');
	//jQuery('<div class="product-loader />"').appendTo('#Loader');
	//jQuery('<img />').attr('src', '/tdo/skin/frontend/default/tdo/images/loader.gif').appendTo('.product-loader');
	jQuery('<div id="zoomWrapper" />').css('width',loupe.width+'px').css('height',loupe.height+'px').css('overflow','hidden').appendTo('#thejLoupe');

	jQuery('.jLoupe').each(function(){
		var s = (jQuery(this).attr('longdesc') != undefined) ? jQuery(this).attr('longdesc') : jQuery(this).attr('src');
		var i = jQuery('<img />').attr('src', s).bind('load',function(){
				iSize = {'width':this.width, 'height':this.height};
				//jQuery('.product-loader').css('display','none');
			}).appendTo('#zoomWrapper');
		iZoom = i;
		
	})
	.bind('mousemove', function(e){
	    if (jQuery('#message').hasClass('opac'))
	    {
		var o = jQuery(this).offset();
		var i = iZoom;
		var zlo = ((e.pageX - o.left) / this.width) * iSize.width - (loupe.width/2) - 14;
		var zto = ((e.pageY - o.top) / this.height) * iSize.height - (loupe.height/2) - 14;
		jQuery(i).css('marginLeft', zlo * -1 + 'px').css('marginTop', zto * -1 + 'px');
	    }
	})
	.bind('click', function(e){
		if (jQuery('#message').hasClass('opac'))
		{
		  //jQuery('#thejLoupe').hide();
		  jQuery('#message').removeClass('opac');
		  jQuery('body').css('cursor','auto');
		}
		else
		{
		  jQuery('#Loader').show();
		  jQuery('#message').addClass('opac');
		  jQuery('#thejLoupe').show();
		  jQuery('body').css('cursor','move');
		}
		
	})
	.bind('mouseout',function(){
	    if (jQuery('#message').hasClass('opac'))
	    {
		  jQuery('#message').removeClass('opac');
		  jQuery('body').css('cursor','auto');
	    }
	})
	
	jQuery('.product-zoom-click').bind('click',function(){
	  if (jQuery('#message').hasClass('opac'))
		{
		  jQuery('#message').removeClass('opac');
		  jQuery('body').css('cursor','auto');
		}
		else
		{
		  //jQuery('#Loader').show();
		  jQuery('#message').addClass('opac');
		  jQuery('body').css('cursor','move');
		}
	});
});

