var newProducts = {
	setGlobals: function() {
		newProducts.collectionLinkHTML = '<a href="#" class="area-collection-link">view collection</a>';
		newProducts.collectionLinkFlag = 0;
		newProducts.areaPhoto = brizo.areaPhoto.find('img');
	},
	
	addCollectionLink: function() {
		brizo.contentBody.append(newProducts.collectionLinkHTML);
		newProducts.areaPhoto.wrap('<a href="#" class="area-photo-link"></a>');
		newProducts.collectionLinkFlag = 1;
	},
	
	changeFeatureLink: function(featureName) {
		if(newProducts.collectionLinkFlag === 0) { newProducts.addCollectionLink(); }
		var cl = jQuery('a.area-collection-link');
		var pl = jQuery('a.area-photo-link');
		var newUrl = featureName;
		// var newLinkId = featureName.replace('.html', '').split('/').reverse()[0].replace('-PC', '');
		
		var newLinkId = featureName.indexOf('products.html') != -1 ? featureName.split('/')[2] : featureName.replace('.html', '').split('/').reverse()[0].replace('-PC', '');
		
		cl.attr('id', 'link-to-'+ newLinkId)
			.attr('href', newUrl)
			.text('view '+ newLinkId.toUpperCase() +' collection');
			// .text('view '+ featureName.replace('-', ' ').toUpperCase() +' collection');
		pl.attr('href', newUrl);
	},

	
	// Convert incoming link path to usable image path for large photo
	checkLinkPath: function(inPath) {
		if(inPath.indexOf('collection') != -1) {
			outPath = '/images/pic/catalog/' + inPath.replace('collection', 'collections/large').replace('html', 'jpg');
			// console.log('product is collection: ', inPath);
		} else if(inPath.indexOf('products.html') != -1) {
			// console.log('product is shower: ', inPath);
			outPath = '/images/pic/catalog/shower/collections/large/' + inPath.split('/')[2] + '.jpg';
			// console.log('shower outpath: ', outPath);
		} else {
			// console.log('product is product: ', inPath);
			outPath = '/images/pic/catalog/product-large/' + inPath.split('/').reverse()[0].replace('html', 'jpg').replace('-PC', '');
		}
		return outPath;
	},
	
	changeFeature: function(featurePath) {
		brizo.changePhoto(newProducts.areaPhoto, newProducts.checkLinkPath(featurePath));
		newProducts.changeFeatureLink(featurePath);
	},
	
	setFirstCollection: function() {
		var newData = '';
		var newImage = '';
		var thisUrl = jQuery.url.attr("relative").replace('html', 'xml');
		jQuery.get(thisUrl, function(data){
			newImagePath = jQuery(data).find('thumb').attr('img');
			newLinkPath = jQuery(data).find('thumb').attr('link').replace("javascript:newProducts.changeFeature('", '').replace("');", '');

			
			brizo.changePhoto(newProducts.areaPhoto, newProducts.checkLinkPath(newLinkPath));
			newProducts.changeFeatureLink(newLinkPath);
		});
	}
};

jQuery(document).ready(function(){
	newProducts.setGlobals();
	newProducts.setFirstCollection();
});
