/*
 * filename: parentgiving_shop.js
*/

$(document).ready(function(){

    // widget toggle content
    $('.wToogle').click(function(){
        $(this).parent().parent().next().slideToggle('fast');
        return false; // Prevent browser from visiting `#`
    });

    // category page tab hover effects
    $(".select_box_tabs_container div a").hover(
        function () {
        var cur_class = $(this).parent().attr("class");
        if(cur_class == "default")
        {
            $(this).parent().removeClass("default");
            $(this).parent().addClass("hover");
        }
        },
        function () {
        var cur_class = $(this).parent().attr("class");
        if(cur_class == "hover")
        {
            $(this).parent().removeClass("hover");
            $(this).parent().addClass("default");
        }
        }
    );

    // category page tab click effects
    $(".select_box_tabs_container div a").click(function(){

        var cur_class = $(this).attr("class");
        var cur_parent_class = $(this).parent().attr("class");

        if(cur_class == "shop_for" && (cur_parent_class == "default" || cur_parent_class == "hover"))
        {
            $("#category_products").removeClass("invisible");
            $(this).parent().removeClass(cur_parent_class);
            $("div.select_box_tabs_container div a.how_to_choose").parent().removeClass("active");
            $("#category_buyer_guides").addClass("invisible");
            $(this).parent().addClass("active");
            $("div.select_box_tabs_container div a.how_to_choose").parent().addClass("default");
            $(".sub_categories_desc_footer").removeClass("invisible");

            $(".select_box_top").toggle();
            $(".select_box_bottom").toggle();

            $("div.select_box div.category_pagination span").removeClass("invisible");
            $("div.select_container form.sort_form").removeClass("invisible");
            $("div.select_container div.buyerguide_select_container").addClass("invisible");
        }

        if(cur_class == "how_to_choose" && (cur_parent_class == "default" || cur_parent_class == "hover"))
        {
            $("#category_buyer_guides").removeClass("invisible");
            $(this).parent().removeClass(cur_parent_class);
            $("div.select_box_tabs_container div a.shop_for").parent().removeClass("active");
            $("#category_products").addClass("invisible");
            $(this).parent().addClass("active");
            $("div.select_box_tabs_container div a.shop_for").parent().addClass("default");
            $(".sub_categories_desc_footer").addClass("invisible");

            $(".select_box_top").toggle();
            $(".select_box_bottom").toggle();

            $("div.select_box div.category_pagination span").addClass("invisible");
            $("div.select_container form.sort_form").addClass("invisible");
            $("div.select_container div.buyerguide_select_container").removeClass("invisible");
        }

        return false; // Prevent browser from visiting `#`
    });

    // if url string comes through with buyer_guide hash
    // http://benalman.com/projects/jquery-hashchange-plugin/
    $(window).hashchange( function(){
        var hash = location.hash.substring(1); //Puts hash in variable, and removes the # character
        if(hash == 'buyer_guide' && $("div.select_box_tabs_container div a.how_to_choose").length != 0) {
            $("#category_buyer_guides").removeClass("invisible");
            $("div.select_box_tabs_container div a.how_to_choose").parent().removeClass();
            $("div.select_box_tabs_container div a.shop_for").parent().removeClass("active");
            $("#category_products").addClass("invisible");
            $("div.select_box_tabs_container div a.how_to_choose").parent().addClass("active");
            $("div.select_box_tabs_container div a.shop_for").parent().addClass("default");
            $(".sub_categories_desc_footer").addClass("invisible");

            $(".select_box_top").toggle();
            $(".select_box_bottom").toggle();

            $("div.select_box div.category_pagination span").addClass("invisible");
            $("div.select_container form.sort_form").addClass("invisible");
            $("div.select_container div.buyerguide_select_container").removeClass("invisible");

            // An offset to push the content down from the top.
            var offset = 150;

            // Our scroll target : the top position of the
            // section that has the id referenced by our href.
            var target = $("#buyer_guide_section").offset().top + offset;

            // The magic...smooth scrollin' goodness.
            $('html, body').animate({scrollTop:target}, 500);

            //prevent the page from jumping down to our section.
            //event.preventDefault();
        }
    });

    // Trigger the event (useful on page load).
    $(window).hashchange();

    // Category Buyer Change Event
    $('.buyerguide_select_container select').change(function()
    {
        $("#category_buyer_guides").fadeTo("fast", 0.33);
        
        //alert('Value change to ' + $(this).val());
        var $buyerguide = $(this).val().replace(/^\s+|\s+$/g, ''); // get buyerguide value

        $.post('//'+global_url+'/ajax_php/category_buyerguide_refresh.php',{'buyerguide':$buyerguide}, function(html){
            $("#category_buyer_guides").html(html).fadeTo("fast", 100);
        });

        $('.buyerguide_select_container select').val($buyerguide);
    });

    // apply gc code -> user pressed the apply button
    $(document).delegate('#gc_apply', 'click', function(){
        var $gc_code = $("#gc_apply_code").val().replace(/^\s+|\s+$/g, ''); // retrieve user gc code and trim

        // remove error message - default
        $("#gc_error").hide();

        if ($gc_code.length) // if gc code is empty
        {
            var $current_apply_html = $("#gc_apply_btn_content").html();
            $("#gc_apply").hide();
            $("#widget_gc_apply_processing").show();

            if ($gc_code.length == 8)
            {

                $.post('//'+global_url+'/ajax_php/gift_certificate_ajax.php',{'gc_code':$gc_code}, function(gc_balance){

                    if(gc_balance == "Gift Certificate Not Found.")
                    {
                        // gift certificate has no value
                        $("#gc_error").show();
                        $('#gc_apply_code').focus(); // focus on gc code input box
                        $("#gc_apply_btn_content").html($current_apply_html);
                    }
                    else
                    {
                        var $gc_balance = accounting.formatMoney(gc_balance.replace(/^\s+|\s+$/g, ''), {symbol : "", precision : 2, thousand : ""}); // trim string

                        // retrieve cart total
                        var $cart_total = accounting.formatMoney($("#cart_total").text(), {symbol : "", precision : 2, thousand : ""});

                        // calculate remaining gc balance
                        var $remaining_balance = ($gc_balance - $cart_total);

                        if ($remaining_balance > 0)
                        {
                            $remaining_balance = accounting.formatMoney($remaining_balance, {symbol : "", precision : 2, thousand : ""});
                        }
                        else
                        {
                            $remaining_balance = accounting.formatMoney(0, {symbol : "", precision : 2, thousand : ""});
                        }

                        /* Default Apply GC Settings */
                        $("#gc_apply_info_content").hide();
                        $("#gc_apply_btn_content").hide();
                        $("#gc_code").text($gc_code);
                        $("#widget_gc_balance").text($gc_balance);

                        if ($gc_balance > 0)
                        {
                            $(".gc_remaining_balance_info").show();
                            $("#widget_gc_post_checkout_balance").text($remaining_balance);
                        }
                        else
                        {
                            $(".gc_remaining_balance_info").hide();
                        }

                        $("#gc_remove_info_content").show();
                        $("#gc_apply_code").val('');
                        $("#gc_apply_btn_content").html($current_apply_html);

                        // calculate new cart total
                        var $amount_due = ($cart_total - $gc_balance);

                        if ($amount_due > 0)
                        {
                            $amount_due = accounting.formatMoney($amount_due, {symbol : "", precision : 2, thousand : ""});
                        }
                        else
                        {
                            $amount_due = accounting.formatMoney(0, {symbol : "", precision : 2, thousand : ""});
                            $("#pay_by_card").hide();
                            $("#pay_by_check").hide();
                            $("#gc_amount_due_container").addClass('bottomborder');
                            $("#gc_amount_due_container").show();
                        }

                        $("#gc_amount_due").text($amount_due);

                        $("#widget_order_total").text($cart_total);
                        $("#widget_amount_due").text($amount_due);

                        // cart defaults
                        if ($amount_due == 0)
                        {
                            $("#cart_gc_amount").text($cart_total);
                            $("#widget_gc_applied_balance").text($cart_total);
                            $("#widget_cc_order_info_note").hide();
                        }
                        else
                        {
                            $("#cart_gc_amount").text($gc_balance);
                            $("#widget_gc_applied_balance").text($gc_balance);
                            if ($gc_balance == 0)
                            {
                                $("#widget_cc_order_info_note").text("Sorry, but your gift certificate has a zero balance. Enter your credit card information below to place your order.");
                            }
                            else
                            {
                                $("#widget_cc_order_info_note").text("Enter credit card info below to place your order.");
                            }
                            $("#widget_cc_order_info_note").show();
                        }

                        $("#cart_amount_due").text($amount_due);
                        $("#cart_gc_amount_container").show();
                        $("#cart_amount_due_container").show();

                    }
                });
            }
            else
            {
                $("#gc_error").show();
                $('#gc_apply_code').focus(); // focus on gc code input box
                $("#gc_apply_btn_content").html($current_apply_html);
            }
        }
        return false; // Prevent browser from visiting `#`
    });

    // apply gc code -> user hit enter key
    $(document).delegate('#gc_apply_code', 'keyup', function(e){
        if (e.keyCode == 13) {
            $('#gc_apply').click();
        }
        else {
            $("#gc_error").hide(); // remove error message -> if gc code is changed
        }
    });

    // remove gc code -> user pressed the remove button
    $(document).delegate('#gc_remove', 'click', function(){

        var $response = confirm("Are you sure you would like to remove the Gift Certificate?");

        if ($response)
        {
            $.post('//'+global_url+'/ajax_php/gift_certificate_ajax.php',{'remove_gc_code':true}, function(gc_removal_msg){

                var $gc_removal_msg = gc_removal_msg.replace(/^\s+|\s+$/g, ''); // trim string

                if($gc_removal_msg == 'Successful')
                {
                    $("#gc_remove_info_content").hide();
                    $("#gc_amount_due_container").hide();
                    $("#gc_apply_info_content").show();
                    $("#gc_apply_btn_content").show();
                    $("#pay_by_check").show();
                    $("#pay_by_card").show();

                    // cart defaults
                    $("#cart_gc_amount").text('');
                    $("#cart_amount_due").text('');
                    $("#cart_gc_amount_container").hide();
                    $("#cart_amount_due_container").hide();
                }
            });
        }

        return false; // Prevent browser from visiting `#`

    });
    
});
