var pickedSwatch = 'swatch_id';
var sizeDropDown = 'drop_down';
var skuId = 'skuId';
var swatchDescription = 'swatch_description';
var selSwatchDesc = '';
var selSwatchS7info = '';

function loadSwatches(){
	$$('.product_swatch').each(
		function(target){
			target.selected = false;
			target.getFirst().addClass('product_swatches_norm');
			target.addEvent('click', function(){swatchClick(target);dropDownSelect();});
			target.addEvent('mouseover',function(){swatchMouseover(target);});
			target.addEvent('mouseout',function(){swatchMouseout(target);});
		});
	
	// dpl added: 03/28/2008; selects first swatch by default.
	var el = document.getElementById("prodctID0");
	swatchMouseover(el);
	swatchClick(el);
	attributeSelected('colorSelected');
}
function dropDownSelect() {
	if ($(sizeDropDown)) {
		var skuIdElement = $(skuId);
		var dropDownValue = $(sizeDropDown).value;
		skuIdElement.setProperty('value', dropDownValue);
	}
/*
	if (window.location.toString().indexOf('/TBG/Gifts/GiftCards_M/PRD_GC') > -1) {
		// strip out "To:" "From:" text...
		var objTo = document.getElementById('gw_to_1');
		var objFrom = document.getElementById('gw_from_1');
		var objMessage = document.getElementById('gw_message_1');
		
		//try {
			var strToPrefix = Left(objTo.value,3);
			alert("'" + strToPrefix + "'");
			if (strToPrefix=='To:') {
				alert(Right(objToValue,(objToValue.length-3)));
				objTo.value = objTo.value.replace("To:","");
			}
		//} catch(e) {
		//	alert(e.toString);
		//}
	}
*/
}

function swatchMouseover(swatch){
	if (swatch.selected == false){
		swatch.getFirst().removeClass('product_swatches_norm');
		swatch.getFirst().addClass('product_swatches_sel');
	}
	$(swatchDescription).innerHTML = swatch.getProperty('rel').split('|')[0];
//	callScene7(swatch.getProperty('rel').split('|')[1]); // set Scene7 to the moused over swatch
}
function swatchMouseout(swatch){
	if (swatch.selected == false){
		swatch.getFirst().removeClass('product_swatches_sel');
		swatch.getFirst().addClass('product_swatches_norm');
	}
	$(swatchDescription).innerHTML = selSwatchDesc;
//	callScene7(selSwatchS7info); // set Scene7 back to the selected swatch
}

function swatchClick(swatch){
	if (swatch.selected == false){
		// set the style of all the classes
		$$('.product_swatch').each(
			function(target){
				target.getFirst().removeClass('product_swatches_sel');
				target.getFirst().addClass('product_swatches_norm');
				target.selected = false;
			});

		// set the style of the clicked item
		swatch.getFirst().removeClass('product_swatches_norm');
		swatch.getFirst().addClass('product_swatches_sel');
		swatch.selected = true;

		// set the hidden swatch and store the selected swatch info
		$(pickedSwatch).value = swatch.id;
		selSwatchDesc = swatch.getProperty('rel').split('|')[0];
		selSwatchS7info = swatch.getProperty('rel').split('|')[1];

		// set the size drop down
		var dropDownElem = $(sizeDropDown);

		if (dropDownElem) {
			//options = dropDownElem.getChildren();

			// this .remove code is not working
			// Replaced this function with a remove childrend
			/*
			if (options) {
				options.remove();
			}*/
			// new method to remove previous sizes -- Creilly 10/12/07
			while (dropDownElem.hasChildNodes())
			{
				dropDownElem.removeChild(dropDownElem.childNodes[0]);
			}
			
		}

		var sizes = swatch.getProperty('rel').split('|');

		// if sizes > 3 it means there should be a size drop down
		if (sizes.length > 3) {
			for (var i=2;i<sizes.length;i=i+2){
				
				// dpl 10/22/2009 - added if statement:
				//if (i==2) {
				//	var option2 = new Element('option');
				//	option2.setProperty('value', '').appendText('Choose Size');
				//	$(sizeDropDown).adopt(option2);
				//}
				var option1 = new Element('option');
				if (sizes[i+1].toString().indexOf('Not Available') > -1) {
					option1.setProperty('class', 'size_na');
					option1.setProperty('onmouseover', 'showSizeNA()');
				} else {
					option1.setProperty('onmouseover', 'clearSizeNA()');
				}
				option1.setProperty('value', sizes[i]).appendText(sizes[i+1]);
				$(sizeDropDown).adopt(option1);
			};

		} else {
			// set the value of skuId
			var thisSkuId = swatch.getProperty('rel').split('|')[2];
			$(skuId).setProperty('value', thisSkuId);

		}

	}
}

function showSizeNA() {
	var objMessage = document.getElementById('divSizeMessage');
	objMessage.innerHTML = 'This size is not available in the color you specified';
}
function clearSizeNA() {
	var objMessage = document.getElementById('divSizeMessage');
	objMessage.innerHTML = '';
	}

function callScene7(scene7info){
	// call Scene 7 with the swatch info
	//window.status = scene7info;
}


