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("#alternateImages a.thumbnail").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 widRegexp = new RegExp('wid=' +imgSizes.normal.width, 'gi');
		var imgToloadbig = imgToloadbig.replace(widRegexp, 'wid=' + imgSizes.big.width);

		var heiRegexp = new RegExp('hei=' + imgSizes.normal.height, 'gi');
		var imgToloadbig = imgToloadbig.replace(heiRegexp, 'hei=' + imgSizes.big.height);

		var imgToloadVerybig = imgToload.replace(imgSizes.normal.width + ',' + imgSizes.normal.width, imgSizes.veryBig.width + ',' + imgSizes.veryBig.width);
		var imgToloadVerybig = imgToloadVerybig.replace('wid=' +imgSizes.normal.width, 'wid=' + imgSizes.veryBig.width);
		var imgToloadVerybig = imgToloadVerybig.replace('hei=' + imgSizes.normal.height, 'hei=' + imgSizes.veryBig.height);
		
		jQuery.preloadImages(imgToload,imgToloadbig,imgToloadVerybig);
	});
	
	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 widRegexp = new RegExp('wid=' +imgSizes.normal.width, 'gi');
		var imgSrcBig = imgSrcBig.replace(widRegexp, 'wid=' + imgSizes.big.width);

		var heiRegexp = new RegExp('hei=' + imgSizes.normal.height, 'gi');
		var imgSrcBig = imgSrcBig.replace(heiRegexp, 'hei=' + imgSizes.big.height);

//		jQuery("#productImage").attr("jqimg", imgSrc);
		jQuery("#productImage").attr("jqimg", imgSrcBig);
//		jQuery("#productImage").attr("jqimgBig", imgSrcBig);
	}
	
	jQuery("#alternateImages 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');
    var firstThumb = thumbnails.eq(0); 
    if(firstThumb){
      thumbnails.bind( 'click', function(e){
       e = new Event(e);
       var thumbLink = this.href;
       jQuery("#productImage").attr("src", thumbLink);
       thumbLink = thumbLink.substring(0).split("?");
       thumbLink = thumbLink[0] + "?wid=1000&hei=1000&resmode=sharp";
       jQuery("#productImage").attr('jqimg', thumbLink);
       imgChange = true;
       e.stop();
    });
    	
      var thumbLen = thumbnails.length;
      var thumbLink = firstThumb.attr('href');
      jQuery('#productImage').attr('src', thumbLink);
      thumbLink = thumbLink.substring(0).split("?");
      thumbLink = thumbLink[0] + "?wid=1000&hei=1000&resmode=sharp";
      jQuery("#productImage").attr('jqimg', thumbLink);
    }
});

