var FrescaFilter=new Class({options:{url:"/fcp/categoryfilter/men/Ties",productsPerPage:"500",jsonData:{},filters:[],loadingContainer:"loading",mainContainer:"tieSelector",productsContainer:"results",purchaseContainer:"purchaseContent",filtersContainer:"filters",loadingText:'<p>Personally Pink <br /> Customise Your Tie <br /> is currently loading...</p><img src="/pws/images/tieSelector/px_TieLoader.gif" height="258" id="tieLoader"/>',tieSelectorImage:"img.jpg",startAllDisabled:false,allowMultipleSameTypeFilters:true},initialize:function(a){this.setOptions(a);this.options.url=this.options.url+"?resetFilters=true&productsPerPage="+this.options.productsPerPage},loadingText:function(a){if(a=="on"){$(this.options.loadingContainer).setHTML(this.options.loadingText);$(this.options.loadingContainer).removeClass("hide");$(this.options.mainContainer).addClass("hide")}else{thisFilter=this;$(thisFilter.options.loadingContainer).setHTML();$(thisFilter.options.loadingContainer).addClass("hide");$(thisFilter.options.mainContainer).removeClass("hide")}},loadProducts:function(){this.loadingText("on");new Json.Remote(this.options.url,{onComplete:function(a){this.options.jsonData=a;this.addActiveStates()}.bind(this)}).send()},addActiveStates:function(){for(i=0;i<this.options.jsonData.products.length;i++){product=this.options.jsonData.products[i];product.active=1;for(f=0;f<product.filters.length;f++){product.filters[f].active=1}}(this.options.startAllDisabled==false)?this.updateTieSelector():this.loadingText("off");this.getFilters()},updateTieSelector:function(){this.loadingText("off");currentImage=$("currentProductImage");if(currentImage!=null){$("currentProductImage").remove()}tieSelectorImage=this.options.tieSelectorImage.split(".");for(i=0;i<this.options.filters.length;i++){currentFilter=this.options.filters[i];if(currentFilter.group_active==1){for(f=0;f<currentFilter.values.length;f++){if(currentFilter.states[f]==1){tieSelectorImage[0]=tieSelectorImage[0]+"_"+currentFilter.values[f]}}}}tieSelectorImage=tieSelectorImage[0]+"."+tieSelectorImage[1];productImage=new Asset.image("/pws/images/tieSelector/"+tieSelectorImage.replace(/\s/g,""));productImage.setProperty("class","productImage");productImage.setStyle("width","330px");productImage.setStyle("height","350px");productImage.setProperty("id","currentProductImage");startAgainBtn=$("startAgain");productImage.injectBefore(startAgainBtn)},previewTieSelector:function(a,b){previewImage=$("previewImage");if(previewImage!=null){$("previewImage").remove()}else{if(previewImage==null){tieSelectorImage=$("currentProductImage").getProperty("src").split(".");tieSelectorImage=tieSelectorImage[0]+"_"+this.options.filters[a].values[b]+"."+tieSelectorImage[1];productImage=new Asset.image(tieSelectorImage.replace(/\s/g,""));productImage.setProperty("id","previewImage");productImage.setStyle("width","330px");productImage.setStyle("height","350px");fadeImageIn=new Fx.Style(productImage,"opacity",{duration:750}).set(0);startAgainBtn=$("startAgain");productImage.injectBefore(startAgainBtn);fadeImageIn.start(0,1)}}},getFilters:function(){for(i=0;i<this.options.jsonData.products.length;i++){productFilter=this.options.jsonData.products[i].filters;for(f=0;f<productFilter.length;f++){this.options.filters.include(productFilter[f].group)}}for(i=0;i<this.options.filters.length;i++){thisFilter=this.options.filters[i];this.options.filters[i]={};this.options.filters[i].group=thisFilter;this.options.filters[i].group_active=0;this.options.filters[i].values=[];this.options.filters[i].states=[];this.options.filters[i].counts=[];for(p=0;p<this.options.jsonData.products.length;p++){productFilter=this.options.jsonData.products[p].filters;for(f=0;f<productFilter.length;f++){if(productFilter[f].group==this.options.filters[i].group){this.options.filters[i].values.include(productFilter[f].value)}}}for(f=0;f<this.options.filters[i].values.length;f++){this.options.filters[i].states[f]=0}}this.countFilters()},countFilters:function(){for(i=0;i<this.options.filters.length;i++){for(v=0;v<this.options.filters[i].values.length;v++){count=0;thisGroup=this.options.filters[i].group;thisValue=this.options.filters[i].values[v];for(p=0;p<this.options.jsonData.products.length;p++){product=this.options.jsonData.products[p];productFilter=this.options.jsonData.products[p].filters;product.possible=false;for(f=0;f<productFilter.length;f++){productGroup=productFilter[f].group;productValue=productFilter[f].value;productValueActive=productFilter[f].active;if(productValueActive==0){if((this.options.allowMultipleSameTypeFilters==true)&&(productGroup==thisGroup)&&(productValue==thisValue)){product.possible=true}else{product.possible=false;break}}else{if((productGroup==thisGroup)&&(productValue==thisValue)){product.possible=true}}}if(product.possible==true){count++}}this.options.filters[i].counts[v]=count}}filterContainer=$(this.options.filtersContainer);currentText=filterContainer.getText();(currentText.length==1)?this.outputFilters():this.outputFilterCount()},outputFilters:function(){filterContainer=$(this.options.filtersContainer);filterContainer.setHTML();for(i=0;i<this.options.filters.length;i++){thisFilter=this;currentFilter=this.options.filters[i];filterH3=new Element("h3",{"class":"filterHeading"});filterH3IMG=new Asset.image("/pws/images/tieSelector/px_Step"+(i+1)+".gif");filterH3IMG.addClass("stepHeading");filterRemoveA=new Element("a",{id:"fr"+i,"class":"filterRemove hide"});filterRemoveA.i=i;filterRemoveA.onclick=function(){purchaseContainer=$(thisFilter.options.purchaseContainer);purchaseContainer.addClass("hide");this.v="undefined";$("fr"+this.i).addClass("hide");$("fg"+this.i).removeClass("hide");$("fs"+this.i).remove();for(v=0;v<thisFilter.options.filters[this.i].states.length;v++){if(thisFilter.options.filters[this.i].states[v]==1){this.v=v;break}}otherFilters=this.i+1;for(s=otherFilters;s<thisFilter.options.filters.length;s++){$("fg"+s).addClass("hide");$("fr"+s).addClass("hide");selectedImage=$("fs"+s);if(selectedImage){selectedImage.remove()}thisFilter.options.filters[s].group_active=0;for(t=0;t<thisFilter.options.filters[s].states.length;t++){$("fg"+s+"fv"+t).removeClass("filterSelected");thisFilter.options.filters[s].states[t]=0}}if(this.v!="undefined"){$("fg"+this.i+"fv"+this.v).toggleClass("filterSelected");thisFilter.applyFilter(this.i,this.v)}};if(i==0){filterUL=new Element("ul",{id:"fg"+i,"class":"filterGroup"})}else{filterUL=new Element("ul",{id:"fg"+i,"class":"filterGroup hide"})}for(v=0;v<this.options.filters[i].values.length;v++){filterValue=this.options.filters[i].values[v];filterGroup=this.options.filters[i].group;filterCount=this.options.filters[i].counts[v];filterLI=new Element("li");filterA=new Element("a",{id:"fg"+i+"fv"+v,"class":"filter"});filterA.i=i;filterA.v=v;filterIMG=new Asset.image("/pws/images/tieSelector/btn_"+filterGroup.replace(/\s/g,"")+"_"+filterValue.replace(/\s/g,"")+"_0.jpg");filterOverIMG=new Asset.image("/pws/images/tieSelector/btn_"+filterGroup.replace(/\s/g,"")+"_"+filterValue.replace(/\s/g,"")+"_1.jpg");filterOverIMG.addClass("hide");filterIMG.injectInside(filterA);filterOverIMG.injectInside(filterA);filterA.injectInside(filterLI);filterLI.injectInside(filterUL);filterA.onmouseover=function(){children=this.getChildren();children[0].addClass("hide");children[1].removeClass("hide");thisFilter.previewTieSelector(this.i,this.v)};filterA.onmouseout=function(){children=this.getChildren();children[0].removeClass("hide");children[1].addClass("hide");thisFilter.previewTieSelector()};filterA.onclick=function(){if(this.hasClass("filterDisabled")==false){nextFilter=this.i+1;thisFilter.applyFilter(this.i,this.v);$("fr"+this.i).removeClass("hide");$("fg"+this.i).addClass("hide");this.toggleClass("filterSelected");thisFilter.showSelected(this.i,this.v);if(nextFilter<thisFilter.options.filters.length){$("fr"+nextFilter).addClass("hide");$("fg"+nextFilter).removeClass("hide")}else{if(thisFilter.options.filters[this.i].counts[this.v]==1){for(p=0;p<thisFilter.options.jsonData.products.length;p++){if(thisFilter.options.jsonData.products[p].active==1){thisFilter.useSelectProduct(thisFilter.options.jsonData.products[p].skus[0].skuCode);break}}}else{alert("error: more than 1 product still possible")}}}}}$("startAgain").onclick=function(){window.location.reload()};filterH3IMG.injectInside(filterH3);filterH3.injectInside(filterContainer);filterRemoveA.setText("change");filterRemoveA.injectAfter(filterH3IMG);filterUL.injectInside(filterContainer)}},outputFilterCount:function(){for(i=0;i<this.options.filters.length;i++){for(v=0;v<this.options.filters[i].values.length;v++){filterState=this.options.filters[i].states[v];filterCount=this.options.filters[i].counts[v];(filterCount==0)?$("fg"+i+"fv"+v).addClass("filterDisabled"):$("fg"+i+"fv"+v).removeClass("filterDisabled")}}},showSelected:function(a,b){selectedIMG=new Asset.image("/pws/images/tieSelector/btn_Remove_"+this.options.filters[a].group.replace(/\s/g,"")+"_"+this.options.filters[a].values[b].replace(/\s/g,"")+".jpg");selectedIMG.setProperty("id","fs"+a);selectedIMG.addClass("selectedFilter");filterRemove=$("fr"+a);selectedIMG.injectAfter(filterRemove)},applyFilter:function(a,b){(this.options.filters[a].states[b]==0)?this.options.filters[a].states[b]=1:this.options.filters[a].states[b]=0;if(this.options.filters[a].states[b]==1&&this.options.filters[a].group_active==0){this.options.filters[a].group_active=1}else{otherFiltersSelected=false;for(f=0;f<this.options.filters[a].states.length;f++){if(this.options.filters[a].states[f]==1){otherFiltersSelected=true}}(otherFiltersSelected==true)?this.options.filters[a].group_active=1:this.options.filters[a].group_active=0}for(i=0;i<this.options.jsonData.products.length;i++){product=this.options.jsonData.products[i];productFilters=this.options.jsonData.products[i].filters;product.active=1;for(v=0;v<productFilters.length;v++){productFilters[v].active=1}}for(i=0;i<this.options.jsonData.products.length;i++){product=this.options.jsonData.products[i];productFilters=this.options.jsonData.products[i].filters;for(f=0;f<this.options.filters.length;f++){if(this.options.filters[f].group_active==1){for(v=0;v<productFilters.length;v++){if(productFilters[v].group==this.options.filters[f].group){for(n=0;n<this.options.filters[f].values.length;n++){if((this.options.filters[f].values[n]==productFilters[v].value)&&(this.options.filters[f].states[n]==0)){product.active=0;productFilters[v].active=0}}}}}}}this.countFilters();this.updateTieSelector()},useSelectProduct:function(a){purchaseContainer=$(this.options.purchaseContainer);purchaseContainer.removeClass("hide");$("addToBag").onclick=function(){quantityOptions=$("selectQuantity").getChildren();quantityOptions.each(function(c,b){if(c.selected==true){selectedQuantity=c.value}});window.location="/pws/UpdateBasket.ice?Update=AddQuantity&quantity="+selectedQuantity+"&ProductID="+a};$("addToWishlist").onclick=function(){window.location="/pws/secure/AddToWishList.ice?ProductID="+a+"&pgForward=myAccount&nextForward=viewWishList"}}});FrescaFilter.implement(new Options);FrescaFilter.implement(new Events);var tieSelector=new FrescaFilter({url:"/fcp/categoryfilter/product/PersonallyPinkTies",allowMultipleSameTypeFilters:false,startAllDisabled:false});