jQuery.noConflict();

var imgChange = false;

var imgSizes = {
	normal : {
		width : 330,
		height : 330
	},
	big : {
		width : 1000,
		height : 1000
	},
	veryBig : {
		width : 1000,
		height : 1000
	}
}


jQuery(document).ready(function() {
	jQuery.preloadImages = function() { // jQuery preloader
		for (var i = 0; i < arguments.length; i++) {
			jQuery("<img>").attr("src", arguments[i]);
		}
	}
	jQuery("#imageNavigator a.thumbnail").each(function() { // preload each full size product image
		var imgToload = jQuery(this).attr("href");
//		console.log('imgToload: ' + imgToload);

		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);
	});
	
	function fnImgSrc(){ // capture image src and repurpose for use with jqzoom
		var imgSrc = jQuery("#productImage").attr("src");
//		console.log('imgSrc: ' + imgSrc);

		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);
	}
	
	jQuery("#imageNavigator a.thumbnail").click(function () { // change product image on click
		var altImage = jQuery(this).attr("href");
		jQuery("#productImage").attr("src", altImage);
		fnImgSrc();
		return false;
	});
	fnImgSrc();
	
	jQuery(".jqzoom").jqueryzoom({
		zoomWidth: 565,
		zoomHeight: 420,
		xOffset: 16, //zooming div default offset(default offset value is 10)
		yOffset: -7,
		position: "right", //zooming div position(default position value is "right")
		showEffect: 'show',
		hideEffect: 'hide',
		fadeinSpeed: 'fast',
		fadeout: 'fast',
		disableDoubleZoom: true,
		lens:1
	});
	 
	// init thumbnail controls
	var thumbnails = jQuery('#alternateImages a.thumbnail');
	if(thumbnails[0]) {
		for (var i = 0; i < thumbnails.length; i++) {
			jQuery(thumbnails[i]).click(function(e) {
				e = new Event(e);
				var thumbLink = this.getProperty('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;
				e.stop();
			});
		}
		var thumbLen = thumbnails.length;
		var thumbLink = jQuery(thumbnails[0]).attr('href');
		jQuery("#productImage").attr('jqimg', thumbLink);
	}
});
