/*-----contentModule.js----------------------------------------------------------

List of products with titles and images. Functions for navigating through items.
Created:	07/03/14
Author: 	Jeffrey D. Davis

-------------------------------------------------------------------------------*/

var arrScrollingModulesNext = new Array();
var arrScrollingModulesPrevious = new Array();
var arrSelectModule = new Array();
var TimeoutID;

function isInArray(arr, strValue)
	{
	for (x=0; x<arr.length; x++)
		{
		var str = arr[x];
		if (str == strValue)
			{
			return(true);
			}
		}
	return(false);
	}
	
function removeFromArray(arr, strValue)
	{
	var arrReturn = new Array();
	for (x=0; x<arr.length; x++)
		{
		if (arr[x] != strValue)
			arrReturn.push(arr[x]);
		}
	return(arrReturn);
	}

function loadModules()
	{
	$(".contentModule").each(modSelectFirstProduct);
	}
	
function modSelectFirstProduct()
	{
	modSelectProduct(1, this.id);
	}
	
function modSelectProductDelay(intRow, strID)
	{
	arrSelectModule.push(strID + ' ' + intRow);
	setTimeout("modSelectProductFire(" + intRow + ", '" + strID + "')", 250);
	}
	
function modSelectProductFire(intRow, strID)
	{
	if (isInArray(arrSelectModule, strID + ' ' + intRow))
		{
			arrSelectModule = removeFromArray(arrSelectModule, strID + intRow);
			modSelectProduct(intRow, strID);
		}
	}
function modSelectProductCancel(intRow, strID)
	{
		arrSelectModule = removeFromArray(arrSelectModule, strID + ' ' + intRow);
	}
	
function modSelectProduct(intRow, strID)
	{
	$("#contentModule-" + strID + " li").removeClass('selected');
	$("#contentModule-" + strID + '-' + intRow).addClass('selected');
	strSRC = $("#contentModule-" + strID + '-' + intRow + " img").attr("src");
	strHREF = $("#contentModule-" + strID + '-' + intRow + " a").attr("href");	
	$("#IMG-content-" + strID).attr({src: strSRC});
	$("#contentModule-" + strID + " a.ImageLink").attr({href: strHREF});
	}
	
function nextModuleScroll(strID)
	{
		if (isInArray(arrScrollingModulesNext, strID) == false)
			{
				ModuleStop(strID);
				arrScrollingModulesNext.push(strID);
				TimeoutID = setTimeout("ScrollNext('" + strID + "')", 250);
			}
	}
	
function ScrollNext(strID)
	{
		if (isInArray(arrScrollingModulesNext, strID))
			{
				nextModuleItem(strID);
				TimeoutID = setTimeout("ScrollNext('" + strID + "')", 1000);
			}
	}

function previousModuleScroll(strID)
	{
		if (isInArray(arrScrollingModulesPrevious, strID) == false)
			{
				ModuleStop(strID);				
				arrScrollingModulesPrevious.push(strID);
				TimeoutID = setTimeout("ScrollPrevious('" + strID + "')", 250);
			}		
	}
	
function ScrollPrevious(strID)
	{
		if (isInArray(arrScrollingModulesPrevious, strID))
			{
				previousModuleItem(strID);
				TimeoutID = setTimeout("ScrollPrevious('" + strID + "')", 1000);
			}
	}	
	
function ModuleStop(strID)
	{
		clearTimeout(TimeoutID);
		if (isInArray(arrScrollingModulesNext, strID))
			arrScrollingModulesNext = removeFromArray(arrScrollingModulesNext, strID);
		if (isInArray(arrScrollingModulesPrevious, strID))
			arrScrollingModulesPrevious = removeFromArray(arrScrollingModulesPrevious, strID);			
	}
	
function previousModuleClick(strID)
	{
		ModuleStop(strID);
		previousModuleItem(strID);
	}
function nextModuleClick(strID)
	{
		ModuleStop(strID);
		nextModuleItem(strID);
	}
	
function previousModuleItem(strID)
	{
	var liID = "";
	$("#" + strID + " li.selected").each(function(i){
	liID = this.id;
	});
	var intSelected = liID.substr(strID.length+15);
	intPrev = parseInt(intSelected) - 1;
	if (intPrev >= 1)
		modSelectProduct(intPrev, strID);
	else
		modSelectProduct($("#" + strID + " li").size(), strID);
	}
	
function nextModuleItem(strID)
	{
	var liID = "";
	$("#" + strID + " li.selected").each(function(i){
	liID = this.id;
	});
	var intSelected = liID.substr(strID.length+15);
	intNext = parseInt(intSelected) + 1;
	if (intNext <= $("#" + strID + " li").size())
		modSelectProduct(intNext, strID);
	else
		modSelectProduct(1, strID);
	}	