function checkout_remove(){
    global.thickboxDone(jQuery("#shoppingCartLink").html());
}

jQuery(document).ready(function() {
    addToCartItems.initialize();
});

var addToCartItems = {
    htmlErrorCode : null,

    initialize : function() {
        this.bindEvents();
        this.htmlErrorCode = '<div id="errorSectionDiv" style="border-bottom: 1px solid black; margin-top: 20px; margin-left: 20px;">';
        this.htmlErrorCode += '<span class="errorMsgHead">Sorry, one or more fields are incomplete or incorrect:</span>';
        this.htmlErrorCode += '<ul>';
    }

    , bindEvents : function() {
        var parentThis = this;

        jQuery("#buyButton").click(function() {
            var validated = parentThis.validate();
            if (validated){
                parentThis.addToCart();
            }else{
                return;
            }
        });

        jQuery("#wishlistButton").click(function() {
            var validated = parentThis.validate();
            if (validated){
            	wishlistConfirmation.addToWishlist({"skuId": $("#skuId").val()}, "/actions/wishlist/addWishlistSku.action");
            }else{
                return;
            }
        });
    }

    , validate : function(){
        if (jQuery("#skuId").val() == ""){
            var html = this.htmlErrorCode;
            html += '<li class="errorMsg">You must fill in field(s) marked with !</li>';
            html += '</ul>';
            html += '</div>';
            jQuery("#skuId").parent().find("tt").html("!");
            jQuery("#errors").html(html);
            jQuery("#errors").show();
            return false;
        }else{
            jQuery("#errors").hide();
            return true;
        }
    }

    , addToCart : function() {
        var postdata = {"skuId": jQuery("#skuId").val(), "giftSender": jQuery("#giftSender").val(), "giftRecipient": jQuery("#giftRecipient").val()};
        jQuery.ajax({ type:"GET", url:"/actions/giftCertificatePageAction!addToShoppingCart.action",
        dataType:"json", data:postdata, cache:false, success:function( data ){
            var cartMessage = data.message;
            var numberCartItems = data.responseData.numberCartItems;
            var responseCode = data.result;
            tb_show(null, "#TB_inline?height=140&width=282", null);
            var htmlMessage = '<div id="cartMessageDiv">' + cartMessage + '</div>';
            if ("error" == responseCode){
                htmlMessage += '<div id="cartMessageButtonDiv"><input type="image" onclick="tb_remove()" src="' + jQuery("#okButton").html() + '" value="ok" id="okButton"/></div>';
            }else{
                htmlMessage += '<div id="cartMessageButtonDiv">';
                htmlMessage += '<input type="image" onclick="tb_remove()" src="' + jQuery("#continueShoppingButton").html() + '" value="continueshopping" id="continueShoppingButton"/>';
                htmlMessage += '&nbsp;&nbsp;<input type="image" onclick="checkout_remove()" src="' + jQuery("#checkoutButtonImage").html() + '" value="checkout" id="checkoutButton"/>';
                htmlMessage += '</div>';
            }
            jQuery("#TB_ajaxContent").html(htmlMessage);
            jQuery("#miniCartQuantity .itemCount").html("(" + numberCartItems + ")");
        }});
    }
}