
/*  this page must be included after config_colorSizeSelect.js since the product object is initialized there */

product.swapImage = function(classStyleColor,folderIndex)
{
	var indexes = product.getProductAndColorIndex(classStyleColor);
	var productIndex = indexes[0];
	var colorIndex = indexes[1];
	// swap image
	var imageDir = '';
	switch(product.config.page)
	{
		case 'pdp':
			imageDir = product.config.quickView ? product.config.folders.quickView[folderIndex] : product.config.folders.full[folderIndex];
			break;
		case 'fi':
			imageDir = '_f?$cat-313$'; //Pre-S7: imageDir = '313x322';
			break;
		default:
			imageDir = product.config.folders.full[folderIndex];
			break;
	}
	//Pre-S7: product.config.productImage.css('background-image', 'url('+product.config.folders.baseDirectory+imageDir+'/'+classStyleColor+'.jpg)');
	product.config.productImage.css('background-image', 'url('+product.config.folders.baseDirectory+classStyleColor+imageDir+')');
	lastUsedPDPDetailView = folderIndex;
	// show cover gradient if there should be one
	if(products[productIndex].colors[colorIndex].hasViewGradient[folderIndex])
		products[productIndex].equityTable.css('background-image', 'url('+product.config.folders.gradient+')');
	else
		products[productIndex].equityTable.css('background-image', 'none');
	// highlight the thumbnail of the current image
	product.swapSelectedImage(productIndex, folderIndex, products[productIndex].classStyle);
}

product.swapSelectedImage = function(productIndex, folderIndex, classStyle)
{
	folderIndex++;
	for(var i=1; i<=8; i++)
	{
		var element = products[productIndex].imageNav.find('.view'+i+'_link');
		if(i == folderIndex)
			element.addClass('selected');
		else
			element.removeClass('selected');
	}
}

var lastUsedPDPDetailView = 0; // initially set to first view

product.changeImageThumbs = function(productIndex, colorIndex)
{
	products[productIndex].imageNav.find('.view_link').remove();
	for(var i=0; i < products[productIndex].colors[colorIndex].hasView.length; i++)
	{
		var viewNum = i+1;
		if(products[productIndex].colors[colorIndex].hasView[i])
		{
			//Pre-S7: products[productIndex].imageNav.append('<a class="view_link view'+viewNum+'_link" href="javascript:product.swapImage(\''+products[productIndex].colors[colorIndex].classStyleColor+'\','+i+');"><img src="'+product.config.folders.baseDirectory+product.config.folders.thumbs[i]+'/'+products[productIndex].colors[colorIndex].classStyleColor+'.jpg" alt="" border="0"></a>');
			products[productIndex].imageNav.append('<a class="view_link view'+viewNum+'_link" href="javascript:product.swapImage(\''+products[productIndex].colors[colorIndex].classStyleColor+'\','+i+');"><img src="'+product.config.folders.baseDirectory+products[productIndex].colors[colorIndex].classStyleColor+product.config.folders.thumbs[i]+'" alt="" border="0"></a>');
		}
	}
	if(!products[productIndex].colors[colorIndex].hasView[lastUsedPDPDetailView]) lastUsedPDPDetailView = 0; // the last used view isn't valid for this new color, set back to first view
	product.swapImage(products[productIndex].colors[colorIndex].classStyleColor, lastUsedPDPDetailView); // change out the main pdp image to the first in the new list
}

product.changeBgImageThumbs = function(productIndex, colorIndex)
{
	var totalNumThumbs = 4;
	var currentNumThumbs = 0;
	var indexVal = productIndex+1;
	// write large thumb
	var imgCol = $('#imageCol'+indexVal);
	//Pre-S7: imgCol.html('<a class="detailThumb" href="#" onclick="GenericPopup(\'/web/browse/show_product_image.jsp?show='+products[productIndex].colors[colorIndex].classStyleColor+'&view=0\', \'500\', \'532\'); return false;"><img id="img_detail_'+indexVal+'" class="detail" src="'+product.config.folders.baseDirectory+product.config.folders.bpLargeThumb+products[productIndex].colors[colorIndex].classStyleColor+'.jpg" width="95" height="98" /></a>');
	imgCol.html('<a class="detailThumb" href="#" onclick="GenericPopup(\'/web/browse/show_product_image.jsp?show='+products[productIndex].colors[colorIndex].classStyleColor+'&view=0\', \'500\', \'532\'); return false;"><img id="img_detail_'+indexVal+'" class="detail" src="'+product.config.folders.baseDirectory+products[productIndex].colors[colorIndex].classStyleColor+product.config.folders.bpLargeThumb+'" /></a>');
	// write smaller thumbs
	for(var i=1; i < products[productIndex].colors[colorIndex].hasView.length; i++)
	{
		if(currentNumThumbs == totalNumThumbs) break; // stop writing after max
		var viewNum = i+1;
		if(products[productIndex].colors[colorIndex].hasView[i])
		{
			//Pre-S7: imgCol.append('<a class="thumb" href="#" onclick="GenericPopup(\'/web/browse/show_product_image.jsp?show='+products[productIndex].colors[colorIndex].classStyleColor+'&view='+i+'\', \'500\', \'532\'); return false;"><img id="img_'+i+'_'+indexVal+'" src="'+product.config.folders.baseDirectory+product.config.folders.thumbs[i]+'/'+products[productIndex].colors[colorIndex].classStyleColor+'.jpg" width="44" height="45" /></a>');
			imgCol.append('<a class="thumb" href="#" onclick="GenericPopup(\'/web/browse/show_product_image.jsp?show='+products[productIndex].colors[colorIndex].classStyleColor+'&view='+i+'\', \'500\', \'532\'); return false;"><img id="img_'+i+'_'+indexVal+'" src="'+product.config.folders.baseDirectory+products[productIndex].colors[colorIndex].classStyleColor+product.config.folders.thumbs[i]+'" /></a>');
			currentNumThumbs++;
		}
	}
	imgCol.append('<a href="#" class="viewDetails" onclick="GenericPopup(\'/web/browse/show_product_image.jsp?show='+products[productIndex].colors[colorIndex].classStyleColor+'&view=0\', \'500\', \'532\'); return false;">View details</a>');
}

product.swapBpImageThumbs = function(productIndex, colorIndex)
{
	var indexVal = productIndex+1;
	product.swapBpLargeThumb(productIndex, colorIndex);
	//Pre-S7: $('#img_front_'+indexVal).attr('src',product.config.folders.baseDirectory+product.config.folders.thumbs[0]+'/'+products[productIndex].colors[colorIndex].classStyleColor+'.jpg');
	$('#img_front_'+indexVal).attr('src',product.config.folders.baseDirectory+products[productIndex].colors[colorIndex].classStyleColor+product.config.folders.thumbs[0]);
	//Pre-S7: $('#img_back_'+indexVal).attr('src',product.config.folders.baseDirectory+product.config.folders.thumbs[1]+'/'+products[productIndex].colors[colorIndex].classStyleColor+'.jpg');
	$('#img_back_'+indexVal).attr('src',product.config.folders.baseDirectory+products[productIndex].colors[colorIndex].classStyleColor+product.config.folders.thumbs[1]);
}
product.swapBpLargeThumb = function(productIndex, colorIndex)
{
	var indexVal = productIndex+1;
	//Pre-S7: $('#img_detail_'+indexVal).attr('src',product.config.folders.baseDirectory+product.config.folders.bpLargeThumb+products[productIndex].colors[colorIndex].classStyleColor+'.jpg');
	$('#img_detail_'+indexVal).attr('src',product.config.folders.baseDirectory+products[productIndex].colors[colorIndex].classStyleColor+product.config.folders.bpLargeThumb);
}

var bpDetailImageInitial = '';

product.swapBpDetailImage = function(classStyleColor, folder)
{
	bpDetailImageInitial = bpDetailImageInitial == '' ? $('#outfitView').html() : bpDetailImageInitial; // set var with content first time around
	//Pre-S7: var newImagePath = product.config.folders.baseDirectory+product.config.folders.bpLargeDetail+classStyleColor+'.jpg';
	var newImagePath = product.config.folders.baseDirectory+classStyleColor+product.config.folders.bpLargeDetail;
	$('#outfitView').html('<img src="'+newImagePath+'" alt="" />');
}
product.revertBpDetailImage = function()
{
	$('#outfitView').html(bpDetailImageInitial);
}
