jQuery.noConflict();

var imgChange = false;

var imgSizes = {
	normal : {
		width : 370,
		height : 370
	},
	big : {
		width : 1000,
		height : 1000
	},
	veryBig : {
		width : 2000,
		height : 2000
	}
}



function enableZoom() {
	jQuery(".jqzoom").jqueryzoom({
		zoomWidth: 544,
		zoomHeight: 440,
		xOffset: 30, //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
	});
}


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.width, 'gi');
	var imgSrcBig = imgSrc.replace(sizeRegexp, imgSizes.big.width + ',' + imgSizes.big.width);

	var widRegexp = new RegExp('sw=' +imgSizes.normal.width, 'gi');
	var imgSrcBig = imgSrcBig.replace(widRegexp, 'sw=' + imgSizes.big.width);

	var heiRegexp = new RegExp('sh=' + imgSizes.normal.height, 'gi');
	var imgSrcBig = imgSrcBig.replace(heiRegexp, 'sh=' + imgSizes.big.height);
//	console.log('imgSrcBig: ' + imgSrcBig);

//	jQuery("#productImage").attr("jqimg", imgSrc);
	jQuery("#productImage").attr("jqimg", imgSrcBig);
	jQuery("#productImage").attr("jqimgBig", imgSrcBig);

	enableZoom();
}

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 widRegexp = new RegExp('sw=' +imgSizes.normal.width, 'gi');
		var imgToloadbig = imgToloadbig.replace(widRegexp, 'sw=' + imgSizes.big.width);

		var heiRegexp = new RegExp('sh=' + imgSizes.normal.height, 'gi');
		var imgToloadbig = imgToloadbig.replace(heiRegexp, 'sh=' + imgSizes.big.height);
//		console.log('imgToloadbig : ' + imgToloadbig);

		var imgToloadVerybig = imgToload.replace(imgSizes.normal.width + ',' + imgSizes.normal.width, imgSizes.veryBig.width + ',' + imgSizes.veryBig.width);
		var imgToloadVerybig = imgToloadVerybig.replace('sw=' +imgSizes.normal.width, 'sw=' + imgSizes.veryBig.width);
		var imgToloadVerybig = imgToloadVerybig.replace('sh=' + imgSizes.normal.height, 'sh=' + imgSizes.veryBig.height);
//		console.log('imgToloadVerybig: ' + imgToloadVerybig);
		
		jQuery.preloadImages(imgToload,imgToloadbig,imgToloadVerybig);
	});
	
	
	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();
	 
	// init thumbnail controls
	var thumbnails = jQuery('#imageNavigator a.thumbnail');
	if(thumbnails[0]) {
		for (var i = 0; i < thumbnails.length; i++) {
			thumbnails[i].addEvent( '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.width, 'gi');
				var thumbLinkBig = thumbLink.replace(sizeRegexp, imgSizes.big.width + ',' + imgSizes.big.width);

				var widRegexp = new RegExp('sw=' +imgSizes.normal.width, 'gi');
				var thumbLinkBig = thumbLinkBig.replace(widRegexp, 'sw=' + imgSizes.big.width);

				var heiRegexp = new RegExp('sh=' + imgSizes.normal.height, 'gi');
				var thumbLinkBig = thumbLinkBig.replace(heiRegexp, '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 = thumbnails[0].getProperty('href');
		jQuery("#productImage").attr('jqimg', thumbLink);
	}
});

