offerSelect=function(){this.initialize.apply(this,arguments)};offerSelect.prototype={initialize:function initialize(c){var b,a;this.profile=true;this.loader=$("offer_select_loading");this.table=$("offer_table");this.resultsWrapper=$("results_wrapper");this.noprice=$("no_prices");this.mainProductId=c;this.requestDone=false;this.loadedProducts={};this.initializeFilters();this.sort="price";this.retailerData={};this.resetCheckboxes();if(this.table.getElementsByTagName("tr").length==1){this.table.style.display="none";this.noprice.style.display=""}if(window.location.hash=="#all"&&$("all_product_variations")){$("all_product_variations").checked=true;this.startLoading();setTimeout(this.filtersChanged.bind(this),1)}},initializeFilters:function initializeFilters(){this.selectorDiv=$("price_selector");this.selectorDiv.observe("click",this.selectorFormClick.bind(this))},resetCheckboxes:function resetCheckboxes(){var b,a;var c={all_on_stock:true,all_certificates:true,all_payment_methods:true};c["id_product_variations_"+productId]=true;var e=document.getElementsByTagName("input");for(b=0,a=e.length;b<a;++b){var d=e[b];d.checked=d.id in c}},updateData:function updateData(g,h,f){var i,e,d;for(var a in g){var c=g[a];c.retailerId=a;var m=[];if(!$("retailer_offers_"+c.retailerId)){this.table.insert(c.template)}for(i=0,e=c.offers.length;i<e;++i){var b=c.offers[i];var j=h[b];j.offerId=b;m.push(j)}if(!(a in this.retailerData)){c.offers=m;d=true}else{c.offers=this.retailerData[a].offers.concat(m);d=false}c.offers.sort(this.inclusiveSort);c.inclusive=(m[0].inclusive)?m[0].inclusive:undefined;c.offers.sort(this.priceSort);c.price=c.offers[0].price;if(!f){this.updateOfferDom(c,m,d)}this.retailerData[a]=c}var l=[];for(var a in this.retailerData){var c=this.retailerData[a];c.retailerId=a;l.push(c)}this.retailerList=l},updateOfferDom:function updateOfferDom(e,d,c){var b,a;this.domSortRetailerOffers(e)},startLoading:function loading(){if(!this.loaded){this.loading=true;this.noprice.style.display="none";this.loader.style.display="block";this.resultsWrapper.style.display="none"}},fakeLoadComplete:function fakeLoadComplete(){this.fakeComplete=true;this.stopLoading()},stopLoading:function stopLoading(){if(this.fakeComplete&&this.computeFilterComplete){this.loading=false;this.loader.style.display="none";this.resultsWrapper.style.display="block"}},fakeLoad:function fakeLoad(){this.loaded=false;setTimeout(this.fakeLoadComplete.bind(this),450)},failure:function failure(){this.stopLoading()},ensureProductsLoaded:function ensureProductsLoaded(b){var c,a;var e=false;this.productIds=b;for(c=0,a=b.length;c<a;++c){var d=b[c];if(!(d in this.loadedProducts)){e=true;this.loadProduct(d)}}if(!e){this.computerFilterIdsBound()}},allLoaded:function allLoaded(){var b,a;var d=true;for(b=0,a=this.productIds.length;b<a;++b){var c=this.productIds[b];if(!(c in this.loadedProducts)){d=false}}return d},loadProduct:function loadProduct(a){var b={};this.loaded=false;if(!this.requestDone){this.requestDone=true;new Ajax.Request("../load_prices/",{method:"post",parameters:Object.toQueryString(b),onFailure:this.failure.bind(this),onLoading:this.startLoading.bind(this),onSuccess:this.loadCompleted.bind(this,a)})}else{this.startLoading();this.loadCompleted(a,false)}},loadCompleted:function loadCompleted(b,c){this.loadedProducts[b]=true;if(c!=false){this.retailerData={};var a=c.responseText.evalJSON();this.updateData(a.retailerJson,a.offerJson,false);this.updateObject=a}else{}if(this.allLoaded()){this.loaded=true;this.computerFilterIdsBound()}},selectorFormClick:function selectFormClick(a){var b=a.element();if(b.type&&b.type=="checkbox"){this.startLoading();setTimeout(this.filtersChanged.bind(this),1)}},orderBy:function orderBy(a){this.sort=a;this.filtersChanged()},filtersChanged:function filtersChanged(){this.fakeComplete=false;this.computeFilterComplete=false;this.startLoading();this.handleAll("certificates");this.handleAll("payment_methods");this.handleAll("on_stock");if($("all_product_variations")){this.handleAll("product_variations")}var c=$("id_on_stock_0").checked;var b=$("id_on_stock_1").checked;var a=$("product_variations_container")?this.retrieveCheckboxList("product_variations"):[this.mainProductId];var e=this.retrieveCheckboxList("payment_methods");var d=this.retrieveCheckboxList("certificates");this.computerFilterIdsBound=this.computeFilterIds.bind(this,c,b,e,d,a);this.ensureProductsLoaded(a);this.fakeLoad()},handleAll:function handleAll(b){var a=document.getElementsByName(b);var d=0;var c=$("all_"+b);for(k=0,len=a.length;k<len;++k){d=(a[k].checked)?d+1:d}if(c.checked&&d>0&&c.className=="isnotchecked"){for(k=0,len=a.length;k<len;++k){a[k].checked=false}d=0}if(d>0){c.checked=false;c.className="isnotchecked"}else{c.checked=true;c.className="ischecked"}},retailerMatch:function retailerMatch(e,i,b){if(b=="any"&&i.length==0){return true}var c,h;var d=e.join(";");var f=true;var g=false;for(c=0,h=i.length;c<h;++c){var a=i[c];if(d.indexOf(a)==-1){f=false}else{g=true}}return(b=="all")?f:g},computeFilterIds:function computeFilterIds(m,g,d,c,a){var e,i,n,l;var p={};for(n=0,i=a.length;n<i;++n){p[a[n]]=true}var b=0;for(retailerKey in this.retailerData){var f=this.retailerData[retailerKey];var q=this.retailerMatch(f.paymentMethods,d,"any");var j=this.retailerMatch(f.certificates,c,"any");var h=q&&j;var r=0;f.selectedOffers=[];for(e=0,i=f.offers.length;e<i;++e){var o=f.offers[e];if(((o.onStock||!m)||(o.onStock==undefined&&g))&&o.entityId in p){if(!m&&g&&o.onStock!=undefined){continue}r+=1;f.selectedOffers.push(o)}}this.updateMoreDisplay(f,r);h=h&&(r>0);if(h){b+=r}l=l+r;if(h){$("retailer_offers_"+retailerKey).show()}else{$("retailer_offers_"+retailerKey).hide()}this.domSortRetailerOffers(f)}this.filterOffers();this.updateRowCycle();this.countOffers(b);this.computeFilterComplete=true;this.stopLoading()},filterOffers:function filterOffers(){this.domSortRetailers(this.sort)},retrieveCheckboxList:function retrieveCheckboxList(d){var c,a;var e=document.getElementsByName(d);var b=[];var f=false;if(d=="product_variations"&&$("all_product_variations").checked==true){f=true}for(c=0,a=e.length;c<a;++c){var g=e[c];if(g.checked||f){b.push(g.value)}}return b},domSortRetailerOffers:function domSortRetailerOffers(f){var b,a;var h=(f.selectedOffers)?f.selectedOffers:f.offers;if(this.sort!="price"){h.sort(this.inclusiveSort)}var g=$("retailer_offers_"+f.retailerId);var c="";for(b=0,a=h.length;b<a;++b){var e=h[b];c+=e.template}g.update(c);var d=b;this.updateMoreDisplay(f,d,g)},_moreOffersHtml:'<span class="link" onclick="oSelect.toggleOffers(retailer_id, this);">Laat overige aanbiedingen van deze winkel zien</span>',updateMoreDisplay:function updateMoreDisplay(f,d,b){if(!b){b=$("retailer_offers_"+f.retailerId)}var j=$("retailer_offers_"+f.retailerId);var a=this._moreOffersHtml.replace("retailer_id",f.retailerId).replace("media_url",MEDIA_URL).replace("offers_count",d).replace("retailer_name",f.name);var h=$("more_"+f.retailerId);var g=$("retailer_offers_"+f.retailerId);if(g.className.indexOf("hidden")==-1){a=a.replace("Laat","Verberg").replace("winkel zien","winkel")}if(h&&d>1){$(h).update(a);b.style.height=null;j.style.height=null}else{if(d>1){var c=document.createElement("TR");var e=document.createElement("TD");var i=document.createElement("TD");c.id="more_"+f.retailerId;$(e).update(a);e.className="more_offers";i.className="more_offers";e.colSpan=5;i.colSpan=2;i.innerHTML="&nbsp;";c.appendChild(i);c.appendChild(e);b.appendChild(c);b.style.height=null;j.style.height=null}else{if(d==1&&h){h.remove();b.style.height="35px";j.style.height="35px"}}}},updateRowCycle:function updateRowCycle(){return false;var c,f,e,b,d,a;e="odd";c=document.getElementsByTagName("tbody");for(f=0;f<c.length;f++){if((c[f].className.indexOf("retailer_element")!=-1)&&c[f].style.display!="none"){b=c[f].getElementsByTagName("tr");var g="";if(b[0].className.indexOf("inactive_retailer")!=-1){g="inactive_retailer "}else{if(b[0].className.indexOf("inactive")!=-1){g="inactive "}}if(b.length>0){for(d=0;d<b.length;d++){b[d].className="alt_offer "+e;tds=b[d].getElementsByTagName("td");for(a=0;a<tds.length;a++){if(tds[a].className=="spacer"){if(d>0){tds[a].style.display=""}else{tds[a].style.display="none"}}if(tds[a].className=="shoplogo"||tds[a].className=="retailerinfo"){if(d>0){tds[a].style.display="none"}else{tds[a].style.display=""}}}}b[0].className="main_offer "+g+e;if(b.length>1){b[b.length-1].className=e}e=(e=="odd")?"even":"odd"}}}},domSortRetailers:function domSortRetailers(h){this.sort=h;if(h=="price"){this.retailerList.sort(this.priceSort);this.table.className="pricetable sort_price"}else{this.retailerList.sort(this.inclusiveSort);this.table.className="pricetable sort_price_inclusive"}var c,a;var b=Array();var g=document.createElement("DIV");for(c=0,a=this.retailerList.length;c<a;++c){var e=this.retailerList[c];var d=$("retailer_offers_"+e.retailerId);b.push(d)}var f=this.table.parentNode;g.appendChild(this.table);for(c=0,a=b.length;c<a;++c){var d=b[c];d.parentNode.appendChild(d)}f.appendChild(this.table);this.updateRowCycle()},inclusiveSort:function inclusiveSort(d,c){if(isNaN(d.inclusive)&&isNaN(c.inclusive)){if((!isNaN(d.price)&&!isNaN(c.price))&&d.price!=c.price){return d.price-c.price}else{if((!isNaN(d.rating)&&!isNaN(c.rating))&&(d.rating!=c.rating)){return c.rating-d.rating}else{if(!isNaN(d.id)&&!isNaN(c.id)){return d.id-c.id}}}}if(isNaN(d.inclusive)){return 50000000000}if(isNaN(c.inclusive)){return -50000000000}if(d.inclusive!=c.inclusive){return d.inclusive-c.inclusive}else{if((!isNaN(d.rating)&&!isNaN(c.rating))&&(d.rating!=c.rating)){return c.rating-d.rating}else{if(!isNaN(d.id)&&!isNaN(c.id)){return d.id-c.id}}}return 0},priceSort:function priceSort(d,c){if(d.price!=c.price){return d.price-c.price}else{if((!isNaN(d.inclusive)&&!isNaN(c.inclusive))&&(d.inclusive!=c.inclusive)){return d.inclusive-c.inclusive}else{if((!isNaN(d.rating)&&!isNaN(c.rating))&&(d.rating!=c.rating)){return c.rating-d.rating}else{if(!isNaN(d.id)&&!isNaN(c.id)){return d.id-c.id}}}}return 0},toggleOffers:function toggleOffers(a,c){var b=$("retailer_offers_"+a);if(b.className.indexOf("hidden")!=-1){b.className="retailer_element";c.innerHTML=c.innerHTML.replace("Laat","Verberg").replace("winkel zien","winkel")}else{b.className="retailer_element hidden";c.innerHTML=c.innerHTML.replace("Verberg","Laat").replace("winkel","winkel zien")}},countOffers:function countOffers(a){$("offercount").innerHTML=a;this.table.style.display=(a==0)?"none":"";this.noprice.style.display=(a==0)?"":"none";if(a==1){$("offercount_plural").innerHTML=""}else{$("offercount_plural").innerHTML="en"}}};