var zpfs_debugMode = false;
var cmIODisplayUrl = '/webapp/wcs/stores/servlet/AJAXCoremetricsIODisplayView';
var zpfs_globalTestData = undefined;

if(zpfs_debugMode){
	zpfs_globalTestData = {
			a : ['P000062997', 'P000157113'], 
			f : [
			     [null, null, null, null, null, '//a248.e.akamai.net/f/248/9086/10h/origin-d5.scene7.com/is/image/Bealls/070-4208-3007-64', 38716], 
			     [null, null, null, null, null, '//a248.e.akamai.net/f/248/9086/10h/origin-d5.scene7.com/is/image/Bealls/472-0159-7130-42', 41207]
			    ],
			c : '_TESTING_'
	};
}

function uniqueArray(blArray) {
    var blArray2 = [], blLength = blArray.length;
    for(var blCount1 = 0; blCount1 < blLength; blCount1++) {
        for(var blCount2 = blCount1 + 1; blCount2 < blLength; blCount2++)
            if (blArray[blCount1] === blArray[blCount2]) blCount2 = ++blCount1;
        blArray2.push(blArray[blCount1]);
    }
    return blArray2;
};

function getValueString(rec_attributes, n_recs, indexIndex){
	var productIds = $.map(rec_attributes,
		function(value, index) {
			return value[indexIndex] + "";
		});

	var productIdsValue = productIds.slice(0, n_recs).join(',');
	return productIdsValue;
}

function replaceScene7Prefix(data){
	var replacementValue;
	
	/*
	 * BEALLSPRO-583 - hard coding to avoid adding an additional cache param, plus the paths don't make sense
	 * and are mixed up everywhere else, needs to be fixed in a separate effort
	 */
	 
	if(window.location.protocol == "https:"){
		replacementValue = 'https://a248.e.akamai.net/f/248/9086/10h/origin-d5.scene7.com/is/image/Bealls/';
	} else {
		replacementValue = 'http://s7d5.scene7.com/is/image/Bealls/';
	}
	data = data.replace(/SCENE7_SCHEME_URL_PREFIX/g,replacementValue);
	return data;
}

function addVirtualTag(data){
	var crossSellIdValue;
	if(typeof crossSellId == 'undefined')
		crossSellIdValue = 'CrossSellProduct';
	else
		crossSellIdValue = crossSellId;
	var replacementValue = '?cm_vc=' + crossSellIdValue;
	var pattern = /[A-Za-z/]+product[0-9_-]+/g;
	var matched = data.match(pattern);
	if(matched != null) {
		var matched2 = uniqueArray(matched);
		
		for(matchedItem in matched2) {
			data = data.replace(new RegExp(matched2[matchedItem], 'g'), matched2[matchedItem] + replacementValue);
		}
	}
	return data;
}

function showData(a_product_ids, zone, symbolic, target_id, category, rec_attributes, target_attributes, target_header_txt, abTest, zoneID, imageWidth, amnt) {
	if (symbolic == '_NR_') {
		//no results returned
		return false;
	}

	var num_to_display = amnt;
	var n_recs = a_product_ids.length;
	if (n_recs > num_to_display) {
		n_recs = num_to_display;
	}

	var productIdsValue = a_product_ids.slice(0, n_recs).join(",");
	var params = {storeId: storeId, catalogId: catalogId, langId: langId, productIds: productIdsValue, zone: zone, list: false};
	
	if (zpfs_debugMode) {
		console.log($.param(params));
	}

	$.get(cmIODisplayUrl, params,
			function(data) {
				if(zoneID == 'MCPZ1'){
					data = data.replace(/\&wid\=140/g, '&wid=80&hei=80');
				}
				
				data = replaceScene7Prefix(data);
//				data = addVirtualTag(data);
				
				$('#' + zoneID).html('<div class="recommend"><h2>May We<br/>Also Suggest:</h2><div>' + data + '</div></div>');
			});
}

function showDataList(a_product_ids, zone, symbolic, target_id, category, rec_attributes, target_attributes, target_header_txt, abTest, zoneID, imageWidth, amnt) {
	//cross sell, located on the bottom of the product page
	//id: PDPCS_zone
	if (symbolic == '_NR_') {
		//no results returned
		return false;
	}
	
	var num_to_display = amnt;
	var n_recs = a_product_ids.length;
	if (n_recs > num_to_display) {
		n_recs = num_to_display;
	}
	
	var productIdsValue = a_product_ids.slice(0, n_recs).join(",");
	var params = {storeId: storeId, catalogId: catalogId, langId: langId, productIds: productIdsValue, zone: zone, list: true};
	
	if (zpfs_debugMode) {
		console.log($.param(params));
	}

	$.get(cmIODisplayUrl, params,
			function(data) {
				if(zoneID == 'MCPZ1'){
					data = data.replace(/\&wid\=140/g, '&wid=80&hei=80');
				}
				
				data = replaceScene7Prefix(data);
//				data = addVirtualTag(data);
				
				$('#' + zoneID).html('<div class="suggested-box"><h3>Customers Suggest</h3><div><ul>' + data + '</ul></div></div>');
			});
}

/* function for CMIO */
function PDPCS_zp(a,b,c,d,e,f,g,h,i){
	if(zpfs_debugMode){
		a = zpfs_globalTestData.a;
		f = zpfs_globalTestData.f;
		c = zpfs_globalTestData.c;
	}
	//cross sell, located on the bottom of the product page
	//id: PDPCS_zone
	showData(a,b,c,d,e,f,g,h,i,'PDPCS_zone', '-yyy?$IntelOf$&wid=140', 4);
}

function MCPZ1_zp(a,b,c,d,e,f,g,h,i){
	//mini shopping cart
	//id: MCPZ1_zone
	showDataList(a,b,c,d,e,f,g,h,i,'MCPZ1_zone', '-yyy?$IntelOf$&wid=80&hei=80', 3);
}

function CPZ1_zp(a,b,c,d,e,f,g,h,i){
	if(zpfs_debugMode){
		a = zpfs_globalTestData.a;
		f = zpfs_globalTestData.f;
		c = zpfs_globalTestData.c;
	}
	//shopping cart
	//id: CPZ1_zone
	showDataList(a,b,c,d,e,f,g,h,i,'CPZ1_zone', '-yyy?$IntelOf$&wid=140', 3);
}

function PDPUS_zp(a,b,c,d,e,f,g,h,i){
	if(zpfs_debugMode){
		a = zpfs_globalTestData.a;
		f = zpfs_globalTestData.f;
		c = zpfs_globalTestData.c;
	}
	//up sell, located on the right side of the product page
	//id: PDPUS_zone
	showDataList(a,b,c,d,e,f,g,h,i,'PDPUS_zone', '-yyy?$IntelOf$&wid=140', 4);
}

