/**
 *  Ajout au panier par drag and drop
 *  @author Ny ando ANDRIAMANANJARA 
 */
jQuery(document).ready(function(){
	/*jQuery('a.product-image').draggable(
	  {
	  	cursor: 'crosshair',
	  	iframeFix: true,
	  	scroll: true,
	  	snap: true,
	  	refreshPositions: true,
	  	revert: 'invalid',
	  	revertDuration: 200,
	  	zIndex: 99999,
	  	helper: 'clone',
	  	start: function(event, ui) {
		        jQuery('#cart_sidebar').addClass('start-drag');
	      },
	    stop: function(event, ui) {
	  	  jQuery('#cart_sidebar').removeClass('start-drag');
	    }
	  }
	);*/
	
	/*jQuery('#cart_sidebar').droppable(
	  {
	      accept: 'a.product-image',
	      hoverClass: 'cart-hover',
	      tolerance: 'intersect',
	      over: function(event, ui) { jQuery(this).removeClass('start-drag'); },
	      drop: function(event, ui) { 
	          ui.draggable.parent().find("button").click(); 
	      }            
	  }
	);*/
});
/* fin ando */

/* 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();
	});

	$j('form[id*=product_addtocart]').each(function() {
		var $t = $j(this);

		var splitting = $t.attr('action').toString();

		var pid = splitting.split('/').reverse();

		var $b = $t.children('button');
		var newB = $b.clone();
//		newB.attr('onclick', '');
		newB.removeAttr('onclick');
		newB.attr('pid', pid[1]);
		newB.attr('link',splitting);
		newB.attr('addType', '1.q');
		newB.insertBefore($b);
		$b.remove();
	});
}

function updateAddToCartWithOptionsLinks(){
	
	$j('button[onClick*=?options=cart]').each(function(){
		if (window.location.pathname.split('/').reverse()[1] != 'cart'){
			var $t = $j(this);
	
			// modifié par ando
			var href =  $t.parent().find("ul.add-to-links").children().children().attr("href");
			if (typeof href == "undefined" || href == null) {
				href = $j(this).next("input[class='use_for_add_to_cart_ajax']").val();
			}
			var id = 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;
			
			/* added by ando */
			if (parseInt($j('fieldset.no-display input[type=hidden]').attr('value')) > 0) {
				_pid = parseInt($j('fieldset.no-display input[type=hidden]').attr('value'));
			} else if (parseInt($j('input[name=product]').attr('value')) > 0) {
				_pid = parseInt($j('input[name=product]').attr('value'));
			} else if (parseInt($j('input[value]:eq(i)').attr('value')) > 0) {
				_pid = parseInt($j('input[value]:eq(i)').attr('value'));
			} else {
				_pid = 0;
			}
			
			// commented by ando
//			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.removeAttr('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(){
			    	/**
			    	 * overture de la fenêtre popup de confirmation
			    	 * @author Ny ando ANDRIAMANANJARA
			    	 */
		            var okFct = function(){
		            	jQuery(this).dialog("close");
//		            	Compare($t.attr("link")); // bug, car $t n'a pas d'attibut link
		            	Compare(newB.attr("link")); // corrigé par ando		                
		            };
		            var deleteOneItemFromComparer = initConfirmDialog("Confirmation", okFct, jQuery("#delete_one_comparer_item"));
			    	deleteOneItemFromComparer.dialog('open');			    	
			    });
			    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(){
			    	/**
			    	 * overture de la fenêtre popup de confirmation
			    	 * @author Ny ando ANDRIAMANANJARA <solofoniando@gmail.com>
			    	 */
		            var okFctAll = function(){
		            	jQuery(this).dialog("close");
//				    	Compare($t.attr("link")); bug, car $t n'a pas d'attibut link
				    	Compare(newB.attr("link")); // corrigé par ando		                
		            };
		            var deleteAllItemFromComparer = initConfirmDialog("Confirmation", okFctAll, jQuery("#delete_all_comparer_item"));
		            deleteAllItemFromComparer.dialog('open');
		            /* fin ando */
			    });				
			    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.removeAttr('onclick');
		    newB.attr('link', link);
		    
		    newB.click(function(){
		    	/**
		    	 * overture de la fenêtre popup de confirmation
		    	 * @author Ny ando ANDRIAMANANJARA <solofoniando@gmail.com>
		    	 */
		    	var okFct = function(){
		    		jQuery(this).dialog("close");
		    		WishlistRemove(link);
		    	};
		    	var deleteItemFromWishlist = initConfirmDialog("Confirmation", okFct, jQuery("#delete-wishlist-item"));
		    	deleteItemFromWishlist.dialog('open');
		    	/* fin ando */
		    });			
		    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);
	    });
	}
}

