/*--------------------------------------------------------------------------*
 * 
 * Soft Add To Cart (Alley)
 * 
 * Version 1.0.4
 * 
 * Copyright (C) 2009-2010 Brand Labs LLC
 * 
 *--------------------------------------------------------------------------*/
var AlleySettings={ENABLED:false,PRODUCT_ENABLED:true,CATEGORY_ENABLED:true,FORM_SELECTOR:'#content_area form[name="MainForm"]',CATEGORY_ADD_TO_CART_BUTTON_SELECTOR:'a[href^="/ShoppingCart.asp?ProductCode="] img[src$="btn_addtocart_small.gif"]',PRODUCT_ADD_TO_CART_BUTTON_SELECTOR:'input[name="btnaddtocart"]',PRODUCT_ADD_TO_WISH_LIST_BUTTON_SELECTOR:'input[name="btnaddtowishlist"]',PRODUCT_UPDATE_PRICE_BUTTON_SELECTOR:'input[name="btnupdateprice"]',CATEGORY_ADD_TO_CART_BUTTON_DISABLED:null,PRODUCT_ADD_TO_CART_BUTTON_DISABLED:null,HIDE_CART_SUMMARY:true,RETURN_TO_PATH:'/v/alley/added_to_cart.asp'};var Alley={LOADED:false,load:function(){try{if(!AlleySettings.ENABLED){return;}
if(Alley.LOADED){return;}
Alley.LOADED=true;if(location.pathname.toLowerCase()=='/productdetails.asp'||location.pathname.toLowerCase().indexOf('-p/')!=-1||location.pathname.toLowerCase().indexOf('_p/')!=-1){if(!AlleySettings.PRODUCT_ENABLED){return;}
Alley.loadProduct();}
else if(location.pathname.toLowerCase()=='/searchresults.asp'||location.pathname.toLowerCase().indexOf('-s/')!=-1||location.pathname.toLowerCase().indexOf('_s/')!=-1){if(!AlleySettings.CATEGORY_ENABLED){return;}
Alley.loadCategory();}}
catch(e){}},getFormElement:function(){var aa=$$(AlleySettings.FORM_SELECTOR);if(aa==null||aa.size()<1){return null;}
return aa.first();},loadProduct:function(){var ba=Alley.getFormElement();if(ba==null){return;}
if(AlleySettings.PRODUCT_ADD_TO_CART_BUTTON_DISABLED!=null){new Image().src=AlleySettings.PRODUCT_ADD_TO_CART_BUTTON_DISABLED;}
new AlleyProductSoftAddToCart(ba);},loadCategory:function(){var ca=Alley.getFormElement();if(ca==null){return;}
if(AlleySettings.CATEGORY_ADD_TO_CART_BUTTON_DISABLED!=null){new Image().src=AlleySettings.CATEGORY_ADD_TO_CART_BUTTON_DISABLED;}$$(AlleySettings.FORM_SELECTOR+' '+AlleySettings.CATEGORY_ADD_TO_CART_BUTTON_SELECTOR).each(function(da){new AlleyCategorySoftAddToCart(da);});}};Event.observe(window,'load',Alley.load);var AlleySoftAddToCart=Class.create({CONFIRMATION_MESSAGE:'The item(s) have been added to your cart.',CART_SUMMARY_ID:'display_cart_summary',displayConfirmation:function(){this.hideCartSummary();window.alert('The item(s) have been added to your cart.');},hideCartSummary:function(){var ea=null;if(!AlleySettings.HIDE_CART_SUMMARY){return;}
ea=$(this.CART_SUMMARY_ID);if(ea==null){return;}
ea.hide();}});var AlleyProductSoftAddToCart=Class.create(AlleySoftAddToCart,{initialize:function(fa){var ga=null;this.formElement=fa;if(this.formElement==null){return;}
ga=$$(AlleySettings.FORM_SELECTOR+' '+AlleySettings.PRODUCT_ADD_TO_CART_BUTTON_SELECTOR);if(ga==null||ga.size()<1){return;}
this.buttonElement=ga.first();Event.observe(this.buttonElement,'click',this.buttonClicked.bind(this));this.otherButtonElements=new Array();$$(AlleySettings.FORM_SELECTOR+' '+AlleySettings.PRODUCT_ADD_TO_WISH_LIST_BUTTON_SELECTOR).each(function(ha){this.otherButtonElements.push(ha);Event.observe(ha,'click',this.buttonClicked.bind(this));},this);$$(AlleySettings.FORM_SELECTOR+' '+AlleySettings.PRODUCT_UPDATE_PRICE_BUTTON_SELECTOR).each(function(ia){this.otherButtonElements.push(ia);Event.observe(ia,'click',this.buttonClicked.bind(this));},this);this.buttonElementSource=null;this.enableButton();this.lastButtonClickedElement=this.buttonElement;Event.observe(this.formElement,'submit',this.formSubmitted.bind(this));},formSubmitted:function(ja){var ka=null;try{if(this.lastButtonClickedElement!=null&&this.lastButtonClickedElement!=this.buttonElement){this.lastButtonClickedElement=null;return;}
this.lastButtonClickedElement=null;Event.stop(ja);this.disableButton();ka=this;this.formElement.request({method:'post',evalJSON:true,sanitizeJSON:true,encoding:'iso-8859-1',parameters:{'ReturnTo':AlleySettings.RETURN_TO_PATH,'btnaddtocart.x':'0','btnaddtocart.y':'0'},onSuccess:function(la){if(la.getResponseHeader('X-Added-To-Cart')==null||la.getResponseHeader('X-Added-To-Cart')==''){ka.manualFormSubmission();return;}
ka.enableButton();ka.formElement.reset();ka.removeErrorMessage();ka.displayConfirmation();},onFailure:function(){ka.manualFormSubmission();}});}
catch(e){this.manualFormSubmission();}},enableButton:function(){this.buttonElement.writeAttribute({disabled:null});this.otherButtonElements.invoke('writeAttribute',{disabled:null});if(this.buttonElementSource!=null&&AlleySettings.PRODUCT_ADD_TO_CART_BUTTON_DISABLED!=null){this.buttonElement.writeAttribute({src:this.buttonElementSource});this.buttonElementSource=null;}},disableButton:function(){this.buttonElement.writeAttribute({disabled:'disabled'});this.otherButtonElements.invoke('writeAttribute',{disabled:'disabled'});if(AlleySettings.PRODUCT_ADD_TO_CART_BUTTON_DISABLED!=null){this.buttonElementSource=this.buttonElement.readAttribute('src');this.buttonElement.writeAttribute({src:AlleySettings.PRODUCT_ADD_TO_CART_BUTTON_DISABLED});}},manualFormSubmission:function(){this.formElement.insert(new Element('input',{type:'hidden',name:'btnaddtocart.x',value:'0'}));this.formElement.insert(new Element('input',{type:'hidden',name:'btnaddtocart.y',value:'0'}));this.formElement.submit();},removeErrorMessage:function(){var ma=null;var na=null;try{ma=$$(AlleySettings.FORM_SELECTOR+' '+'table tbody tr td table tbody tr td img[src="/a/a/i/error_alert.gif"]');na=null;if(ma==null||ma.size()<1){return;}
na=ma.first();na=na.up().up().up().up().up().up().up().up();na.previous('br').remove();na.next('br').remove();na.remove();}
catch(e){}},buttonClicked:function(oa){try{this.lastButtonClickedElement=oa.element();}
catch(e){}}});var AlleyCategorySoftAddToCart=Class.create(AlleySoftAddToCart,{initialize:function(pa){this.buttonElement=pa;if(this.buttonElement==null){return;}
this.anchorElement=this.buttonElement.up();if(this.anchorElement==null){return;}
this.anchorElementHref=this.anchorElement.readAttribute('href');this.productCode=this.getProductCodeFromURL(this.anchorElementHref);if(this.anchorElementHref==null||this.productCode==null){return;}
this.buttonElementSource=null;this.buttonClickedBound=this.buttonClicked.bind(this);this.enableButton();},buttonClicked:function(qa){var ra=null;try{Event.stop(qa);this.disableButton();ra=this;new Ajax.Request(this.anchorElementHref,{method:'get',evalJSON:true,evalJS:false,sanitizeJSON:true,encoding:'iso-8859-1',parameters:{'ReturnTo':AlleySettings.RETURN_TO_PATH},onSuccess:function(sa){if(sa.getResponseHeader('X-Added-To-Cart')==null||sa.getResponseHeader('X-Added-To-Cart')==''){window.location.href=ra.anchorElementHref;return;}
ra.enableButton();ra.displayConfirmation();},onFailure:function(){window.location.href=ra.anchorElementHref;}});}
catch(e){window.location.href=this.anchorElementHref;}},enableButton:function(){Event.observe(this.anchorElement,'click',this.buttonClickedBound);this.anchorElement.writeAttribute({href:this.anchorElementHref,onclick:null});if(this.buttonElementSource!=null&&AlleySettings.CATEGORY_ADD_TO_CART_BUTTON_DISABLED!=null){this.buttonElement.writeAttribute({src:this.buttonElementSource});this.buttonElementSource=null;}},disableButton:function(){Event.stopObserving(this.anchorElement,'click',this.buttonClickedBound);this.anchorElement.writeAttribute({href:'#',onclick:'return false;'});if(AlleySettings.CATEGORY_ADD_TO_CART_BUTTON_DISABLED!=null){this.buttonElementSource=this.buttonElement.readAttribute('src');this.buttonElement.writeAttribute({src:AlleySettings.CATEGORY_ADD_TO_CART_BUTTON_DISABLED});}},getProductCodeFromURL:function(ta){var ua=null;if(ta==null){return null;}
ua=ta.match(/[\/]?shoppingcart\.asp\?(?:[\&]?.*\=.*)*productcode=([^\&\#]+)/i);if(ua!=null&&ua.length>=2){return unescape(ua[1]);}
return null;}});
