// For defaultfooter.mako floating footer load
function floatingfooter_window_load() {
    // Shows the discount code floating footer
    $("#floatingfooter").fadeIn(2000);
    $("#floatingfooter p a").click(function(event) {
       event.preventDefault();
       $("#floatingfooter").fadeOut(1000);
       $.post(PROCESSURL + 'hide_floating_footer');
    });
}

// For defaultfooter.mako floating footer discount code validation
function defaultfooter_document_ready() {
    $("#floatingfooter form").submit(function(event) {
        // stop form from submitting normally
        event.preventDefault();
        // get some values from elements on the page:
        var $form = $(this);
        // Send the data using post and put the results in a div
        $.post( PROCESSURL + 'validate_discount_code_json', $form.serialize(),
          function(data) {
              if (data.discount_code_status) {
                  alert('Your discount code has been accepted. Discounts and free products are displayed once you enter the checkout.');
                  $.post(PROCESSURL + 'hide_floating_footer');
                  $("#floatingfooter").hide();
              } else if (data.referral_code_status) {
                  alert('Your code has been accepted. When you complete your purchase, you will be entered into the monthly prize draw.');
                  $.post(PROCESSURL + 'hide_floating_footer');
                  $("#floatingfooter").hide();
              } else {
                  alert('Sorry, we do not recognise the code you gave us. Please check the code and try again.');
              }
          }, "json"
        );
    });
}

// Moves HTML content from one element to another
function move_content_in_container(id_from, id_to) {
    $(id_to).html($(id_from).html());
    $(id_from).html("");
}

// Show the pop-up shopping basket
function show_basket_popup(product_id) {
    var width = $("#inner_frame").width() + 20;
    if (width > $(window).width()) {
        width = $(window).width();
    }
    $.colorbox({href:"/norobot/popupbasket?add_product=" + product_id, fixed:true, maxHeight:$(window).height(), width:width});
}

// FOR product.mako
// Javascript initialisation for product page
function product_init() {
    // Override the normal add to basket action and show pop-up shopping basket window
    $("form.add_product_form").submit(function(event) {
        // stop form from submitting normally
        event.preventDefault();
        product_id_field = $(this).children("p.product_image").find("input[name=product_id]");
        show_basket_popup(product_id_field.val());
    });
}

// FOR popupbasket.mako
// Javascript initialisation for pop-up shopping basket
function popup_basket_init() {
    // Enable the buy now buttons for the lubes
    function enable_add_to_basket(the_path, product_id) {
        $(the_path).click(function() {
            // Add the specified product and reshow the basket window
            show_basket_popup(product_id);
        });
    }
    
    enable_add_to_basket("input#add_YSLBWT75.submit_button", "YSLBWT75");
    enable_add_to_basket("input#add_YSLBOL75.submit_button", "YSLBOL75");
    enable_add_to_basket("input#add_YSLBSM.submit_button", "YSLBSM");
    
    // Override the continue shopping button to just close the window
    $("#continue_shopping_button").click(function(event) {
        // stop form from submitting normally
        event.preventDefault();
        $.colorbox.close();
    });
}



