function setTab(link, content) {
    var tabContainer = $(link).up().up();
    var tab = $(link).up();

    tabContainer.getElementsBySelector('li').each(function(element) {
        if (element.hasClassName('current')) element.removeClassName('current');
        if (element == tab) element.addClassName('current');
    } .bind(this));

    $('ProductTabContent').childElements().each(function(element) {
        if (element.hasClassName('Description')) element.hide().removeClassName('Description');
        if (element.identify() == content) {
            element.appear({ duration: 0.3 });
        }
        else {
            if (element.visible()) element.fade({ duration: 0.3 });
        }
    } .bind(this));
}

//function toggleProductTab(divID, parentID) {
//	if ($(divID).hasClassName('Description')) {
//		$(divID).hide().removeClassName('Description');
//	}
//
//	var divs = $(parentID).getElementsBySelector('div');
//	$(divs).each(function(e) {
//		if ($(e).visible() && $(e).identify() != divID) {
//			$(e).fade({ duration: 0.3 });
//		}
//
//		if (!$(e).visible() && $(e).identify() == divID) {
//			$(e).appear({ duration: 0.3 });
//		}
//	});
//}


function formatCurrency(amount) {
	var i = parseFloat(amount);
	if (isNaN(i)) { i = 0.00; }
	var minus = '';
	if (i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if (s.indexOf('.') < 0) { s += '.00'; }
	if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
	
	var AddToCart = Class.create({
	initialize: function() {
		this.addToCartContainer = 'addToCartContainer';
		this.addToCartButton = 'addToCartButton';
	},
	hide: function() {
		if ($(this.addToCartContainer)) {
			$(this.addToCartContainer).fade({ duration: 0.3 });
		}
	},
	show: function() {
		if ($(this.addToCartContainer)) {
			$(this.addToCartContainer).appear({ duration: 0.3 });
		}
	},
	fade: function() {
		if ($(this.addToCartContainer) && $(this.addToCartContainer).visible()) {
			if ($(this.addToCartButton) && ($(this.addToCartButton).getStyle('visibility') == 'visible') || ($(this.addToCartButton).getStyle('visibility') == 'inherit')) {
				new Effect.Opacity($(this.addToCartButton), {
					from: 1.0,
					to: 0.0,
					duration: 0.3,
					afterFinish: function() {
						$(this.addToCartButton).setStyle({ visibility: 'hidden' });
					} .bind(this)
				});
			}
		}
	},
	appear: function() {
		if ($(this.addToCartContainer) && $(this.addToCartContainer).visible()) {
			if ($(this.addToCartButton) && $(this.addToCartButton).getStyle('visibility') == 'hidden') {
				$(this.addToCartButton).setStyle({ visibility: 'visible' });
				new Effect.Opacity($(this.addToCartButton), {
					from: 0.0,
					to: 1.0,
					duration: 0.3
				});
			}
		}
	}
});

var addToCart;
document.observe('dom:loaded', function() {
	addToCart = new AddToCart();
	if ($('isProductPage')) {
		if ($('ddlSize')) {
			addToCart.fade();
		}		
	}
});
	
	var imageToShow;
	var imageThumbToShow;
	function populateColors(lbReflector,lbreflectorFoundonPage) {
		//alert('Choose A ' + lbReflector);
		addToCart.fade();
		
		if (lbreflectorFoundonPage == 'true') {
			//alert('changing back to default');
			//default change to normal image inc ase we have to change it
			//alert('imgMainsrc ' + $('imgMain').src);
			document.getElementById('imgDefault');
			//alert('imgDefault ' + $('imgDefault').value);

			//$('imgMain').src = $('imgDefault').value;
			imageToShow = $('imgDefault').value;
			imageThumbToShow = $('imgMainThumbDefault').value;
			
		}
		//$('options').value = '';
		
		//reset the prices so we start from the hidden field value
		if ($('regularPriceBase') && $('regularPrice')) {
			$('regularPrice').innerHTML = '$' + document.getElementById('regularPriceBase').value;
		}
	
		if ($('salePriceBase') && $('salePrice')) {
			$('salePrice').innerHTML = '$' + document.getElementById('salePriceBase').value;
		}
		
		if ($('ddlColor') && $('productOptionValues')) {
			clearColors();
			if ($('ddlSize').value != '') {
				var sSize = $('ddlSize').value.split('=')[1];
				
				var aValues = new Array();
				var aOptions = $('productOptionValues').getElementsBySelector('input[@type="hidden"]');
				//alert('aOptions.length ' + aOptions.length);
				for (var i = 0, len = aOptions.length; i < len; ++i) {
					//75|color=cocoa;size=XL|3|||
					var aOption = aOptions[i].value.split('|')[1].split(';');
					var iQty = aOptions[i].value.split('|')[3];
					//var iImg = aOptions[i].value.split('|')[4];
					//alert('iQty ' + iQty);
					if (iQty != '' && iQty > 0) {
						//alert('aOption.length ' + aOption.length);
						//if (!$('ddlSize')) {
						if (aOption.length != 2) { 
							//alert('Warning:  The options for this product were not set up correctly.  Please contact an administrator.'); break; }
							//$('selectedOptions').value = $('ddlSize').value;
							//alert('iImg ' + iImg);
							//if (iImg != '') {
							//	replaceImageAndZoom('imgMain', 275, iImg);
							//}
							addToCart.appear();
							return;
						}
						else {
							var sValue = aOption[0].split('=')[1];
							if (aOption[1].split('=')[1] == sSize && aValues.indexOf(sValue) == -1) {
								aValues.splice(aValues.length + 1, 0, sValue);
							}
						}
					}
				}

				var itemCost = 0;
				//reset the values
				if ($('regularPriceBase') && $('regularPrice')) {
					$('regularPrice').innerHTML = '$' + formatCurrency(parseFloat($('regularPriceBase').value));
					itemCost = $('regularPrice').innerHTML.replace('$','')*1;
				}
			
				if ($('salePriceBase') && $('salePrice')) {
					$('salePrice').innerHTML = '$' + formatCurrency(parseFloat($('salePriceBase').value));
					itemCost = $('salePrice').innerHTML.replace('$','')*1;
				}

				
				//var Option1=document.getElementById("ddlSize");
				//var Option1 = document.getElementById('ddlSize').selectedIndex;
				var optionDD = document.getElementById('ddlSize')
				//alert(optionDD[document.getElementById('ddlSize').selectedIndex].text);
				
				var oStr = new String(optionDD[document.getElementById('ddlSize').selectedIndex].text);
				if (oStr.indexOf('$',1) > -1) {
					var aCost = optionDD[document.getElementById('ddlSize').selectedIndex].text.split('+')[1];
					aCost = aCost.replace(')','');
					aCost = aCost.replace('$','');
					aCost = aCost.replace(',','');
					addCost = aCost*1;
					
					var totalCost = addCost + itemCost;
					if ($('regularPriceBase') && $('regularPrice')) {
						$('regularPrice').innerHTML = '$' + formatCurrency(parseFloat(totalCost));
					}
				
					if ($('salePriceBase') && $('salePrice')) {
						$('salePrice').innerHTML = '$' + formatCurrency(parseFloat(totalCost));
					}
				}
				
				
				for (var i = 0, len = aValues.length; i < len; ++i) {
					addSelectOption($('ddlColor'), aValues[i], 'color=' + aValues[i], false);
				}
	
				if (aValues.length > 0) {
					if (!$('ddlColor').up().visible()) $('ddlColor').up().slideDown({ duration: 0.3 });
					//if ($('outOfStockMessage').visible()) $('outOfStockMessage').slideUp({ duration: 0.3 });
				}
				else {
					if ($('ddlColor').up().visible()) $('ddlColor').up().slideUp({ duration: 0.3 });
					//if (!$('outOfStockMessage').visible()) $('outOfStockMessage').slideDown({ duration: 0.3 });
				}
			}
			else {
				if ($('ddlColor').up().visible()) $('ddlColor').up().slideUp({ duration: 0.3 });
				//if ($('outOfStockMessage').visible()) $('outOfStockMessage').slideUp({ duration: 0.3 });
			}
		}
		
		//we now have the price set if there are options, now we need to add the accessory prices if selected
		//var accOptions = $('productrightsidebold').getElementsBySelector('input[@type="dropdownlist"]');
		
		var accCount = document.getElementById("accCount");
		var accOption;
		//alert('count of acc options ' + accCount.value);
		var optoget = '';
		var opSelected;
		var priceAdd=0;
		var hiddenToGet;
		var totalCostplus=0;
		//accCount = accCount*1;
		var baseReg;
		var baseSale;
		var hiddenToGetImage;
		
		var HiddenimgtogetID;
		var HiddenimgThumbtogetID;
		var hiddenThumb;
		var DIVaccOption;
		var DIVoptoget = '';
		//var imgZoomZoom;
		for (var i = 0, len = accCount.value; i < len; ++i) {
			optoget = 'checkacc_' + i;
			accOption = document.getElementById(optoget);
			opSelected = accOption.selectedIndex

			DIVoptoget = 'DIVcheckacc_' + i;
			DIVaccOption = document.getElementById(DIVoptoget);
			
			if (opSelected > 0) {
				//get the selected item index value and then 
				
				hiddenToGet = document.getElementById(accOption[accOption.selectedIndex].value)
				priceAdd = document.getElementById(accOption[accOption.selectedIndex].value).value
				//alert('hiddenToGet.id ' + hiddenToGet.id);
				
				if (lbReflector == 'true' || opSelected > 0) {
					//alert('change the image reflector');
					if (DIVaccOption.innerHTML.indexOf("Choose a Reflector") != -1) {
						//alert('setting values');//try {
						HiddenimgtogetID = 'chooseA_' + hiddenToGet.id;
						HiddenimgThumbtogetID = 'chooseA_addImg_' + hiddenToGet.id;
						//alert('get HiddenimgtogetID=' + HiddenimgtogetID);
						hiddenToGetImage = document.getElementById(HiddenimgtogetID);
						hiddenThumb = document.getElementById(HiddenimgThumbtogetID);
						//get the main image to change to 
						//chooseA_3053
						//alert('hiddenToGetImage.id ' + hiddenToGetImage.id);
						//alert('hiddenToGetImage.value ' + hiddenToGetImage.value);
						//$('imgMain').src = hiddenToGetImage.value;
						imageToShow = hiddenToGetImage.value;
						//$('imgMain').src = imageToShow;
						
						imageThumbToShow = hiddenThumb.value;
						
						//alert(document.getElementById('chooseA_rawImg_' + hiddenToGet.id).value);
						//original working replaceImageAndZoom('imgMain', 275, document.getElementById('chooseA_rawImg_' + hiddenToGet.id).value);
						document.getElementById("productthumbsarea").innerHTML = imageThumbToShow;
						replaceImageAndZoomChangeDropList('imgMain', 275, document.getElementById('chooseA_rawImg_' + hiddenToGet.id).value, 'checkacc_0', document.getElementById("checkacc_0").selectedIndex,'true')
						//imgZoomZoom = document.getElementById("imgZoom");
						//imgZoomZoom = imageToShow;
						//}
						//catch (e) {
							//ignore
						//}
					}
				}
				//alert('adding cost ' + priceAdd);
				totalCostplus = (priceAdd*1) ;
				if ($('regularPriceBase') && $('regularPrice')) {
					$('regularPrice').innerHTML = '$' + formatCurrency(parseFloat(($('regularPrice').innerHTML.replace('$','')*1) + totalCostplus));
				}
			
				if ($('salePriceBase') && $('salePrice')) {
					$('salePrice').innerHTML = '$' + formatCurrency(parseFloat(($('salePrice').innerHTML.replace('$','')*1) + totalCostplus));
				}
				
			}
		}
		$('imgMain').src = imageToShow;
		document.getElementById("productthumbsarea").innerHTML = imageThumbToShow;
	}
	
	
	//function replaceImageAndZoom(element, size, image) {
	function replaceImageAndZoom(element, size, image) {
		if ($(element)) {
			$(element).src = '/GetDynamicImage.aspx?path=' + image + '&w=' + size + '&h=' + size;
			$(element).up().href = '/itemimages/' + image;
		}
	}

	function replaceImageAndZoomChangeDropList(element, size, image, dropListName, itemSelected, exitIf) {
		if ($(element)) {
			$(element).src = '/GetDynamicImage.aspx?path=' + image + '&w=' + size + '&h=' + size;
			$(element).up().href = '/itemimages/' + image;
			document.getElementById(dropListName);
			//loop through all items in here and select the itemSelected to be selected.
			document.getElementById(dropListName).selectedIndex = itemSelected;
			if (exitIf == "false") {
				populateColors('true','true');
			}
		}	
	}
	//function replaceImageAndZoom(imgName, zoomName, fSize, imgFile) {
		//document.getElementById(zoomName).href = "/itemimages/" + imgFile;
		//replaceImage(imgName, fSize, imgFile);
	//}
	
	function clearColors() {
		if ($('ddlColor')) {
			var aOptions = $('ddlColor').getElementsBySelector('option');
			for (var i = 1, len = aOptions.length; i < len; ++i) {
				aOptions[i].remove();
			}
		}
	}

	function addSelectOption(selectObj, text, value, isSelected) {
		if (selectObj != null && selectObj.options != null) {
			selectObj.options[selectObj.options.length] = new Option(text, value, false, isSelected);
		}
	}
	
	function validateOptions() {
		$('options').value = '';
		if ($('ddlColor') && $('ddlColor').value != '' && $('selectedOptions') && $('productOptionValues')) {
			var selectedOptions = $('ddlColor').value + ';' + $('ddlSize').value;
	
			var aOptions = $('productOptionValues').getElementsBySelector('input[@type="hidden"]');
			var optionFound = false;
			for (var i = 0, len = aOptions.length; i < len; ++i) {
				var aOption = aOptions[i].value.split('|')[1];
				var dOffset = aOptions[i].value.split('|')[2];
				var iQty = aOptions[i].value.split('|')[3];
				var sImage = aOptions[i].value.split('|')[4];
				var sSKU = aOptions[i].value.split('|')[5];
				if (aOption.split(';').uniq().sort().join(';') == selectedOptions.split(';').uniq().sort().join(';') && iQty != "" && iQty > 0) {
					$('selectedOptions').value = selectedOptions;
					if (sImage != '') replaceImageAndZoom('imgMain', 380, sImage);
					else replaceImageAndZoom('imgMain', 380, $('defaultImage').value);
					setSKU(sSKU);
					setPrice(dOffset);
					optionFound = true;
					break;
				}
			}
	
			//  Take care of our add to cart button
			if (optionFound) {
			   addToCart.appear();
				//alert('show add button');
				//addToCartContainer
			}
			else {
				addToCart.fade();
			}
		}
	}
	
	function toggleProductTab(divID, parentID) {
		if ($(divID).hasClassName('Description')) {
			$(divID).hide().removeClassName('Description');
		}
	
		var divs = $(parentID).getElementsBySelector('div');
		$(divs).each(function(e) {
			if ($(e).visible() && $(e).identify() != divID) {
				$(e).fade({ duration: 0.3 });
			}
	
			if (!$(e).visible() && $(e).identify() == divID) {
				$(e).appear({ duration: 0.3 });
			}
		});
	}
	
	function setPrice(offset) {
		var dOffset = (isNaN(offset) ? 0 : parseFloat(offset));
		
		if ($('regularPriceBase') && $('regularPrice')) {
			$('regularPrice').innerHTML = '$' + formatCurrency(parseFloat($('regularPriceBase').value) + dOffset);
		}
	
		if ($('salePriceBase') && $('salePrice')) {
			$('salePrice').innerHTML = '$' + formatCurrency(parseFloat($('salePriceBase').value) + dOffset);
		}
	}


	/*
	Disable right click script II (on images)- By Dynamicdrive.com
	For full source, Terms of service, and 100s DTHML scripts
	Visit http://www.dynamicdrive.com
	*/

	var clickmessage="Right clicking has been disabled on images.  Thank you."
	var returnVal = true;
	function disableclick(e) {
		returnVal = true;
		if (document.all) {
			if (event.button==2||event.button==3) {
				if (event.srcElement.tagName=="IMG"){
					alert(clickmessage);
					returnVal = false;
					return false;
				}
			}
		}
		else if (document.layers) {
			if (e.which == 3) {
				alert(clickmessage);
				returnVal = false;
				return false;
			}
		}
		else if (document.getElementById){
			if (e.which==3 && e.target.tagName=="IMG"){
				alert(clickmessage)
				returnVal = false;
				return false}
			}
	}

	function associateimages(){
		for(i=0;i<document.images.length;i++)
		document.images[i].onmousedown=disableclick;
	}

//	if (document.all)
//		document.onmousedown=disableclick
//	else if (document.getElementById)
//		document.onmouseup=disableclick
//	else if (document.layers)
	//	    associateimages()

	if (!document.all) {
	    if (document.getElementById)
	        document.onmouseup = disableclick
	    else if (document.layers)
	        associateimages()
	}
	else {
	    document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
	    var contentloadtag = document.getElementById("contentloadtag");
	    contentloadtag.onreadystatechange = function() { if (this.readyState == "complete") { new associateimages(); } }
	}
		
	function showContext() {
		return(returnVal);	
	}
