﻿
// Check that the page has loaded
$(document).ready(function() {

    // LEFT NAV DROP DOWNS
    $('#leftNav LI A').click(function() {
        // Work out if there are sub-cats and return true if there are not
        if (!$(this).next().is('div')) {
            // No subcats so go to category
            return true;
        }

        // Reset active
        //$('#leftNav LI A').removeClass('selected');

        // If not already open, open and add selected class
        if (!$(this).parent().is('.open')) {

            $(this).parent().find('.subCatList').slideDown();
            $(this).parent('LI').addClass('open');


            // If open, close
        } else {

            $(this).parent('LI').find('.subCatList').slideUp(function() {
                $(this).parent('LI').removeClass('open');
            });

        }
        return false;

    });

    // Setup print button(s)
    $('.print').click(function() {
        window.print();
    });

    // BIG TARGET FEATURED PRODUCTS
    $(".featuredProducts .featuredProduct .learnMore").bigTarget({
        clickZone: '.featuredProduct' // jQuery parent selector
    });
    $('.featuredProduct').css({ 'cursor': 'pointer' });

    // PRODUCT GALLERY

    // Add lightbox
    $('A.lightbox').lightBox();

    $('.productGallery .thumbnail A').click(function() {

        // Get new src of image
        var src = $(this).attr('href');

        // Change src of main image
        $(this).parents('.productGallery').find('.mainImg').attr('src', src);

        // Get new src of lightbox

        var srcLighbox = src.replace("-img2", "-img1");

        // Change src of lightbox image
        $(this).parents('.productGallery').find('.lightbox').attr('href', srcLighbox);

        return false

    });

    // LOGIN/REGISTER
    $('.loginRegister .loginRegisterTabs A').click(function() {

        // login clicked
        if ($(this).hasClass('login')) {

            // Close register form, show login form
            $('#registerForm').hide();
            $('#loginForm').show();

            // Change background
            $(this).parent('.loginRegisterTabs').css({ backgroundPosition: '0 0' });

            // register clicked
        } else if ($(this).hasClass('register')) {

            // Close register form, show login form
            $('#loginForm').hide();
            $('#registerForm').show();

            // Change background
            $(this).parent('.loginRegisterTabs').css({ backgroundPosition: '0 -39px' });

        }

        return false;

    });

    setFocusBlur($('input'));

});

function pageLoad() {
    // Hide added to basket after a few seconds
    $('.added').animate({ opacity: 1.0 }, 800).fadeOut(function() {
        $(this).replaceWith("<A class=\"viewBasket\" href=\"basket.aspx\">View Basket</a>").show('fast');
    });

}

function resetInputs(){
    $('input[type=text]').css("background-color", "#f1f6ff");
    $('input[type=password]').css("background-color", "#f1f6ff");
}

function clear_checkboxes(myID) {
    var frm = document.forms[0];
    for (i = 0; i < frm.length; i++) {
        if (frm.elements[i].id != myID) {
            frm.elements[i].checked = false;
            $('input').next().removeClass('selected');
        }
    }
}

// Focus/blur textboxes
function setFocusBlur(inputs) {

    // Function to set focus-blur on textboxes
    inputs.each(function() {

        // For password boxes remove the background on focus and restore it on blur if its empty
        //        if ($(this).attr('type') === 'password') {

        //            $(this).focus(function() {
        //                if ($(this).val() === '') {
        //                    $(this).data('background', $(this).css('background-image'));
        //                    $(this).css('background', '#ffffff');
        //                }
        //            });
        //            $(this).blur(function() {
        //                if ($(this).val() === '') {
        //                    $(this).css('background-image', $(this).data('background'));
        //                }
        //            });
        //        }
        //else 
        if ($(this).attr('type') === 'text' || $(this).is('TEXTAREA')) {

            // Check that this isn't prefilled from the querystring
            if (document.location.search.indexOf($(this).val()) > 0) {
                return;
            }

            // For normal
            $(this).data('original', $(this).val());

            $(this).focus(function() {
                if ($(this).val() === $(this).data('original')) {
                    $(this).val('');
                }
            });

            $(this).blur(function() {
                if ($(this).val() === '') {
                    $(this).val($(this).data('original'));
                }
            });
        }
    });
};