var imgChange = false;

var imgSizes = {
	normal : {
		width : 400,
		height : 400
	},
	big : {
		width : 900,
		height : 900
	},
	veryBig : {
		width : 1500,
		height : 1500
	}
}


jQuery(window).load(function() {
	jQuery.preloadImages = function() { // jQuery preloader
		for (var i = 0; i < arguments.length; i++) {
			jQuery("<img>").attr("src", arguments[i]);
		}
	}
	
	var thumbnails = jQuery('#imageNavigator a.thumbnail');
	
	thumbnails
		.each(function() { // preload each full size product image
			var imgToload = jQuery(this).attr("href");
	
			var sizeRegexp = new RegExp(imgSizes.normal.width + ',' + imgSizes.normal.width, 'gi');
			var imgToloadbig = imgToload.replace(sizeRegexp, imgSizes.big.width + ',' + imgSizes.big.width);
			
			var itsRegexp = new RegExp('sw=' + imgSizes.normal.width + '&sh=' + imgSizes.normal.height, 'gi');
			var imgToloadbig = imgToloadbig.replace(itsRegexp, 'sw=' + imgSizes.big.width + '&sh=' + imgSizes.big.height);		
			
			var imgToloadVerybig = imgToload.replace(imgSizes.normal.width + ',' + imgSizes.normal.width, imgSizes.veryBig.width + ',' + imgSizes.veryBig.width);
			
			jQuery.preloadImages(imgToload,imgToloadbig,imgToloadVerybig);
		})
		.click(function() {		
			var thumbLink = jQuery(this).attr('href');
			jQuery("#productImage").attr("src", thumbLink);

			var sizeRegexp = new RegExp(imgSizes.normal.width + ',' + imgSizes.normal.height, 'gi');
			var thumbLinkBig = thumbLink.replace(sizeRegexp, imgSizes.big.width + ',' + imgSizes.big.height);
		
			var itsRegexp = new RegExp('sw=' + imgSizes.normal.width + '&sh=' + imgSizes.normal.height, 'gi');
			var thumbLinkBig = thumbLink.replace(itsRegexp, 'sw=' + imgSizes.big.width + '&sh=' + imgSizes.big.height);	
			
			jQuery("#productImage").attr('jqimgbig', thumbLinkBig);
//			jQuery("#productImage").attr('jqimg', thumbLink);
			jQuery("#productImage").attr('jqimg', thumbLinkBig);
 
			imgChange = true;
			
			fnImgSrc();
		});		
	
	function fnImgSrc(){ // capture image src and repurpose for use with jqzoom
		var imgSrc = jQuery("#productImage").attr("src");

		var sizeRegexp = new RegExp(imgSizes.normal.width + ',' + imgSizes.normal.height, 'gi');
		var imgSrcBig = imgSrc.replace(sizeRegexp, imgSizes.big.width + ',' + imgSizes.big.height);
	
		var itsRegexp = new RegExp('sw=' + imgSizes.normal.width + '&sh=' + imgSizes.normal.height, 'gi');
		var imgSrcBig = imgSrcBig.replace(itsRegexp, 'sw=' + imgSizes.big.width + '&sh=' + imgSizes.big.height);	

//		jQuery("#productImage").attr("jqimg", imgSrc);
		jQuery("#productImage").attr("jqimg", imgSrcBig);
		jQuery("#productImage").attr("jqimgBig", imgSrcBig);
	}
	
	fnImgSrc();
	
	if(thumbnails.length > 0) {
		var thumbLen = thumbnails.length;
		var thumbLink = jQuery(thumbnails.get(0)).attr('href');
		jQuery("#productImage").attr('jqimg', thumbLink);
		fnImgSrc();
	}	
	
	jQuery(".jqzoom").jqueryzoom({
		zoomWidth: 538,
		zoomHeight: 352,
		xOffset: 18, //zooming div default offset(default offset value is 10)
		yOffset: 49, //zooming div default offset(default offset value is 10)
		position: "right", //zooming div position(default position value is "right")
		showEffect: 'show',
		hideEffect: 'hide',
		fadeinSpeed: 'fast',
		fadeout: 'fast',
		disableDoubleZoom: true,
		lens: 1
	});	 
});
