/*
 * Override the function to disable wishlist with AjaxCartPro
 */
setLocation = function(url){
	if(window.location.href.match('https://') && !url.match('https://')){
		url = url.replace('http://','https://')
	}
	if(AW_ACP.isCartPage && ((url.search('/add') != -1 ) || (url.search('/remove') != -1 )) ){
		ajaxcartsend(url+'awacp/1/is_checkout/1', 'url', '', '');
	}else if (url.search('checkout/cart/add') != -1){	
		ajaxcartsend(url+'awacp/1', 'url', '', '');
	}else if (url.search('options=cart') != -1){
		ajaxcartsendconfigurable(url);
	}
	else
	{
		window.location.href = url;
	}
}

/*
 * Set the fonctions init at document.loaded in ajaxcartpro module
 */
function reloadBaseFunctions(){
	$('aw_acp_continue').onclick = function(e){
		e = e||event;
		if(e.preventDefault)
			e.preventDefault()
		$$('.ajaxcartpro_confirm')[0].style.display='none';$$('.ajaxcartpro_overlay')[0].style.display = 'none';return false;
	}
	
	$('aw_acp_checkout').onclick = function(e){
		$$('.ajaxcartpro_confirm')[0].style.display='none';$$('.ajaxcartpro_overlay')[0].style.display='none';return true;
	}	
}

function ajaxcartsend(url, type, obj){
    url = getCommonUrl(url)
	showProgressAnimation();
	if (type == 'form'){		
		var aForm = $('product_addtocart_form_acp') ? $('product_addtocart_form_acp') : $('product_addtocart_form');
        aForm.action += url;

        aForm.request({
            onComplete:  function(resp){
                if (typeof(resp.responseText) == 'string'){
                    try{
                        eval('resp = ' + resp.responseText);
                    }catch(e){
                        if (obj.form.submit()){
                            return;
                        }
                        else{
                            win.location.href = obj.form.action;
                            return;
                        }
                    }
                }
				hideProgressAnimation();
				if (resp.r != 'success'){
					obj.form.submit();
				}
				else{
					__onACPRender();
                    if(AW_ACP.useConfirmation && (url.search('is_checkout/1') != 1)){	
						showConfirmDialog(resp.product_name,resp.popup_content,resp.related_product); // Add the popup_content parameter
					}
					updateCartView(resp);
				}
			}
        })

	}
	if (type == 'url'){

		new Ajax.Request(url, {
          onSuccess: function(resp){
          		console.log(resp);
				try{
					if (typeof(resp.responseText) == 'string') eval('resp = ' + resp.responseText);
				}catch(e){
					win.location.href=url;
					hideProgressAnimation();
					return;
				}
				hideProgressAnimation();
                if (resp.r != 'success'){
					win.location.href=url;
				}
				else{		
                    if(AW_ACP.useConfirmation && (url.search('is_checkout/1') == -1)){
						showConfirmDialog(resp.product_name,resp.popup_content,resp.related_product); // Add the popup_content parameter
					}
					__onACPRender();
					updateCartView(resp);
				}
			}
        });

	}
}

function showConfirmDialog(product_name,popup_content,related_product){
	
	block = $$('.ajaxcartpro_confirm')[0];
	
	// If exist, set the content in the popup
	if (popup_content){
		block.innerHTML = popup_content;
		
		reloadBaseFunctions();
	}
	else{
		if (product_name) $('acp_product_name').innerHTML = product_name;
	}
	
	if(related_product){
	
		$('popup-related-product').innerHTML = related_product; //ajout des relateds
		
		jQuery("#popup-related-product .item").hover( // toggleItem
		  function () {
		    toogleItem(jQuery(this),true);
		  },
		  function () {
			toogleItem(jQuery(this),false);
		  }
		);
		
	}

    alignBlock(block, 800, 300, 'confirmation');
    block.style.display = 'block';
	$$('.ajaxcartpro_overlay')[0].style.display = 'block';
    if (typeof($$('.ajaxcartpro_confirm .focus')[0]) != 'undefined') $$('.ajaxcartpro_confirm .focus')[0].focus();

    var ACPcountdown = $('ACPcountdown');
    if(typeof ACPcountdown != 'undefined' && AW_ACP.counterBegin>0)
    {
        ACPcountdown.innerHTML = AW_ACP.counterBegin;
        if (typeof __intId3 != 'undefined') clearInterval(__intId3);
        __intId3 = setInterval(
            function(){
                if ( parseInt(ACPcountdown.innerHTML) ){
                    ACPcountdown.innerHTML = parseInt(ACPcountdown.innerHTML)-1;
                }
                else
                { 
                    clearInterval(__intId3);
                    block.style.display = "none";
					$$('.ajaxcartpro_overlay')[0].style.display = 'none';
                    ACPcountdown.innerHTML = AW_ACP.counterBegin;
                }

            },
            1000
        );
    }
}
