var ShopFunctions = new (function()
{
	/**
	 *
	 */	
		function CombinationId ( )
		{
			this.tocId = null;
			
			this.activePackagingUnit = null;
			this.activeOptionValueIds = new Array;
			
			this.selectedOptionValueIds = new Array;
		}
	
	/**
	 *
	 */	
		this.urlCombinationId = new (function ( )
		{
			var combinationIds = new Array;
		
			/**
			 * URL-Parameter parsen:
			 */
				var requestParam = new RegExp ( '&?_cid=([^&]+)' ).exec ( window.location.href );
				if ( requestParam )
				{
					$.each ( requestParam[ 1 ].split ( /-/ ), function ( i, val )
					{
						var packagingUnit = null;
						var tocId = null;
						var optionValueIds = new Array;
					
						var tmp = val.split ( /_/ );
					
						if ( tmp.length > 1 ) // mit ve
						{
							packagingUnit = tmp[ 0 ];
							tmp = tmp[ 1 ];
						}
						else
						{
							tmp = tmp[ 0 ];
						}
						
						tmp = tmp.split ( /\./ );
						
						tocId = tmp[ 0 ];
						if ( tmp.length > 1 ) optionValueIds = tmp.splice ( 1 );
						
						var combinationId = new CombinationId;
						
							combinationId.tocId = tocId;							
							combinationId.activePackagingUnit = packagingUnit;
							combinationId.activeOptionValueIds = optionValueIds;
							
						combinationIds.push ( combinationId );						
					} );
				}
							
			this.setCombinationId = function ( combinationId )
			{
				var found = false;
			
				$.each ( combinationIds, function ( i, val )
				{
					if ( val.tocId == combinationId.tocId )
					{
						combinationIds[ i ] = combinationId;
						found = true;
					}				
				} );
				
				if ( ! found ) combinationIds.push ( combinationId );				
			}
							
			this.getCombinationIdFor = function ( tocId )
			{
				var result = new CombinationId;
					result.tocId = tocId;
					
				$.each ( combinationIds, function ( i, val )
				{
					if ( val.tocId == tocId ) result = val;
				} );
					
				return result;		
			}
			
			this.goto = function ( )
			{
				if ( requestParam )
				{
					window.location.href = window.location.href.replace
					(
						requestParam[ 0 ], this.toString ( )
					);
				}
				else				
				{
					if ( window.location.href.match ( /\?/ ) )
					{
						window.location.href = window.location.href += this.toString ( );
					}
					else
					{
						window.location.href = window.location.href += '?' + this.toString ( );
					}
				}
			}
						
			this.toString = function ( )
			{
				var result = new Array;
				
				$.each ( combinationIds, function ( i, val )
				{
					var tmp = '';
				
					if ( val.activePackagingUnit ) tmp += val.activePackagingUnit + '_'; // ve					
					
					tmp += val.tocId; // toc-id
					
					if ( val.activeOptionValueIds.length > 0 ) // optionen
					{
						tmp += '.' + val.activeOptionValueIds.join ( '.' );
					}
					
					result.push ( tmp );
				} );
			
				return '&_cid=' + result.join ( '-' );
			}			
		})();
	
	/**
	 *
	 */
		this.util = {};

	/**
	 * Gibt die Option-Value-Ids für einen Artikel zurück
	 */
		this.util.getSelectedOptionValueIdsFor = function ( tocId )
		{
			var optionValueIds = new Array;
			$.each ( $('select[name=option_value_ids_' + tocId + ']'), function ( i, val ) { optionValueIds.push ( val.value ); } );
			return optionValueIds;			
		}

	/**
	 * Gibt an ob die übergebenen optionValueIds eine gültige Kombination sind
	 */
		this.util.isValidCombination = function ( optionValueIds )
		{
			var result = optionValueIds.length > 0;			
			$.each ( optionValueIds, function ( i, val ) { if ( val == 0 ) result = false; } );			
			return result;
		}
})();

function selectCombinationId ( tocId )
{
	var combinationId = ShopFunctions.urlCombinationId.getCombinationIdFor ( tocId );

	var optionValueIds = ShopFunctions.util.getSelectedOptionValueIdsFor ( tocId );

	if ( ShopFunctions.util.isValidCombination ( optionValueIds ) )
	{
		combinationId.activeOptionValueIds = optionValueIds;
		ShopFunctions.urlCombinationId.setCombinationId ( combinationId );
		ShopFunctions.urlCombinationId.goto ( );
	}
	
	return false;
}

function selectPackagingUnit ( packagingUnit, tocId )
{
	var combinationId = ShopFunctions.urlCombinationId.getCombinationIdFor ( tocId );
		combinationId.activePackagingUnit = packagingUnit;
		
	ShopFunctions.urlCombinationId.setCombinationId ( combinationId );		
	ShopFunctions.urlCombinationId.goto ( );
}