/** 
 * Coremetrics "hacks" for Selfridges.
 */

var _sfcm_parentPageId;
var _sfcm_parentCategoryId;
 
var _sfcm_isVIP = document.cookie.search( /isVIP\=Y/ ) > 0;

var _sfcm_isCSR = false;  

/**
 * Make a pageId including all of the additional stuff that Selfridges requires on different pages.
 */ 
function sfcm_makePageId( pageId, isChild ) {

	var _pageId = '';

	// Add the CSR prefix if the user is a CSR.
	if ( _sfcm_isCSR == true && ! pageId.match( /^CSR[_\s]/ ) ) {
		_pageId += 'CSR ';
	}
	
	if ( isChild ) {
		if ( _sfcm_parentPageId ) {
			_pageId += _sfcm_parentPageId + ' ';
		} 
	} else {
		_sfcm_parentPageId = pageId;
	}
	
	_pageId += pageId;

	// Trim and format the resulting string.
	_pageId = _pageId.replace( /^\s+/, '' ).replace( /\s+$/, '' ).replace( /\s+/g, '_' );
	
	return _pageId;
}

/**
 * Make a categoryId including all of the additional stuff that Selfridges requires on different pages.
 */
function sfcm_makeCategoryId( categoryId, productId, shopAction ) {

	// Defect 4161/1760: Store Product categories in a cookie
	// so for the session we always return the same category
	if(productId && shopAction) {
		
		var prodCats = _sfcm_getProductCategories();
	
		if(prodCats['SA_' + productId]) {
			return prodCats['SA_' + productId];		
		}
	
		if(prodCats[productId]) {
			// save the last one as the shop action
			prodCats['SA_' + productId] =  prodCats[productId];
			
			_sfcm_setProductCategories( prodCats );
			
			return prodCats['SA_' + productId];		
		}
		
	}
	
	if ( _sfcm_parentCategoryId ) {
		if ( categoryId == 'USE_PARENT' || categoryId == '' ) {
			categoryId = _sfcm_parentCategoryId;
		}
	} else {
		_sfcm_parentCategoryId = categoryId;
	}

	var returnCategoryId = categoryId;

	if (productId) {
		var prepend = sfcm_productRegistration( productId );
		if ( prepend ) {
			returnCategoryId = prepend + categoryId;
		}
	}

	// Defect 4161/1760: Store Product categories in a cookie
	if(productId) {
		
		var prodCats = _sfcm_getProductCategories();
	
		prodCats[productId] = returnCategoryId;
		
		_sfcm_setProductCategories( prodCats );
				
	}
	
	return returnCategoryId;
	
} 

/**
 * 
 *
 * Get all categories for products
 */
function _sfcm_getProductCategories() {
	
	var parts = document.cookie.split( /\;\s?/ );
	var match;
	for (var part_i = 0; part_i < parts.length; part_i++) {		
		match = parts[part_i].match( /^CM_PROD_CATS\=(.+)$/ );
		if (match) {
			break;
		} 
	}
	
	var products = {};	
	if (match) {
		parts = match[1].split( ',' ); 
		for (var part_j = 0; part_j < parts.length; part_j++) {
			var subParts = parts[part_j].split( '|' );
			products[subParts[0]] = subParts[1];
		}
	}
	
	return products;
}

/**
 *
 */
function _sfcm_setProductCategories( products ) {
	
	var parts = document.cookie.split( /\;/ );
	
	var cookie = '';

	// Insert the cmProducts.
	cookie += 'CM_PROD_CATS=';
	
	var productCount = 0;
	
	for ( productId in products ) {
		if ( productCount++ > 0 ) {
			cookie += ',';
		}
		cookie += productId;
		cookie += '|';
		cookie += products[productId];
	}
	
	cookie += ';path=/';
	
	document.cookie = cookie;
}

/**
 *
 */
function sfcm_setCSR() {
	_sfcm_isCSR = true;
}

//
var _taste = '';

/**
 *
 */
function sfcm_tasteBreadcrumb() {
	
	_taste = '';
	
	$('div#breadcrumb:first ul li').each( function( i  ) { 
		
		var text = $( this ).text();
		
		if ( ! text.match( /^\s*HOME\s*$/i ) ) {
			_taste = _taste + ' ' + text 
		}
		
	} );
	
	return _taste.replace( /\s{2,}/g, " " );
}

/**
 * Associate a 'prepend category' with a particular product.
 * eg. product_of_the_day_, treasure_rail_
 */
function sfcm_registerProduct( productId, prependCategory ) {
	var products = _sfcm_getProducts();

	products[ productId ] = arguments[1];

	_sfcm_setProducts( products );	
}
 
/**
 * Clear all the prepend categories for all products (done on the checkout screen).
 */
function sfcm_clearProducts( ) {
	_sfcm_setProducts( {} ); 
}

/**
 * Clear the prepend category for an individual product.
 */
function sfcm_clearProduct( productId ) {
	var products = _sfcm_getProducts();

	delete products[ productId ];

	_sfcm_setProduts( products );
}

/**
 * 
 */
function sfcm_productRegistration( productId ) {
	var products = _sfcm_getProducts();
	
	return products[ productId ];
}

/**
 *
 */
function _sfcm_getProducts() {
	
	var parts = document.cookie.split( /\;\s?/ );
	var match;
	for (var part_i = 0; part_i < parts.length; part_i++) {		
		match = parts[part_i].match( /^CM_PRODUCTS\=(.+)$/ );
		if (match) {
			break;
		} 
	}
	
	var products = {};
	if (match) {
		parts = match[1].split( ',' ); 
		for (var part_j = 0; part_j < parts.length; part_j++) {		
			var subParts = parts[part_j].split( '|' );
			products[subParts[0]] = subParts[1];
		}
	}
	
	return products;
}

/**
 *
 */
function _sfcm_setProducts( products ) {
	
	var parts = document.cookie.split( /\;/ );
	
	var cookie = '';

	// Insert the cmProducts.
	cookie += 'CM_PRODUCTS=';
	
	var productCount = 0;
	
	for ( productId in products ) {
		if ( productCount++ > 0 ) {
			cookie += ',';
		}
		cookie += productId;
		cookie += '|';
		cookie += products[productId];
	}
	
	cookie += ';path=/';
	
	document.cookie = cookie;
}

function _sfcm_findAndRunScript( container ) {

	var sfcmScript = $( "" + container + " .sfcmScript" ).html();

	if ( sfcmScript ) {
		eval( sfcmScript );
	}

}


$.subscribe("moreFromOpening", function() { _sfcm_findAndRunScript("#moreFrom"); } );
$.subscribe("showcaseOpening", function() { _sfcm_findAndRunScript("#showcase"); } );
	
	
