/**
* Plugin: jQuery AJAX-ZOOM, Magento JS helper file: magento_axZm.js
* Copyright: Copyright (c) 2010 Vadim Jacobi
* Licence: Commercial, free for personal use (demo version)
* License Agreement: http://www.ajax-zoom.com/index.php?cid=download
* Version: 2.1.4 Patch 2010-09-09
* Date: 2010-09-03
* URL: http://www.ajax-zoom.com
* Description: jQuery AJAX-ZOOM plugin - adds zoom & pan functionality to images and image galleries with javascript & PHP
* Documentation: http://www.ajax-zoom.com/index.php?cid=docs
*/

;(function($) {
	
	// Switch images function
	jQuery.fn.rollImage = function (url, orgImage, fadeInSpeed, linkObj){
		
		if ($('#axZm-img').attr('src') == url){return false;}
		
		if ($.axZmLoading){
			 $.axZmBreak = true;
		}
		
		$.axZmLoading = true;
				
		$('#axZm-product-link').attr('id','axZm-product-link_old').css({zIndex: 1, position: 'absolute'});
		$('#axZm-img').attr('id','axZm-img_old');

		var loadImg = new Image();

		$('<DIV />').attr('id', 'axZmLoadingMsg').css({
			width: (parseInt($('#axZm-product-image').width())),
			height: 15,
			padding: 5,
			position: 'absolute',
			/*backgroundColor: '#FFFFFF',*/
			opacity: 0,
			zIndex: 99
		}).html('Loading...').prependTo('#axZm-product-image').fadeTo('fast', 0.75);

		
		$(loadImg).load(function(){
			$('#axZmLoadingMsg').fadeTo(fadeInSpeed, 0).remove();
			
			var link = $('<a />').attr('id', 'axZm-product-link').css({display: 'block', opacity: 0, position: 'absolute', zIndex: 2});
			$('<img>').attr('src',url).attr('id', 'axZm-img').appendTo(link);
			
			if (!$.axZmBreak){
				$(link).prependTo('#axZm-product-image').fadeTo(fadeInSpeed, 1, function(){
					$('#axZm-product-link_old').remove();
					$('#axZm-img_old').remove();
					$(this).css({cursor: 'pointer'});
					jQuery.fn.zoomImage(orgImage, linkObj);
				});
			}else{
				$.axZmBreak = false;
			}
			
			$.axZmLoading = false;
			
		}).attr('src', url);
	}
	
	// Add AJAX-ZOOM in fancybox to the switched image
	jQuery.fn.zoomImage = function(orgImage, linkObj){
		function isObject(x) {
			return (typeof x === 'object') && !(x instanceof Array) && (x !== null);
		}
		
		var pArray = orgImage.split("/").reverse();
		var zoomFile = pArray[0]; // filename
		var zoomDir = orgImage.split(pArray[0]).join(''); // path
		
		// remove .jpg from filename, will be added in zoomObjects.inc.php
		zoomFile = zoomFile.split('.jpg').join('');
		
		// Query string
		var alink = axZm_BaseUrl + '/axZm/zoomLoad.php?zoomLoadAjax=1&example=magento&zoomFile='+zoomFile+'&zoomDir='+zoomDir;
		if (jQuery.zoomData){alink += '&zoomData='+jQuery.zoomData;}

		$('#axZm-img').attr('alt','').attr('title','');
		$('#axZm-product-link').attr('href', alink).unbind().fancybox({
			padding				: 14,
			overlayShow			: (jQuery.browser.msie ? false : true), // overlay can slow down performance in ie
			overlayOpacity		: 0.5,
			overlayColor		: '#000000',
			zoomSpeedIn			: 0,
			zoomSpeedOut		: 100,
			easingIn			: "swing",
			easingOut			: "swing",
			hideOnContentClick	: false, // Important
			centerOnScroll		: true,
			imageScale			: true,
			autoDimensions		: true,
			callbackOnShow		: function(){
				jQuery.fn.axZm(); // Important callback after loading
			}
		});
		
		if ((typeof linkObj === 'object') && !(linkObj instanceof Array) && (linkObj !== null)){
			$(linkObj).unbind().click(function(){
				jQuery('#axZm-product-link').click();
			});
		}
	}
	
	// jQuery 1.4.2 bugfix ie unload Ticket #6452
	if (jQuery.browser.msie) {
		$(window).unload(function(){
			for ( var id in jQuery.cache ) {
				var item = jQuery.cache[ id ];
				if ( item.handle ) {
					if ( item.handle.elem === window ) {
						for ( var type in item.events ) {
							if ( type !== "unload" ) {
								// Try/Catch is to handle iframes being unloaded, see #4280
								try {
									jQuery.event.remove( item.handle.elem, type );
								} catch(e) {}
							}
						}
					} else {
						// Try/Catch is to handle iframes being unloaded, see #4280
						try {
							jQuery.event.remove( item.handle.elem );
						} catch(e) {}
					}
				}
			}  
		});
	}
})(jQuery);

