﻿$.fn.delay = function(time, callback)
{
    return this.animate({ opacity: '+=0' }, time, callback);
}

// Check that the page has loaded
$(document).ready(function() {

    // Left nav dropdown (only works when dropdown present)
    $('#leftNav LI A').parent('LI').find('UL').prev('A').click(function() {
        var thisLI = $(this).parent();
        // If menu item not already selected, open
        if (!thisLI.hasClass('selected')) {

            $(this).next().slideDown().parent().addClass('selected');

        } else {
            $(this).next().slideUp(function() {
                thisLI.removeClass('selected');
            });

        }

        return false;

    });

    // BIG TARGET HOMEPAGE FEATURED
    $(".homepage .featuredProduct A").bigTarget({
        clickZone: '.featuredProduct' // jQuery parent selector
    });
    $('.homepage .featuredProduct').css({ 'cursor': 'pointer' });

    // BIG TARGET LIST ITEM
    $(".sidebarList .listItem:not(.basket) A").bigTarget({
        clickZone: '.listItem' // jQuery parent selector
    });
    $('.sidebarList .listItem:not(.basket)').css({ 'cursor': 'pointer' });


    // BIG TARGET LIST BROWSE ITEM
    $(".itemBrowse .item A").bigTarget({
        clickZone: '.item' // jQuery parent selector
    });
    $('.itemBrowse .item').css({ 'cursor': 'pointer' });


    // LOGIN/REGISTER
    $('.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 -36px' });

        }

        return false;

    });

    // PRODUCT GALLERY

    // Add lightbox
    $('A.lightbox').lightBox();

    $('.productGallery .mainImgContainer A').hide();
    $('.productGallery .mainImgContainer A:eq(0)').show();

    $('.productGallery .thumbnail').click(function() {


        // Get which thumnail clicked
        var href = $(this).attr('href');

        // Show correct image
        $('.productGallery .mainImgContainer A').hide();
        $('.productGallery .mainImgContainer A[href="' + href + '"]').show();

        return false

    });

    // ACOUNT DETAILS SLIDE UP/DOWN
    $('.account .top').click(function() {

        // If not already open, then open
        if (!$(this).parents('.account').hasClass('open')) {

            $(this).parents('.account').find('.middle').slideDown(function() {

                $(this).parents('.account').addClass('open');

            });

        } else {

            $(this).parents('.account').find('.middle').slideUp(function() {

                $(this).parents('.account').removeClass('open');

            });

        }


    });


    // ORDER TRACKING STRIPING
    $('.orderTracking TR:even').addClass('odd');


    // HOMEPAGE SLIDESHOW
    $('.featuredProductSlideshowContainer').delay(5000).fadeIn(function() {

        slideshow();

    });


    // TELL A FRIEND SLIDE UP/DOWN
    $('.tellAFriend .top').click(function() {

        // If not already open, then open
        if (!$(this).parents('.tellAFriend').hasClass('open')) {

            $(this).parents('.tellAFriend').find('.tellAFriendContainer2').slideDown(function() {

                $(this).parents('.tellAFriend').addClass('open');

            });

        } else {

            $(this).parents('.tellAFriend').find('.tellAFriendContainer2').slideUp(function() {

                $(this).parents('.tellAFriend').removeClass('open');

            });

        }


    });

    // Focus/blur textboxes
    setFocusBlur($('INPUT,TEXTAREA'));

});

// Slideshow
function slideshow() {

    // Slide in/out container
    $('.featuredProductSlideshowContainer').fadeOut(1000, function() {
        
        // Move first image to end of stack
        $('.featuredProductSlideshow A:first').appendTo('.featuredProductSlideshowContainer');

        $('.featuredProductSlideshowContainer').fadeIn(1000, function() {
        
            slideshow();
        
        }).delay(3000);

    });

};

// 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'));
                }
            });
        }
    });
};

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');
        }
    }
}
