
//base_url
var base_url = 'http://a1reproductions.com/';

(function($){
    $.fn.get_cart_amount = function(){
        $.ajax({
            url: '/cart/get_count_items',
            cache: false,
            dataType: "json",
            success: function(data){
                if(data.error==0 && data.amount>0)
                {
                    $("#hcart").css('opacity','1');
                    $("#hcart-items").html(data.amount+' Painting');
                    $("#hcart-total").html('($'+data.total+')');
                    $("#hcart-vcart").html('<a href="/order/view-cart.html?ref=view_cart">View Cart</a>');
                    $('#hcart-amount').val(data.amount);
                }
                else
                {
                    $("#hcart").css('opacity','0.6');
                    $("#hcart-items").html('Your Cart is Empty');
                    $("#hcart-total").html('');
                    $("#hcart-vcart").html('');
                    $('#hcart-amout').val('0');
                }
            }
        });
    };

    $.fn.add_cart_to_cart = function(data){
        if(typeof _gaq != 'undefined')
        {
            _gaq.push(['_trackPageview', '/order/add_to_cart.html']);
        }
        $.post(
            '/cart/add_to_cart',
            data,
            function(data){
                if(data.error==0)
                    {
                    $("body").append("<div class=\"cart-message\" style=\"display: none;\"><span><b>"+data.product_name+"</b> has been added to your cart.</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\""+base_url+"order/view-cart.html\"><div>Proceed to Checkout</div></a>  <a href=\"#\" class=\"close-cart-message\"><div>Continue Shopping</div></a></div>");
                        $(".cart-message").slideDown();
                        $('.close-cart-message').click(
                            function(){
                                $(".cart-message").slideUp('slow');
                        });
                        $(this).get_cart_amount();
                    }
            },
            'json');
    };
    
    $.fn.get_promo_type_code = function()
    {
        if($('input[name$="promo_code"]').val()!='')
        {
            var data = {'promo_code':$('input[name$="promo_code"]').val()};
            $.post(
                '/deals/get_promo_type_via_code',
                data,
                function(data){
                    if(data.error == 0)
                    {
                        $("#validate_code_result").removeClass().addClass('validate_code_success').html('Code Valid');
                        $("#user_code_verifed").val(1);
                        $("#promo_codeFailed").removeClass().addClass("hidden");
                        $(window.location).attr('href',base_url+'promo/'+data.result);
                    }
                    else
                    {
                        $("#validate_code_result").removeClass().addClass('validate_code_error').html('Code Invalid');
                        $("#user_code_verifed").val(0);
                    }    
                },
                'json'
            );
        }
        else
        {
            $("#validate_code_result").removeClass().addClass('validate_code_error').html('Code Invalid');
        }
    }
})(jQuery);
