var cart_widget;

$().ready(function(){
    cart_widget = new Cart_Widget();
});

function Cart_Widget(){
    var hideIn;
    var scrollTime;
    var ph;
    
    InitiateWidget();
    
    //Public Methods
    this.addToCart = AddToCart;
    this.close = Close;
    
    //private methods	
    function InitiateWidget(){
        hideIn = 15000;
        scrollTime = 1000;
        ph = $('#cw_ph');
        if (ph.length != 1) {
            ph = '<div id="cw_ph"></div>';
            ph = $(ph);
            $('#wrapper').append(ph);
        }
        ph.css('width', '348');
    }
    
    function AddToCart(resp){
                //insert new content in placeholder
                ph.html(resp);
                //updating image and cart totals
                var miniCartCookie = readCookie("WCX_MINICART");
				var totalItems = 0;
				var totalAmount = "$0.00";
				if (miniCartCookie != null)
				{
					var miniCartTotalsArray = miniCartCookie.split('|');
					totalItems = miniCartTotalsArray[1];
					totalAmount = miniCartTotalsArray[0];
				}
                ph.find('#addToCartOverlay_totals').html("<li><b>"+totalItems+" items in cart</b></li><li>Cart Subtotal: $"+totalAmount+"</li>");
				ScrollToTop();
                DisplayWidget();
        //stop browser defaults
        return false;
    }
    
    function DisplayWidget(){
        ph.show();
        if($().bgiframe){ph.bgiframe();}
        ph.find('.close').click(function(){
            cart_widget.close();
        });
        setTimeout(function(){
            Close();
        }, hideIn);
    }
    
    function ScrollToTop(){
        $('html,body').animate({
            scrollTop: 0
        }, scrollTime);
    }
    
    function Close(){
        ph.hide();
        //stop browser defaults
        return false;
    }
}
