jQuery.noConflict();
/* Product Variations */
var baseProduct = {};

function updateSelectedVariation(parent, variation, id) {
	if(parent == undefined) {
		parent = jQuery('body');
	}
	else {
		parent = jQuery(parent);
	}

	if(typeof(baseProduct.price) == 'undefined') {
		if(jQuery('.AddCartButton', parent).css('display') == "none") {
			var cartVisible = false;
		}
		else {
			var cartVisible = true;
		}
		baseProduct = {
			saveAmount: jQuery('.YouSaveAmount', parent).html(),
			price: jQuery('.VariationProductPrice', parent).html(),
			sku: jQuery('.VariationProductSKU', parent).html(),
			weight: jQuery('.VariationProductWeight', parent).html(),
			thumb: jQuery('.ProductThumb img', parent).attr('src'),
			thumbLink: jQuery('.ProductThumb .ViewLarger', parent).html(),
			cartButton: cartVisible
		};
	}

	// Show the defaults again
//	alert(variation);
	if(typeof(variation) == 'undefined') {
		if(baseProduct.saveAmount) {
			jQuery('.YouSave', parent).show();
			jQuery('.YouSaveAmount').html(baseProduct.saveAmount);
		} else {
			jQuery('.YouSave', parent).hide();
		}
		jQuery('.VariationProductPrice', parent).html(baseProduct.price);
		jQuery('.VariationProductSKU', parent).html(baseProduct.sku);
		jQuery('.VariationProductWeight', parent).html(baseProduct.weight);
		jQuery('.CartVariationId', parent).val('');
		jQuery('.ProductThumb img', parent).attr('src', baseProduct.thumb);
		jQuery('.ProductThumb .ViewLarger', parent).html(baseProduct.thumbLink);
		jQuery(parent).attr('currentVariation', '');
		jQuery(parent).attr('currentVariationImage', '')
		jQuery('.VariationOutOfStockMessage', parent).remove();
		if(baseProduct.sku == '') {
			jQuery('.ProductSKU', parent).hide();
		}
		if(baseProduct.cartButton) {
			jQuery('.AddCartButton', parent).show();
			if(typeof ShowAddToCartQtyBox != 'undefined' && ShowAddToCartQtyBox=='1') {
				jQuery('.QuantityInput', parent).show();
			}
		}


		jQuery('.InventoryLevel', parent).hide();
	}
	// Othersie, showing a specific variation
	else {
		jQuery('.VariationProductPrice', parent).html(variation.price);
		if(variation.sku != '') {
			jQuery('.VariationProductSKU', parent).html(variation.sku);
			jQuery('.ProductSKU', parent).show();
		}
		else {
			jQuery('.VariationProductSKU', parent).html(baseProduct.sku);
			if(baseProduct.sku) {
				jQuery('.ProductSKU', parent).show();
			}
			else {
				jQuery('.ProductSKU', parent).hide();
			}
		}
		jQuery('.VariationProductWeight', parent).html(variation.weight);
		jQuery('.CartVariationId', parent).val(id);
		if(variation.instock == true) {
			jQuery('.AddCartButton', parent).show();
			if(typeof ShowAddToCartQtyBox != 'undefined' && ShowAddToCartQtyBox=='1') {
				jQuery('.QuantityInput', parent).show();
			}
			jQuery('.VariationOutOfStockMessage', parent).remove();
		}
		else {
			jQuery('.AddCartButton, .QuantityInput', parent).hide();
			jQuery('.VariationOutOfStockMessage', parent).remove();
			jQuery('.AddCartButton', parent).before('<p class="VariationOutOfStockMessage">'+lang.VariationSoldOutMessage+'</p>');
		}
		
		if(variation.thumb != '') {
			
			
			jQuery('.ProductThumb img', parent).attr('src', variation.thumb);
			jQuery('.ProductThumb .ViewLarger', parent).html('See larger picture');
			jQuery(parent).attr('currentVariation', id);
			jQuery(parent).attr('currentVariationImage', variation.image);
			
			
			
		}
		else {
			
			
			jQuery('.ProductThumb img', parent).attr('src', baseProduct.thumb);
			jQuery('.ProductThumb .ViewLarger', parent).html(baseProduct.thumbLink);
			jQuery(parent).attr('currentVariation', '');
			jQuery(parent).attr('currentVariationImage', '');
			
			
		}
		if(variation.stock) {
			jQuery('.InventoryLevel', parent).show();
			jQuery('.VariationProductInventory', parent).html(variation.stock);
		}
		else {
			jQuery('.InventoryLevel', parent).hide();
		}
		if(variation.saveAmount) {
			jQuery('.YouSave', parent).show();
			jQuery('.YouSaveAmount').html(variation.saveAmount);		
		} else {
			jQuery('.YouSave', parent).hide();
		}
	}
}

function initializeVariations(parent, VariationList)
{
	if(parent == undefined) {
		parent = jQuery('body');
	}
	else {
		parent = jQuery(parent);
	}

	// Select boxes are used if there is more than one variation type
	if(jQuery('.ProductOptionList select', parent).length > 0) {
		jQuery('.ProductOptionList select', parent).each(function(index) {
																 
			jQuery(this).change(function() {
				if(jQuery(this).val()) {
					var next = jQuery('.ProductOptionList select', parent).get(index+1);
					var i =1; //alert(index);
					while(typeof(next) != 'undefined') {
					
						jQuery('.ProductOptionList select', parent).get(index+i).resetNext();
						jQuery('.ProductOptionList select', parent).get(index+i).fill();
						jQuery('.ProductOptionList select', parent).get(index+i).disabled = false;
						jQuery('.ProductOptionList select', parent).get(index+i).selectedIndex= 1;
						i++;
						next = jQuery('.ProductOptionList select', parent).get(index+i);
						
					}
					ourCombination = this.getFullCombination();
					//alert(ourCombination);
					for(x in VariationList) {
						variation = VariationList[x];
						if(variation.combination == ourCombination) {
							updateSelectedVariation(parent, variation, x);
						//	jQuery('.ProductThumb2').css('display', 'none');
							return;
						}
				}
				}
				else {
					this.resetNext();
					//alert(this.resetNext());
				}

				// Do we have a full match?
				ourCombination = this.getFullCombination();
				//alert(ourCombination);
				for(x in VariationList) {
					variation = VariationList[x];
					if(variation.combination == ourCombination) {
						updateSelectedVariation(parent, variation, x);
					//	jQuery('.ProductThumb2').css('display', 'none');
						return;
					}
				}
				// No match or incomplete selection
				updateSelectedVariation(parent);
			//	jQuery('.ProductThumb2').css('display', 'block');
			});

			this.getFullCombination = function() {
				var selected = new Array();
				jQuery('.ProductOptionList select', parent).each(function() {
					selected[selected.length] = jQuery(this).val();
				});
				return selected.join(',');
			}


			this.getCombination = function() {
				var selected = new Array();
				var thisSelect = this;
				jQuery('.ProductOptionList select', parent).each(function() {
					if(thisSelect == this) {
						return false;
					}
					selected[selected.length] = jQuery(this).val();
				});
				// Add the current item
				selected[selected.length] = jQuery(this).val();
				return selected.join(',');
			}

			this.resetNext = function() {
				jQuery(this).nextAll().each(function() {
					this.selectedIndex = 0;
					this.disabled = true;
				});
			};

			this.fill = function(selectedVal) {
				// Remove everything but the first option
				jQuery(this).find('option:gt(0)').remove();

				var show = true;
				var previousSelection;

				// Get the values of the previous selects
				var previous = jQuery('.ProductOptionList select', parent).get(index-1);
				if(previous) {
					previousSelection = previous.getCombination();
				}
				for(var i = 1; i < this.variationOptions.length; i++) {
					for(x in VariationList) {
						variation = VariationList[x];
						if(previousSelection) {
							var show = false;
							if((variation.combination+',').indexOf(previousSelection+','+this.variationOptions[i].value+',') == 0) {
								var show = true;
								break;
							}
							else {
							}
						}
					}
					if(show) {
						this.options[this.options.length] = new Option(this.variationOptions[i].text, this.variationOptions[i].value);
					}
				}
				
				if(selectedVal != undefined) {
					jQuery(this).val(selectedVal);
				}
			};

			// Steal the options and store them away for later
			variationOptions = new Array();
			jQuery(this).find('option').each(function() {
				if(typeof(this.text) == undefined) {
					this.text = this.innerHTML;
				}
				variationOptions[variationOptions.length] = {value: this.value, text: this.text };
			});
			selectedVal = jQuery(this).val();
			this.variationOptions = variationOptions;
			if(index == 0) {
				this.fill(selectedVal);
			}
			else if(!selectedVal) {
				this.disabled = true;
			}
		});
	}
	// Otherwise, radio buttons which are very easy to deal with
	else {
		jQuery('.ProductOptionList input[type=radio]', parent).click(function() {
			for(x in VariationList) {
				variation = VariationList[x];
				if(variation.combination == jQuery(this).val()) {
					//alert(variation);
					updateSelectedVariation(parent, variation, x);
					return;
				}
			}
			// No match or incomplete selection
			updateSelectedVariation(parent);
		});
		jQuery('.ProductOptionList input[type=radio]:checked', parent).trigger('click');
	}
}

function change_image(id)
{
	if(id != '')
		document.getElementById('main_thumb_image').src = document.getElementById(id).src;
}


jQuery(document).ready(function() {
	if(typeof VariationList == 'undefined') {
		return;
	}
	initializeVariations('body', VariationList);
});
