// all custom brandfuse js
var cm_nums = ['5','7793','34','45','8039','8210','76','55','8301','152','56','5836','146','62','189','91','95','103','7959','89','138','111','130','8281','156','7880'];
$j(document).ready(function() {
    
    //$j('#bf_search input[name=submit]').click(function() { $j('#bf_search').submit(); return false; });
    $j('#bf_search').submit(function(e) {
        var minimums = $j('#search_minimums input[name=minimums]:checked').val(),
            imprinted = $j('#search_imprinted input[name=imprint]:checked').val(),
            cats = [],
            query,
            url,
            gastring,
            keywords  = $j('#nav_search_input').val();
        
        if (minimums === 'both' || imprinted === 'both') {
            // + bf cm cp
            cats = ['bf', 'cp', 'cm'];
            gastring = 'Minimums:both, Imprinted:both';
        }
        if (minimums == 'yes') { 
            // + cm, - bf cp DONE
            if (imprinted == 'no') {
                // - cm bf cp
                cats = [];  
                gastring = 'Minimums:yes, Imprinted:no';
            } 
            else {
                // + cm, - bf cp
                cats = ['cm'];
                gastring = 'Minimums:yes, Imprinted:yes';
            }
        }
        else if (minimums == 'no') {
            if (imprinted == 'yes') {
                // + cp, - cm bf
                cats.push('cp');
                gastring = 'Minimums:no, Imprinted:yes';
            } 
            else if (imprinted == 'no') {
                // + bf, - cm cp
                cats.push('bf');
                gastring = 'Minimums:no, Imprinted:no';
            }
            else {
                cats = ['bf', 'cp'];
                gastring = 'Minimums:no, Imprinted:both';
            }
        }
        else if (imprinted == 'yes') {
            // + cp cm, - bf
            cats = ['cp', 'cm'];
            gastring = 'Minimums:both, Imprinted:yes';
        }
        else if (imprinted == 'no') {
            // + bf, - cp cm
            cats = ['bf'];
            gastring = 'Minimums:both, Imprinted:no';
        }
        if (cats.length < 1) { cats = ['-']; }
        // build array of all numbers
        for (var i=0; i < cats.length; i++) {
            cats[i] = cats[i].replace(/bf/g, '15148').replace(/cp/g, '15137');
            if(cats[i] == 'cm') {
                cats.splice(cats.indexOf('cm', 1));
                cats = cats.concat(cm_nums);
            }
        }
        query = cats.toString();
        //query = query.replace(/,/g, '-');
        url = '/results.php?keywords=' +keywords+ '&cat=' + query;
           
        _gaq.push(['_trackEvent', 'Search', gastring, 'Search Term: ' + keywords]);
        e.preventDefault();
        window.location = url;
        return false;
    });

    $j('#bf_category_page div > li a').mouseenter(function() {
        $j(this).siblings('.tier1').slideDown('fast');
    }).mouseleave(function() {
        var that = $j(this);
        setTimeout(function() {
            that.siblings('.tier1').slideUp('fast');
        },150);
    });
       

    $j('#bf_category_page #close').click(function(e) {
        e.preventDefault();
        $j('#bf_category_page').fadeOut('slow');
    });
            

    $j('.search_box').hide();
    $j('#search_boxes a').click(function(e) {
        e.preventDefault();
        if($j(this).next().next(':first').attr('id') === 'search_minimums') {
            // show minimums
            if($j(this).next().next(':first').css('display') === 'none') {
                $j(this).next().next(':first').slideDown('slow');
            }
            else {
                $j('#search_minimums').slideUp('fast');
            }

        }
        else {
            // show imprinted
            if($j(this).next().next(':first').css('display') === 'none') {
                $j(this).next().next(':first').slideDown('slow');
            }
            else {
                $j('#search_imprinted').slideUp('fast');
            }

        }
    });
    $j('#allcats').click(function(e) {
        e.preventDefault();
        if($j('#bf_category_page').css('display') === 'none') {
            $j('#bf_category_page').fadeIn('slow');
        }
        else { 
            $j('#bf_category_page').fadeOut('slow');
        }
    });
});

