var $j = jQuery.noConflict();

$j(document).ready(function(){
	
	nohide = false;
    // enable transparent overlay on FF/Linux
    $j.blockUI.defaults.applyPlatformOpacityRules = false;
    //clear the default style for message
    $j.blockUI.defaults.css = {};
	
	if (module_enabled()){
		if(!inComparePopUp()){
			update();
			//initDragNDrop();
		}else{
			comparePopUpLinks();
		}
	}
	
	var eapUrl = gup( 'eapUrl' );
	if (eapUrl != ''){
		new $j.ajax({
		      url: "http://"+eapUrl+"eap/1/",
		      type: "GET",
		      dataType: "html",
		      success: function(msg){
				callbacks(msg, 5);
			}
			});
	}
});

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


		    $t.live('click',(function() {
		    	window.opener.addProduct($t);
				return false;
		    }));
		});
	}
	
	if (wishlist_add_enabled()){
		$j('a[onClick*=wishlist/index/add]').each(function(){
			
		    var $t = $j(this);
		    
		    var splitting = $t.attr('onClick').toString().split("'");
		    
		    var newB = $t.clone();
		    newB.removeAttr("href");
		    newB.addClass("addLink");
		    newB.attr('link',splitting[1]);
		    newB.attr('onclick',  '');
		    newB.insertBefore($t);
		    $t.remove();
		    

		    $t.live('click',(function() {
		    	window.opener.Wishlist($t.attr("link"));
		    	return false;
			}));
		});
	}
	
	if (compare_delete_enabled()){
		$j('a[onClick*=product_compare/remove/]').each(function(){
			
		    var $t = $j(this);
		    
		    var splitting = $t.attr('onClick').toString().split("'");
		    
		    var newB = $t.clone();
		    
		    newB.removeAttr("href");
		    newB.addClass("addLink");
		    newB.attr('link',  splitting[1]);
		    newB.attr('onclick', '');
		    newB.live('click',(function() {
		    	window.opener.Compare($t.attr("link"));
		    	refreshComparePopUp();
				return false;
		    }));
		    newB.insertBefore($t);
		    $t.remove();

		});
	}
}

function update(){
	
	$j('div.block.block-cart').attr('id', 'cart_sidebar');
	$j('div.block.block-wishlist').attr('id', 'wishlist_sidebar');
	$j('div.block.block-compare').attr('id', 'compare_sidebar');

	if(cart_add_enabled()){
		updateAddToCartLinks();
		
		if(options_enabled()){
			updateAddToCartWithOptionsLinks();
		}
		updateAddToCartProductPageLinks();
	}

	if(compare_add_enabled()){
		updateCompareLinks();
	}

	
	if(wishlist_add_enabled()){
		updateWishlistLinks();
	}
	
	$j('button[addType=2]').live('click',(function(){
		if(submitFormWithOptions(false) != 1){
			showAll();
			showPopUpLoading();
		
			cart_opacity_less();
		}
		return false;
    }));

	$j('button[addType=1]').live('click',(function(){
		addProduct($j(this), false);
		return false;
    }));
	$j('button[addType=1.5]').live('click',(function(){
		addProduct($j(this), true);
		return false;
    }));
		
	$j('button[addType=1.q]').live('click',(function(){
		addProduct($j(this), false, $j(this).prevAll('#qty').attr('value'));
		return false;
	}));
				
	$j("#ajax_continue_shopping").live('click',(function(){
		hideAll();
		return false;
	}));
	$j("#ajax_checkout").live('click',(function(){
		window.setLocation($j("#ajax_checkout").attr("dest"));
		return false;
	}));
	
	refreshWishlistLinks();
	refreshCompareLinks();
}


function refreshWishList(msg){
	reponse = msg.replace('<div class="block block-wishlist">', '');
	var sidebar = $j("#wishlist_sidebar");
	sidebar.html(reponse);
	wishlist_opacity_more();
}

function refreshCompare(msg){
	reponse = msg.replace('<div class="block block-compare">', '');
	var sidebar = $j("#compare_sidebar");
	sidebar.html(reponse);
	compare_opacity_more();
}

function refreshComparePopUp(){
	window.location = $j("#easyajaxproduct_linkurl").attr("value")+"catalog/product_compare/index/";
}


function addProduct(x, options, qty_){
	showAll();
	showPopUpLoading();
	
	cart_opacity_less();
	
	if(options){
		var link = $j("#easyajaxproduct_linkurl").attr("value")+"checkout/cart/add/options/1/product/"+x.attr("pid")+"/";
	}else{
	 	var link = x.attr("link");
		var qty = $j("#qty").attr("value");
		if (!IsNumeric(qty)){
			qty = 1;}
	}
	
	//related products
	var related = '';
	$j('input[id^=related-checkbox]:checked').each(function(){
		related = related+$j(this).attr('value')+',';
	});

	if(qty_ != undefined)
		qty = qty_;
	
	new $j.ajax({
      url: link+"qty/"+qty+"/eap/1/related_products/"+related,
      type: "GET",
      dataType: "html",
      success: function(msg){
         callbacks(msg, 1);
      }
   });
}

function addWishlistProduct(x){
	showAll();
	showPopUpLoading();

	cart_opacity_less();
	wishlist_opacity_less();

	new $j.ajax({
      url: x+"eap/1/",
      type: "GET",
      dataType: "html",
      success: function(msg){
         callbacks(msg, 6);
      }
   });
}

function submitFormWithOptions(fromwishlist){
    var productAddToCartForm = new VarienForm('product_addtocart_form');
        if (productAddToCartForm.validator.validate()) {
        	hidePopUpOptions();
        	showPopUpLoading();
        	
        	cart_opacity_less();
        	var serialized = productAddToCartForm.form.serialize();	
        	var ncallback = 1;
        	if (fromwishlist == 1){
        		ncallback = 6;
        	}
    		var qty = $j("#qty").value;
    		if (!IsNumeric(qty)){
    			qty = 1;}
        	new $j.ajax({
        	      url: $j("#easyajaxproduct_linkurl").attr("value")+"checkout/cart/add/qty/"+qty+"/params/1/eap/1/fromwishlist/"+fromwishlist+"/?"+serialized,
        	      type: "GET",
        	      dataType: "html",
        	      success: function(msg){
        	         callbacks(msg, ncallback);
        	      }
        	   });
        }else{
		return 1;
	}
}

function Wishlist(x){
	showAll();
	showPopUpLoading();
	
	wishlist_opacity_less();

	new $j.ajax({
      url: x+"eap/1/",
      type: "GET",
      dataType: "html",
      success: function(msg){
        callbacks(msg, 5);
     }
   });
}

function WishlistRemove(url){
	showAll();
	showPopUpLoading();
	
	wishlist_opacity_less();

	new $j.ajax({
      url: url+"eap/1/",
      type: "GET",
      dataType: "html",
      success: function(msg){
        callbacks(msg, 7);
     }
   });
}

function Compare(url){
	showAll();
	showPopUpLoading();
	
	compare_opacity_less();

	new $j.ajax({
      url: url+"eap/1/",
      type: "GET",
      dataType: "html",
      success: function(msg){
        callbacks(msg, 8);
     }
   });
}

