////////////  Settings   ////////////////////////
var selectedAttributeValueColor	= 	'#990000';
var enabledAttributeValueColor	= 	'#000000';
var disabledAttributeValueColor	= 	'#999999';
var debugMode = false;
/////////////////////////////////////////////////

Instances = new Array();
Attributes = new Array();
Values = new Array();					

function in_array(needle, haystack, argStrict) 
{					 
	var key = '', strict = !!argStrict;
 
	if (strict) 
	{
		for (key in haystack) 
		{
			if (haystack[key] === needle) 
			{
				return true;
			}
		}
	} 
	else 
	{
		for (key in haystack) 
		{
			if (haystack[key] == needle) 
			{
				return true;
			}
		}
	} 
	return false;
}		

function ShowInstance(InstanceID, Mode)
{
	$('.Field').each(							
		function()
		{				   	   								   	   
			$(this).css('visibility','hidden');
			$(this).css('display','none');						
		}							
	);	

	$('.Caption').each(							
			function()
			{				   	   								   	   
				$(this).css('visibility','hidden');
				$(this).css('display','none');						
			}							
		);
	
	$('.Instance'+InstanceID).css('visibility','visible');
	$('.Instance'+InstanceID).css('display','block');
	
	$('.ItemSetControl').each(							
		function()
		{	
			//Comment the line below if you want to keep the current attributes' selection
			//$(this).get(0).value= '';
		}							
	);	
	
	$('#instance_id').get(0).value = InstanceID;
	
	////////////////////////////////////////////////////////////////////////////////////
	if(Mode == 'Initialize')
	{
		for(i=0; i<Instances.length; i++)
		{
			if(Instances[i]==InstanceID)
			{
				$('#'+Attributes[i]).get(0).value = Values[i];
				//alert(Attributes[i]+' '+Values[i]);
			}
		}
		$('.ItemSetControl').each(							
			function()
			{
				ShowAttributesAvailability($(this).get(0).value);
				
			}
		);
	}
	//////////////////////////////////////////////////////////////////////////////////	
}


function SetAttributeValue(ItemSet, Item)
{
	$('#'+ItemSet).get(0).value = Item;
	ShowAttributesAvailability(Item);
	GetCurrentInstance();
}

function ShowAttributesAvailability(Item)
{
	var Found = false;
	var Index = null;
	var enabledInstances = new Array();
	var j = 0;
	
	for(i=0; i<Values.length; i++)
	{
		if(Values[i] == Item)
		{
			enabledInstances[j] = Instances[i];
			j++;
		}
	}
	
	$('.Item').each(							
		function()
		{	
			for(i=0; i<Values.length; i++)
			{							
				if(Values[i] == $(this).get(0).id)
				{
					for(j=0; j<enabledInstances.length; j++)
					{
						if(Instances[i] == enabledInstances[j])
						{
							Found = true;
							Index = i;
						}
					}
				}
			}
			
			if(Found == false)
			{
				$(this).css('color',disabledAttributeValueColor);
				var objectVal = $(this).get(0).id;
				$('.ItemSetControl').each(							
					function()
					{
						if($(this).get(0).value == objectVal)
						{
							$(this).get(0).value = '';
						}
					}
				);
			}
			else
			{
				if($('#'+Attributes[Index]).get(0).value == Values[Index])
				{
					$(this).css('color',selectedAttributeValueColor);
				}
				else
				{
					$(this).css('color', enabledAttributeValueColor);
				}
			}

			Found = false;
			Index = null;
		}							
		
	);	

	if(debugMode)
	{
		var dbg = '';						
		$('.ItemSetControl').each(							
			function()
			{
				dbg = dbg + $(this).get(0).id +'= '+$(this).get(0).value+ '<br>';
			}
		);				
		if($('#debuggerPanel').get(0) != null)
		$('#debuggerPanel').get(0).innerHTML = dbg;
	}
}

function GetCurrentInstance()
{		
	var CurInstance = null;
	CurControlValues = new Array();
	CurControls = new Array();
	var cnt=0;
	var NoEmptyValue = true;
							
	$('.ItemSetControl').each(							
		function()
		{																		   	   
			if($(this).get(0).value != '')
			{
				CurControlValues[cnt] = $(this).get(0).value;
				CurControls[cnt] = $(this).get(0).id;
				cnt++;
			}
			else
			{
				NoEmptyValue = false;
				return false;
			}
		}							
	);	
	
	if(NoEmptyValue == true)
	{		
		uniqueInstances = new Array();
		var cnt=0;
		for(j=0; j<Instances.length; j++)
		{
			if(in_array(Instances[j], uniqueInstances) == true && uniqueInstances!= null)
			{
				//Do nothing
			}
			else
			{
				uniqueInstances[cnt] = Instances[j];
				cnt++;
			}
		}

		for(k=0; k<uniqueInstances.length; k++)
		{	
			var TempInstance = uniqueInstances[k];
			var Matches = 0; 
			for(i=0; i<CurControls.length; i++)
			{												
				
				for(j=0; j<Instances.length; j++)
				{																
					if(Values[j] == CurControlValues[i] && Instances[j] == TempInstance)
					{
						Matches++;
					}
				}									
			}
											
			if(Matches == CurControls.length)
			{
				CurInstance = TempInstance;									
				break;
			}
			else
			{
				//Do nothing
			}
		}
	}
	else
	{
		//Do nothing
	}

	if(CurInstance == null)
	{
		$('.AddToCartAction').css('visibility','hidden');
		$('.AddToCartAction').css('display','none');						
		return false;
	}
	else
	{	
		$('.AddToCartAction').css('visibility','visible');
		$('.AddToCartAction').css('display','block');	
		ShowInstance(CurInstance, null);
	}
}	
