﻿// jquery should be loaded before this

// javascript for all pages
//     mainly the menu system
var menu_callbackcount = 0;
var wide_image_max_height = 0;

function delayed_menuoff() {
    menu_callbackcount--;
    if ($('._navsubmenu').length == 0 && $('._navselection').length == 0 && menu_callbackcount == 0)
        menuoff();
}

function menuoff() {
    $('._lastselection').css('background','transparent');
    $('._lastselection').parent().prev().children('.nav_div').css('background','transparent');
    $('._lastselection').next('.nav_div').css('background','transparent');
    $('._lastselection').parent().children('ul').hide();
    $('._lastselection').removeClass('_lastselection');
}

// for IE since img.onLoad isn't always called for some reason...
$(window).load(function() {
    process_desc();
    $('._item_image').add('.default_img').add('.old_default_img').each(function () { process_img ($(this)); });
});

$(document).ready( function () {
    
    // navigation menu
    $('#nav_menu > li > a').not('#nav_cap > a').mouseover(function () {
    //  set up the selection background
        menuoff();
        $(this).css('background','url(/img/web/barbg.gif) repeat-x');
        $(this).parent().prev().children('.nav_div').css('background','url(/img/web/barbg1.gif) top right no-repeat');
        $(this).next('.nav_div').css('background','url(/img/web/barbg2.gif) top right no-repeat');
    //  javascript magic for menu system
        $('._navselection').removeClass('_navselection');
        $(this).addClass('_navselection');
        $('#nav_menu ul').not('._navselection ul').hide();
        $(this).parent().children('ul').show();
    }).mouseout(function () {
        $(this).addClass('_lastselection');
        $(this).removeClass('_navselection');
        if ($(this).parent().children('ul').length > 0)
        {
            menu_callbackcount++;
            setTimeout(delayed_menuoff,300);
        }
        else
        {
            menu_callbackcount++;
            setTimeout(delayed_menuoff,100);
        }
    });
    
    $('#nav_menu ul').mouseover(function () {
        $(this).addClass('_navsubmenu');
    }).mouseout(function () {
        $(this).removeClass('_navsubmenu');
        menu_callbackcount++;
        setTimeout(delayed_menuoff,300);
    });
    
    // replace store image on mouse-over
    $('.item_list tr:not(.head)').hover(
        function () {
            $(this).next('.optrow').andSelf().addClass('_item_hover');
            if ($(this).is('.optrow'))
                $(this).prev().addClass('_item_hover');
                
        },
        function () {
            $(this).next('.optrow').andSelf().removeClass('_item_hover')
                .prev().removeClass('_item_hover');
        }
    );
    $('._item_image').parents('tr').hover(
        function () {
            $(this).addClass('_item_hover');
            $('.prod_img .default_img').hide();
            $('._item_hover ._item_image').clone().appendTo('.prod_img').show();
            set_desc_margin();
        },
        function () {
            $(this).removeClass('_item_hover');
            $('.prod_img ._item_image').remove();
            $('.prod_img .default_img').show();
            set_desc_margin();
        }
    );
    
    $('.prod_desc_container').height($('.prod_desc .wide').height());
    
    // call this once initially in case there is no image
    if ($('._item_image').add('.default_img').add('.old_default_img').length == 0)
        set_desc_margin();
    
    process_desc();
    $('._item_image').add('.default_img').add('.old_default_img').load(function () { process_img ($(this)); });

    // remove left column if it's empty
    /*
    if ($('#navigation').html().replace(/\s+/,'').length < 1)
    {
        $('#navigation').hide();
        $('#maincontent').css('width','960px');
    }
    */
    
    // form helpers:
    //    input.labeled: makes default value into a gray internal label that disappears when editing
    //    input.getFocus: makes the input element selected when the page loads
    $('input.labeled').focus( function () {
        if (this.value == this.defaultValue)
        {
            this.value = '';
            this.select();
            this.style.color = 'black';
        }
    }).blur( function () {
        if (this.value == '' || this.value == this.defaultValue)
        {
            this.style.color = '#AAA';
            this.value = this.defaultValue;
        }
    }).not(':selected').each( function () {
        if (this.value == this.defaultValue)
            this.style.color = '#AAA';
    });

    $('input.getFocus').focus().filter('input.labeled').each(function () {
        this.value = this.defaultValue;
        this.select();
    });
    
    // cbReveal - allow a checkbox to show/hide
    $(':checkbox.cbRevealer, .cbRevealer :checkbox').click(function() {
        if ($(this).is(':checked'))
            $('.cbReveal').show();
        else
            $('.cbReveal').hide();
    });
    
    if ($(':checkbox.cbRevealer, .cbRevealer :checkbox').is(':checked'))
        $('.cbReveal').show();
    else
        $('.cbReveal').hide();
        
    // specific to Freight Quote page
    
    $('.fq_zip .submit, .fq_item_list .submit, .fq_zip submit, .fq_item_list submit').focus(function () {
        $(this).addClass('_submit_focus').css('background','#CDF').css('border-color','#89B');
    }).hover(function() {
        $(this).addClass('_submit_hover').css('background','#CDF').css('border-color','#89B');
    }, function () {
        $(this).removeClass('_submit_hover').not('._submit_focus').css('background','#DDD').css('border-color','#999');
    }).blur(function () {
        $(this).removeClass('_submit_focus').not('._submit_hover').css('background','#DDD').css('border-color','#999');
    });
        
});

function set_desc_margin ()
{
    if ($('.prod_img img:visible').length < 1 || $('.prod_img img:visible').width() > 265)
    {
        //$('.prod_img').css('margin-left','0').height(wide_image_max_height);
        $('.prod_img').css('padding-bottom','10px').height(wide_image_max_height);
        //$('.prod_desc').css('padding-left','0').children().width('700px');
        $('.prod_desc .narrow').css({'position':'absolute','visibility':'hidden'});
        $('.prod_desc .wide').css({'position':'static','visibility':'visible'});
    }
    else
    {
        //$('.prod_img').css('margin-left','-270px').height('auto');
        $('.prod_img').css('padding-bottom','0').height('auto');
        //$('.prod_desc').css('padding-left','270px').children().width('auto');
        $('.prod_desc .wide').css({'position':'absolute','visibility':'hidden'});
        $('.prod_desc .narrow').css({'position':'static','visibility':'visible'});
    }
}

function process_desc ()
{
    if ($('.prod_desc .narrow').height() > $('.prod_desc_container').height())
        $('.prod_desc_container').height($('.prod_desc .narrow').height());
}

function process_img (img)
{
    var test_height = img.height();
    if ( img.width() > 265 )
    {
        test_height += $('.prod_desc .wide').height() + 10; // "10" accounts for padding between image and text
        if (img.height() > wide_image_max_height)
            wide_image_max_height = img.height();
    }
    
    // on the store page if the image is extra-wide remove the space for it
    if (img.is('.default_img'))
        set_desc_margin();
    
    if ( test_height > $('.prod_desc_container').height())
        $('.prod_desc_container').height(test_height);
}
