/* fonctions de mise a jour des liens */

function updateAddToCartLinks(){
	
	$j('button[onClick*=cart/add]').each(function() {
	    var $t = $j(this);
	    var splitting = $t.attr('onClick').toString().split("'");
	    
	    var pid = splitting[1].split('/').reverse();
	    
	    var newB = $t.clone();
	    newB.attr('onclick', '');
	    newB.attr('pid', pid[1]);
	    newB.attr('link',splitting[1]);
	    newB.attr('addType', 1);
	    newB.insertBefore($t);
	    $t.remove();
	    
	});
}

function updateAddToCartWithOptionsLinks(){
	
	$j('button[onClick*=?options=cart]').each(function(){
		if (window.location.pathname.split('/').reverse()[1] != 'cart'){
			var $t = $j(this);
	
			var id = $t.next().children().children().attr("href").split('/').reverse()[1];
			
		    var newB = $t.clone();
		    newB.attr('onclick', '');
		    newB.attr('pid',  id);
		    newB.attr('addType', 1.5);
		    newB.insertBefore($t);
		    $t.remove();
		    
		}
	});
}

function updateAddToCartProductPageLinks(){

	$j('button[onClick=productAddToCartForm.submit()]').each(function(){
		var $t = $j(this);
		if( $j("#product-options-wrapper").length > 0){
			if(options_enabled()){
				var _pid = $j('fieldset.no-display input[type=hidden],input[name=product],input[value]').attr('value');
				
			    var newB = $t.clone();
			    newB.attr('onclick', '');
			    newB.attr('pid',  _pid);
			    newB.attr('addType', 2);
			    newB.insertBefore($t);
			    $t.remove();
			    
			}
		}else{
			var _pid = $j('fieldset.no-display input[type=hidden],input[name=product],input[value]').attr('value');
			var i = 1;
			while(!IsNumeric(_pid)){
				_pid = $j('fieldset.no-display input[type=hidden],input[name=product],input[value]:eq(i)').attr('value');}
			
			
		    var newB = $t.clone();
		    newB.attr('pid', _pid);
		    newB.attr('onclick', '');
		    newB.attr('addType', 1);
		    newB.attr('link', $j("#easyajaxproduct_linkurl").attr("value")+"checkout/cart/add/product/"+_pid+"/");
		    newB.insertBefore($t);
		    $t.remove();
		    
 
		}
	});
}

function updateCompareLinks(){
	$j('a[href*=catalog/product_compare/add]').each(function(){
	    var $t = $j(this);
	    var link = $t.attr("href");
	    
	    var newB = $t.clone();		    
	    newB.removeAttr("href");
	    newB.addClass("addLink");
	    newB.attr('link', link);
	    newB.attr('onclick', '');

	    newB.click(function(){
	    	Compare(link);
	    });
	    newB.insertBefore($t);
	    $t.remove();

	});
}
function updateWishlistLinks(){
	$j('a[href*=wishlist/index/add]').each(function(){
	    var $t = $j(this);
	    var link = $t.attr("href");
	    
		var newB = $t.clone();
		newB.removeAttr("href");
	    newB.addClass("addLink");
	    newB.attr('link', link);
	    
	    newB.click(function(){
	    	Wishlist(link);
	    });
	    newB.insertBefore($t);
	    $t.remove();
	    

	});
}

function refreshCompareLinks(){
	if(compare_delete_enabled() || compare_add_to_cart_enabled()){
		$j('button[onclick*=popWin]').each(function(){
		    var $t = $j(this);
		    var link = $t.attr("onClick");
		    res = link.toString().replace(/\/\'/gi,"/eap/1/'");
		    
			var newB = $t.clone();
			newB.attr('onClick', res);
		    newB.insertBefore($t);
		    $t.remove();
		    

		});
		
		if(compare_delete_enabled()){
			$j('a[href*=catalog/product_compare/remove]').each(function(){
			    var $t = $j(this);
			    link = $t.attr("href");
			    
				var newB = $t.clone();
							    
			    newB.removeAttr("href");
			    newB.addClass("addLink");
			    
			    newB.children("img").removeAttr("onclick");
			    
			    newB.attr('link', link);
			    newB.attr('onclick', '');
			    
			    newB.click(function(){
			    	Compare(link);
			    });
			    newB.insertBefore($t);
			    $t.remove();

			});
		}
		if(compare_clear_all_enabled()){
			$j('a[href*=catalog/product_compare/clear]').each(function(){
			    var $t = $j(this);
			    link = $t.attr("href");
			    
				var newB = $t.clone();
			    newB.removeAttr("href");
			    newB.addClass("addLink");
		    
			    newB.attr('link',  link);
			    newB.attr('onclick', '');

			    newB.click(function(){
			    	Compare(link);
			    });				
			    newB.insertBefore($t);
			    $t.remove();

			});
		}
	}
}

function refreshWishlistLinks(){
	if(wishlist_delete_enabled()){
		$j('#wishlist_sidebar a[href*=wishlist/index/remove]').each(function(){
		    var $t = $j(this);
		    var link = $t.attr("href");
		    
			var newB = $t.clone();
		    newB.removeAttr("href");
		    newB.addClass("addLink");
		    newB.attr('onclick', "");
		    newB.attr('link', link);
		    
		    newB.click(function(){
		    	WishlistRemove(link);
		    });			
		    newB.insertBefore($t);
		    $t.remove();
		    

		});
	}
	
	if(wishlist_add_to_cart_enabled()){
		$j('#wishlist_sidebar a[href*=wishlist/index/cart]').each(function(){
		    var $t = $j(this);
		    var link = $t.attr("href");
		    
			var newB = $t.clone();
			newB.removeAttr("href");
		    newB.addClass("addLink");
		    newB.attr('link', link);
		    newB.attr('addType', 3);
		    newB.insertBefore($t);
		    $t.remove();

		});
		$j('a[addType=3]').click(function(){
	    	addWishlistProduct(link);
	    });
	}
}

