var inCart=false;if(window.location.toString().search('/product_compare/')!=-1){var win=window.opener;}
else{var win=window;}
if(window.location.toString().search('/checkout/cart/')!=-1){inCart=true;}
function setLocation(url){if(!inCart&&((url.search('/add')!=-1)||(url.search('/remove')!=-1)||url.search('checkout/cart/add')!=-1)){sendcart(url,'url');}else{window.location.href=url;}}
function sendcart(url,type){showLoading();if(type=='form'){url=($('product_addtocart_form').action).replace('checkout','j2tajaxcheckout/index/cart');var myAjax=new Ajax.Request(url,{method:'post',postBody:$('product_addtocart_form').serialize(),parameters:Form.serialize("product_addtocart_form"),onException:function(xhr,e)
{alert('Exception : '+e);},onComplete:function(xhr)
{var start3=xhr.responseText.indexOf('<div class="j2t_ajax_message">')+30;var end3=xhr.responseText.indexOf("<span>j2t_ajax_auto_add</span></div>",start3);var return_message=xhr.responseText.substring(start3,end3);var start=xhr.responseText.indexOf('<div id="back-ajax-add">')+24;var end=xhr.responseText.indexOf("</div>",start);$('j2t_ajax_confirm').innerHTML=return_message+xhr.responseText.substring(start,end);var start2=xhr.responseText.indexOf('<div id="cart_content">')+23;var end2=xhr.responseText.indexOf("</div>",start2);$$('.top-link-cart').each(function(el){el.innerHTML=xhr.responseText.substring(start2,end2);});var start4=xhr.responseText.indexOf('<div class="cart_side_ajax">')+28;var end4=xhr.responseText.indexOf("<span>j2t_ajax_auto_add</span></div>",start4);$$('.mini-cart').each(function(el){el.replace(xhr.responseText.substring(start4,end4));});$$('.block-cart').each(function(el){el.replace(xhr.responseText.substring(start4,end4));});var start5=xhr.responseText.indexOf('<div id="cart_content">Mon panier (')+35;var end5=xhr.responseText.indexOf(" articles)</div>",start5);var start6=xhr.responseText.indexOf('<span class="price">')+20;var end6=xhr.responseText.indexOf("</span>",start5);if(end5==-1)
{end5=xhr.responseText.indexOf(" article)</div>",start5);$$('.quick-cart .items').each(function(el){el.replace('<div class="items"><strong>'+xhr.responseText.substring(start5,end5)+' produit</strong></div>');});}
else
{$$('.quick-cart .items strong').each(function(el){el.replace('<strong>'+xhr.responseText.substring(start5,end5)+' produits</strong>');});}
$$('.quick-cart .total .price').each(function(el){el.replace('<span class="price">'+xhr.responseText.substring(start6,end6)+'</span>');});replaceDelUrls();if(ajax_cart_show_popup){showConfirm();}else{hideJ2tOverlay();}}});}else if(type=='url'){url=url.replace('checkout','j2tajaxcheckout/index/cart');var myAjax=new Ajax.Request(url,{method:'post',postBody:'',onException:function(xhr,e)
{alert('Exception : '+e);},onComplete:function(xhr)
{var start3=xhr.responseText.indexOf('<div class="j2t_ajax_message">')+30;var end3=xhr.responseText.indexOf("<span>j2t_ajax_auto_add</span></div>",start3);var return_message=xhr.responseText.substring(start3,end3);var start=xhr.responseText.indexOf('<div id="back-ajax-add">')+24;var end=xhr.responseText.indexOf("</div>",start);$('j2t_ajax_confirm').innerHTML=return_message+xhr.responseText.substring(start,end);var start2=xhr.responseText.indexOf('<div id="cart_content">')+23;var end2=xhr.responseText.indexOf("</div>",start2);$$('.top-link-cart').each(function(el){el.innerHTML=xhr.responseText.substring(start2,end2);});var start4=xhr.responseText.indexOf('<div class="cart_side_ajax">')+28;var end4=xhr.responseText.indexOf("<span>j2t_ajax_auto_add</span></div>",start4);$$('.mini-cart').each(function(el){el.replace(xhr.responseText.substring(start4,end4));});$$('.block-cart').each(function(el){el.replace(xhr.responseText.substring(start4,end4));});replaceDelUrls();if(ajax_cart_show_popup){showConfirm();}else{hideJ2tOverlay();}}});}}
function replaceDelUrls(){if(!inCart){$$('a').each(function(el){if(el.href.search('checkout/cart/delete')!=-1){el.href='javascript:cartdelete(\''+el.href+'\')';}});}}
function replaceAddUrls(){$$('a').each(function(link){if(link.href.search('checkout/cart/add')!=-1){link.href='javascript:setLocation(\''+link.href+'\'); void(0);';}});}
function cartdelete(url){showLoading();url=url.replace('checkout','j2tajaxcheckout/index/cart');var myAjax=new Ajax.Request(url,{method:'post',postBody:'',onException:function(xhr,e)
{alert('Exception : '+e);},onComplete:function(xhr)
{var start2=xhr.responseText.indexOf('<div id="cart_content">')+23;var end2=xhr.responseText.indexOf("</div>",start2);$$('.top-link-cart').each(function(el){el.innerHTML=xhr.responseText.substring(start2,end2);});var start4=xhr.responseText.indexOf('<div class="cart_side_ajax">')+28;var end4=xhr.responseText.indexOf("<span>j2t_ajax_auto_add</span></div>",start4);$$('.mini-cart').each(function(el){el.replace(xhr.responseText.substring(start4,end4));});$$('.block-cart').each(function(el){el.replace(xhr.responseText.substring(start4,end4));});var start5=xhr.responseText.indexOf('<div id="cart_content">Mon panier (')+35;var end5=xhr.responseText.indexOf(" articles)</div>",start5);var start6=xhr.responseText.indexOf('<span class="price">')+20;var end6=xhr.responseText.indexOf("</span>",start5);if(start5==34)
{$$('.quick-cart .items').each(function(el){el.replace('<div class="items"><strong>Aucun produit</strong></div>');});}
else if(end5==-1)
{end5=xhr.responseText.indexOf(" article)</div>",start5);$$('.quick-cart .items').each(function(el){el.replace('<div class="items"><strong>'+xhr.responseText.substring(start5,end5)+' produit</strong></div>');});}
else
{$$('.quick-cart .items strong').each(function(el){el.replace('<strong>'+xhr.responseText.substring(start5,end5)+' produits</strong>');});}
if(start6==19)
{$$('.quick-cart .total .price').each(function(el){el.replace('<span class="price">0,00&nbsp;€</span>');});}
else
{$$('.quick-cart .total .price').each(function(el){el.replace('<span class="price">'+xhr.responseText.substring(start6,end6)+'</span>');});}
replaceDelUrls();hideJ2tOverlay();}});}
function showJ2tOverlay(){new Effect.Appear($('j2t-overlay'),{duration:0.5,to:0.8});}
function hideJ2tOverlay(){$('j2t-overlay').hide();$('j2t_ajax_progress').hide();$('j2t_ajax_confirm').hide();}
function j2tCenterWindow(element){if($(element)!=null){var el=$(element);var elDims=el.getDimensions();var browserName=navigator.appName;if(browserName==="Microsoft Internet Explorer"){if(document.documentElement.clientWidth==0){var y=(document.viewport.getScrollOffsets().top+(document.body.clientHeight-elDims.height)/2);var x=(document.viewport.getScrollOffsets().left+(document.body.clientWidth-elDims.width)/2);}
else{var y=(document.viewport.getScrollOffsets().top+(document.documentElement.clientHeight-elDims.height)/2);var x=(document.viewport.getScrollOffsets().left+(document.documentElement.clientWidth-elDims.width)/2);}}
else{var y=Math.round(document.viewport.getScrollOffsets().top+((window.innerHeight-$(element).getHeight()))/2);var x=Math.round(document.viewport.getScrollOffsets().left+((window.innerWidth-$(element).getWidth()))/2);}
var styles={position:'absolute',top:y+'px',left:x+'px'};el.setStyle(styles);}}
function showLoading(){showJ2tOverlay();var progress_box=$('j2t_ajax_progress');progress_box.show();progress_box.style.width=loadingW+'px';progress_box.style.height=loadingH+'px';progress_box.style.position='absolute';j2tCenterWindow(progress_box);}
function showConfirm(){$('j2t_ajax_progress').hide();var confirm_box=$('j2t_ajax_confirm');confirm_box.show();confirm_box.style.width=confirmW+'px';confirm_box.style.height=confirmH+'px';confirm_box.style.position='absolute';j2tCenterWindow(confirm_box);}
document.observe("dom:loaded",function(){replaceDelUrls();replaceAddUrls();var cartInt=setInterval(function(){if(typeof productAddToCartForm!='undefined'){if($('j2t-overlay')){Event.observe($('j2t-overlay'),'click',hideJ2tOverlay);}
productAddToCartForm.submit=function(url){if(this.validator&&this.validator.validate()){sendcart('','form');clearInterval(cartInt);}
return false;}}else{clearInterval(cartInt);}},500);});
