
function Database(){if(!Database.existing)Database.existing=this;return Database.existing;}
Database.Table=function(){this.database=new Database();this.rows=$A();}
Database.Table.Row=function(id,data){this.constructor=Database.Table.Row;this.id=id;this.data=$H(data);}
Database.Table.prototype.selectWhereEquals=function(query){var results=new Database.Table();results.rows=this.rows.findAll(function(row){var match=true;$H(query).each(function(item){if(row.data[item.key]!=item.value)match=false;});return match;});return results;}
Database.Table.prototype.updateValues=function(values){this.rows.each(function(row){for(var value in values){row.data[value]=values[value];}});}
Database.Table.prototype.selectDistinctValuesFromColumn=function(fieldName){if(fieldName=='size'){return this.rows.pluck('data').pluck(fieldName).uniq().sort(sizeOrder);}
return this.rows.pluck('data').pluck(fieldName).uniq();}
Database.Table.prototype.add=function(row){this.rows.push((row.constructor==Database.Table.Row)?row:new Database.Table.Row(this.rows.length,row));}
Database.Table.VariationRow=function(id,data){this.id=id;this.data=$H({swatchId:data[0],offerListingId:data[1],offerAmount:data[2],offerFormat:data[3],regularPriceAmount:data[4],regularPriceFormat:data[5],salePriceAmount:data[6],salePriceFormat:data[7],isSalePrice:data[8],percentageSaved:data[9],priceMessageText:data[10],color:data[11],size:data[12],swatchImageUrl:data[13],available:data[14],availabilityText:data[15],SKU:data[16]});}
Database.Table.VariationRow.prototype=new Database.Table.Row();function ParentItemManager(){this.data=new Database.Table();this.sizeSelect=null;this.colorSelect=null;this.quantitySelect=null;this.submitWrap=null;this.regularPriceDisplayNode=null;this.salePriceDisplayNode=null;this.derivedOfferIdInput=null;this.derivedAsinInput=null;this.eventHandler=new ParentItemManager.EventHandler();this.eventHandler.setTiedObject(this);this.priceDisplay=new ParentItemManager.PriceDisplay();this.priceDisplay.init();this.painter=new ParentItemManager.Painter();this.painter.setEventHandler(this.eventHandler);}
ParentItemManager.prototype={init:function(){this.painter.init();var val=this.selectedColor;this.selectedColor=undefined;this.setSelectedColor(val?val:(this.colorSelect?this.colorSelect.getValue():null));var val=this.selectedSize;this.selectedSize=undefined;this.setSelectedSize(val?val:(this.sizeSelect?this.sizeSelect.getValue():null));var val=this.selectedQuantity;this.selectedQuantity=undefined;this.setSelectedQuantity(val?val:(this.quantitySelect?this.quantitySelect.getValue():1));this.updateDerivedChild();},setNode:function(node){this.painter.setNode(node);},setAsin:function(asin){this.asin=asin;},getAsin:function(){return this.asin;},setData:function(data){this.data=data;data.updateValues({parentItemManager:this});data.allAvailableChildren=data.selectWhereEquals({available:true});data.allAvailableSizes=data.allAvailableChildren.selectDistinctValuesFromColumn('size');data.allAvailableColors=data.allAvailableChildren.selectDistinctValuesFromColumn('color');this.painter.setData(data);},setSubmitWrap:function(element){if(this.submitWrap===element)return;this.submitWrap=element;var errorMsg=element.down('.errorWrap');var button=element.down('input');var manager=this;errorMsg.testDisplayCase=function(){errorMsg.style.display='block';}
errorMsg.testHideCase=function(){if(!errorMsg.isHovered&&!button.isHovered)errorMsg.style.display="none";}
if(button&&errorMsg){Event.observe(button,'click',function(){button.isHovered=true;if(!manager.isSubmittable())window.setTimeout(function(){errorMsg.testDisplayCase();},10);});Event.observe(button,'mouseout',function(){button.isHovered=false;window.setTimeout(function(){errorMsg.testHideCase();},10);});Event.observe(errorMsg,'mouseover',function(){errorMsg.isHovered=true;window.setTimeout(function(){errorMsg.testDisplayCase();},10);});Event.observe(errorMsg,'mouseout',function(){errorMsg.isHovered=false;window.setTimeout(function(){errorMsg.testHideCase();},10);});}},isSubmittable:function(){return(this.selectedColor!=null&&this.selectedSize!=null&&this.selectedQuantity>=1);},setSizeSelect:function(select){this.sizeSelect=new ParentItemManager.SizeSelect();this.sizeSelect.setNode($(select));this.sizeSelect.setEventHandler(this.eventHandler);return this.sizeSelect;},setColorSelect:function(select){this.colorSelect=new ParentItemManager.ColorSelect();this.colorSelect.setNode($(select));this.colorSelect.setEventHandler(this.eventHandler);return this.colorSelect;},setQuantitySelect:function(select){this.quantitySelect=new ParentItemManager.QuantitySelect();this.quantitySelect.setNode($(select));this.quantitySelect.setEventHandler(this.eventHandler);return this.quantitySelect;},setPriceDisplayNode:function(node){this.priceDisplay.setNode(node);},setDerivedOfferIdInput:function(input){this.derivedOfferIdInput=$(input);},setDerivedAsinInput:function(input){this.derivedAsinInput=$(input);},setSelectedSize:function(size){if(this.selectedSize!==size){this.selectedSize=size;if(this.colorSelect){if(size!=null){var colors=this.data.allAvailableChildren.selectWhereEquals({'size':size}).selectDistinctValuesFromColumn('color');}else{var colors=this.data.allAvailableColors;}
this.colorSelect.overwriteOptions(colors,this.data);}
if(this.sizeSelect)this.sizeSelect.setValue(size);this.painter.setSelectedSize(size);this.updateDerivedChild();}},setSelectedColor:function(color){if(this.selectedColor!==color){this.selectedColor=color;if(this.sizeSelect){if(color!=null){var sizes=this.data.allAvailableChildren.selectWhereEquals({'color':color}).selectDistinctValuesFromColumn('size');}else{var sizes=this.data.allAvailableSizes;}
this.sizeSelect.overwriteOptions(sizes,this.data);}
if(this.colorSelect)this.colorSelect.setValue(color);this.painter.setSelectedColor(color);this.updateDerivedChild();}},setSelectedQuantity:function(quantity){if(this.selectedQuantity!=quantity){this.selectedQuantity=quantity;if(this.quantitySelect)this.quantitySelect.setValue(quantity);}},updateDerivedChild:function(){var query=new Object();if(this.selectedColor)query.color=this.selectedColor;if(this.selectedSize)query.size=this.selectedSize;var item=this.data.allAvailableChildren.selectWhereEquals(query);if(this.derivedAsinInput){if(item.rows.length==1){this.derivedAsinInput.value=item.rows[0].id;}else{this.derivedAsinInput.value="";}}
if(this.derivedOfferIdInput){if(item.rows.length==1){this.derivedOfferIdInput.value=item.selectDistinctValuesFromColumn('offerListingId')[0];}else{this.derivedOfferIdInput.value="";}}
this.priceDisplay.setData(item);},setHoveredCube:function(cube){this.painter.setHoveredCube(cube);},setUnhoveredCube:function(cube){this.painter.setUnhoveredCube(cube);},setClickedCube:function(cube){this.painter.setClickedCube(cube);}};ParentItemManager.PriceDisplay=function(){}
ParentItemManager.PriceDisplay.templateInnerHTML="#{mainPriceFormat}";ParentItemManager.PriceDisplay.prototype={data:null,node:document.createElement('div'),init:function(){this.template=new Template(ParentItemManager.PriceDisplay.templateInnerHTML);if(ParentItemManager.PriceDisplay.saleTemplateInnerHTML)this.saleTemplate=new Template(ParentItemManager.PriceDisplay.saleTemplateInnerHTML);},setNode:function(node){this.node=node;},setData:function(data){this.data=new Object();this.useSaleTemplate=false;var amounts=data.selectDistinctValuesFromColumn('regularPriceAmount').without(null).sort();var saleAmounts=data.selectDistinctValuesFromColumn('salePriceAmount').without(null).sort();var offerAmounts=data.selectDistinctValuesFromColumn('offerAmount').without(null).sort();var percentageSaved=data.selectDistinctValuesFromColumn('percentageSaved').without(null).without(0).sort();if(amounts.length>0){var lower=data.selectWhereEquals({'regularPriceAmount':amounts.first()}).selectDistinctValuesFromColumn('regularPriceFormat')[0];var upper=data.selectWhereEquals({'regularPriceAmount':amounts.last()}).selectDistinctValuesFromColumn('regularPriceFormat')[0];this.data.regularPriceFormat=lower==upper?lower:lower+' - '+upper;if(saleAmounts.length>0){this.useSaleTemplate=true;this.data.salePriceFormat=this.correctSalePriceFormat(data,offerAmounts);if(percentageSaved.length>0){this.data.percentageSavedFormat=(percentageSaved.first()==percentageSaved.last())?percentageSaved.first()+'%':percentageSaved.first()+'% - '+percentageSaved.last()+'%';}}}
this.updateDisplay();},correctSalePriceFormat:function(data,offerAmounts){var lower=data.selectWhereEquals({'offerAmount':offerAmounts.first()}).selectDistinctValuesFromColumn('offerFormat')[0];var upper=data.selectWhereEquals({'offerAmount':offerAmounts.last()}).selectDistinctValuesFromColumn('offerFormat')[0];if(lower==upper)return lower;if(offerAmounts.first()>offerAmounts.last()){return upper+' - '+lower;}
return lower+' - '+upper;},updateDisplay:function(){if(this.useSaleTemplate&&this.saleTemplate){this.node.innerHTML=this.saleTemplate.evaluate(this.data);}else{this.node.innerHTML=this.template.evaluate(this.data);}}}
ParentItemManager.EventHandler=function(){this.tiedObject=null;}
ParentItemManager.EventHandler.prototype={setTiedObject:function(obj){this.tiedObject=obj;},salePriceTemplate:new Template('<div id=#{0} class="promo now productPrice"><span class="salePriceStatus">#{1} </span><span class="rangedSalePrice">#{2}</span></div>'),regularPriceTemplate:new Template('<div id="#{0}" class="productPrice"><span class="priceStatus">#{1} </span><span class="regularPrice">#{2}</span></div>'),userChangedSelect:function(select){switch(select.type){case'color':this.userSelectedColor(select.getValue());break;case'size':this.userSelectedSize(select.getValue());break;case'quantity':this.userSelectedQuantity(select.getValue());break;}},selectedOptions:function(asin){var color=$('colorSelect_'+asin)?$('colorSelect_'+asin).value:'';var size=$('sizesSelect_'+asin)?$('sizesSelect_'+asin).value:'';color=(color=='Then, Select Color')?'':color;size=(size=='First, Select Size')?'':size;if(color!=''&&size!=''){return'updatePrice';}
else if(color==''||size==''){return'restorePrice';}
else{return;}},userSelectedSize:function(size){this.tiedObject.setSelectedSize(size);if($A(this.tiedObject.data.selectDistinctValuesFromColumn('offerFormat')).size()<2)return;if(this.selectedOptions(this.tiedObject.asin)=='updatePrice'){var itemData=this.getItemDataForEvent(this);this.updatePriceDisplay(this.tiedObject,itemData);}
else if(this.selectedOptions(this.tiedObject.asin)=='restorePrice'){this.restoreParentPrice(this.tiedObject);}},userSelectedColor:function(color){this.tiedObject.setSelectedColor(color);if($A(this.tiedObject.data.selectDistinctValuesFromColumn('offerFormat')).size()<2)return;if(this.selectedOptions(this.tiedObject.asin)=='updatePrice'){var itemData=this.getItemDataForEvent(this);this.updatePriceDisplay(this.tiedObject,itemData);}
else if(this.selectedOptions(this.tiedObject.asin)=='restorePrice'){this.restoreParentPrice(this.tiedObject);}},userSelectedQuantity:function(quantity){this.tiedObject.setSelectedQuantity(quantity);},userHoveredCube:function(cube){this.tiedObject.setHoveredCube(cube);},userUnhoveredCube:function(cube){this.tiedObject.setUnhoveredCube(cube);},userClickedCube:function(cube){this.tiedObject.setClickedCube(cube);},getItemDataForEvent:function(currentElement){var query=new Object();query.size=currentElement.tiedObject.selectedSize;query.color=currentElement.tiedObject.selectedColor;return this.tiedObject.data.allAvailableChildren.selectWhereEquals(query);},updatePriceDisplay:function(itemObject,item){if($('colorSelectLine_'+itemObject.asin)==null)return;var regularPriceNode=itemObject.asin+"_price";var salePriceNode=itemObject.asin+"_sale_price";var isSale=item.selectDistinctValuesFromColumn('isSalePrice');var offerPrice=item.selectDistinctValuesFromColumn('offerFormat');if(isSale[0]==true){if($(regularPriceNode))$(regularPriceNode).remove();nodeType=($(salePriceNode))?'oldNode':'newNode';this.updatePriceNode(salePriceNode,'',offerPrice,'oldNode',itemObject.asin,this.salePriceTemplate);}
else{if($(salePriceNode))$(salePriceNode).innerHTML='</span></span>';nodeType=($(regularPriceNode))?'oldNode':'newNode';this.updatePriceNode(regularPriceNode,'',offerPrice,'oldNode',itemObject.asin,this.regularPriceTemplate);}},restoreParentPrice:function(itemObject){var regularPriceNode=itemObject.asin+"_price";var salePriceNode=itemObject.asin+"_sale_price";var regularPrice=itemObject.priceDisplay.data.regularPriceFormat;var offerAmounts=itemObject.data.selectDistinctValuesFromColumn('offerAmount').without(null).sort();var salePrice=itemObject.priceDisplay.correctSalePriceFormat(itemObject.data,offerAmounts);this.updatePriceNode(regularPriceNode,'Was',regularPrice,'oldNode',itemObject.asin,this.regularPriceTemplate);this.updatePriceNode(salePriceNode,'Now',salePrice,'oldNode',itemObject.asin,this.salePriceTemplate);},updatePriceNode:function(id,status,price,nodeType,asin,priceTemplate){var priceHTML=priceTemplate.evaluate([id,status,price]);if(nodeType=='newNode'){this.addNewNode(priceHTML,asin);}
else if($(id)){$(id).replace(priceHTML);}
else{(id==asin+'_price')?new Insertion.Before(asin+'_sale_price',priceHTML):new Insertion.After(asin+'_price',priceHTML);}},addNewNode:function(priceHTML,asin){newNode=document.createElement('div');newNode.innerHTML=priceHTML;$(asin+"_heading").appendChild(newNode);}};ParentItemManager.Select=function(){}
ParentItemManager.Select.prototype={value:null,node:null,eventHandler:null,setEventHandler:function(handler){this.eventHandler=handler;},setNode:function(node){this.node=node;this.value=node.selectedIndex==0?null:node.options[node.selectedIndex].value;var obj=this;Event.observe(node,'change',function(){obj.value=node.selectedIndex==0?null:(node.value);obj.eventHandler.userChangedSelect(obj);});},getNode:function(){return this.node;},getName:function(){return this.node.name;},setValue:function(value){if(value!=null){var options=this.node.options;for(var i=options.length-1;i>=0;i--){if(options[i].value==value){this.value=options[i].value;try{this.node.options[i].selected=true;}catch(e){this.node.selectedIndex=i;}
return;}}}
this.value=null;this.node.selectedIndex=0;},getValue:function(){return this.value;},overwriteOptions:function(values,data){var newIndex=0;while(this.node.options[1]){Element.remove(this.node.options[1]);}
for(var i=0;i<values.length;i++){var option=document.createElement('option');option.value=values[i];if(data!=null&&this.type=='color'){option.innerHTML=parseColor(values[i]);}
else option.innerHTML=parseColor(values[i]);this.node.appendChild(option);if(option.value==this.value)newIndex=i+1;}
if(newIndex==0)this.value=null;try{this.node.options[newIndex].selected=true;}catch(e){this.node.selectedIndex=newIndex;}}};ParentItemManager.ColorSelect=function(){}
ParentItemManager.ColorSelect.prototype=new ParentItemManager.Select();ParentItemManager.ColorSelect.prototype.type='color';ParentItemManager.SizeSelect=function(){}
ParentItemManager.SizeSelect.prototype=new ParentItemManager.Select();ParentItemManager.SizeSelect.prototype.type='size';ParentItemManager.QuantitySelect=function(){}
ParentItemManager.QuantitySelect.prototype=new ParentItemManager.Select();ParentItemManager.QuantitySelect.prototype.type='quantity';ParentItemManager.QuantitySelect.prototype.setNode=function(node){this.node=node;this.value=node.options[node.selectedIndex].value;var obj=this;Event.observe(node,'change',function(){obj.value=node.options[node.selectedIndex].value;obj.eventHandler.userChangedSelect(obj);});}
ParentItemManager.QuantitySelect.prototype.setValue=function(value){var options=this.node.options;for(var i=options.length-1;i>=0;i--){if(options[i].value==value){this.value=options[i].value;try{this.node.options[i].selected=true;}catch(e){this.node.selectedIndex=i;}
return;}}
this.value=options[0].value;this.node.selectedIndex=0;}
ParentItemManager.Painter=function(){this.swatches=$A();this.eventHandler=null;this.node=document.createElement('div');this.data=null;this.selectedSize=null;this.selectedColor=null;this.selectedCube=null;}
ParentItemManager.Painter.prototype={init:function(){var painter=this;this.data.selectDistinctValuesFromColumn('swatchId').each(function(swatchId){var swatch=new ParentItemManager.Painter.Swatch();swatch.setEventHandler(painter.eventHandler);swatch.setData(painter.data.selectWhereEquals({'swatchId':swatchId}));swatch.init();painter.node.appendChild(swatch.getNode());painter.swatches.push(swatch);});},setEventHandler:function(handler){this.eventHandler=handler;},setNode:function(element){this.node=$(element);},getNode:function(){return this.node;},getFirstCubeMatchingColorAndSize:function(color,size){var query=new Object();if(size)query.size=size;if(color)query.color=color;return this.data.selectWhereEquals(query).selectDistinctValuesFromColumn('cube')[0];},setData:function(data){this.data=data;},setSelectedSize:function(size){if(this.selectedSize!=size){this.selectedSize=size;this.swatches.each(function(swatch){swatch.setSelectedSize(size);});if(this.selectedCube){if(size){if(!this.selectedCube.isAvailableInSize(size)){this.setSelectedCube(this.getFirstCubeMatchingColorAndSize(this.selectedColor,size));}}}}},setSelectedColor:function(color){if(this.selectedColor!=color){this.selectedColor=color;if(this.selectedCube){if(this.selectedSize){if(this.selectedCube.getColor()!=color||!this.selectedCube.isAvailableInSize(this.selectedSize)){if(color){this.setSelectedCube(this.getFirstCubeMatchingColorAndSize(color,this.selectedSize));}else{this.setDeselectedCube(this.selectedCube);}}}else{if(this.selectedCube.getColor()!=color){;if(color){this.setSelectedCube(this.getFirstCubeMatchingColorAndSize(color,this.selectedSize));}else{this.setDeselectedCube(this.selectedCube);}}}}else if(color){this.setSelectedCube(this.getFirstCubeMatchingColorAndSize(color,this.selectedSize));}
this.swatches.each(function(swatch){swatch.setSelectedColor(color);});}},setHoveredCube:function(cube){cube.getSwatch().setHoveredCube(cube);},setUnhoveredCube:function(cube){cube.getSwatch().setUnhoveredCube(cube);},setClickedCube:function(cube){if(this.selectedCube==cube){this.setDeselectedCube(cube);}else{this.setSelectedCube(cube);}},setSelectedCube:function(cube){if(this.selectedCube!=cube){if(this.selectedCube)this.setDeselectedCube(this.selectedCube);this.selectedCube=cube;this.selectedCube.setSelected(true);this.selectedCube.getSwatch().setSelectedCube(cube);this.eventHandler.userSelectedColor(cube.getColor());}},setDeselectedCube:function(cube){if(this.selectedCube==cube){this.selectedCube.setSelected(false);this.selectedCube.getSwatch().setDeselectedCube(cube);this.selectedCube=null;this.eventHandler.userSelectedColor(null);}}}
ParentItemManager.Painter.Swatch=function(){this.cubes=new Object();this.data=new Database.Table();this.colors=$A();this.selectedCube=null;this.selectedColor=null;this.selectedSize=null;this.eventHandler=null;this.setNode(ParentItemManager.Painter.Swatch.templateNode.cloneNode(true));}
ParentItemManager.Painter.Swatch.templateNode=document.createElement('div');ParentItemManager.Painter.Swatch.templateNode.innerHTML='<strong>Select Color:</strong> <select class="colorSwatchSelect"><option>Select Color</option></select><div class="colorSwatchText"></div><div class="colorSwatchWorkspace"></div>';ParentItemManager.Painter.Swatch.prototype={init:function(){var swatch=this;this.colors.each(function(color){var data=swatch.data.selectWhereEquals({'color':color});var cube=new ParentItemManager.Painter.Swatch.Cube();cube.setEventHandler(swatch.eventHandler);cube.setColor(color);cube.setData(data);cube.setSwatch(swatch);cube.setAvailable(true);cube.setSKU(data.selectDistinctValuesFromColumn('SKU')[0]);swatch.cubes[color]=cube;cube.init();swatch.workspace.appendChild(cube.htmlObj);});if(this.priceMessageNode)this.priceMessageNode.innerHTML=this.data.selectDistinctValuesFromColumn('priceMessageText')[0];},setNode:function(node){this.node=node;this.workspace=this.node.down('.colorSwatchWorkspace');this.priceMessageNode=this.node.down('.priceMessage');this.colorSelect=null;if(this.node.down('.colorSwatchSelect')){this.colorSelect=new ParentItemManager.ColorSelect();this.colorSelect.setNode(this.node.down('.colorSwatchSelect'));}
this.sizeSelect=null;if(this.node.down('.sizeSelect')){this.sizeSelect=new ParentItemManager.SizeSelect();this.sizeSelect.setNode(this.node.down('.sizeSelect'));}
this.textDisplay=null;if(this.node.down('.colorSwatchText')){this.textDisplay=new ParentItemManager.Painter.Swatch.TextDisplay();this.textDisplay.setNode(this.node.down('.colorSwatchText'));}},getNode:function(){return this.node;},setData:function(data){this.data=data;this.data.updateValues({colorSwatch:this});this.colors=data.selectDistinctValuesFromColumn('color');},setEventHandler:function(handler){this.eventHandler=handler;},setSelectedColor:function(color){this.selectedColor=color;if(this.colorSelect)this.colorSelect.setValue(color);if(this.sizeSelect){if(this.selectedColor){var sizes=this.data.selectWhereEquals({color:this.selectedColor}).selectDistinctValuesFromColumn('size');}else{var sizes=this.data.selectDistinctValuesFromColumn('size');}
this.sizeSelect.overwriteOptions(sizes,this.data);}
for(id in this.cubes)this.cubes[id].setSelectedColor(color);},setSelectedSize:function(size){this.selectedSize=size;if(this.sizeSelect)this.sizeSelect.setValue(size);if(this.colorSelect){if(this.selectedSize){var colors=this.data.selectWhereEquals({color:this.selectedSize}).selectDistinctValuesFromColumn('color');}else{var colors=this.data.selectDistinctValuesFromColumn('color');}
this.colorSelect.overwriteOptions(colors,this.data);}
for(id in this.cubes)this.cubes[id].setSelectedSize(size);},setSelectedCube:function(cube){this.selectedCube=cube;if(this.textDisplay)this.textDisplay.setSelectedCube(cube);},setDeselectedCube:function(cube){if(cube&&this.selectedCube==cube){if(this.textDisplay)this.textDisplay.setSelectedCube(null);this.selectedCube=null;}},setHoveredCube:function(cube){cube.setHovered(true);if(this.textDisplay)this.textDisplay.addHoveredCube(cube);},setUnhoveredCube:function(cube){cube.setHovered(false);if(this.textDisplay)this.textDisplay.removeHoveredCube(cube);}};ParentItemManager.Painter.Swatch.TextDisplay=function(){this.unavailableTextNode=document.createElement('span');Element.extend(this.unavailableTextNode);this.unavailableTextNode.innerHTML="&#160;Not Available";this.unavailableTextNode.addClassName('availabilityText');this.setNode(document.createElement('span'));this.hoveredCubes=new Array();this.selectedCube=null;this.setSelectedCube(null);}
ParentItemManager.Painter.Swatch.TextDisplay.prototype={setNode:function(node){this.node=node;this.node.appendChild(this.unavailableTextNode);},addHoveredCube:function(cube){this.hoveredCubes.unshift(cube);this.updateDisplay();},removeHoveredCube:function(cube){for(var i=0;i<this.hoveredCubes.length;i++){if(this.hoveredCubes[i]==cube){this.hoveredCubes.splice(i,1);break;}}
this.updateDisplay();},setSelectedCube:function(cube){this.selectedCube=cube;this.updateDisplay();},updateDisplay:function(){this.node.removeChild(this.unavailableTextNode);if(this.hoveredCubes[0]){this.node.innerHTML=this.hoveredCubes[0].getColor();if(this.hoveredCubes[0].getAvailable()){this.unavailableTextNode.style.display="none";}else{this.unavailableTextNode.style.display="inline";}}else if(this.selectedCube!=null){this.node.innerHTML=this.selectedCube.getColor();if(this.selectedCube.getAvailable()){this.unavailableTextNode.style.display="none";}else{this.unavailableTextNode.style.display="inline";}}else{this.node.innerHTML="";this.unavailableTextNode.style.display="none";}
this.node.appendChild(this.unavailableTextNode);}};ParentItemManager.Painter.Swatch.Cube=function(){this.htmlObj=document.createElement('div');Element.extend(this.htmlObj);this.greyBox=document.createElement('div');Element.extend(this.greyBox);this.greyBox.addClassName("swatchGrey");this.htmlImg=document.createElement('img');Element.extend(this.htmlImg);this.setImageUrl('http://g-ec2.images-amazon.com/images/G/01/Lacoste/en_US/core/buttons/defaultSwatchImage.png');this.htmlObj.appendChild(this.htmlImg);this.htmlObj.appendChild(this.greyBox);new Element.GreyManager().addTie(this.greyBox,this.htmlObj);this.selectedColor=null;this.selectedSize=null;this.SKU=null;this.swatch=null;this.setColor(null);this.setAvailable(false);this.availableSizes=new Array();this.data=new Database.Table();this.eventHandler=null;this.clickedEventFunction=ParentItemManager.Painter.Swatch.Cube.userClickedCubeEVENT.bindAsEventListener(this);}
ParentItemManager.Painter.Swatch.Cube.userClickedCubeEVENT=function(){this.eventHandler.userClickedCube(this);}
ParentItemManager.Painter.Swatch.Cube.userHoveredCubeEVENT=function(){this.eventHandler.userHoveredCube(this);}
ParentItemManager.Painter.Swatch.Cube.userUnhoveredCubeEVENT=function(){this.eventHandler.userUnhoveredCube(this);}
ParentItemManager.Painter.Swatch.Cube.prototype={init:function(){this.setSelected(false);this.setHovered(false);var cube=this;if(this.eventHandler)Event.observe(this.htmlObj,"mouseover",ParentItemManager.Painter.Swatch.Cube.userHoveredCubeEVENT.bindAsEventListener(this));if(this.eventHandler)Event.observe(this.htmlObj,"mouseout",ParentItemManager.Painter.Swatch.Cube.userUnhoveredCubeEVENT.bindAsEventListener(this));},setEventHandler:function(handler){this.eventHandler=handler;},setSwatch:function(swatch){this.swatch=swatch;},getSwatch:function(){return this.swatch;},setData:function(data){this.data=data;this.data.updateValues({cube:this});var url=data.selectDistinctValuesFromColumn('swatchImageUrl')[0];if(url&&url.length>0)this.setImageUrl(url);this.setAvailableSizes(data.selectWhereEquals({'available':true}).selectDistinctValuesFromColumn('size'));},setImageUrl:function(url){if(url==null||url.length<1)return false;this.htmlImg.style.width=this.htmlImg.style.height="18px";var htmlObj=this.htmlObj;this.htmlImg.setAttribute('src',url);},setAvailableSizes:function(sizes){this.availableSizes=sizes;},getAvailableSizes:function(){return this.availableSizes;},isAvailableInSize:function(size){return(this.availableSizes.indexOf(size)!=-1||!size);},setSelectedSize:function(size){this.selectedSize=size;this.setAvailable(this.isAvailableInSize(size));},setSelectedColor:function(color){if(this.selected){var selectedSKU=this.SKU.substr(0,this.SKU.lastIndexOf('_'));}},setAvailable:function(bool){if(bool==this.available)return;this.available=bool;Event.stopObserving(this.htmlObj,"click",this.clickedEventFunction);if(bool&&this.eventHandler){Event.observe(this.htmlObj,"click",this.clickedEventFunction);}
this.updateDisplay();},getAvailable:function(){return this.available;},setSelected:function(bool){this.selected=bool;this.updateDisplay();},getSelected:function(){return this.selected;},setHovered:function(bool){this.hovered=bool;this.updateDisplay();},setColor:function(color){this.color=color;this.htmlImg.setAttribute("alt",parseColor(color));},getColor:function(){return this.color;},setSKU:function(sku){this.SKU=sku;},getSKU:function(){return this.SKU;},updateDisplay:function(){this.htmlObj.className="colorSwatchCube";if(this.selected==true){this.htmlObj.addClassName("selected");}else{if(this.hovered==true)this.htmlObj.addClassName("hovered");}
if(this.available){this.htmlObj.ungrey();this.htmlObj.style.cursor="pointer";}else{this.htmlObj.grey();this.htmlObj.style.cursor="default";}}};