/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/LICENSE-M1.txt
 *
 * @category   AW
 * @package    AW_Ajaxcartpro
 * @copyright  Copyright (c) 2009-2010 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/LICENSE-M1.txt
 */

window.intPrevious = setInterval(function(){
	if(typeof AW_ACP != 'undefined' && document.body){
		if(typeof aw_cartDivClass == 'undefined'){
			 aw_cartDivClass =
				AW_ACP.theme == 'blank' ?
					'.block-cart' :
					'.mini-cart';

			if(!$$(aw_cartDivClass).length || !$$(aw_cartDivClass)[0].tagName){
				 aw_cartDivClass =  '.block-cart'
			}
		}
		if(typeof aw_topLinkCartClass == 'undefined'){
			 aw_topLinkCartClass = '.top-link-cart';
		}
		if(typeof aw_addToCartButtonClass == 'undefined'){
			 aw_addToCartButtonClass = '.form-button';
		}
		if(typeof aw_bigCartClass == 'undefined'){
            if (typeof($$('.layout-1column')[0]) != 'undefined')
                aw_bigCartClass = '.layout-1column';
            else if (typeof($$('.col-main')[0]) != 'undefined')
				aw_bigCartClass = '.col-main';
            else
                aw_bigCartClass = '.cart';
		}
        if(typeof aw_wishlistClass == 'undefined'){
			 if (typeof($$('.my-wishlist')[0]) != 'undefined')
                aw_wishlistClass = '.my-wishlist';
             else
                aw_wishlistClass = '.padder';
		}

        if(typeof aw_topWishlistLinkCartClass == 'undefined'){
            aw_topWishlistLinkCartClass = '.top-link-wishlist';
        }

		if (window.location.toString().search('/product_compare/') != -1){
			win = window.opener;
		}
		else{
			win = window;
		}
		clearInterval(intPrevious)
	}
}, 500);

function ajaxcartprodelete(url){
	showProgressAnimation();
	url = getCommonUrl(url)


    new Ajax.Request(url, {
          onSuccess: function(resp){
				try{
					if (typeof(resp.responseText) == 'string') eval('resp = ' + resp.responseText);
				}catch(e){

					return;
				}
				hideProgressAnimation();
                __onACPRender()
                updateCartView(resp, '');
			}
        });


}

function updateCartView(resp){
    if (AW_ACP.isCartPage) return updateBigCartView(resp);

	var __cartObj = $$(aw_cartDivClass)[0];

	if(__cartObj)
    {
        if (typeof(__cartObj.length) == 'number') __cartObj = __cartObj[0];
        var oldHeight = __cartObj.offsetHeight;

        var tmpDiv = win.document.createElement('div');
        tmpDiv.innerHTML = resp.cart;
        $(tmpDiv).cleanWhitespace();

        var tmpParent = __cartObj.parentNode;
        tmpParent.replaceChild($(tmpDiv).select(aw_cartDivClass)[0], __cartObj);
        cartScrollSize();

        /* Details popup support */

        var __cartObj = $$(aw_cartDivClass)[0];
        var newHeight = __cartObj.offsetHeight;

        addEffectACP(__cartObj, aw_ajaxcartpro_cartanim);
        truncateOptions();
    }
    updateDeleteLinks();
	updateTopLinks(resp);
    
    // REDSTAGE CUSTOM START (Ryan)
    // To check all properties of resp object uncomment below
    
    /*for(var propertyName in resp){
        alert(propertyName + ": \n" + resp[propertyName]);
    }*/
    var cart_qty;
    var cart_qtyspace;
    if (resp.cart_qty == null){
        cart_qty = resp.links.substr(9);
        cart_qty_space = cart_qty.indexOf("i");
        cart_qty = cart_qty.substr(0, (cart_qty_space - 1))
    } else {
        cart_qty = resp.cart_qty;
    }
    if (cart_qty == ""){
        cart_qty = 0;
        jQuery('#slideTopPk-cart').css('display', 'none');
        jQuery('#topCartEmpty').css('display', 'block');
    }
    if (cart_qty == 1){
        jQuery('#topCartEmpty').css('display', 'none');
        jQuery('#slideTopPk-cart').css('display', 'block');
    }
    jQuery('#rsnCartCount').html('(' + cart_qty + ') ');
    
    var cart_subtotal;
    var cart_subtotalspace_start = resp.cart.indexOf("topCartSubtotal") + 1; 
    cart_subtotal = resp.cart.substr(cart_subtotalspace_start);
    cart_subtotalspace_start = cart_subtotal.indexOf("price") + 7;
    cart_subtotal = cart_subtotal.substr(cart_subtotalspace_start);
    //alert(cart_subtotal);
    var cart_subtotalspace_end = cart_subtotal.indexOf("<");
    cart_subtotal = cart_subtotal.substr(0, cart_subtotalspace_end);
    if (cart_qty > 0) jQuery('#topCartSubtotal').html(cart_subtotal);
    
    // update jScrollPane
    //cartScrollSize();
    
    // REDSTAGE CUSTOM END
}

function updateWishlist(resp)
{
	var wishlistObj = $$(aw_wishlistClass)[0];

    if(wishlistObj){
        var tmpDiv = win.document.createElement('div');
        tmpDiv.innerHTML = resp.wishlist;

        var tmpParent = wishlistObj.parentNode;
        tmpParent.replaceChild(tmpDiv.firstChild, wishlistObj);
    }
}
