﻿
// Check that the page has loaded
$(document).ready(function() {

    // Hide more button for menus with less than 4 items
    $('#mainLeftCol .leftNavPanel').each(function() {
        if ($(this).find('li').length < 4) {
            $(this).find('.moreButton').hide();
        }
    });

    // LEFT MENU SLIDE DOWN
    $('#mainLeftCol .leftNavPanel .moreButton').click(function() {

        // Only open if not already open
        if (!$(this).parents('.leftNavPanel').hasClass('open')) {

            // Get list height
            var UL_Height = $(this).parents('.leftNavPanel').find('UL').height();

            // Slide down
            $(this).parents('.leftNavPanel').find('.middle').animate({ height: UL_Height });

            // Hide more button
            $(this).parents('.leftNavPanel').addClass('open');

        } else { // If open, close it

            // Slide down
            $(this).parents('.leftNavPanel').find('.middle').animate({ height: '53px' });

            // Hide more button
            $(this).parents('.leftNavPanel').removeClass('open');

        }

        return false;

    });


    // SEARCH PANEL 
    $('#searchPanel #searchDiscsLink').click(function() {

        // Show searchDisks Panel (hide searchEquipment Panel)
        $('#searchPanel .TVI-form').hide();
        $('#searchPanel #searchDiscs').show();

        // Show searchEquipment Linke (hid searchDisks Link)
        $('#searchPanel #searchDiscsLink').hide();
        $('#searchPanel #searchEquipmentLink').show();

        // Change background image
        $('#searchPanel .top').css({ backgroundPosition: '-200px 0' });

        return false;

    });

    $('#searchPanel #searchEquipmentLink').click(function() {

        // Show searchDisks Panel (hide searchEquipment Panel)
        $('#searchPanel .TVI-form').hide();
        $('#searchPanel #searchEquipment').show();

        // Show searchEquipment Linke (hid searchDisks Link)
        $('#searchPanel #searchEquipmentLink').hide();
        $('#searchPanel #searchDiscsLink').show();

        // Change background image
        $('#searchPanel .top').css({ backgroundPosition: '0 0' });

        return false;

    });


    // BIG TARGET FOR PRODUCT WINDOWS
    $(".productWindow .moreButton").bigTarget({
        clickZone: '.productWindow' // jQuery parent selector
    });
    $('.productWindow').css({ 'cursor': 'pointer' });


    // PRODUCT GALLERY

    // Select first image as default
    $('.productGallery .thumbnails A:first').addClass('selected');

    // Rollover hightlight
    $('.productGallery .thumbnails A').hover(

    // mouseover
        function() {
            $(this).addClass('rollover');
        },

    // mouseout
        function() {
            $(this).removeClass('rollover');
        }

    );

    // Thumbnail click
    $('.productGallery .thumbnails A').click(function() {

        // Remove previous selected highlight
        $('.productGallery .thumbnails A').removeClass('selected');

        // Add selected to correct thumbnail
        $(this).addClass('selected');

        // Change image
        $('.productGallery .mainImg a IMG').attr('src', $(this).attr('href'));
        $('.productGallery .mainImg a').attr('href', $(this).attr('href').replace('img4','image'));

        return false;

    });
    
    //setup lightbox for product images
    
    // Remove the first one so that it isn't repeated in the lightbox
     $('.productGallery .thumbnails A.lightbox:first').remove();
    
    $('.lightbox').lightBox();


    // BIG TARGET FOR MUSIC DISCS
    $(".musicListItem .col1 A").bigTarget({
        clickZone: '.musicListItem' // jQuery parent selector
    });
    $('.musicListItem').css({ 'cursor': 'pointer' });


    // DISC INFORMATION PANEL 

    // Hide active link to start
    $('.discInformation .discTracksLink').hide();

    $('.discInformation .discTracksLink').click(function() {

        // Show searchDisks Panel (hide searchEquipment Panel)
        $('.discInformation .discDetails').hide();
        $('.discInformation .discTracks').show();

        // Show searchEquipment Linke (hid searchDisks Link)
        $('.discInformation .discTracksLink').hide();
        $('.discInformation .discDetailsLink').show();

        // Change background image
        $('.discInformation .top').css({ backgroundPosition: '0 0' });

        return false;

    });

    $('.discInformation .discDetailsLink').click(function() {

        // Show searchDisks Panel (hide searchEquipment Panel)
        $('.discInformation .discTracks').hide();
        $('.discInformation .discDetails').show();

        // Show searchEquipment Linke (hid searchDisks Link)
        $('.discInformation .discDetailsLink').hide();
        $('.discInformation .discTracksLink').show();

        // Change background image
        $('.discInformation .top').css({ backgroundPosition: '0 -32px' });

        return false;

    });


    // GLOSSARY SELECT
    $('#glossarySelect LI A').click(function() {

        // Get which letter cooked
        var letter = $(this).html();

        // Hide all, show correct one
        $('.glossarySection').hide();
        $('#glossarySection' + letter).show();

        // Add selected class to correct letter
        $('#glossarySelect LI A').removeClass('selected');
        $(this).addClass('selected');

        return false;

    });


    // FAQ
    $('.faq .question').toggle(

        function() {

            // Show answer and change arrow
            $(this).next('.answer').slideDown(300);
            $(this).css({ backgroundPosition: '0 -20px' });

        },

        function() {

            // Hide answer and change arrow
            $(this).next('.answer').slideUp(300);
            $(this).css({ backgroundPosition: '0 0' });

        }

    );

    //// Login

    // Clear login box when focused
    $('.login_email').focus(function() {
        if ($(this).val() == "Your Email Address") {
            $(this).val('');
        }
    });
    $('.login_email').blur(function() {
        if ($(this).val() == "") {
            $(this).val('Your Email Address');
        }
    });
    $('.login_email').live('click', function() {
        if ($(this).val() == "Your Email Address") {
            $(this).val('');
        }
    });

    // Hide fake password box and show real one on focus
    $('.login_password').focus(function() {
        if ($(this).val() == "Password") {
            $(this).hide();
            $('.login_passwordReal').show()
            $('.login_passwordReal').focus();
        }
    });

    $('.clearWhenClicked').live('click', function() {
        if ($(this).val() == "Artist" || $(this).val() == "Disc Code" || $(this).val() == "Track Title") {
            $(this).data('originalValue', $(this).val());
            $(this).val('');
        }
    });

    $('.clearWhenClicked').blur(function() {
        if ($(this).val() == "") {
            $(this).val($(this).data('originalValue'));
        }
    });

    setForms();

});

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("#loginForm");

    // Equipment Form
    setForm("#searchEquipment");

    // Disc form
    setForm("#searchDiscs");

}

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;
    }

}