﻿
// Check that the page has loaded
$(document).ready(function() {

    // categoriesNav nav dropdown (only works when dropdown present)
    $('.categoriesNav LI A').parent('LI').find('UL').parents('LI').find('A:first').click(function() {

        // If menu item not already selected, open
        if (!$(this).parent('LI').hasClass('selected')) {

            $(this).parent('LI').addClass('selected').find('UL').slideDown();

        } else {

            $(this).parent('LI').find('UL').slideUp(function() {
                $(this).parent('LI').removeClass('selected');
            });

        }

        return false;

    });

    // GALLERY

    // Add lightbox
    $('A.lightbox').lightBox();

    // Functionality
    $('.gallery .thumbnail').click(function() {

        // Get which thumnail clicked
        var href = $(this).attr('href');

        // Move correct image to top of stack
        $('.gallery .mainImgContainer A IMG').parents('A').hide();
        $('.gallery .mainImgContainer A IMG[src*="' + href + '"]').parents('A').show();

        return false

    });
    
        // LOGIN/REGISTER
    $('#loginForm .register, #registerForm .login').click(function() {

        // login clicked
        if ($(this).hasClass('login')) {
        
            // Close register form, show login form
            $('#registerForm').hide();
            $('#loginForm').show();           

        } else if ($(this).hasClass('register')) {

            // Close register form, show login form
            $('#loginForm').hide();
            $('#registerForm').show();

        }

        return false;

    });


    // BIG PRODUCT LIST
    $(".productList .product A").bigTarget({
        clickZone: '.product' // jQuery parent selector
    });
    $('.productList .product').css({ 'cursor': 'pointer' });

    // Language flags
    $('.flagList a').click(function() {
        var language = $(this).parent().attr('class');
        $('.languageContent').hide();
        $('.' + language + 'Content').show();
        $('.flagList a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    });

});

function pageLoad() {
    setFocusBlur($('input'));
    setForms();
}

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 {
            // 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');
        }
    }
}

//function to bind return key to form submits
function setForms() {

    //master login box
    setForm("#newsletterForm");

    // Equipment Form
    setForm("#searchForm");

    // Disc form
    setForm("#accountForm");

}

function setForm(formname) {

    //clear events
    $(formname).unbind();

    $(formname).live('keypress', function(e) {


        //check return key is pressed
        if (returnKeyPressed(e)) {
            console.log("return key");
            //check input is not textarea
            if (e.target.type != 'textarea') {

                //get postback arguments from button
                var postback = $(this).find("a:first").attr("href");
                if (postback != undefined) {

                    postback = postback.replace("javascript:__doPostBack('", "");
                    postback = postback.replace("','')", "");

                    //call postback
                    __doPostBack(postback, '');

                }

            }

        }

    });

}


function returnKeyPressed(e) {

    //check if event has been passed in, otherwise set it to the window event
    if (!e) e = event;

    //check return key has been pressed
    if ((e.which == 13) || (e.keyCode == 13)) {

        //check input is not textarea
        if (e.target.type != 'textarea') {

            //stop postback
            e.returnValue = false;
            if (!$.browser.mozilla) {
                event.returnValue = false;
            }

        }

        return true;

    } else {
        return false;
    }

}