/**
 * @fileoverview Venda.Widget.ProductCycling - Locate previous and next products in the same parent category so that user can cycle through products.
 *
 * The next and previous invtref is retrieved using either a <venda_category> or <venda_locayta> tag to build an array of all refs.
 * Then the current invtref is located within that array to determine what the next and previous invtrefs are.
 * The results are then used to dictate what loads when the next/previous buttons are clicked.
 * 
 * @requires /venda-support/js/external/yui/build/yahoo/yahoo-min.js
 * @requires /venda-support/js/external/yui/build/event/event-min.js
 * @author Hayley Easton <heaston@venda.com>
 */

//create ProductCycling namespace
Venda.namespace('Widget.ProductCycling');

/**
 * Stub function is used to support JSDoc.
 * @class Venda.Widget.ProductCycling
 * @constructor
 */
Venda.Widget.ProductCycling = function(){};

/**
 * Construct a new features object
 * @tags {object} tags pass venda tags into js functions (curinvtref:string, allrefs:string)
 */	
Venda.Widget.ProductCycling.create = function(tags) {
	Venda.Widget.ProductCycling.tags = tags;
	var cycleids = ["previousInvt", "nextInvt"];
	//register listener for objects in 'cycleids' array
	YAHOO.util.Event.addListener(cycleids, "click", Venda.Widget.ProductCycling.gatherRefs);
};

/**
 * Identify which product refs sequence to use (default order, price order, brand order)
 * Then turn the string into an array and trigger function to load new product detail
 * @param {event} e used to suppress default link behaviour
 */
Venda.Widget.ProductCycling.gatherRefs = function(e) {
	YAHOO.util.Event.preventDefault(e); // suppress default link behaviour
	Venda.Widget.ProductCycling.cycleEl = this.id;
	var productsString;
	
	// decide what sequence of refs are used depending on the link id that has been clicked
	if (Venda.Widget.ProductCycling.cycleEl.match('Invt')!=null){
		// take the string provided by the icat/product_cycling template
		var productsString = Venda.Widget.ProductCycling.tags.allrefs;
	}
	
	// turn the string into an array
	// need to remove the final / because we don't want an empty value being used for the final member of the array
	var finalCharPos = (productsString.lastIndexOf('/'));
	var newproductsString = productsString.substring(0,finalCharPos);
	// now we have the string with the final / character removed we can turn it into an array, using / as the separator
	Venda.Widget.ProductCycling.products = new Array();
	
	Venda.Widget.ProductCycling.products = newproductsString.split('/');
	Venda.Widget.ProductCycling.index = Venda.Widget.ProductCycling.findPos(Venda.Widget.ProductCycling.tags.curinvtref);
	
	// decide whether to load previous or next product according to the id that has been clicked
	if (Venda.Widget.ProductCycling.cycleEl.match('previous')!=null){Venda.Widget.ProductCycling.previousInvt();}
	if (Venda.Widget.ProductCycling.cycleEl.match('next')!=null){Venda.Widget.ProductCycling.nextInvt();}
};

/**
 * Find the position in the array of the current product
 * When we have the position in the array of the current sku, we can minus and plus one to find the previous and next products 
 * @param {object} value is the current invtref
 */	
Venda.Widget.ProductCycling.findPos = function(value) {
	var pos;
	for (var i = 0; i<Venda.Widget.ProductCycling.products.length; i++) {
		if (Venda.Widget.ProductCycling.products[i] == value) {
			pos = i;
			break;
		} else {
			pos = false;
		}
	}
	return pos;
};

/**
 * Find the next product reference in the array
 * Then load the product detail of that ref
 */
Venda.Widget.ProductCycling.nextInvt = function(){
	var nextProduct = parseInt(Venda.Widget.ProductCycling.index); // ensures the variable is a number not a string
	nextProduct += 1;
	if (nextProduct == Venda.Widget.ProductCycling.products.length) {
		nextProduct = 0;
	};
	window.location.href = "/invt/" + Venda.Widget.ProductCycling.products[nextProduct];
};

/**
 * Find the previous product reference in the array
 * Then load the product detail of that ref
 */
Venda.Widget.ProductCycling.previousInvt = function(){
	var prevProduct = parseInt(Venda.Widget.ProductCycling.index); // ensures the variable is a number not a string
	prevProduct -= 1;
	if (prevProduct < 0) {
		prevProduct = Venda.Widget.ProductCycling.products.length - 1;
	};
	window.location.href = "/invt/" + Venda.Widget.ProductCycling.products[prevProduct];
};